add -o option, remove "flac - outfilename" mode
[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 <ctype.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h> /* for unlink() */
25 #include "FLAC/all.h"
26 #include "analyze.h"
27 #include "decode.h"
28 #include "encode.h"
29 #include "file.h"
30
31 static int usage(const char *message, ...);
32 static int encode_file(const char *infilename, const char *forced_outfilename);
33 static int decode_file(const char *infilename, const char *forced_outfilename);
34
35 bool verify = false, verbose = true, lax = 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 bool force_to_stdout = false, delete_input = false;
38 const char *cmdline_forced_outfilename = 0;
39 analysis_options aopts = { false, false };
40 unsigned padding = 0;
41 unsigned max_lpc_order = 8;
42 unsigned qlp_coeff_precision = 0;
43 uint64 skip = 0;
44 int format_is_wave = -1, format_is_big_endian = -1, format_is_unsigned_samples = false;
45 int format_channels = -1, format_bps = -1, format_sample_rate = -1;
46 int blocksize = -1, min_residual_partition_order = -1, max_residual_partition_order = -1, rice_parameter_search_dist = -1;
47 char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
48 int num_requested_seek_points = -1; /* -1 => no -S options were given, 0 => -S- was given */
49
50 int main(int argc, char *argv[])
51 {
52         int i, retval = 0;
53         bool mode_decode = false;
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], "-c"))
73                         force_to_stdout = true;
74                 else if(0 == strcmp(argv[i], "-s"))
75                         verbose = false;
76                 else if(0 == strcmp(argv[i], "-s-"))
77                         verbose = true;
78                 else if(0 == strcmp(argv[i], "-S")) {
79                         if(num_requested_seek_points < 0)
80                                 num_requested_seek_points = 0;
81                         num_requested_seek_points++;
82                         strcat(requested_seek_points, argv[++i]);
83                         strcat(requested_seek_points, "<");
84                 }
85                 else if(0 == strcmp(argv[i], "-S-")) {
86                         num_requested_seek_points = 0;
87                         requested_seek_points[0] = '\0';
88                 }
89                 else if(0 == strcmp(argv[i], "--delete-input-file"))
90                         delete_input = true;
91                 else if(0 == strcmp(argv[i], "--delete-input-file-"))
92                         delete_input = false;
93                 else if(0 == strcmp(argv[i], "--skip"))
94                         skip = (uint64)atoi(argv[++i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
95                 else if(0 == strcmp(argv[i], "--lax"))
96                         lax = true;
97                 else if(0 == strcmp(argv[i], "--lax-"))
98                         lax = false;
99                 else if(0 == strcmp(argv[i], "-b"))
100                         blocksize = atoi(argv[++i]);
101                 else if(0 == strcmp(argv[i], "-e"))
102                         do_exhaustive_model_search = true;
103                 else if(0 == strcmp(argv[i], "-e-"))
104                         do_exhaustive_model_search = false;
105                 else if(0 == strcmp(argv[i], "-l"))
106                         max_lpc_order = atoi(argv[++i]);
107                 else if(0 == strcmp(argv[i], "-m")) {
108                         do_mid_side = true;
109                         loose_mid_side = false;
110                 }
111                 else if(0 == strcmp(argv[i], "-m-"))
112                         do_mid_side = loose_mid_side = false;
113                 else if(0 == strcmp(argv[i], "-M"))
114                         loose_mid_side = do_mid_side = true;
115                 else if(0 == strcmp(argv[i], "-M-"))
116                         loose_mid_side = do_mid_side = false;
117                 else if(0 == strcmp(argv[i], "-o"))
118                         cmdline_forced_outfilename = argv[++i];
119                 else if(0 == strcmp(argv[i], "-p"))
120                         do_qlp_coeff_prec_search = true;
121                 else if(0 == strcmp(argv[i], "-p-"))
122                         do_qlp_coeff_prec_search = false;
123                 else if(0 == strcmp(argv[i], "-P"))
124                         padding = atoi(argv[++i]);
125                 else if(0 == strcmp(argv[i], "-q"))
126                         qlp_coeff_precision = atoi(argv[++i]);
127                 else if(0 == strcmp(argv[i], "-r")) {
128                         char *p = strchr(argv[++i], ',');
129                         if(0 == p) {
130                                 min_residual_partition_order = 0;
131                                 max_residual_partition_order = atoi(argv[i]);
132                         }
133                         else {
134                                 min_residual_partition_order = atoi(argv[i]);
135                                 max_residual_partition_order = atoi(++p);
136                         }
137                 }
138                 else if(0 == strcmp(argv[i], "-R"))
139                         rice_parameter_search_dist = atoi(argv[++i]);
140                 else if(0 == strcmp(argv[i], "-V"))
141                         verify = true;
142                 else if(0 == strcmp(argv[i], "-V-"))
143                         verify = false;
144                 else if(0 == strcmp(argv[i], "-fb"))
145                         format_is_big_endian = true;
146                 else if(0 == strcmp(argv[i], "-fl"))
147                         format_is_big_endian = false;
148                 else if(0 == strcmp(argv[i], "-fc"))
149                         format_channels = atoi(argv[++i]);
150                 else if(0 == strcmp(argv[i], "-fp"))
151                         format_bps = atoi(argv[++i]);
152                 else if(0 == strcmp(argv[i], "-fs"))
153                         format_sample_rate = atoi(argv[++i]);
154                 else if(0 == strcmp(argv[i], "-fu"))
155                         format_is_unsigned_samples = true;
156                 else if(0 == strcmp(argv[i], "-fr"))
157                         format_is_wave = false;
158                 else if(0 == strcmp(argv[i], "-fw"))
159                         format_is_wave = true;
160                 else if(0 == strcmp(argv[i], "--a-rgp"))
161                         aopts.do_residual_gnuplot = true;
162                 else if(0 == strcmp(argv[i], "--a-rgp-"))
163                         aopts.do_residual_gnuplot = false;
164                 else if(0 == strcmp(argv[i], "--a-rtext"))
165                         aopts.do_residual_text = true;
166                 else if(0 == strcmp(argv[i], "--a-rtext-"))
167                         aopts.do_residual_text = false;
168                 else if(0 == strcmp(argv[i], "-0")) {
169                         do_exhaustive_model_search = false;
170                         do_mid_side = false;
171                         loose_mid_side = false;
172                         qlp_coeff_precision = 0;
173                         min_residual_partition_order = max_residual_partition_order = 2;
174                         rice_parameter_search_dist = 0;
175                         max_lpc_order = 0;
176                 }
177                 else if(0 == strcmp(argv[i], "-1")) {
178                         do_exhaustive_model_search = false;
179                         do_mid_side = true;
180                         loose_mid_side = true;
181                         qlp_coeff_precision = 0;
182                         min_residual_partition_order = max_residual_partition_order = 2;
183                         rice_parameter_search_dist = 0;
184                         max_lpc_order = 0;
185                 }
186                 else if(0 == strcmp(argv[i], "-2")) {
187                         do_exhaustive_model_search = false;
188                         do_mid_side = true;
189                         loose_mid_side = false;
190                         qlp_coeff_precision = 0;
191                         min_residual_partition_order = 0;
192                         max_residual_partition_order = 3;
193                         rice_parameter_search_dist = 0;
194                         max_lpc_order = 0;
195                 }
196                 else if(0 == strcmp(argv[i], "-3")) {
197                         do_exhaustive_model_search = false;
198                         do_mid_side = false;
199                         loose_mid_side = false;
200                         qlp_coeff_precision = 0;
201                         min_residual_partition_order = max_residual_partition_order = 3;
202                         rice_parameter_search_dist = 0;
203                         max_lpc_order = 6;
204                 }
205                 else if(0 == strcmp(argv[i], "-4")) {
206                         do_exhaustive_model_search = false;
207                         do_mid_side = true;
208                         loose_mid_side = true;
209                         qlp_coeff_precision = 0;
210                         min_residual_partition_order = max_residual_partition_order = 3;
211                         rice_parameter_search_dist = 0;
212                         max_lpc_order = 8;
213                 }
214                 else if(0 == strcmp(argv[i], "-5")) {
215                         do_exhaustive_model_search = false;
216                         do_mid_side = true;
217                         loose_mid_side = false;
218                         qlp_coeff_precision = 0;
219                         min_residual_partition_order = max_residual_partition_order = 3;
220                         rice_parameter_search_dist = 0;
221                         max_lpc_order = 8;
222                 }
223                 else if(0 == strcmp(argv[i], "-6")) {
224                         do_exhaustive_model_search = false;
225                         do_mid_side = true;
226                         loose_mid_side = false;
227                         qlp_coeff_precision = 0;
228                         min_residual_partition_order = 0;
229                         max_residual_partition_order = 4;
230                         rice_parameter_search_dist = 0;
231                         max_lpc_order = 8;
232                 }
233                 else if(0 == strcmp(argv[i], "-7")) {
234                         do_exhaustive_model_search = true;
235                         do_mid_side = true;
236                         loose_mid_side = false;
237                         qlp_coeff_precision = 0;
238                         min_residual_partition_order = 0;
239                         max_residual_partition_order = 6;
240                         rice_parameter_search_dist = 0;
241                         max_lpc_order = 8;
242                 }
243                 else if(0 == strcmp(argv[i], "-8")) {
244                         do_exhaustive_model_search = true;
245                         do_mid_side = true;
246                         loose_mid_side = false;
247                         qlp_coeff_precision = 0;
248                         min_residual_partition_order = 0;
249                         max_residual_partition_order = 6;
250                         rice_parameter_search_dist = 0;
251                         max_lpc_order = 12;
252                 }
253                 else if(0 == strcmp(argv[i], "-9")) {
254                         do_exhaustive_model_search = true;
255                         do_mid_side = true;
256                         loose_mid_side = false;
257                         do_qlp_coeff_prec_search = true;
258                         min_residual_partition_order = 0;
259                         max_residual_partition_order = 16;
260                         rice_parameter_search_dist = 0;
261                         max_lpc_order = 32;
262                 }
263                 else if(isdigit((int)(argv[i][1]))) {
264                         return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
265                 }
266                 else {
267                         return usage("ERROR: invalid option '%s'\n", argv[i]);
268                 }
269         }
270
271         /* tweak options; validate the values */
272         if(!mode_decode) {
273                 if(blocksize < 0) {
274                         if(max_lpc_order == 0)
275                                 blocksize = 1152;
276                         else
277                                 blocksize = 4608;
278                 }
279                 if(max_residual_partition_order < 0) {
280                         if(blocksize <= 1152)
281                                 max_residual_partition_order = 2;
282                         else if(blocksize <= 2304)
283                                 max_residual_partition_order = 3;
284                         else if(blocksize <= 4608)
285                                 max_residual_partition_order = 3;
286                         else
287                                 max_residual_partition_order = 4;
288                         min_residual_partition_order = max_residual_partition_order;
289                 }
290                 if(rice_parameter_search_dist < 0) {
291                         rice_parameter_search_dist = 0;
292                 }
293         }
294         else {
295                 if(test_only) {
296                         if(skip > 0)
297                                 return usage("ERROR: --skip is not allowed in test mode\n");
298                 }
299         }
300
301         FLAC__ASSERT(blocksize >= 0 || mode_decode);
302
303         if(format_channels >= 0) {
304                 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
305                         return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
306         }
307         if(format_bps >= 0) {
308                 if(format_bps != 8 && format_bps != 16 && format_bps != 24)
309                         return usage("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", format_bps);
310         }
311         if(format_sample_rate >= 0) {
312                 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
313                         return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
314         }
315         if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
316                 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
317         }
318         if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
319                 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
320         }
321
322         if(verbose) {
323                 fprintf(stderr, "\n");
324                 fprintf(stderr, "flac %s, Copyright (C) 2000,2001 Josh Coalson\n", FLAC__VERSION_STRING);
325                 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
326                 fprintf(stderr, "welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
327
328                 if(!mode_decode) {
329                         fprintf(stderr, "options:%s%s -P %u -b %u%s -l %u%s%s -q %u -r %u,%u -R %u%s\n",
330                                 delete_input?" --delete-input-file":"", lax?" --lax":"",
331                                 padding, (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
332                                 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
333                                 qlp_coeff_precision,
334                                 (unsigned)min_residual_partition_order, (unsigned)max_residual_partition_order, (unsigned)rice_parameter_search_dist,
335                                 verify? " -V":""
336                         );
337                 }
338         }
339
340         if(mode_decode) {
341                 bool first = true;
342                 int save_format;
343
344                 if(i == argc) {
345                         retval = decode_file("-", 0);
346                 }
347                 else {
348                         if(i + 1 != argc)
349                                 cmdline_forced_outfilename = 0;
350                         for(retval = 0; i < argc && retval == 0; i++) {
351                                 if(0 == strcmp(argv[i], "-") && !first)
352                                         continue;
353                                 save_format = format_is_wave;
354                                 retval = decode_file(argv[i], 0);
355                                 format_is_wave = save_format;
356                                 first = false;
357                         }
358                 }
359         }
360         else { /* encode */
361                 bool first = true;
362                 int save_format;
363
364                 if(i == argc) {
365                         retval = encode_file("-", 0);
366                 }
367                 else {
368                         if(i + 1 != argc)
369                                 cmdline_forced_outfilename = 0;
370                         for(retval = 0; i < argc && retval == 0; i++) {
371                                 if(0 == strcmp(argv[i], "-") && !first)
372                                         continue;
373                                 save_format = format_is_wave;
374                                 retval = encode_file(argv[i], 0);
375                                 format_is_wave = save_format;
376                                 first = false;
377                         }
378                 }
379         }
380
381         return retval;
382 }
383
384 int usage(const char *message, ...)
385 {
386         va_list args;
387
388         if(message) {
389                 va_start(args, message);
390
391                 (void) vfprintf(stderr, message, args);
392
393                 va_end(args);
394
395         }
396         fprintf(stderr, "===============================================================================\n");
397         fprintf(stderr, "flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
398         fprintf(stderr, "Copyright (C) 2000,2001  Josh Coalson\n");
399         fprintf(stderr, "\n");
400         fprintf(stderr, "This program is free software; you can redistribute it and/or\n");
401         fprintf(stderr, "modify it under the terms of the GNU General Public License\n");
402         fprintf(stderr, "as published by the Free Software Foundation; either version 2\n");
403         fprintf(stderr, "of the License, or (at your option) any later version.\n");
404         fprintf(stderr, "\n");
405         fprintf(stderr, "This program is distributed in the hope that it will be useful,\n");
406         fprintf(stderr, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
407         fprintf(stderr, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
408         fprintf(stderr, "GNU General Public License for more details.\n");
409         fprintf(stderr, "\n");
410         fprintf(stderr, "You should have received a copy of the GNU General Public License\n");
411         fprintf(stderr, "along with this program; if not, write to the Free Software\n");
412         fprintf(stderr, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
413         fprintf(stderr, "===============================================================================\n");
414         fprintf(stderr, "Usage:\n");
415         fprintf(stderr, "  flac [options] [infile [...]]\n");
416         fprintf(stderr, "\n");
417         fprintf(stderr, "For encoding:\n");
418         fprintf(stderr, "  the input file(s) may be a PCM RIFF WAVE file or raw samples\n");
419         fprintf(stderr, "  the output file(s) will be in FLAC format\n");
420         fprintf(stderr, "For decoding, the reverse is true\n");
421         fprintf(stderr, "\n");
422         fprintf(stderr, "A single 'infile' may be - for stdin.  No 'infile' implies stdin.  Use of\n");
423         fprintf(stderr, "stdin implies -c (write to stdout).  Normally you should use:\n");
424         fprintf(stderr, "   flac [options] -o outfilename  or  flac -d [options] -o outfilename\n");
425         fprintf(stderr, "instead of:\n");
426         fprintf(stderr, "   flac [options] > outfilename   or  flac -d [options] > outfilename\n");
427         fprintf(stderr, "since the former allows flac to seek backwards to write the STREAMINFO or\n");
428         fprintf(stderr, "RIFF WAVE header contents when necessary.\n");
429         fprintf(stderr, "\n");
430         fprintf(stderr, "If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
431         fprintf(stderr, "RIFF WAVE.  Otherwise, flac will check for the presence of a RIFF header.  If\n");
432         fprintf(stderr, "any infile is raw you must specify the format options {-fb|fl} -fc -fp and -fs,\n");
433         fprintf(stderr, "which will apply to all raw files.  You can force a .wav file to be treated as\n");
434         fprintf(stderr, "a raw file using -fr.\n");
435         fprintf(stderr, "\n");
436         fprintf(stderr, "generic options:\n");
437         fprintf(stderr, "  -d : decode (default behavior is encode)\n");
438         fprintf(stderr, "  -t : test (same as -d except no decoded file is written)\n");
439         fprintf(stderr, "  -a : analyze (same as -d except an analysis file is written)\n");
440         fprintf(stderr, "  -c : write output to stdout\n");
441         fprintf(stderr, "  -s : silent (do not write runtime encode/decode statistics)\n");
442         fprintf(stderr, "  -o filename : force the output file name (usually flac just changes the\n");
443         fprintf(stderr, "                extension)\n");
444         fprintf(stderr, "  --delete-input-file : deletes the input file after a successful encode/decode\n");
445         fprintf(stderr, "  --skip samples : can be used both for encoding and decoding\n");
446         fprintf(stderr, "analyze options:\n");
447         fprintf(stderr, "  --a-rtext : include residual signal in text output\n");
448         fprintf(stderr, "  --a-rgp : generate gnuplot files of residual distribution of each subframe\n");
449         fprintf(stderr, "encoding options:\n");
450         fprintf(stderr, "  --lax : allow encoder to generate non-Subset files\n");
451         fprintf(stderr, "  -S { # | X | #x } : include a point or points in a SEEKTABLE\n");
452         fprintf(stderr, "       #  : a specific sample number for a seek point\n");
453         fprintf(stderr, "       X  : a placeholder point (always goes at the end of the SEEKTABLE)\n");
454         fprintf(stderr, "       #x : # evenly spaced seekpoints, the first being at sample 0\n");
455         fprintf(stderr, "     You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
456         fprintf(stderr, "           ified union of all such values.\n");
457         fprintf(stderr, "     With no -S options, flac defaults to '-S 100x'.  Use -S- for no SEEKTABLE.\n");
458         fprintf(stderr, "     Note: -S #x will not work if the encoder can't determine the input size\n");
459         fprintf(stderr, "           before starting.\n");
460         fprintf(stderr, "     Note: if you use -S # and # is >= samples in the input, there will be\n");
461         fprintf(stderr, "           either no seek point entered (if the input size is determinable\n");
462         fprintf(stderr, "           before encoding starts) or a placeholder point (if input size is not\n");
463         fprintf(stderr, "           determinable)\n");
464         fprintf(stderr, "  -P # : write a PADDING block of # bytes (goes after SEEKTABLE)\n");
465         fprintf(stderr, "         (0 => no PADDING block, default is -P 0)\n");
466         fprintf(stderr, "  -b # : specify blocksize in samples; default is 1152 for -l 0, else 4608;\n");
467         fprintf(stderr, "         must be 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768\n");
468         fprintf(stderr, "         (unless --lax is used)\n");
469         fprintf(stderr, "  -m   : try mid-side coding for each frame (stereo input only)\n");
470         fprintf(stderr, "  -M   : loose mid-side coding for all frames (stereo input only)\n");
471         fprintf(stderr, "  -0 .. -9 : fastest compression .. highest compression, default is -5\n");
472         fprintf(stderr, "             these are synonyms for other options:\n");
473         fprintf(stderr, "  -0   : synonymous with -l 0 -b 1152 -r 2,2\n");
474         fprintf(stderr, "  -1   : synonymous with -l 0 -b 1152 -M -r 2,2\n");
475         fprintf(stderr, "  -2   : synonymous with -l 0 -b 1152 -m -r 3\n");
476         fprintf(stderr, "  -3   : synonymous with -l 6 -b 4608 -r 3,3\n");
477         fprintf(stderr, "  -4   : synonymous with -l 8 -b 4608 -M -r 3,3\n");
478         fprintf(stderr, "  -5   : synonymous with -l 8 -b 4608 -m -r 3,3\n");
479         fprintf(stderr, "  -6   : synonymous with -l 8 -b 4608 -m -r 4\n");
480         fprintf(stderr, "  -7   : synonymous with -l 8 -b 4608 -m -e -r 6\n");
481         fprintf(stderr, "  -8   : synonymous with -l 12 -b 4608 -m -e -r 6\n");
482         fprintf(stderr, "  -9   : synonymous with -l 32 -b 4608 -m -e -r 16 -p (very slow!)\n");
483         fprintf(stderr, "  -e   : do exhaustive model search (expensive!)\n");
484         fprintf(stderr, "  -l # : specify max LPC order; 0 => use only fixed predictors\n");
485         fprintf(stderr, "  -p   : do exhaustive search of LP coefficient quantization (expensive!);\n");
486         fprintf(stderr, "         overrides -q, does nothing if using -l 0\n");
487         fprintf(stderr, "  -q # : specify precision in bits of quantized linear-predictor coefficients;\n");
488         fprintf(stderr, "         0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
489         fprintf(stderr, "  -r [#,]# : [min,]max residual partition order (# is 0..16; min defaults to 0;\n");
490         fprintf(stderr, "         default is -r 0; above 4 doesn't usually help much)\n");
491         fprintf(stderr, "  -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much)\n");
492         fprintf(stderr, "  -V   : verify a correct encoding by decoding the output in parallel and\n");
493         fprintf(stderr, "         comparing to the original\n");
494         fprintf(stderr, "  -S-, -m-, -M-, -e-, -p-, -V-, --delete-input-file-, --lax- can all be used to\n");
495         fprintf(stderr, "  turn off a particular option\n");
496         fprintf(stderr, "format options:\n");
497         fprintf(stderr, "  -fb | -fl : big-endian | little-endian byte order\n");
498         fprintf(stderr, "  -fc channels\n");
499         fprintf(stderr, "  -fp bits_per_sample\n");
500         fprintf(stderr, "  -fs sample_rate : in Hz\n");
501         fprintf(stderr, "  -fu : unsigned samples (default is signed)\n");
502         fprintf(stderr, "  -fr : force to raw format (even if filename ends in .wav)\n");
503         fprintf(stderr, "  -fw : force to RIFF WAVE\n");
504         return 1;
505 }
506
507 int encode_file(const char *infilename, const char *forced_outfilename)
508 {
509         FILE *encode_infile;
510         char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
511         char *p;
512         int retval;
513
514         if(0 == strcmp(infilename, "-")) {
515                 encode_infile = stdin;
516         }
517         else {
518                 if(0 == (encode_infile = fopen(infilename, "rb"))) {
519                         fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
520                         return 1;
521                 }
522         }
523
524         if(verbose)
525                 fprintf(stderr, "%s:\n", infilename);
526
527         if(format_is_wave < 0) {
528                 /* lamely attempt to guess the file type based on the first 4 bytes (which is all ungetc will guarantee us) */
529                 char head[4];
530                 int h, n;
531                 /* first set format based on name */
532                 if(strstr(infilename, ".wav") == infilename + (strlen(infilename) - strlen(".wav")))
533                         format_is_wave = true;
534                 else
535                         format_is_wave = false;
536                 if((n = fread(head, 1, 4, encode_infile)) < 4) {
537                         if(format_is_wave)
538                                 fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename);
539                         format_is_wave = false;
540                 }
541                 else {
542                         if(strncmp(head, "RIFF", 4)) {
543                                 if(format_is_wave)
544                                         fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename);
545                                 format_is_wave = false;
546                         }
547                         else
548                                 format_is_wave = true;
549                 }
550                 for(h = n-1; h >= 0; h--)
551                         ungetc(head[h], encode_infile);
552         }
553
554         if(!format_is_wave) {
555                 if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
556                         return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
557         }
558
559         if(encode_infile == stdin || force_to_stdout)
560                 strcpy(outfilename, "-");
561         else {
562                 strcpy(outfilename, infilename);
563                 if(0 == (p = strrchr(outfilename, '.')))
564                         strcat(outfilename, ".flac");
565                 else {
566                         if(0 == strcmp(p, ".flac"))
567                                 strcpy(p, "_new.flac");
568                         else
569                                 strcpy(p, ".flac");
570                 }
571         }
572         if(0 == forced_outfilename)
573                 forced_outfilename = outfilename;
574         if(0 != cmdline_forced_outfilename)
575                 forced_outfilename = cmdline_forced_outfilename;
576
577         if(format_is_wave)
578                 retval = flac__encode_wav(encode_infile, infilename, forced_outfilename, 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);
579         else
580                 retval = flac__encode_raw(encode_infile, infilename, forced_outfilename, 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);
581
582         if(retval == 0 && strcmp(infilename, "-")) {
583                 if(strcmp(forced_outfilename, "-"))
584                         flac__file_copy_metadata(infilename, forced_outfilename);
585                 if(delete_input)
586                         unlink(infilename);
587         }
588
589         return retval;
590 }
591
592 int decode_file(const char *infilename, const char *forced_outfilename)
593 {
594         static const char *suffixes[] = { ".wav", ".raw", ".ana" };
595         char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
596         char *p;
597         int retval;
598
599         if(!test_only && !analyze) {
600                 if(format_is_wave < 0) {
601                         format_is_wave = true;
602                 }
603                 if(!format_is_wave) {
604                         if(format_is_big_endian < 0)
605                                 return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
606                 }
607         }
608
609         if(0 == strcmp(infilename, "-") || force_to_stdout)
610                 strcpy(outfilename, "-");
611         else {
612                 const char *suffix = suffixes[analyze? 2 : format_is_wave? 0 : 1];
613                 strcpy(outfilename, infilename);
614                 if(0 == (p = strrchr(outfilename, '.')))
615                         strcat(outfilename, suffix);
616                 else {
617                         if(0 == strcmp(p, suffix)) {
618                                 strcpy(p, "_new");
619                                 strcat(p, suffix);
620                         }
621                         else
622                                 strcpy(p, suffix);
623                 }
624         }
625         if(0 == forced_outfilename)
626                 forced_outfilename = outfilename;
627         if(0 != cmdline_forced_outfilename)
628                 forced_outfilename = cmdline_forced_outfilename;
629
630         if(format_is_wave)
631                 retval = flac__decode_wav(infilename, test_only? 0 : forced_outfilename, analyze, aopts, verbose, skip);
632         else
633                 retval = flac__decode_raw(infilename, test_only? 0 : forced_outfilename, analyze, aopts, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
634
635         if(retval == 0 && strcmp(infilename, "-")) {
636                 if(strcmp(forced_outfilename, "-"))
637                         flac__file_copy_metadata(infilename, forced_outfilename);
638                 if(delete_input)
639                         unlink(infilename);
640         }
641
642         return retval;
643 }