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