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