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