add support for MD5 checking and a test mode
[platform/upstream/flac.git] / src / flac / main.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "FLAC/all.h"
26 #include "decode.h"
27 #include "encode.h"
28
29 static int usage(const char *message, ...);
30
31 int main(int argc, char *argv[])
32 {
33         int i;
34         bool verify = false, verbose = true, lax = false, mode_decode = false, test_only = false;
35         bool do_mid_side = true, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false;
36         unsigned max_lpc_order = 8;
37         unsigned qlp_coeff_precision = 0;
38         uint64 skip = 0;
39         int format_is_wave = -1, format_is_big_endian = -1, format_is_unsigned_samples = false;
40         int format_channels = -1, format_bps = -1, format_sample_rate = -1;
41         int blocksize = -1, rice_optimization_level = -1;
42
43         if(argc <= 1)
44                 return usage(0);
45
46         /* get the options */
47         for(i = 1; i < argc; i++) {
48                 if(argv[i][0] != '-' || argv[i][1] == 0)
49                         break;
50                 if(0 == strcmp(argv[i], "-d"))
51                         mode_decode = true;
52                 else if(0 == strcmp(argv[i], "-t")) {
53                         mode_decode = true;
54                         test_only = true;
55                 }
56                 else if(0 == strcmp(argv[i], "-s"))
57                         verbose = false;
58                 else if(0 == strcmp(argv[i], "-s-"))
59                         verbose = true;
60                 else if(0 == strcmp(argv[i], "--skip"))
61                         skip = (uint64)atoi(argv[++i]); /* takes a pretty damn big file to overflow atoi() here, but it could happen */
62                 else if(0 == strcmp(argv[i], "--lax"))
63                         lax = true;
64                 else if(0 == strcmp(argv[i], "--lax-"))
65                         lax = false;
66                 else if(0 == strcmp(argv[i], "-b"))
67                         blocksize = atoi(argv[++i]);
68                 else if(0 == strcmp(argv[i], "-e"))
69                         do_exhaustive_model_search = true;
70                 else if(0 == strcmp(argv[i], "-e-"))
71                         do_exhaustive_model_search = false;
72                 else if(0 == strcmp(argv[i], "-l"))
73                         max_lpc_order = atoi(argv[++i]);
74                 else if(0 == strcmp(argv[i], "-m"))
75                         do_mid_side = true;
76                 else if(0 == strcmp(argv[i], "-m-"))
77                         do_mid_side = false;
78                 else if(0 == strcmp(argv[i], "-p"))
79                         do_qlp_coeff_prec_search = true;
80                 else if(0 == strcmp(argv[i], "-p-"))
81                         do_qlp_coeff_prec_search = false;
82                 else if(0 == strcmp(argv[i], "-q"))
83                         qlp_coeff_precision = atoi(argv[++i]);
84                 else if(0 == strcmp(argv[i], "-r"))
85                         rice_optimization_level = atoi(argv[++i]);
86                 else if(0 == strcmp(argv[i], "-V"))
87                         verify = true;
88                 else if(0 == strcmp(argv[i], "-V-"))
89                         verify = false;
90                 else if(0 == strcmp(argv[i], "-fb"))
91                         format_is_big_endian = true;
92                 else if(0 == strcmp(argv[i], "-fl"))
93                         format_is_big_endian = false;
94                 else if(0 == strcmp(argv[i], "-fc"))
95                         format_channels = atoi(argv[++i]);
96                 else if(0 == strcmp(argv[i], "-fp"))
97                         format_bps = atoi(argv[++i]);
98                 else if(0 == strcmp(argv[i], "-fs"))
99                         format_sample_rate = atoi(argv[++i]);
100                 else if(0 == strcmp(argv[i], "-fu"))
101                         format_is_unsigned_samples = true;
102                 else if(0 == strcmp(argv[i], "-fr"))
103                         format_is_wave = false;
104                 else if(0 == strcmp(argv[i], "-fw"))
105                         format_is_wave = true;
106                 else if(0 == strcmp(argv[i], "-0")) {
107                         do_exhaustive_model_search = false;
108                         do_mid_side = false;
109                         qlp_coeff_precision = 0;
110                         rice_optimization_level = 0;
111                         max_lpc_order = 0;
112                 }
113                 else if(0 == strcmp(argv[i], "-1")) {
114                         do_exhaustive_model_search = false;
115                         do_mid_side = true;
116                         qlp_coeff_precision = 0;
117                         rice_optimization_level = 0;
118                         max_lpc_order = 0;
119                 }
120                 else if(0 == strcmp(argv[i], "-2")) {
121                         do_exhaustive_model_search = false;
122                         do_mid_side = true;
123                         qlp_coeff_precision = 0;
124                         max_lpc_order = 0;
125                 }
126                 else if(0 == strcmp(argv[i], "-4")) {
127                         do_exhaustive_model_search = false;
128                         do_mid_side = false;
129                         qlp_coeff_precision = 0;
130                         rice_optimization_level = 0;
131                         max_lpc_order = 8;
132                 }
133                 else if(0 == strcmp(argv[i], "-5")) {
134                         do_exhaustive_model_search = false;
135                         do_mid_side = true;
136                         qlp_coeff_precision = 0;
137                         rice_optimization_level = 0;
138                         max_lpc_order = 8;
139                 }
140                 else if(0 == strcmp(argv[i], "-6")) {
141                         do_exhaustive_model_search = false;
142                         do_mid_side = true;
143                         qlp_coeff_precision = 0;
144                         max_lpc_order = 8;
145                 }
146                 else if(0 == strcmp(argv[i], "-8")) {
147                         do_exhaustive_model_search = false;
148                         do_mid_side = true;
149                         qlp_coeff_precision = 0;
150                         max_lpc_order = 32;
151                 }
152                 else if(0 == strcmp(argv[i], "-9")) {
153                         do_exhaustive_model_search = true;
154                         do_mid_side = true;
155                         do_qlp_coeff_prec_search = true;
156                         rice_optimization_level = 99;
157                         max_lpc_order = 32;
158                 }
159                 else if(isdigit((int)(argv[i][1]))) {
160                         return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
161                 }
162                 else {
163                         return usage("ERROR: invalid option '%s'\n", argv[i]);
164                 }
165         }
166         if(i + (test_only? 1:2) != argc)
167                 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (test_only? 1:2), (test_only? "":"s"));
168
169         /* tweak options based on the filenames; validate the values */
170         if(!mode_decode) {
171                 if(format_is_wave < 0) {
172                         if(strstr(argv[i], ".wav") == argv[i] + (strlen(argv[i]) - strlen(".wav")))
173                                 format_is_wave = true;
174                         else
175                                 format_is_wave = false;
176                 }
177                 if(!format_is_wave) {
178                         if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
179                                 return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
180                 }
181                 if(blocksize < 0) {
182                         if(max_lpc_order == 0)
183                                 blocksize = 1152;
184                         else
185                                 blocksize = 4608;
186                 }
187                 if(rice_optimization_level < 0) {
188                         if(blocksize <= 1152)
189                                 rice_optimization_level = 4;
190                         else if(blocksize <= 2304)
191                                 rice_optimization_level = 4;
192                         else if(blocksize <= 4608)
193                                 rice_optimization_level = 4;
194                         else
195                                 rice_optimization_level = 5;
196                 }
197         }
198         else {
199                 if(!test_only) {
200                         if(format_is_wave < 0) {
201                                 if(strstr(argv[i+1], ".wav") == argv[i+1] + (strlen(argv[i+1]) - strlen(".wav")))
202                                         format_is_wave = true;
203                                 else
204                                         format_is_wave = false;
205                         }
206                         if(!format_is_wave) {
207                                 if(format_is_big_endian < 0)
208                                         return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
209                         }
210                 }
211         }
212
213         assert(blocksize >= 0 || mode_decode);
214
215         if(format_channels >= 0) {
216                 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
217                         return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
218         }
219         if(format_bps >= 0) {
220                 if(format_bps != 8 && format_bps != 16)
221                         return usage("ERROR: invalid bits per sample '%u' (must be 8 or 16)\n", format_bps);
222         }
223         if(format_sample_rate >= 0) {
224                 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
225                         return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
226         }
227         if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
228                 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
229         }
230         if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
231                 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
232         }
233
234         /* turn off verbosity if the output stream is going to stdout */
235         if(!test_only && 0 == strcmp(argv[i+1], "-"))
236                 verbose = false;
237
238         if(verbose) {
239                 printf("\n");
240                 printf("flac v%u.%u, Copyright (C) 2000 Josh Coalson\n", FLAC__MAJOR_VERSION, FLAC__MINOR_VERSION);
241                 printf("flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
242                 printf("welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
243
244                 if(!mode_decode) {
245                         printf("options:%s -b %u%s -l %u%s%s -q %u -r %u%s\n",
246                                 lax?" --lax":"", (unsigned)blocksize, do_mid_side?" -m":"", max_lpc_order,
247                                 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
248                                 qlp_coeff_precision, (unsigned)rice_optimization_level,
249                                 verify? " -V":""
250                         );
251                 }
252         }
253
254         if(mode_decode)
255                 if(format_is_wave)
256                         return decode_wav(argv[i], test_only? 0 : argv[i+1], verbose, skip);
257                 else
258                         return decode_raw(argv[i], test_only? 0 : argv[i+1], verbose, skip, format_is_big_endian, format_is_unsigned_samples);
259         else
260                 if(format_is_wave)
261                         return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision);
262                 else
263                         return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
264
265         return 0;
266 }
267
268 int usage(const char *message, ...)
269 {
270         va_list args;
271
272         if(message) {
273                 va_start(args, message);
274
275                 (void) vfprintf(stderr, message, args);
276
277                 va_end(args);
278
279         }
280         printf("==============================================================================\n");
281         printf("flac - Command-line FLAC encoder/decoder version %u.%u\n", FLAC__MAJOR_VERSION, FLAC__MINOR_VERSION);
282         printf("Copyright (C) 2000  Josh Coalson\n");
283         printf("\n");
284         printf("This program is free software; you can redistribute it and/or\n");
285         printf("modify it under the terms of the GNU General Public License\n");
286         printf("as published by the Free Software Foundation; either version 2\n");
287         printf("of the License, or (at your option) any later version.\n");
288         printf("\n");
289         printf("This program is distributed in the hope that it will be useful,\n");
290         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
291         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
292         printf("GNU General Public License for more details.\n");
293         printf("\n");
294         printf("You should have received a copy of the GNU General Public License\n");
295         printf("along with this program; if not, write to the Free Software\n");
296         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
297         printf("==============================================================================\n");
298         printf("Usage:\n");
299         printf("  flac [options] infile outfile\n");
300         printf("\n");
301         printf("For encoding:\n");
302         printf("  infile may be a PCM RIFF WAVE file or raw samples\n");
303         printf("  outfile will be in FLAC format\n");
304         printf("For decoding, the reverse will be true\n");
305         printf("\n");
306         printf("infile may be - for stdin, outfile may be - for stdout\n");
307         printf("\n");
308         printf("If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
309         printf("RIFF WAVE.  Otherwise, it's assumed to be raw samples and you have to specify\n");
310         printf("all the format options.  You can force a .wav file to be treated as a raw file\n");
311         printf("using -fr.\n");
312         printf("\n");
313         printf("generic options:\n");
314         printf("  -d : decode (default behavior is encode)\n");
315         printf("  -s : silent (do not write runtime encode/decode statistics to stdout)\n");
316         printf("  --skip samples : can be used both for encoding and decoding\n");
317         printf("encoding options:\n");
318         printf("  --lax : allow encoder to generate non-Subset files\n");
319         printf("  -b blocksize : default is 1152 for -l 0, else 4608; should be 192/576/1152/2304/4608 (unless --lax is used)\n");
320         printf("  -m : try mid-side coding for each frame (stereo input only)\n");
321         printf("  -0 .. -9 : fastest compression .. highest compression, default is -6\n");
322         printf("             these are synonyms for other options:\n");
323         printf("  -0 : synonymous with -l 0\n");
324         printf("  -1 : synonymous with -l 0 -m\n");
325         printf("  -2 : synonymous with -l 0 -m -r # (# is automatically determined by the block size)\n");
326         printf("  -3 : reserved\n");
327         printf("  -4 : synonymous with -l 8\n");
328         printf("  -5 : synonymous with -l 8 -m\n");
329         printf("  -6 : synonymous with -l 8 -m -r # (# is automatically determined by the block size)\n");
330         printf("  -7 : reserved\n");
331         printf("  -8 : synonymous with -l 32 -m -r # (# is automatically determined by the block size)\n");
332         printf("  -9 : synonymous with -l 32 -m -e -r 99 -p (very slow!)\n");
333         printf("  -e : do exhaustive model search (expensive!)\n");
334         printf("  -l max_lpc_order : 0 => use only fixed predictors\n");
335         printf("  -p : do exhaustive search of LP coefficient quantization (expensive!); overrides -q\n");
336         printf("  -q bits : precision of the quantized linear-predictor coefficients, 0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
337         printf("  -r level : rice parameter optimization level (level is 0..99, 0 => none, default is -r 0, above 4 doesn't usually help much)\n");
338         printf("  -V : verify a correct encoding by decoding the output in parallel and comparing to the original\n");
339         printf("  -m-, -e-, -p-, -V-, --lax- can all be used to turn off a particular option\n");
340         printf("format options:\n");
341         printf("  -fb | -fl : big-endian | little-endian byte order\n");
342         printf("  -fc channels\n");
343         printf("  -fp bits_per_sample\n");
344         printf("  -fs sample_rate : in Hz\n");
345         printf("  -fu : unsigned samples (default is signed)\n");
346         printf("  -fr : force to raw format (even if filename ends in .wav)\n");
347         printf("  -fw : force to RIFF WAVE\n");
348         return 1;
349 }