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