add -v, --version options
[platform/upstream/flac.git] / src / flac / main.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000,2001,2002  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 #if !defined _MSC_VER && !defined __MINGW32__
25 /* unlink is in stdio.h in VC++ */
26 #include <unistd.h> /* for unlink() */
27 #else
28 #define strcasecmp stricmp
29 #endif
30 #include "FLAC/all.h"
31 #include "analyze.h"
32 #include "decode.h"
33 #include "encode.h"
34 #include "file.h"
35
36 #if 0
37 /*[JEC] was:#if HAVE_GETOPT_LONG*/
38 /*[JEC] see flac/include/share/getopt.h as to why the change */
39 #  include <getopt.h>
40 #else
41 #  include "share/getopt.h"
42 #endif
43
44 typedef enum { RAW, WAV, AIF } FileFormat;
45
46 static int do_it();
47
48 static void init_options();
49 static int parse_options(int argc, char *argv[]);
50 static int parse_option(int short_option, const char *long_option, const char *option_argument);
51 static void free_options();
52
53 static int usage_error(const char *message, ...);
54 static void short_usage();
55 static void show_version();
56 static void show_help();
57 static void show_explain();
58 static void format_mistake(const char *infilename, const char *wrong, const char *right);
59
60 static int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file);
61 static int decode_file(const char *infilename, const char *forced_outfilename);
62
63 static void die(const char *message);
64 static char *local_strdup(const char *source);
65
66
67 /*
68  * FLAC__share__getopt format struct; note that for long options with no
69  * short option equivalent we just set the 'val' field to 0.
70  */
71 static struct FLAC__share__option long_options_[] = {
72         /*
73          * general options
74          */
75         { "help", 0, 0, 'h' },
76         { "explain", 0, 0, 'H' },
77         { "version", 0, 0, 'v' },
78         { "decode", 0, 0, 'd' },
79         { "analyze", 0, 0, 'a' },
80         { "test", 0, 0, 't' },
81         { "stdout", 0, 0, 'c' },
82         { "silent", 0, 0, 's' },
83         { "delete-input-file", 0, 0, 0 },
84         { "output-prefix", 1, 0, 0 },
85         { "output-name", 1, 0, 'o' },
86         { "skip", 1, 0, 0 },
87
88         /*
89          * decoding options
90          */
91         { "decode-through-errors", 0, 0, 'F' },
92
93         /*
94          * encoding options
95          */
96         { "compression-level-0", 0, 0, '0' },
97         { "compression-level-1", 0, 0, '1' },
98         { "compression-level-2", 0, 0, '2' },
99         { "compression-level-3", 0, 0, '3' },
100         { "compression-level-4", 0, 0, '4' },
101         { "compression-level-5", 0, 0, '5' },
102         { "compression-level-6", 0, 0, '6' },
103         { "compression-level-7", 0, 0, '7' },
104         { "compression-level-8", 0, 0, '8' },
105         { "compression-level-9", 0, 0, '9' },
106         { "best", 0, 0, '8' },
107         { "fast", 0, 0, '0' },
108         { "super-secret-impractical-compression-level", 0, 0, 0 },
109         { "verify", 0, 0, 'V' },
110         { "force-raw-input", 0, 0, 0 },
111         { "lax", 0, 0, 0 },
112         { "sector-align", 0, 0, 0 },
113         { "seekpoint", 1, 0, 'S' },
114         { "padding", 1, 0, 'P' },
115 #ifdef FLAC__HAS_OGG
116         { "ogg", 0, 0, 0 },
117 #endif
118         { "blocksize", 1, 0, 'b' },
119         { "exhaustive-model-search", 0, 0, 'e' },
120 #if 0
121         /* @@@ deprecated: */
122         { "escape-coding", 0, 0, 'E' },
123 #endif
124         { "max-lpc-order", 1, 0, 'l' },
125         { "mid-side", 0, 0, 'm' },
126         { "adaptive-mid-side", 0, 0, 'M' },
127         { "qlp-coeff-precision-search", 0, 0, 'p' },
128         { "qlp-coeff-precision", 1, 0, 'q' },
129         { "rice-partition-order", 1, 0, 'r' },
130 #if 0
131         /* @@@ deprecated: */
132         { "rice-parameter-search-distance", 1, 0, 'R' },
133 #endif
134         { "endian", 1, 0, 0 },
135         { "channels", 1, 0, 0 },
136         { "bps", 1, 0, 0 },
137         { "sample-rate", 1, 0, 0 },
138         { "sign", 1, 0, 0 },
139
140         /*
141          * analysis options
142          */
143         { "residual-gnu-plot", 0, 0, 0 },
144         { "residual-text", 0, 0, 0 },
145
146         /*
147          * negatives
148          */
149         { "no-decode-through-errors", 0, 0, 0 },
150         { "no-silent", 0, 0, 0 },
151         { "no-seektable", 0, 0, 0 },
152         { "no-delete-input-file", 0, 0, 0 },
153         { "no-sector-align", 0, 0, 0 },
154         { "no-lax", 0, 0, 0 },
155 #ifdef FLAC__HAS_OGG
156         { "no-ogg", 0, 0, 0 },
157 #endif
158         { "no-exhaustive-model-search", 0, 0, 0 },
159 #if 0
160         /* @@@ deprecated: */
161         { "no-escape-coding", 0, 0, 0 },
162 #endif
163         { "no-mid-side", 0, 0, 0 },
164         { "no-adaptive-mid-side", 0, 0, 0 },
165         { "no-qlp-coeff-prec-search", 0, 0, 0 },
166         { "no-padding", 0, 0, 0 },
167         { "no-verify", 0, 0, 0 },
168         { "no-residual-gnuplot", 0, 0, 0 },
169         { "no-residual-text", 0, 0, 0 },
170
171         {0, 0, 0, 0}
172 };
173
174
175 /*
176  * global to hold command-line option values
177  */
178
179 static struct {
180         FLAC__bool show_help;
181         FLAC__bool show_explain;
182         FLAC__bool show_version;
183         FLAC__bool mode_decode;
184         FLAC__bool verify;
185         FLAC__bool verbose;
186         FLAC__bool continue_through_decode_errors;
187         FLAC__bool lax;
188         FLAC__bool test_only;
189         FLAC__bool analyze;
190         FLAC__bool use_ogg;
191         FLAC__bool do_mid_side;
192         FLAC__bool loose_mid_side;
193         FLAC__bool do_exhaustive_model_search;
194         FLAC__bool do_escape_coding;
195         FLAC__bool do_qlp_coeff_prec_search;
196         FLAC__bool force_to_stdout;
197         FLAC__bool force_raw_format;
198         FLAC__bool delete_input;
199         FLAC__bool sector_align;
200         const char *cmdline_forced_outfilename;
201         const char *output_prefix;
202         analysis_options aopts;
203         int padding;
204         unsigned max_lpc_order;
205         unsigned qlp_coeff_precision;
206         FLAC__uint64 skip;
207         int format_is_big_endian;
208         int format_is_unsigned_samples;
209         int format_channels;
210         int format_bps;
211         int format_sample_rate;
212         int blocksize;
213         int min_residual_partition_order;
214         int max_residual_partition_order;
215         int rice_parameter_search_dist;
216         char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
217         int num_requested_seek_points; /* -1 => no -S options were given, 0 => -S- was given */
218
219         unsigned num_files;
220         char **filenames;
221 } option_values;
222
223
224 /*
225  * miscellaneous globals
226  */
227
228 static FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
229 static FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
230 static unsigned align_reservoir_samples = 0; /* 0 .. 587 */
231
232 static const char *flac_suffix = ".flac", *ogg_suffix = ".ogg";
233
234
235 int main(int argc, char *argv[])
236 {
237         int retval = 0;
238
239         init_options();
240
241         if((retval = parse_options(argc, argv)) == 0)
242                 retval = do_it();
243
244         free_options();
245
246         return retval;
247 }
248
249 int do_it()
250 {
251         int retval = 0;
252
253         if(option_values.show_version) {
254                 show_version();
255                 return 0;
256         }
257         else if(option_values.show_explain) {
258                 show_explain();
259                 return 0;
260         }
261         else if(option_values.show_help) {
262                 show_help();
263                 return 0;
264         }
265         else {
266                 if(option_values.num_files == 0) {
267                         short_usage();
268                         return 0;
269                 }
270
271                 /*
272                  * tweak options; validate the values
273                  */
274                 if(!option_values.mode_decode) {
275                         if(option_values.blocksize < 0) {
276                                 if(option_values.max_lpc_order == 0)
277                                         option_values.blocksize = 1152;
278                                 else
279                                         option_values.blocksize = 4608;
280                         }
281                         if(option_values.max_residual_partition_order < 0) {
282                                 if(option_values.blocksize <= 1152)
283                                         option_values.max_residual_partition_order = 2;
284                                 else if(option_values.blocksize <= 2304)
285                                         option_values.max_residual_partition_order = 3;
286                                 else if(option_values.blocksize <= 4608)
287                                         option_values.max_residual_partition_order = 3;
288                                 else
289                                         option_values.max_residual_partition_order = 4;
290                                 option_values.min_residual_partition_order = option_values.max_residual_partition_order;
291                         }
292                         if(option_values.rice_parameter_search_dist < 0) {
293                                 option_values.rice_parameter_search_dist = 0;
294                         }
295                 }
296                 else {
297                         if(option_values.test_only) {
298                                 if(option_values.skip > 0)
299                                         return usage_error("ERROR: --skip is not allowed in test mode\n");
300                         }
301                 }
302
303                 FLAC__ASSERT(option_values.blocksize >= 0 || option_values.mode_decode);
304
305                 if(option_values.format_channels >= 0) {
306                         if(option_values.format_channels == 0 || (unsigned)option_values.format_channels > FLAC__MAX_CHANNELS)
307                                 return usage_error("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", option_values.format_channels, FLAC__MAX_CHANNELS);
308                 }
309                 if(option_values.format_bps >= 0) {
310                         if(option_values.format_bps != 8 && option_values.format_bps != 16 && option_values.format_bps != 24)
311                                 return usage_error("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", option_values.format_bps);
312                 }
313                 if(option_values.format_sample_rate >= 0) {
314                         if(!FLAC__format_sample_rate_is_valid(option_values.format_sample_rate))
315                                 return usage_error("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", option_values.format_sample_rate, FLAC__MAX_SAMPLE_RATE);
316                 }
317                 if(!option_values.mode_decode && ((unsigned)option_values.blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)option_values.blocksize > FLAC__MAX_BLOCK_SIZE)) {
318                         return usage_error("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)option_values.blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
319                 }
320                 if(option_values.qlp_coeff_precision > 0 && option_values.qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
321                         return usage_error("ERROR: invalid value '%u' for qlp coeff precision, must be 0 or >= %u\n", option_values.qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
322                 }
323
324                 if(option_values.sector_align) {
325                         if(option_values.mode_decode)
326                                 return usage_error("ERROR: --sector-align only allowed for encoding\n");
327                         else if(option_values.skip > 0)
328                                 return usage_error("ERROR: --sector-align not allowed with --skip\n");
329                         else if(option_values.format_channels >= 0 && option_values.format_channels != 2)
330                                 return usage_error("ERROR: --sector-align can only be done with stereo input\n");
331                         else if(option_values.format_bps >= 0 && option_values.format_bps != 16)
332                                 return usage_error("ERROR: --sector-align can only be done with 16-bit samples\n");
333                         else if(option_values.format_sample_rate >= 0 && option_values.format_sample_rate != 44100)
334                                 return usage_error("ERROR: --sector-align can only be done with a sample rate of 44100\n");
335                 }
336                 if(option_values.num_files > 1 && option_values.cmdline_forced_outfilename) {
337                         return usage_error("ERROR: -o/--output-name cannot be used with multiple files\n");
338                 }
339                 if(option_values.cmdline_forced_outfilename && option_values.output_prefix) {
340                         return usage_error("ERROR: --output-prefix conflicts with -o/--output-name\n");
341                 }
342         }
343         if(option_values.verbose) {
344                 fprintf(stderr, "\n");
345                 fprintf(stderr, "flac %s, Copyright (C) 2000,2001,2002 Josh Coalson\n", FLAC__VERSION_STRING);
346                 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
347                 fprintf(stderr, "welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
348
349                 if(!option_values.mode_decode) {
350                         char padopt[16];
351                         if(option_values.padding < 0)
352                                 strcpy(padopt, "-");
353                         else
354                                 sprintf(padopt, " %d", option_values.padding);
355                         fprintf(stderr,
356                                 "options:%s%s"
357 #ifdef FLAC__HAS_OGG
358                                 "%s"
359 #endif
360                                 "%s -P%s -b %u%s -l %u%s%s%s -q %u -r %u,%u%s\n",
361                                 option_values.delete_input?" --delete-input-file":"",
362                                 option_values.sector_align?" --sector-align":"",
363 #ifdef FLAC__HAS_OGG
364                                 option_values.use_ogg?" --ogg":"",
365 #endif
366                                 option_values.lax?" --lax":"",
367                                 padopt,
368                                 (unsigned)option_values.blocksize,
369                                 option_values.loose_mid_side?" -M":option_values.do_mid_side?" -m":"",
370                                 option_values.max_lpc_order,
371                                 option_values.do_exhaustive_model_search?" -e":"",
372                                 option_values.do_escape_coding?" -E":"",
373                                 option_values.do_qlp_coeff_prec_search?" -p":"",
374                                 option_values.qlp_coeff_precision,
375                                 (unsigned)option_values.min_residual_partition_order,
376                                 (unsigned)option_values.max_residual_partition_order,
377                                 option_values.verify? " -V":""
378                         );
379                 }
380         }
381
382         if(option_values.mode_decode) {
383                 FLAC__bool first = true;
384
385                 if(option_values.num_files == 0) {
386                         retval = decode_file("-", 0);
387                 }
388                 else {
389                         unsigned i;
390                         if(option_values.num_files > 1)
391                                 option_values.cmdline_forced_outfilename = 0;
392                         for(i = 0, retval = 0; i < option_values.num_files; i++) {
393                                 if(0 == strcmp(option_values.filenames[i], "-") && !first)
394                                         continue;
395                                 retval |= decode_file(option_values.filenames[i], 0);
396                                 first = false;
397                         }
398                 }
399         }
400         else { /* encode */
401                 FLAC__bool first = true;
402
403                 if(option_values.num_files == 0) {
404                         retval = encode_file("-", 0, true);
405                 }
406                 else {
407                         unsigned i;
408                         if(option_values.num_files > 1)
409                                 option_values.cmdline_forced_outfilename = 0;
410                         for(i = 0, retval = 0; i < option_values.num_files; i++) {
411                                 if(0 == strcmp(option_values.filenames[i], "-") && !first)
412                                         continue;
413                                 retval |= encode_file(option_values.filenames[i], 0, i == (option_values.num_files-1));
414                                 first = false;
415                         }
416                 }
417         }
418
419         return retval;
420 }
421
422 void init_options()
423 {
424         option_values.show_help = false;
425         option_values.show_explain = false;
426         option_values.mode_decode = false;
427         option_values.verify = false;
428         option_values.verbose = true;
429         option_values.continue_through_decode_errors = false;
430         option_values.lax = false;
431         option_values.test_only = false;
432         option_values.analyze = false;
433         option_values.use_ogg = false;
434         option_values.do_mid_side = true;
435         option_values.loose_mid_side = false;
436         option_values.do_exhaustive_model_search = false;
437         option_values.do_escape_coding = false;
438         option_values.do_qlp_coeff_prec_search = false;
439         option_values.force_to_stdout = false;
440         option_values.force_raw_format = false;
441         option_values.delete_input = false;
442         option_values.sector_align = false;
443         option_values.cmdline_forced_outfilename = 0;
444         option_values.output_prefix = 0;
445         option_values.aopts.do_residual_text = false;
446         option_values.aopts.do_residual_gnuplot = false;
447         option_values.padding = -1;
448         option_values.max_lpc_order = 8;
449         option_values.qlp_coeff_precision = 0;
450         option_values.skip = 0;
451         option_values.format_is_big_endian = -1;
452         option_values.format_is_unsigned_samples = false;
453         option_values.format_channels = -1;
454         option_values.format_bps = -1;
455         option_values.format_sample_rate = -1;
456         option_values.blocksize = -1;
457         option_values.min_residual_partition_order = -1;
458         option_values.max_residual_partition_order = -1;
459         option_values.rice_parameter_search_dist = -1;
460         option_values.requested_seek_points[0] = '\0';
461         option_values.num_requested_seek_points = -1;
462
463         option_values.num_files = 0;
464         option_values.filenames = 0;
465 }
466
467 int parse_options(int argc, char *argv[])
468 {
469         int short_option;
470         int option_index = 1;
471         FLAC__bool had_error = false;
472         /*@@@ E and R: are deprecated */
473         const char *short_opts = "0123456789ab:cdeFhHl:mMo:pP:q:r:sS:tvV";
474
475         while ((short_option = FLAC__share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
476                 switch (short_option) {
477                         case 0: /* long option with no equivalent short option */
478                                 had_error |= (parse_option(short_option, long_options_[option_index].name, FLAC__share__optarg) != 0);
479                                 break;
480                         case '?':
481                         case ':':
482                                 had_error = true;
483                                 break;
484                         default: /* short option */
485                                 had_error |= (parse_option(short_option, 0, FLAC__share__optarg) != 0);
486                                 break;
487                 }
488         }
489
490         if(had_error) {
491                 return 1;
492         }
493
494         FLAC__ASSERT(FLAC__share__optind <= argc);
495
496         option_values.num_files = argc - FLAC__share__optind;
497
498         if(option_values.num_files > 0) {
499                 unsigned i = 0;
500                 if(0 == (option_values.filenames = malloc(sizeof(char *) * option_values.num_files)))
501                         die("out of memory allocating space for file names list");
502                 while(FLAC__share__optind < argc)
503                         option_values.filenames[i++] = local_strdup(argv[FLAC__share__optind++]);
504         }
505
506         return 0;
507 }
508
509 int parse_option(int short_option, const char *long_option, const char *option_argument)
510 {
511         char *p;
512
513         if(short_option == 0) {
514                 FLAC__ASSERT(0 != long_option);
515                 if(0 == strcmp(long_option, "delete-input-file")) {
516                         option_values.delete_input = true;
517                 }
518                 else if(0 == strcmp(long_option, "output-prefix")) {
519                         FLAC__ASSERT(0 != option_argument);
520                         option_values.output_prefix = option_argument;
521                 }
522                 else if(0 == strcmp(long_option, "skip")) {
523                         FLAC__ASSERT(0 != option_argument);
524                         option_values.skip = (FLAC__uint64)atoi(option_argument); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
525                 }
526                 else if(0 == strcmp(long_option, "super-secret-impractical-compression-level")) {
527                         option_values.do_exhaustive_model_search = true;
528                         option_values.do_escape_coding = true;
529                         option_values.do_mid_side = true;
530                         option_values.loose_mid_side = false;
531                         option_values.do_qlp_coeff_prec_search = true;
532                         option_values.min_residual_partition_order = 0;
533                         option_values.max_residual_partition_order = 16;
534                         option_values.rice_parameter_search_dist = 0;
535                         option_values.max_lpc_order = 32;
536                 }
537                 else if(0 == strcmp(long_option, "force-raw-input")) {
538                         option_values.force_raw_format = true;
539                 }
540                 else if(0 == strcmp(long_option, "lax")) {
541                         option_values.lax = true;
542                 }
543                 else if(0 == strcmp(long_option, "sector-align")) {
544                         option_values.sector_align = true;
545                 }
546 #ifdef FLAC__HAS_OGG
547                 else if(0 == strcmp(long_option, "ogg")) {
548                         option_values.use_ogg = true;
549                 }
550 #endif
551                 else if(0 == strcmp(long_option, "endian")) {
552                         FLAC__ASSERT(0 != option_argument);
553                         if(0 == strncmp(option_argument, "big", strlen(option_argument)))
554                                 option_values.format_is_big_endian = true;
555                         else if(0 == strncmp(option_argument, "little", strlen(option_argument)))
556                                 option_values.format_is_big_endian = false;
557                         else
558                                 return usage_error("ERROR: argument to --endian must be \"big\" or \"little\"\n");
559                 }
560                 else if(0 == strcmp(long_option, "channels")) {
561                         FLAC__ASSERT(0 != option_argument);
562                         option_values.format_channels = atoi(option_argument);
563                 }
564                 else if(0 == strcmp(long_option, "bps")) {
565                         FLAC__ASSERT(0 != option_argument);
566                         option_values.format_bps = atoi(option_argument);
567                 }
568                 else if(0 == strcmp(long_option, "sample-rate")) {
569                         FLAC__ASSERT(0 != option_argument);
570                         option_values.format_sample_rate = atoi(option_argument);
571                 }
572                 else if(0 == strcmp(long_option, "sign")) {
573                         FLAC__ASSERT(0 != option_argument);
574                         if(0 == strncmp(option_argument, "signed", strlen(option_argument)))
575                                 option_values.format_is_unsigned_samples = false;
576                         else if(0 == strncmp(option_argument, "unsigned", strlen(option_argument)))
577                                 option_values.format_is_unsigned_samples = true;
578                         else
579                                 return usage_error("ERROR: argument to --sign must be \"signed\" or \"unsigned\"\n");
580                 }
581                 else if(0 == strcmp(long_option, "residual-gnu-plot")) {
582                         option_values.aopts.do_residual_gnuplot = true;
583                 }
584                 else if(0 == strcmp(long_option, "residual-text")) {
585                         option_values.aopts.do_residual_text = true;
586                 }
587                 /*
588                  * negatives
589                  */
590                 else if(0 == strcmp(long_option, "no-decode-through-errors")) {
591                         option_values.continue_through_decode_errors = false;
592                 }
593                 else if(0 == strcmp(long_option, "no-silent")) {
594                         option_values.verbose = true;
595                 }
596                 else if(0 == strcmp(long_option, "no-seektable")) {
597                         option_values.num_requested_seek_points = 0;
598                         option_values.requested_seek_points[0] = '\0';
599                 }
600                 else if(0 == strcmp(long_option, "no-delete-input-file")) {
601                         option_values.delete_input = false;
602                 }
603                 else if(0 == strcmp(long_option, "no-sector-align")) {
604                         option_values.sector_align = false;
605                 }
606                 else if(0 == strcmp(long_option, "no-lax")) {
607                         option_values.lax = false;
608                 }
609 #ifdef FLAC__HAS_OGG
610                 else if(0 == strcmp(long_option, "no-ogg")) {
611                         option_values.use_ogg = false;
612                 }
613 #endif
614                 else if(0 == strcmp(long_option, "no-exhaustive-model-search")) {
615                         option_values.do_exhaustive_model_search = false;
616                 }
617 #if 0
618                 /* @@@ deprecated: */
619                 else if(0 == strcmp(long_option, "no-escape-coding")) {
620                         option_values.do_escape_coding = false;
621                 }
622 #endif
623                 else if(0 == strcmp(long_option, "no-mid-side")) {
624                         option_values.do_mid_side = option_values.loose_mid_side = false;
625                 }
626                 else if(0 == strcmp(long_option, "no-adaptive-mid-side")) {
627                         option_values.loose_mid_side = option_values.do_mid_side = false;
628                 }
629                 else if(0 == strcmp(long_option, "no-qlp-coeff-prec-search")) {
630                         option_values.do_qlp_coeff_prec_search = false;
631                 }
632                 else if(0 == strcmp(long_option, "no-padding")) {
633                         option_values.padding = -1;
634                 }
635                 else if(0 == strcmp(long_option, "no-verify")) {
636                         option_values.verify = false;
637                 }
638                 else if(0 == strcmp(long_option, "no-residual-gnuplot")) {
639                         option_values.aopts.do_residual_gnuplot = false;
640                 }
641                 else if(0 == strcmp(long_option, "no-residual-text")) {
642                         option_values.aopts.do_residual_text = false;
643                 }
644         }
645         else {
646                 switch(short_option) {
647                         case 'h':
648                                 option_values.show_help = true;
649                                 break;
650                         case 'H':
651                                 option_values.show_explain = true;
652                                 break;
653                         case 'v':
654                                 option_values.show_version = true;
655                                 break;
656                         case 'd':
657                                 option_values.mode_decode = true;
658                                 break;
659                         case 'a':
660                                 option_values.mode_decode = true;
661                                 option_values.analyze = true;
662                                 break;
663                         case 't':
664                                 option_values.mode_decode = true;
665                                 option_values.test_only = true;
666                                 break;
667                         case 'c':
668                                 option_values.force_to_stdout = true;
669                                 break;
670                         case 's':
671                                 option_values.verbose = false;
672                                 break;
673                         case 'o':
674                                 FLAC__ASSERT(0 != option_argument);
675                                 option_values.cmdline_forced_outfilename = option_argument;
676                                 break;
677                         case 'F':
678                                 option_values.continue_through_decode_errors = true;
679                                 break;
680                         case '0':
681                                 option_values.do_exhaustive_model_search = false;
682                                 option_values.do_escape_coding = false;
683                                 option_values.do_mid_side = false;
684                                 option_values.loose_mid_side = false;
685                                 option_values.qlp_coeff_precision = 0;
686                                 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
687                                 option_values.rice_parameter_search_dist = 0;
688                                 option_values.max_lpc_order = 0;
689                                 break;
690                         case '1':
691                                 option_values.do_exhaustive_model_search = false;
692                                 option_values.do_escape_coding = false;
693                                 option_values.do_mid_side = true;
694                                 option_values.loose_mid_side = true;
695                                 option_values.qlp_coeff_precision = 0;
696                                 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
697                                 option_values.rice_parameter_search_dist = 0;
698                                 option_values.max_lpc_order = 0;
699                                 break;
700                         case '2':
701                                 option_values.do_exhaustive_model_search = false;
702                                 option_values.do_escape_coding = false;
703                                 option_values.do_mid_side = true;
704                                 option_values.loose_mid_side = false;
705                                 option_values.qlp_coeff_precision = 0;
706                                 option_values.min_residual_partition_order = 0;
707                                 option_values.max_residual_partition_order = 3;
708                                 option_values.rice_parameter_search_dist = 0;
709                                 option_values.max_lpc_order = 0;
710                                 break;
711                         case '3':
712                                 option_values.do_exhaustive_model_search = false;
713                                 option_values.do_escape_coding = false;
714                                 option_values.do_mid_side = false;
715                                 option_values.loose_mid_side = false;
716                                 option_values.qlp_coeff_precision = 0;
717                                 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
718                                 option_values.rice_parameter_search_dist = 0;
719                                 option_values.max_lpc_order = 6;
720                                 break;
721                         case '4':
722                                 option_values.do_exhaustive_model_search = false;
723                                 option_values.do_escape_coding = false;
724                                 option_values.do_mid_side = true;
725                                 option_values.loose_mid_side = true;
726                                 option_values.qlp_coeff_precision = 0;
727                                 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
728                                 option_values.rice_parameter_search_dist = 0;
729                                 option_values.max_lpc_order = 8;
730                                 break;
731                         case '5':
732                                 option_values.do_exhaustive_model_search = false;
733                                 option_values.do_escape_coding = false;
734                                 option_values.do_mid_side = true;
735                                 option_values.loose_mid_side = false;
736                                 option_values.qlp_coeff_precision = 0;
737                                 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
738                                 option_values.rice_parameter_search_dist = 0;
739                                 option_values.max_lpc_order = 8;
740                                 break;
741                         case '6':
742                                 option_values.do_exhaustive_model_search = false;
743                                 option_values.do_escape_coding = false;
744                                 option_values.do_mid_side = true;
745                                 option_values.loose_mid_side = false;
746                                 option_values.qlp_coeff_precision = 0;
747                                 option_values.min_residual_partition_order = 0;
748                                 option_values.max_residual_partition_order = 4;
749                                 option_values.rice_parameter_search_dist = 0;
750                                 option_values.max_lpc_order = 8;
751                                 break;
752                         case '7':
753                                 option_values.do_exhaustive_model_search = true;
754                                 option_values.do_escape_coding = false;
755                                 option_values.do_mid_side = true;
756                                 option_values.loose_mid_side = false;
757                                 option_values.qlp_coeff_precision = 0;
758                                 option_values.min_residual_partition_order = 0;
759                                 option_values.max_residual_partition_order = 6;
760                                 option_values.rice_parameter_search_dist = 0;
761                                 option_values.max_lpc_order = 8;
762                                 break;
763                         case '8':
764                                 option_values.do_exhaustive_model_search = true;
765                                 option_values.do_escape_coding = false;
766                                 option_values.do_mid_side = true;
767                                 option_values.loose_mid_side = false;
768                                 option_values.qlp_coeff_precision = 0;
769                                 option_values.min_residual_partition_order = 0;
770                                 option_values.max_residual_partition_order = 6;
771                                 option_values.rice_parameter_search_dist = 0;
772                                 option_values.max_lpc_order = 12;
773                                 break;
774                         case '9':
775                                 return usage_error("ERROR: compression level '9' is reserved\n");
776                         case 'V':
777                                 option_values.verify = true;
778                                 break;
779                         case 'S':
780                                 FLAC__ASSERT(0 != option_argument);
781                                 if(option_values.num_requested_seek_points < 0)
782                                         option_values.num_requested_seek_points = 0;
783                                 option_values.num_requested_seek_points++;
784                                 strcat(option_values.requested_seek_points, option_argument);
785                                 strcat(option_values.requested_seek_points, "<");
786                                 break;
787                         case 'P':
788                                 FLAC__ASSERT(0 != option_argument);
789                                 option_values.padding = atoi(option_argument);
790                                 if(option_values.padding < 0)
791                                         return usage_error("ERROR: argument to -P must be >= 0\n");
792                                 break;
793                         case 'b':
794                                 FLAC__ASSERT(0 != option_argument);
795                                 option_values.blocksize = atoi(option_argument);
796                                 break;
797                         case 'e':
798                                 option_values.do_exhaustive_model_search = true;
799                                 break;
800                         case 'E':
801                                 option_values.do_escape_coding = true;
802                                 break;
803                         case 'l':
804                                 FLAC__ASSERT(0 != option_argument);
805                                 option_values.max_lpc_order = atoi(option_argument);
806                                 break;
807                         case 'm':
808                                 option_values.do_mid_side = true;
809                                 option_values.loose_mid_side = false;
810                                 break;
811                         case 'M':
812                                 option_values.loose_mid_side = option_values.do_mid_side = true;
813                                 break;
814                         case 'p':
815                                 option_values.do_qlp_coeff_prec_search = true;
816                                 break;
817                         case 'q':
818                                 FLAC__ASSERT(0 != option_argument);
819                                 option_values.qlp_coeff_precision = atoi(option_argument);
820                                 break;
821                         case 'r':
822                                 FLAC__ASSERT(0 != option_argument);
823                                 p = strchr(option_argument, ',');
824                                 if(0 == p) {
825                                         option_values.min_residual_partition_order = 0;
826                                         option_values.max_residual_partition_order = atoi(option_argument);
827                                 }
828                                 else {
829                                         option_values.min_residual_partition_order = atoi(option_argument);
830                                         option_values.max_residual_partition_order = atoi(++p);
831                                 }
832                                 break;
833                         case 'R':
834                                 FLAC__ASSERT(0 != option_argument);
835                                 option_values.rice_parameter_search_dist = atoi(option_argument);
836                                 break;
837                         default:
838                                 FLAC__ASSERT(0);
839                 }
840         }
841
842         return 0;
843 }
844
845 void free_options()
846 {
847         if(0 != option_values.filenames)
848                 free(option_values.filenames);
849 }
850
851 int usage_error(const char *message, ...)
852 {
853         va_list args;
854
855         FLAC__ASSERT(0 != message);
856
857         va_start(args, message);
858
859         (void) vfprintf(stderr, message, args);
860
861         va_end(args);
862
863         printf("Type \"flac\" for a usage summary or \"flac --help\" for all options\n");
864
865         return 1;
866 }
867
868 void show_version()
869 {
870         printf("flac %s\n", FLAC__VERSION_STRING);
871 }
872
873 static void usage_header()
874 {
875         printf("===============================================================================\n");
876         printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
877         printf("Copyright (C) 2000,2001,2002  Josh Coalson\n");
878         printf("\n");
879         printf("This program is free software; you can redistribute it and/or\n");
880         printf("modify it under the terms of the GNU General Public License\n");
881         printf("as published by the Free Software Foundation; either version 2\n");
882         printf("of the License, or (at your option) any later version.\n");
883         printf("\n");
884         printf("This program is distributed in the hope that it will be useful,\n");
885         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
886         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
887         printf("GNU General Public License for more details.\n");
888         printf("\n");
889         printf("You should have received a copy of the GNU General Public License\n");
890         printf("along with this program; if not, write to the Free Software\n");
891         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
892         printf("===============================================================================\n");
893 }
894
895 static void usage_summary()
896 {
897         printf("Usage:\n");
898         printf("\n");
899         printf(" Encoding: flac [-s] [--skip #] [<encoding/format-options>] [INPUTFILE [...]]\n");
900         printf(" Decoding: flac -d [-s] [--skip #] [-F] [<format-options>] [INPUTFILE [...]]\n");
901         printf("  Testing: flac -t [-s] [INPUTFILE [...]]\n");
902         printf("Analyzing: flac -a [-s] [--skip #] [<analysis-options>] [INPUTFILE [...]]\n");
903         printf("\n");
904 }
905
906 void short_usage()
907 {
908         usage_header();
909         printf("\n");
910         printf("This is the short help; for all options use 'flac --help'; for even more\n");
911         printf("instructions use 'flac --explain'\n");
912         printf("\n");
913         printf("To encode:\n");
914         printf("  flac [-#] [INPUTFILE [...]]\n");
915         printf("\n");
916         printf("  -# is -0 (fastest compression) to -8 (highest compression); -5 is the default\n");
917         printf("\n");
918         printf("To decode:\n");
919         printf("  flac -d [INPUTFILE [...]]\n");
920         printf("\n");
921         printf("To test:\n");
922         printf("  flac -t [INPUTFILE [...]]\n");
923 }
924
925 void show_help()
926 {
927         usage_header();
928         usage_summary();
929         printf("generic options:\n");
930         printf("  -v, --version                Show the flac version number\n");
931         printf("  -h, --help                   Show this screen\n");
932         printf("  -H, --explain                Show detailed explanation of usage and options\n");
933         printf("  -d, --decode                 Decode (the default behavior is to encode)\n");
934         printf("  -t, --test                   Same as -d except no decoded file is written\n");
935         printf("  -a, --analyze                Same as -d except an analysis file is written\n");
936         printf("  -c, --stdout                 Write output to stdout\n");
937         printf("  -s, --silent                 Do not write runtime encode/decode statistics\n");
938         printf("  -o, --output-name=FILENAME   Force the output file name\n");
939         printf("      --output-prefix=STRING   Prepend STRING to output names\n");
940         printf("      --delete-input-file      Deletes after a successful encode/decode\n");
941         printf("      --skip=#                 Skip the first # samples of each input file\n");
942         printf("analysis options:\n");
943         printf("      --residual-text          Include residual signal in text output\n");
944         printf("      --residual-gnuplot       Generate gnuplot files of residual distribution\n");
945         printf("decoding options:\n");
946         printf("  -F, --decode-through-errors  Continue decoding through stream errors\n");
947         printf("encoding options:\n");
948         printf("  -V, --verify                 Verify a correct encoding\n");
949 #ifdef FLAC__HAS_OGG
950         printf("      --ogg                    Use Ogg as transport layer\n");
951 #endif
952         printf("      --lax                    Allow encoder to generate non-Subset files\n");
953         printf("      --sector-align           Align multiple files on sector boundaries\n");
954         printf("  -S, --seekpoint={#|X|#x}     Add seek point(s)\n");
955         printf("  -P, --padding=#              Write a PADDING block of length #\n");
956         printf("  -0, --compression-level-0, --fast  Synonymous with -l 0 -b 1152 -r 2,2\n");
957         printf("  -1, --compression-level-1          Synonymous with -l 0 -b 1152 -M -r 2,2\n");
958         printf("  -2, --compression-level-2          Synonymous with -l 0 -b 1152 -m -r 3\n");
959         printf("  -3, --compression-level-3          Synonymous with -l 6 -b 4608 -r 3,3\n");
960         printf("  -4, --compression-level-4          Synonymous with -l 8 -b 4608 -M -r 3,3\n");
961         printf("  -5, --compression-level-5          Synonymous with -l 8 -b 4608 -m -r 3,3\n");
962         printf("  -6, --compression-level-6          Synonymous with -l 8 -b 4608 -m -r 4\n");
963         printf("  -7, --compression-level-7          Synonymous with -l 8 -b 4608 -m -e -r 6\n");
964         printf("  -8, --compression-level-8, --best  Synonymous with -l 12 -b 4608 -m -e -r 6\n");
965         printf("  -b, --blocksize=#                  Specify blocksize in samples\n");
966         printf("  -m, --mid-side                     Try mid-side coding for each frame\n");
967         printf("  -M, --adaptive-mid-side            Adaptive mid-side coding for all frames\n");
968         printf("  -e, --exhaustive-model-search      Do exhaustive model search (expensive!)\n");
969 #if 0
970         /*@@@ deprecated: */
971         printf("  -E, --escape-coding                Do escape coding in the entropy coder\n");
972 #endif
973         printf("  -l, --max-lpc-order=#              Max LPC order; 0 => only fixed predictors\n");
974         printf("  -p, --qlp-coeff-precision-search   Exhaustively search LP coeff quantization\n");
975         printf("  -q, --qlp-coeff-precision=#        Specify precision in bits\n");
976         printf("  -r, --rice-partition-order=[#,]#   Set [min,]max residual partition order\n");
977 #if 0
978         /*@@@ deprecated: */
979         printf("  -R, -rice-parameter-search-distance=#   Rice parameter search distance\n");
980 #endif
981         printf("format options:\n");
982         printf("      --endian={big|little}    Set byte order for samples\n");
983         printf("      --channels=#             Number of channels\n");
984         printf("      --bps=#                  Number of bits per sample\n");
985         printf("      --sample-rate=#          Sample rate in Hz\n");
986         printf("      --sign={signed|unsigned} Sign of samples\n");
987         printf("      --force-raw-input        Force input to be treated as raw samples\n");
988         printf("negative options:\n");
989         printf("      --no-adaptive-mid-side\n");
990         printf("      --no-decode-through-errors\n");
991         printf("      --no-delete-input-file\n");
992 #if 0
993 /* @@@ deprecated: */
994         printf("      --no-escape-coding\n");
995 #endif
996         printf("      --no-exhaustive-model-search\n");
997         printf("      --no-lax\n");
998         printf("      --no-mid-side\n");
999 #ifdef FLAC__HAS_OGG
1000         printf("      --no-ogg\n");
1001 #endif
1002         printf("      --no-padding\n");
1003         printf("      --no-qlp-coeff-prec-search\n");
1004         printf("      --no-residual-gnuplot\n");
1005         printf("      --no-residual-text\n");
1006         printf("      --no-sector-align\n");
1007         printf("      --no-seektable\n");
1008         printf("      --no-silent\n");
1009         printf("      --no-verify\n");
1010 }
1011
1012 void show_explain()
1013 {
1014         usage_header();
1015         usage_summary();
1016         printf("For encoding:\n");
1017         printf("  The input file(s) may be a PCM RIFF WAVE file, AIFF file, or raw samples.\n");
1018         printf("  The output file(s) will be in native FLAC or Ogg FLAC format\n");
1019         printf("For decoding, the reverse is true.\n");
1020         printf("\n");
1021         printf("A single INPUTFILE may be - for stdin.  No INPUTFILE implies stdin.  Use of\n");
1022         printf("stdin implies -c (write to stdout).  Normally you should use:\n");
1023         printf("   flac [options] -o outfilename  or  flac -d [options] -o outfilename\n");
1024         printf("instead of:\n");
1025         printf("   flac [options] > outfilename   or  flac -d [options] > outfilename\n");
1026         printf("since the former allows flac to seek backwards to write the STREAMINFO or\n");
1027         printf("WAVE/AIFF header contents when necessary.\n");
1028         printf("\n");
1029         printf("flac checks for the presence of a AIFF/WAVE header to decide whether or not to\n");
1030         printf("treat an input file as AIFF/WAVE format or raw samples.  If any input file is\n");
1031         printf("raw you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n");
1032         printf("apply to all raw files.  You can force AIFF/WAVE files to be treated as raw\n");
1033         printf("files using -fr.\n");
1034         printf("\n");
1035         printf("generic options:\n");
1036         printf("  -v, --version                Show the flac version number\n");
1037         printf("  -h, --help                   Show basic usage a list of all options\n");
1038         printf("  -H, --explain                Show this screen\n");
1039         printf("  -d, --decode                 Decode (the default behavior is to encode)\n");
1040         printf("  -t, --test                   Same as -d except no decoded file is written\n");
1041         printf("  -a, --analyze                Same as -d except an analysis file is written\n");
1042         printf("  -c, --stdout                 Write output to stdout\n");
1043         printf("  -s, --silent                 Do not write runtime encode/decode statistics\n");
1044         printf("  -o, --output-name=FILENAME   Force the output file name; usually flac just\n");
1045         printf("                               changes the extension.  May only be used when\n");
1046         printf("                               encoding a single file.  May not be used in\n");
1047         printf("                               conjunction with --output-prefix.\n");
1048         printf("      --output-prefix=STRING   Prefix each output file name with the given\n");
1049         printf("                               STRING.  This can be useful for encoding or\n");
1050         printf("                               decoding files to a different directory.  Make\n");
1051         printf("                               sure if your STRING is a path name that it ends\n");
1052         printf("                               with a '/' slash.\n");
1053         printf("      --delete-input-file      Automatically delete the input file after a\n");
1054         printf("                               successful encode or decode.  If there was an\n");
1055         printf("                               error (including a verify error) the input file\n");
1056         printf("                               is left intact.\n");
1057         printf("      --skip=#                 Skip the first # samples of each input file; can\n");
1058         printf("                               be used both for encoding and decoding\n");
1059         printf("analysis options:\n");
1060         printf("      --residual-text          Include residual signal in text output.  This\n");
1061         printf("                               will make the file very big, much larger than\n");
1062         printf("                               even the decoded file.\n");
1063         printf("      --residual-gnuplot       Generate gnuplot files of residual distribution\n");
1064         printf("                               of each subframe\n");
1065         printf("decoding options:\n");
1066         printf("  -F, --decode-through-errors  By default flac stops decoding with an error\n");
1067         printf("                               and removes the partially decoded file if it\n");
1068         printf("                               encounters a bitstream error.  With -F, errors\n");
1069         printf("                               are still printed but flac will continue\n");
1070         printf("                               decoding to completion.  Note that errors may\n");
1071         printf("                               cause the decoded audio to be missing some\n");
1072         printf("                               samples or have silent sections.\n");
1073         printf("encoding options:\n");
1074         printf("  -V, --verify                 Verify a correct encoding by decoding the\n");
1075         printf("                               output in parallel and comparing to the\n");
1076         printf("                               original\n");
1077 #ifdef FLAC__HAS_OGG
1078         printf("      --ogg                    When encoding, generate Ogg-FLAC output instead\n");
1079         printf("                               of native-FLAC.  Ogg-FLAC streams are FLAC\n");
1080         printf("                               streams wrapped in an Ogg transport layer.  The\n");
1081         printf("                               resulting file should have an '.ogg' extension\n");
1082         printf("                               and will still be decodable by flac.  When\n");
1083         printf("                               decoding, force the input to be treated as\n");
1084         printf("                               Ogg-FLAC.  This is useful when piping input\n");
1085         printf("                               from stdin or when the filename does not end in\n");
1086         printf("                               '.ogg'.\n");
1087 #endif
1088         printf("      --lax                    Allow encoder to generate non-Subset files\n");
1089         printf("      --sector-align           Align encoding of multiple CD format WAVE files\n");
1090         printf("                               on sector boundaries.\n");
1091         printf("  -S, --seekpoint={#|X|#x}     Include a point or points in a SEEKTABLE\n");
1092         printf("       #  : a specific sample number for a seek point\n");
1093         printf("       X  : a placeholder point (always goes at the end of the SEEKTABLE)\n");
1094         printf("       #x : # evenly spaced seekpoints, the first being at sample 0\n");
1095         printf("     You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
1096         printf("           ified union of all such values.\n");
1097         printf("     With no -S options, flac defaults to '-S 100x'.  Use -S- for no SEEKTABLE.\n");
1098         printf("     Note: -S #x will not work if the encoder can't determine the input size\n");
1099         printf("           before starting.\n");
1100         printf("     Note: if you use -S # and # is >= samples in the input, there will be\n");
1101         printf("           either no seek point entered (if the input size is determinable\n");
1102         printf("           before encoding starts) or a placeholder point (if input size is not\n");
1103         printf("           determinable)\n");
1104         printf("  -P, --padding=#              Tell the encoder to write a PADDING metadata\n");
1105         printf("                               block of the given length (in bytes) after the\n");
1106         printf("                               STREAMINFO block.  This is useful if you plan\n");
1107         printf("                               to tag the file later with an APPLICATION\n");
1108         printf("                               block; instead of having to rewrite the entire\n");
1109         printf("                               file later just to insert your block, you can\n");
1110         printf("                               write directly over the PADDING block.  Note\n");
1111         printf("                               that the total length of the PADDING block will\n");
1112         printf("                               be 4 bytes longer than the length given because\n");
1113         printf("                               of the 4 metadata block header bytes.  You can\n");
1114         printf("                               force no PADDING block at all to be written with\n");
1115         printf("                               --no-padding, which is the default.\n");
1116         printf("  -b, --blocksize=#            Specify the blocksize in samples; the default is\n");
1117         printf("                               1152 for -l 0, else 4608; must be one of 192,\n");
1118         printf("                               576, 1152, 2304, 4608, 256, 512, 1024, 2048,\n");
1119         printf("                               4096, 8192, 16384, or 32768 (unless --lax is\n");
1120         printf("                               used)\n");
1121         printf("  -0, --compression-level-0, --fast  Synonymous with -l 0 -b 1152 -r 2,2\n");
1122         printf("  -1, --compression-level-1          Synonymous with -l 0 -b 1152 -M -r 2,2\n");
1123         printf("  -2, --compression-level-2          Synonymous with -l 0 -b 1152 -m -r 3\n");
1124         printf("  -3, --compression-level-3          Synonymous with -l 6 -b 4608 -r 3,3\n");
1125         printf("  -4, --compression-level-4          Synonymous with -l 8 -b 4608 -M -r 3,3\n");
1126         printf("  -5, --compression-level-5          Synonymous with -l 8 -b 4608 -m -r 3,3\n");
1127         printf("                                     -5 is the default setting\n");
1128         printf("  -6, --compression-level-6          Synonymous with -l 8 -b 4608 -m -r 4\n");
1129         printf("  -7, --compression-level-7          Synonymous with -l 8 -b 4608 -m -e -r 6\n");
1130         printf("  -8, --compression-level-8, --best  Synonymous with -l 12 -b 4608 -m -e -r 6\n");
1131         printf("  -m, --mid-side                     Try mid-side coding for each frame\n");
1132         printf("                                     (stereo only)\n");
1133         printf("  -M, --adaptive-mid-side            Adaptive mid-side coding for all frames\n");
1134         printf("                                     (stereo only)\n");
1135         printf("  -e, --exhaustive-model-search      Do exhaustive model search (expensive!)\n");
1136 #if 0
1137         /*@@@ deprecated: */
1138         printf("  -E, --escape-coding                Do escape coding in the entropy coder.\n");
1139         printf("                                     This causes the encoder to use an\n");
1140         printf("                                     unencoded representation of the residual\n");
1141         printf("                                     in a partition if it is smaller.  It\n");
1142         printf("                                     increases the runtime and usually results\n");
1143         printf("                                     in an improvement of less than 1%.\n");
1144 #endif
1145         printf("  -l, --max-lpc-order=#              Max LPC order; 0 => only fixed predictors\n");
1146         printf("  -p, --qlp-coeff-precision-search   Do exhaustive search of LP coefficient\n");
1147         printf("                                     quantization (expensive!); overrides -q;\n");
1148         printf("                                     does nothing if using -l 0\n");
1149         printf("  -q, --qlp-coeff-precision=#        Specify precision in bits of quantized\n");
1150         printf("                                     linear-predictor coefficients; 0 => let\n");
1151         printf("                                     encoder decide (the minimun is %u, the\n", FLAC__MIN_QLP_COEFF_PRECISION);
1152         printf("                                     default is -q 0)\n");
1153         printf("  -r, --rice-partition-order=[#,]#   Set [min,]max residual partition order\n");
1154         printf("                                     (# is 0..16; min defaults to 0; the\n");
1155         printf("                                     default is -r 0; above 4 doesn't usually\n");
1156         printf("                                     help much)\n");
1157 #if 0
1158         /*@@@ deprecated: */
1159         printf("  -R, -rice-parameter-search-distance=#   Rice parameter search distance\n");
1160 #endif
1161         printf("format options:\n");
1162         printf("      --endian={big|little}    Set byte order for samples\n");
1163         printf("      --channels=#             Number of channels\n");
1164         printf("      --bps=#                  Number of bits per sample\n");
1165         printf("      --sample-rate=#          Sample rate in Hz\n");
1166         printf("      --sign={signed|unsigned} Sign of samples (the default is signed)\n");
1167         printf("      --force-raw-input        Force input to be treated as raw samples\n");
1168         printf("negative options:\n");
1169         printf("      --no-adaptive-mid-side\n");
1170         printf("      --no-decode-through-errors\n");
1171         printf("      --no-delete-input-file\n");
1172 #if 0
1173 /* @@@ deprecated: */
1174         printf("      --no-escape-coding\n");
1175 #endif
1176         printf("      --no-exhaustive-model-search\n");
1177         printf("      --no-lax\n");
1178         printf("      --no-mid-side\n");
1179 #ifdef FLAC__HAS_OGG
1180         printf("      --no-ogg\n");
1181 #endif
1182         printf("      --no-padding\n");
1183         printf("      --no-qlp-coeff-prec-search\n");
1184         printf("      --no-residual-gnuplot\n");
1185         printf("      --no-residual-text\n");
1186         printf("      --no-sector-align\n");
1187         printf("      --no-seektable\n");
1188         printf("      --no-silent\n");
1189         printf("      --no-verify\n");
1190 }
1191
1192 void
1193 format_mistake(const char *infilename, const char *wrong, const char *right)
1194 {
1195         fprintf(stderr, "WARNING: %s is not a %s file; treating as a %s file\n", infilename, wrong, right);
1196 }
1197
1198 int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file)
1199 {
1200         FILE *encode_infile;
1201         char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
1202         char *p;
1203         FLAC__byte lookahead[12];
1204         unsigned lookahead_length = 0;
1205         FileFormat fmt= RAW;
1206         int retval;
1207         long infilesize;
1208         encode_options_t common_options;
1209
1210         if(0 == strcmp(infilename, "-")) {
1211                 infilesize = -1;
1212                 encode_infile = file__get_binary_stdin();
1213         }
1214         else {
1215                 infilesize = flac__file_get_filesize(infilename);
1216                 if(0 == (encode_infile = fopen(infilename, "rb"))) {
1217                         fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
1218                         return 1;
1219                 }
1220         }
1221
1222         if(!option_values.force_raw_format) {
1223                 /* first set format based on name */
1224                 if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav"))
1225                         fmt= WAV;
1226                 else if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".aif"))
1227                         fmt= AIF;
1228                 else if(strlen(infilename) > 4 && 0 == strcasecmp(infilename+(strlen(infilename)-5), ".aiff"))
1229                         fmt= AIF;
1230
1231                 /* attempt to guess the file type based on the first 12 bytes */
1232                 if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
1233                         if(fmt != RAW)
1234                                 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1235                         fmt= RAW;
1236                 }
1237                 else {
1238                         if(!strncmp(lookahead, "RIFF", 4) && !strncmp(lookahead+8, "WAVE", 4))
1239                                 fmt= WAV;
1240                         else if(!strncmp(lookahead, "FORM", 4) && !strncmp(lookahead+8, "AIFF", 4))
1241                                 fmt= AIF;
1242                         else {
1243                                 if(fmt != RAW)
1244                                         format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1245                                 fmt= RAW;
1246                         }
1247                 }
1248         }
1249
1250         if(option_values.sector_align && fmt == RAW && infilesize < 0) {
1251                 fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n");
1252                 return 1;
1253         }
1254
1255         if(fmt == RAW) {
1256                 if(option_values.format_is_big_endian < 0 || option_values.format_channels < 0 || option_values.format_bps < 0 || option_values.format_sample_rate < 0)
1257                         return usage_error("ERROR: for encoding a raw file you must specify a value for --endian, --channels, --bps, and --sample-rate\n");
1258         }
1259
1260         if(encode_infile == stdin || option_values.force_to_stdout)
1261                 strcpy(outfilename, "-");
1262         else {
1263                 const char *suffix = (option_values.use_ogg? ogg_suffix : flac_suffix);
1264                 strcpy(outfilename, option_values.output_prefix? option_values.output_prefix : "");
1265                 strcat(outfilename, infilename);
1266                 if(0 == (p = strrchr(outfilename, '.')))
1267                         strcat(outfilename, suffix);
1268                 else {
1269                         if(0 == strcmp(p, suffix)) {
1270                                 strcpy(p, "_new");
1271                                 strcat(p, suffix);
1272                         }
1273                         else
1274                                 strcpy(p, suffix);
1275                 }
1276         }
1277         if(0 == forced_outfilename)
1278                 forced_outfilename = outfilename;
1279         if(0 != option_values.cmdline_forced_outfilename)
1280                 forced_outfilename = option_values.cmdline_forced_outfilename;
1281
1282         common_options.verbose = option_values.verbose;
1283         common_options.skip = option_values.skip;
1284         common_options.verify = option_values.verify;
1285 #ifdef FLAC__HAS_OGG
1286         common_options.use_ogg = option_values.use_ogg;
1287 #endif
1288         common_options.lax = option_values.lax;
1289         common_options.do_mid_side = option_values.do_mid_side;
1290         common_options.loose_mid_side = option_values.loose_mid_side;
1291         common_options.do_exhaustive_model_search = option_values.do_exhaustive_model_search;
1292         common_options.do_escape_coding = option_values.do_escape_coding;
1293         common_options.do_qlp_coeff_prec_search = option_values.do_qlp_coeff_prec_search;
1294         common_options.min_residual_partition_order = option_values.min_residual_partition_order;
1295         common_options.max_residual_partition_order = option_values.max_residual_partition_order;
1296         common_options.rice_parameter_search_dist = option_values.rice_parameter_search_dist;
1297         common_options.max_lpc_order = option_values.max_lpc_order;
1298         common_options.blocksize = (unsigned)option_values.blocksize;
1299         common_options.qlp_coeff_precision = option_values.qlp_coeff_precision;
1300         common_options.padding = option_values.padding;
1301         common_options.requested_seek_points = option_values.requested_seek_points;
1302         common_options.num_requested_seek_points = option_values.num_requested_seek_points;
1303         common_options.is_last_file = is_last_file;
1304         common_options.align_reservoir = align_reservoir;
1305         common_options.align_reservoir_samples = &align_reservoir_samples;
1306         common_options.sector_align = option_values.sector_align;
1307
1308         if(fmt == RAW) {
1309                 raw_encode_options_t options;
1310
1311                 options.common = common_options;
1312                 options.is_big_endian = option_values.format_is_big_endian;
1313                 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1314                 options.channels = option_values.format_channels;
1315                 options.bps = option_values.format_bps;
1316                 options.sample_rate = option_values.format_sample_rate;
1317
1318                 retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1319         }
1320         else {
1321                 wav_encode_options_t options;
1322
1323                 options.common = common_options;
1324
1325                 if(fmt == AIF)
1326                         retval = flac__encode_aif(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1327                 else
1328                         retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1329         }
1330
1331         if(retval == 0 && strcmp(infilename, "-")) {
1332                 if(strcmp(forced_outfilename, "-"))
1333                         flac__file_copy_metadata(infilename, forced_outfilename);
1334                 if(option_values.delete_input)
1335                         unlink(infilename);
1336         }
1337
1338         return retval;
1339 }
1340
1341 int decode_file(const char *infilename, const char *forced_outfilename)
1342 {
1343         static const char *suffixes[] = { ".wav", ".raw", ".ana" };
1344         char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
1345         char *p;
1346         int retval;
1347         FLAC__bool treat_as_ogg = false;
1348         decode_options_t common_options;
1349
1350         if(!option_values.test_only && !option_values.analyze) {
1351                 if(option_values.force_raw_format && option_values.format_is_big_endian < 0)
1352                         return usage_error("ERROR: for decoding to a raw file you must specify a value for --endian\n");
1353         }
1354
1355         if(0 == strcmp(infilename, "-") || option_values.force_to_stdout)
1356                 strcpy(outfilename, "-");
1357         else {
1358                 const char *suffix = suffixes[option_values.analyze? 2 : option_values.force_raw_format? 1 : 0];
1359                 strcpy(outfilename, option_values.output_prefix? option_values.output_prefix : "");
1360                 strcat(outfilename, infilename);
1361                 if(0 == (p = strrchr(outfilename, '.')))
1362                         strcat(outfilename, suffix);
1363                 else {
1364                         if(0 == strcmp(p, suffix)) {
1365                                 strcpy(p, "_new");
1366                                 strcat(p, suffix);
1367                         }
1368                         else
1369                                 strcpy(p, suffix);
1370                 }
1371         }
1372         if(0 == forced_outfilename)
1373                 forced_outfilename = outfilename;
1374         if(0 != option_values.cmdline_forced_outfilename)
1375                 forced_outfilename = option_values.cmdline_forced_outfilename;
1376
1377         if(option_values.use_ogg)
1378                 treat_as_ogg = true;
1379         else if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".ogg"))
1380                 treat_as_ogg = true;
1381         else
1382                 treat_as_ogg = false;
1383
1384 #ifndef FLAC__HAS_OGG
1385         if(treat_as_ogg) {
1386                 fprintf(stderr, "%s: Ogg support has not been built into this copy of flac\n", infilename);
1387                 return 1;
1388         }
1389 #endif
1390
1391         common_options.verbose = option_values.verbose;
1392         common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
1393 #ifdef FLAC__HAS_OGG
1394         common_options.is_ogg = treat_as_ogg;
1395 #endif
1396         common_options.skip = option_values.skip;
1397
1398         if(!option_values.force_raw_format) {
1399                 wav_decode_options_t options;
1400
1401                 options.common = common_options;
1402
1403                 retval = flac__decode_wav(infilename, option_values.test_only? 0 : forced_outfilename, option_values.analyze, option_values.aopts, options);
1404         }
1405         else {
1406                 raw_decode_options_t options;
1407
1408                 options.common = common_options;
1409                 options.is_big_endian = option_values.format_is_big_endian;
1410                 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1411
1412                 retval = flac__decode_raw(infilename, option_values.test_only? 0 : forced_outfilename, option_values.analyze, option_values.aopts, options);
1413         }
1414
1415         if(retval == 0 && strcmp(infilename, "-")) {
1416                 if(strcmp(forced_outfilename, "-"))
1417                         flac__file_copy_metadata(infilename, forced_outfilename);
1418                 if(option_values.delete_input && !option_values.test_only && !option_values.analyze)
1419                         unlink(infilename);
1420         }
1421
1422         return retval;
1423 }
1424
1425 void die(const char *message)
1426 {
1427         FLAC__ASSERT(0 != message);
1428         fprintf(stderr, "ERROR: %s\n", message);
1429         exit(1);
1430 }
1431
1432 char *local_strdup(const char *source)
1433 {
1434         char *ret;
1435         FLAC__ASSERT(0 != source);
1436         if(0 == (ret = strdup(source)))
1437                 die("out of memory during strdup()");
1438         return ret;
1439 }