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