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