-
Notifications
You must be signed in to change notification settings - Fork 18
/
optvgm.cpp
170 lines (144 loc) · 4.31 KB
/
optvgm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Optimizes Mega Drive/Sega Genesis VGMs. Always gzip compresses output.
//
// optvgm [-s] [-n] file.vgm/vgz [out.vgz]
//
// Specifying only input path optimizes in place. Optimized data is always verified
// *before* being written.
//
// By default, unshared samples are also added to PCM data, reducing file size slightly
// while slightly increasing memory usage in a player. Specify -s to add shared data only.
#include "pcm_optimizer.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <zlib.h>
/* Copyright (C) 2005 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
static void throw_error( const char* str )
{
if ( str )
{
fprintf( stderr, "Error: %s\n", str );
exit( EXIT_FAILURE );
}
}
// gzip lameness
static const char* get_eof( FILE* file, long* eof )
{
unsigned char buf [4];
if ( !fread( buf, 2, 1, file ) )
return "Couldn't read from file";
if ( buf [0] == 0x1F && buf [1] == 0x8B )
{
if ( fseek( file, -4, SEEK_END ) )
return "Couldn't seek in file";
if ( !fread( buf, 4, 1, file ) )
return "Couldn't read from file";
*eof = buf [3] * 0x1000000L + buf [2] * 0x10000L + buf [1] * 0x100L + buf [0];
}
else
{
if ( fseek( file, 0, SEEK_END ) )
return "Couldn't seek in file";
*eof = ftell( file );
}
return NULL;
}
static const char* get_eof( const char* path, long* eof )
{
FILE* file = fopen( path, "rb" );
if ( !file )
return "Couldn't open file";
const char* error = get_eof( file, eof );
fclose( file );
return error;
}
int main( int argc, char** argv )
{
// supply defaults
if ( !argc || !*argv )
{
static char* args [] = { "optvgm", "in.vgz", "out.vgz", NULL };
argc = sizeof args / sizeof *args - 1;
argv = args;
}
int opt_flags = 0;
// parse options
int arg = 1;
while ( arg < argc && argv [arg] [0] == '-' )
{
switch ( toupper( argv [arg++] [1] ) )
{
case 'S':
opt_flags |= optimize_shared_only;
break;
case 'N':
opt_flags |= skip_verification;
break;
default:
printf( "Invalid option\n" );
arg = argc; // cause help to be printed
break;
}
}
// print help
if ( arg >= argc )
{
printf( "%s [-s] [-n] file.vgm/vgz [out.vgz] # Optimize Mega Drive/Sega Genesis VGM/VGZ\n",
argv [0] );
printf( "-s # Optimized shared samples only\n" );
printf( "-n # skip verification (use at own risk)\n" );
return 0;
}
// allocate memory
long in_size = 0;
throw_error( get_eof( argv [arg], &in_size ) );
char* in_data = (char*) malloc( in_size );
char* out_data = (char*) malloc( in_size + pcm_optimizer_extra );
if ( !in_data || !out_data )
{
free( in_data );
free( out_data );
throw_error( "Out of memory" );
}
// read input
gzFile in_file = gzopen( argv [arg], "rb" );
if ( !in_file )
throw_error( "Couldn't open input file" );
if ( gzread( in_file, in_data, in_size ) < in_size )
throw_error( "Error reading input" );
gzclose( in_file );
if ( 0 != memcmp( in_data, "Vgm ", 4 ) )
throw_error( "Not a VGM/VGZ file\n" );
// check and update header
throw_error( update_vgm_header( in_data ) );
// optimize
long out_size = optimize_pcm_data( in_data, in_size, out_data, opt_flags );
if ( !out_size )
throw_error( "Out of memory" );
if ( out_size < 0 )
throw_error( "Verify failed" );
if ( out_size == in_size )
printf( "No optimization achieved.\n" );
free( in_data );
// write output
if ( arg + 1 < argc )
arg++;
gzFile out_file = gzopen( argv [arg], "wb9" ); // max compression
if ( !out_file )
throw_error( "Couldn't open output file" );
if ( gzwrite( out_file, out_data, out_size ) < out_size )
throw_error( "Error writing output" );
gzclose( out_file );
free( out_data );
return 0;
}