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