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