oops, put handling for --a-rtext option in
[flac.git] / src / flac / main.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000,2001  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 "analyze.h"
27 #include "decode.h"
28 #include "encode.h"
29
30 static int usage(const char *message, ...);
31
32 int main(int argc, char *argv[])
33 {
34         int i;
35         bool verify = false, verbose = true, lax = false, mode_decode = false, test_only = false, analyze = false;
36         bool do_mid_side = true, loose_mid_side = false, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false;
37         analysis_options aopts;
38         unsigned padding = 0;
39         unsigned max_lpc_order = 8;
40         unsigned qlp_coeff_precision = 0;
41         uint64 skip = 0;
42         int format_is_wave = -1, format_is_big_endian = -1, format_is_unsigned_samples = false;
43         int format_channels = -1, format_bps = -1, format_sample_rate = -1;
44         int blocksize = -1, min_residual_partition_order = -1, max_residual_partition_order = -1, rice_parameter_search_dist = -1;
45         char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
46         int num_requested_seek_points = -1; /* -1 => no -S options were given, 0 => -S- was given */
47
48         aopts.do_residual_text = false;
49         aopts.do_residual_gnuplot = false;
50
51         if(argc <= 1)
52                 return usage(0);
53
54         /* get the options */
55         for(i = 1; i < argc; i++) {
56                 if(argv[i][0] != '-' || argv[i][1] == 0)
57                         break;
58                 if(0 == strcmp(argv[i], "-d"))
59                         mode_decode = true;
60                 else if(0 == strcmp(argv[i], "-a")) {
61                         mode_decode = true;
62                         analyze = true;
63                 }
64                 else if(0 == strcmp(argv[i], "-t")) {
65                         mode_decode = true;
66                         test_only = true;
67                 }
68                 else if(0 == strcmp(argv[i], "-s"))
69                         verbose = false;
70                 else if(0 == strcmp(argv[i], "-s-"))
71                         verbose = true;
72                 else if(0 == strcmp(argv[i], "-S")) {
73                         if(num_requested_seek_points < 0)
74                                 num_requested_seek_points = 0;
75                         num_requested_seek_points++;
76                         strcat(requested_seek_points, argv[++i]);
77                         strcat(requested_seek_points, "<");
78                 }
79                 else if(0 == strcmp(argv[i], "-S-")) {
80                         num_requested_seek_points = 0;
81                         requested_seek_points[0] = '\0';
82                 }
83                 else if(0 == strcmp(argv[i], "--skip"))
84                         skip = (uint64)atoi(argv[++i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
85                 else if(0 == strcmp(argv[i], "--lax"))
86                         lax = true;
87                 else if(0 == strcmp(argv[i], "--lax-"))
88                         lax = false;
89                 else if(0 == strcmp(argv[i], "-b"))
90                         blocksize = atoi(argv[++i]);
91                 else if(0 == strcmp(argv[i], "-e"))
92                         do_exhaustive_model_search = true;
93                 else if(0 == strcmp(argv[i], "-e-"))
94                         do_exhaustive_model_search = false;
95                 else if(0 == strcmp(argv[i], "-l"))
96                         max_lpc_order = atoi(argv[++i]);
97                 else if(0 == strcmp(argv[i], "-m"))
98                         do_mid_side = true;
99                 else if(0 == strcmp(argv[i], "-m-"))
100                         do_mid_side = false;
101                 else if(0 == strcmp(argv[i], "-M"))
102                         loose_mid_side = do_mid_side = true;
103                 else if(0 == strcmp(argv[i], "-M-"))
104                         loose_mid_side = do_mid_side = false;
105                 else if(0 == strcmp(argv[i], "-p"))
106                         do_qlp_coeff_prec_search = true;
107                 else if(0 == strcmp(argv[i], "-p-"))
108                         do_qlp_coeff_prec_search = false;
109                 else if(0 == strcmp(argv[i], "-P"))
110                         padding = atoi(argv[++i]);
111                 else if(0 == strcmp(argv[i], "-q"))
112                         qlp_coeff_precision = atoi(argv[++i]);
113                 else if(0 == strcmp(argv[i], "-r")) {
114                         char *p = strchr(argv[++i], ',');
115                         if(0 == p) {
116                                 min_residual_partition_order = 0;
117                                 max_residual_partition_order = atoi(argv[i]);
118                         }
119                         else {
120                                 min_residual_partition_order = atoi(argv[i]);
121                                 max_residual_partition_order = atoi(++p);
122                         }
123                 }
124                 else if(0 == strcmp(argv[i], "-R"))
125                         rice_parameter_search_dist = atoi(argv[++i]);
126                 else if(0 == strcmp(argv[i], "-V"))
127                         verify = true;
128                 else if(0 == strcmp(argv[i], "-V-"))
129                         verify = false;
130                 else if(0 == strcmp(argv[i], "-fb"))
131                         format_is_big_endian = true;
132                 else if(0 == strcmp(argv[i], "-fl"))
133                         format_is_big_endian = false;
134                 else if(0 == strcmp(argv[i], "-fc"))
135                         format_channels = atoi(argv[++i]);
136                 else if(0 == strcmp(argv[i], "-fp"))
137                         format_bps = atoi(argv[++i]);
138                 else if(0 == strcmp(argv[i], "-fs"))
139                         format_sample_rate = atoi(argv[++i]);
140                 else if(0 == strcmp(argv[i], "-fu"))
141                         format_is_unsigned_samples = true;
142                 else if(0 == strcmp(argv[i], "-fr"))
143                         format_is_wave = false;
144                 else if(0 == strcmp(argv[i], "-fw"))
145                         format_is_wave = true;
146                 else if(0 == strcmp(argv[i], "--a-rgp"))
147                         aopts.do_residual_gnuplot = true;
148                 else if(0 == strcmp(argv[i], "--a-rgp-"))
149                         aopts.do_residual_gnuplot = false;
150                 else if(0 == strcmp(argv[i], "--a-rtext"))
151                         aopts.do_residual_text = true;
152                 else if(0 == strcmp(argv[i], "--a-rtext-"))
153                         aopts.do_residual_text = false;
154                 else if(0 == strcmp(argv[i], "-0")) {
155                         do_exhaustive_model_search = false;
156                         do_mid_side = false;
157                         loose_mid_side = false;
158                         qlp_coeff_precision = 0;
159                         min_residual_partition_order = max_residual_partition_order = 0;
160                         rice_parameter_search_dist = 0;
161                         max_lpc_order = 0;
162                 }
163                 else if(0 == strcmp(argv[i], "-1")) {
164                         do_exhaustive_model_search = false;
165                         do_mid_side = true;
166                         loose_mid_side = true;
167                         qlp_coeff_precision = 0;
168                         min_residual_partition_order = max_residual_partition_order = 0;
169                         rice_parameter_search_dist = 0;
170                         max_lpc_order = 0;
171                 }
172                 else if(0 == strcmp(argv[i], "-2")) {
173                         do_exhaustive_model_search = false;
174                         do_mid_side = true;
175                         loose_mid_side = false;
176                         qlp_coeff_precision = 0;
177                         rice_parameter_search_dist = 0;
178                         max_lpc_order = 0;
179                 }
180                 else if(0 == strcmp(argv[i], "-4")) {
181                         do_exhaustive_model_search = false;
182                         do_mid_side = false;
183                         loose_mid_side = false;
184                         qlp_coeff_precision = 0;
185                         min_residual_partition_order = max_residual_partition_order = 0;
186                         rice_parameter_search_dist = 0;
187                         max_lpc_order = 8;
188                 }
189                 else if(0 == strcmp(argv[i], "-5")) {
190                         do_exhaustive_model_search = false;
191                         do_mid_side = true;
192                         loose_mid_side = true;
193                         qlp_coeff_precision = 0;
194                         min_residual_partition_order = max_residual_partition_order = 0;
195                         rice_parameter_search_dist = 0;
196                         max_lpc_order = 8;
197                 }
198                 else if(0 == strcmp(argv[i], "-6")) {
199                         do_exhaustive_model_search = false;
200                         do_mid_side = true;
201                         loose_mid_side = false;
202                         qlp_coeff_precision = 0;
203                         rice_parameter_search_dist = 0;
204                         max_lpc_order = 8;
205                 }
206                 else if(0 == strcmp(argv[i], "-8")) {
207                         do_exhaustive_model_search = false;
208                         do_mid_side = true;
209                         loose_mid_side = false;
210                         qlp_coeff_precision = 0;
211                         rice_parameter_search_dist = 0;
212                         max_lpc_order = 32;
213                 }
214                 else if(0 == strcmp(argv[i], "-9")) {
215                         do_exhaustive_model_search = true;
216                         do_mid_side = true;
217                         loose_mid_side = false;
218                         do_qlp_coeff_prec_search = true;
219                         min_residual_partition_order = 0;
220                         max_residual_partition_order = 16;
221                         rice_parameter_search_dist = 32;
222                         max_lpc_order = 32;
223                 }
224                 else if(isdigit((int)(argv[i][1]))) {
225                         return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
226                 }
227                 else {
228                         return usage("ERROR: invalid option '%s'\n", argv[i]);
229                 }
230         }
231         if(i + (test_only? 1:2) != argc)
232                 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (test_only? 1:2), (test_only? "":"s"));
233
234         /* tweak options based on the filenames; validate the values */
235         if(!mode_decode) {
236                 if(format_is_wave < 0) {
237                         if(strstr(argv[i], ".wav") == argv[i] + (strlen(argv[i]) - strlen(".wav")))
238                                 format_is_wave = true;
239                         else
240                                 format_is_wave = false;
241                 }
242                 if(!format_is_wave) {
243                         if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
244                                 return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
245                 }
246                 if(blocksize < 0) {
247                         if(max_lpc_order == 0)
248                                 blocksize = 1152;
249                         else
250                                 blocksize = 4608;
251                 }
252                 if(min_residual_partition_order < 0) {
253                         min_residual_partition_order = 0;
254                         if(blocksize <= 1152)
255                                 max_residual_partition_order = 4;
256                         else if(blocksize <= 2304)
257                                 max_residual_partition_order = 4;
258                         else if(blocksize <= 4608)
259                                 max_residual_partition_order = 4;
260                         else
261                                 max_residual_partition_order = 5;
262                 }
263                 if(rice_parameter_search_dist < 0) {
264                         rice_parameter_search_dist = 0;
265                 }
266         }
267         else {
268                 if(test_only) {
269                         if(skip > 0)
270                                 return usage("ERROR: --skip is not allowed in test mode\n");
271                 }
272                 else if(!analyze) {
273                         if(format_is_wave < 0) {
274                                 if(strstr(argv[i+1], ".wav") == argv[i+1] + (strlen(argv[i+1]) - strlen(".wav")))
275                                         format_is_wave = true;
276                                 else
277                                         format_is_wave = false;
278                         }
279                         if(!format_is_wave) {
280                                 if(format_is_big_endian < 0)
281                                         return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
282                         }
283                 }
284         }
285
286         assert(blocksize >= 0 || mode_decode);
287
288         if(format_channels >= 0) {
289                 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
290                         return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
291         }
292         if(format_bps >= 0) {
293                 if(format_bps != 8 && format_bps != 16 && format_bps != 24)
294                         return usage("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", format_bps);
295         }
296         if(format_sample_rate >= 0) {
297                 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
298                         return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
299         }
300         if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
301                 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
302         }
303         if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
304                 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
305         }
306
307         /* turn off verbosity if the output stream is going to stdout */
308         if(!test_only && 0 == strcmp(argv[i+1], "-"))
309                 verbose = false;
310
311         if(verbose) {
312                 printf("\n");
313                 printf("flac %s, Copyright (C) 2000,2001 Josh Coalson\n", FLAC__VERSION_STRING);
314                 printf("flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
315                 printf("welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
316
317                 if(!mode_decode) {
318                         printf("options:%s -P %u -b %u%s -l %u%s%s -q %u -r %u,%u -R %u%s\n",
319                                 lax?" --lax":"", padding, (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
320                                 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
321                                 qlp_coeff_precision,
322                                 (unsigned)min_residual_partition_order, (unsigned)max_residual_partition_order, (unsigned)rice_parameter_search_dist,
323                                 verify? " -V":""
324                         );
325                 }
326         }
327
328         if(mode_decode)
329                 if(format_is_wave)
330                         return decode_wav(argv[i], test_only? 0 : argv[i+1], analyze, aopts, verbose, skip);
331                 else
332                         return decode_raw(argv[i], test_only? 0 : argv[i+1], analyze, aopts, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
333         else
334                 if(format_is_wave)
335                         return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points);
336                 else
337                         return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
338
339         return 0;
340 }
341
342 int usage(const char *message, ...)
343 {
344         va_list args;
345
346         if(message) {
347                 va_start(args, message);
348
349                 (void) vfprintf(stderr, message, args);
350
351                 va_end(args);
352
353         }
354         printf("==============================================================================\n");
355         printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
356         printf("Copyright (C) 2000,2001  Josh Coalson\n");
357         printf("\n");
358         printf("This program is free software; you can redistribute it and/or\n");
359         printf("modify it under the terms of the GNU General Public License\n");
360         printf("as published by the Free Software Foundation; either version 2\n");
361         printf("of the License, or (at your option) any later version.\n");
362         printf("\n");
363         printf("This program is distributed in the hope that it will be useful,\n");
364         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
365         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
366         printf("GNU General Public License for more details.\n");
367         printf("\n");
368         printf("You should have received a copy of the GNU General Public License\n");
369         printf("along with this program; if not, write to the Free Software\n");
370         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
371         printf("==============================================================================\n");
372         printf("Usage:\n");
373         printf("  flac [options] infile outfile\n");
374         printf("\n");
375         printf("For encoding:\n");
376         printf("  infile may be a PCM RIFF WAVE file or raw samples\n");
377         printf("  outfile will be in FLAC format\n");
378         printf("For decoding, the reverse is be true\n");
379         printf("\n");
380         printf("infile may be - for stdin, outfile may be - for stdout\n");
381         printf("\n");
382         printf("If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
383         printf("RIFF WAVE.  Otherwise, it's assumed to be raw samples and you have to specify\n");
384         printf("all the format options.  You can force a .wav file to be treated as a raw file\n");
385         printf("using -fr.\n");
386         printf("\n");
387         printf("generic options:\n");
388         printf("  -d : decode (default behavior is encode)\n");
389         printf("  -t : test (same as -d except no decoded file is written)\n");
390         printf("  -a : analyze (same as -d except an analysis file is written)\n");
391         printf("  -s : silent (do not write runtime encode/decode statistics to stdout)\n");
392         printf("  --skip samples : can be used both for encoding and decoding\n");
393         printf("analyze options:\n");
394         printf("  --a-rtext : include residual signal in text output\n");
395         printf("  --a-rgp : generate gnuplot files of residual distribution of each subframe\n");
396         printf("encoding options:\n");
397         printf("  --lax : allow encoder to generate non-Subset files\n");
398         printf("  -S { # | X | #x } : include a point or points in a SEEKTABLE\n");
399         printf("       #  : a specific sample number for a seek point\n");
400         printf("       X  : a placeholder point (always goes at the end of the SEEKTABLE)\n");
401         printf("       #x : # evenly spaced seekpoints, the first being at sample 0\n");
402         printf("     You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
403         printf("           ified union of all such values.\n");
404         printf("     With no -S options, flac defaults to '-S 100x'.  Use -S- for no SEEKTABLE.\n");
405         printf("     Note: -S #x will not work if the encoder can't determine the input size\n");
406         printf("           before starting.\n");
407         printf("     Note: if you use -S # and # is >= samples in the input, there will be\n");
408         printf("           either no seek point entered (if the input size is determinable\n");
409         printf("           before encoding starts) or a placeholder point (if input size is not\n");
410         printf("           determinable)\n");
411         printf("  -P # : write a PADDING block of # bytes (goes after SEEKTABLE)\n");
412         printf("         (0 => no PADDING block, default is -P 0)\n");
413         printf("  -b # : specify blocksize in samples; default is 1152 for -l 0, else 4608;\n");
414         printf("         must be 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768\n");
415         printf("         (unless --lax is used)\n");
416         printf("  -m   : try mid-side coding for each frame (stereo input only)\n");
417         printf("  -M   : loose mid-side coding for all frames (stereo input only)\n");
418         printf("  -0 .. -9 : fastest compression .. highest compression, default is -6\n");
419         printf("             these are synonyms for other options:\n");
420         printf("  -0   : synonymous with -l 0 -b 1152\n");
421         printf("  -1   : synonymous with -l 0 -b 1152 -M\n");
422         printf("  -2   : synonymous with -l 0 -b 1152 -m -r 4\n");
423         printf("  -3   : reserved\n");
424         printf("  -4   : synonymous with -l 8 -b 4608 \n");
425         printf("  -5   : synonymous with -l 8 -b 4608 -M\n");
426         printf("  -6   : synonymous with -l 8 -b 4608 -m -r 4\n");
427         printf("  -7   : reserved\n");
428         printf("  -8   : synonymous with -l 32 -b 4608 -m -r 4\n");
429         printf("  -9   : synonymous with -l 32 -m -e -r 16 -R 32 -p (very slow!)\n");
430         printf("  -e   : do exhaustive model search (expensive!)\n");
431         printf("  -l # : specify max LPC order; 0 => use only fixed predictors\n");
432         printf("  -p   : do exhaustive search of LP coefficient quantization (expensive!);\n");
433         printf("         overrides -q, does nothing if using -l 0\n");
434         printf("  -q # : specify precision in bits of quantized linear-predictor coefficients;\n");
435         printf("         0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
436         printf("  -r [#,]# : [min,]max residual partition order (# is 0..16; min defaults to 0;\n");
437         printf("         default is -r 0; above 4 doesn't usually help much)\n");
438         printf("  -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much\n");
439         printf("  -V   : verify a correct encoding by decoding the output in parallel and\n");
440         printf("         comparing to the original\n");
441         printf("  -S-, -m-, -M-, -e-, -p-, -V-, --lax- can all be used to turn off a particular\n");
442         printf("  option\n");
443         printf("format options:\n");
444         printf("  -fb | -fl : big-endian | little-endian byte order\n");
445         printf("  -fc channels\n");
446         printf("  -fp bits_per_sample\n");
447         printf("  -fs sample_rate : in Hz\n");
448         printf("  -fu : unsigned samples (default is signed)\n");
449         printf("  -fr : force to raw format (even if filename ends in .wav)\n");
450         printf("  -fw : force to RIFF WAVE\n");
451         return 1;
452 }