1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001,2002 Josh Coalson
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.
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.
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.
30 #if !defined _MSC_VER && !defined __MINGW32__
31 /* unlink is in stdio.h in VC++ */
32 #include <unistd.h> /* for unlink() */
34 #define strcasecmp stricmp
37 #include "share/grabbag.h"
42 #include "vorbiscomment.h"
45 /*[JEC] was:#if HAVE_GETOPT_LONG*/
46 /*[JEC] see flac/include/share/getopt.h as to why the change */
49 # include "share/getopt.h"
52 typedef enum { RAW, WAV, AIF } FileFormat;
56 static FLAC__bool init_options();
57 static int parse_options(int argc, char *argv[]);
58 static int parse_option(int short_option, const char *long_option, const char *option_argument);
59 static void free_options();
61 static int usage_error(const char *message, ...);
62 static void short_usage();
63 static void show_version();
64 static void show_help();
65 static void show_explain();
66 static void format_mistake(const char *infilename, const char *wrong, const char *right);
68 static int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file);
69 static int decode_file(const char *infilename);
71 static const char *get_encoded_outfilename(const char *infilename);
72 static const char *get_decoded_outfilename(const char *infilename);
74 static void die(const char *message);
75 static char *local_strdup(const char *source);
79 * share__getopt format struct; note that for long options with no
80 * short option equivalent we just set the 'val' field to 0.
82 static struct share__option long_options_[] = {
86 { "help", 0, 0, 'h' },
87 { "explain", 0, 0, 'H' },
88 { "version", 0, 0, 'v' },
89 { "decode", 0, 0, 'd' },
90 { "analyze", 0, 0, 'a' },
91 { "test", 0, 0, 't' },
92 { "stdout", 0, 0, 'c' },
93 { "silent", 0, 0, 's' },
94 { "delete-input-file", 0, 0, 0 },
95 { "output-prefix", 1, 0, 0 },
96 { "output-name", 1, 0, 'o' },
102 { "decode-through-errors", 0, 0, 'F' },
107 { "cuesheet", 1, 0, 0 },
108 { "no-cued-seekpoints", 0, 0, 0 },
109 { "tag", 1, 0, 'T' },
110 { "compression-level-0", 0, 0, '0' },
111 { "compression-level-1", 0, 0, '1' },
112 { "compression-level-2", 0, 0, '2' },
113 { "compression-level-3", 0, 0, '3' },
114 { "compression-level-4", 0, 0, '4' },
115 { "compression-level-5", 0, 0, '5' },
116 { "compression-level-6", 0, 0, '6' },
117 { "compression-level-7", 0, 0, '7' },
118 { "compression-level-8", 0, 0, '8' },
119 { "compression-level-9", 0, 0, '9' },
120 { "best", 0, 0, '8' },
121 { "fast", 0, 0, '0' },
122 { "super-secret-totally-impractical-compression-level", 0, 0, 0 },
123 { "verify", 0, 0, 'V' },
124 { "force-raw-format", 0, 0, 0 },
126 { "replay-gain", 0, 0, 0 },
127 { "sector-align", 0, 0, 0 },
128 { "seekpoint", 1, 0, 'S' },
129 { "padding", 1, 0, 'P' },
132 { "serial-number", 1, 0, 0 },
134 { "blocksize", 1, 0, 'b' },
135 { "exhaustive-model-search", 0, 0, 'e' },
137 /* @@@ deprecated: */
138 { "escape-coding", 0, 0, 'E' },
140 { "max-lpc-order", 1, 0, 'l' },
141 { "mid-side", 0, 0, 'm' },
142 { "adaptive-mid-side", 0, 0, 'M' },
143 { "qlp-coeff-precision-search", 0, 0, 'p' },
144 { "qlp-coeff-precision", 1, 0, 'q' },
145 { "rice-partition-order", 1, 0, 'r' },
147 /* @@@ deprecated: */
148 { "rice-parameter-search-distance", 1, 0, 'R' },
150 { "endian", 1, 0, 0 },
151 { "channels", 1, 0, 0 },
153 { "sample-rate", 1, 0, 0 },
159 { "residual-gnu-plot", 0, 0, 0 },
160 { "residual-text", 0, 0, 0 },
165 { "no-decode-through-errors", 0, 0, 0 },
166 { "no-silent", 0, 0, 0 },
167 { "no-seektable", 0, 0, 0 },
168 { "no-delete-input-file", 0, 0, 0 },
169 { "no-replay-gain", 0, 0, 0 },
170 { "no-sector-align", 0, 0, 0 },
171 { "no-lax", 0, 0, 0 },
173 { "no-ogg", 0, 0, 0 },
175 { "no-exhaustive-model-search", 0, 0, 0 },
177 /* @@@ deprecated: */
178 { "no-escape-coding", 0, 0, 0 },
180 { "no-mid-side", 0, 0, 0 },
181 { "no-adaptive-mid-side", 0, 0, 0 },
182 { "no-qlp-coeff-prec-search", 0, 0, 0 },
183 { "no-padding", 0, 0, 0 },
184 { "no-verify", 0, 0, 0 },
185 { "no-residual-gnuplot", 0, 0, 0 },
186 { "no-residual-text", 0, 0, 0 },
188 * undocumented debugging options for the test suite
190 { "disable-constant-subframes", 0, 0, 0 },
191 { "disable-fixed-subframes", 0, 0, 0 },
192 { "disable-verbatim-subframes", 0, 0, 0 },
199 * global to hold command-line option values
203 FLAC__bool show_help;
204 FLAC__bool show_explain;
205 FLAC__bool show_version;
206 FLAC__bool mode_decode;
209 FLAC__bool continue_through_decode_errors;
211 FLAC__bool test_only;
214 FLAC__bool has_serial_number; /* true iff --serial-number was used */
215 long serial_number; /* this is the Ogg serial number and is unused for native FLAC */
216 FLAC__bool do_mid_side;
217 FLAC__bool loose_mid_side;
218 FLAC__bool do_exhaustive_model_search;
219 FLAC__bool do_escape_coding;
220 FLAC__bool do_qlp_coeff_prec_search;
221 FLAC__bool force_to_stdout;
222 FLAC__bool force_raw_format;
223 FLAC__bool delete_input;
224 FLAC__bool replay_gain;
225 FLAC__bool sector_align;
226 const char *cmdline_forced_outfilename;
227 const char *output_prefix;
228 analysis_options aopts;
230 unsigned max_lpc_order;
231 unsigned qlp_coeff_precision;
232 const char *skip_specification;
233 int format_is_big_endian;
234 int format_is_unsigned_samples;
237 int format_sample_rate;
239 int min_residual_partition_order;
240 int max_residual_partition_order;
241 int rice_parameter_search_dist;
242 char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER but buffer overflow is checked */
243 int num_requested_seek_points; /* -1 => no -S options were given, 0 => -S- was given */
244 const char *cuesheet_filename;
245 FLAC__bool cued_seekpoints;
250 FLAC__StreamMetadata *vorbis_comment;
253 FLAC__bool disable_constant_subframes;
254 FLAC__bool disable_fixed_subframes;
255 FLAC__bool disable_verbatim_subframes;
261 * miscellaneous globals
264 static FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
265 static FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
266 static unsigned align_reservoir_samples = 0; /* 0 .. 587 */
269 int main(int argc, char *argv[])
273 setlocale(LC_ALL, "");
274 if(!init_options()) {
275 fprintf(stderr, "ERROR: allocating memory\n");
279 if((retval = parse_options(argc, argv)) == 0)
292 if(option_values.show_version) {
296 else if(option_values.show_explain) {
300 else if(option_values.show_help) {
305 if(option_values.num_files == 0) {
311 * tweak options; validate the values
313 if(!option_values.mode_decode) {
314 if(option_values.blocksize < 0) {
315 if(option_values.max_lpc_order == 0)
316 option_values.blocksize = 1152;
318 option_values.blocksize = 4608;
320 if(option_values.max_residual_partition_order < 0) {
321 if(option_values.blocksize <= 1152)
322 option_values.max_residual_partition_order = 2;
323 else if(option_values.blocksize <= 2304)
324 option_values.max_residual_partition_order = 3;
325 else if(option_values.blocksize <= 4608)
326 option_values.max_residual_partition_order = 3;
328 option_values.max_residual_partition_order = 4;
329 option_values.min_residual_partition_order = option_values.max_residual_partition_order;
331 if(option_values.rice_parameter_search_dist < 0) {
332 option_values.rice_parameter_search_dist = 0;
336 if(option_values.test_only) {
337 if(0 != option_values.skip_specification)
338 return usage_error("ERROR: --skip is not allowed in test mode\n");
342 FLAC__ASSERT(option_values.blocksize >= 0 || option_values.mode_decode);
344 if(option_values.format_channels >= 0) {
345 if(option_values.format_channels == 0 || (unsigned)option_values.format_channels > FLAC__MAX_CHANNELS)
346 return usage_error("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", option_values.format_channels, FLAC__MAX_CHANNELS);
348 if(option_values.format_bps >= 0) {
349 if(option_values.format_bps != 8 && option_values.format_bps != 16 && option_values.format_bps != 24)
350 return usage_error("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", option_values.format_bps);
352 if(option_values.format_sample_rate >= 0) {
353 if(!FLAC__format_sample_rate_is_valid(option_values.format_sample_rate))
354 return usage_error("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", option_values.format_sample_rate, FLAC__MAX_SAMPLE_RATE);
356 if(option_values.mode_decode) {
357 if(!option_values.force_raw_format) {
358 if(option_values.format_is_big_endian >= 0)
359 return usage_error("ERROR: --endian only allowed with --force-raw-format\n");
360 if(option_values.format_is_unsigned_samples >= 0)
361 return usage_error("ERROR: --sign only allowed with --force-raw-format\n");
363 if(option_values.format_channels >= 0)
364 return usage_error("ERROR: --channels not allowed with --decode\n");
365 if(option_values.format_bps >= 0)
366 return usage_error("ERROR: --bps not allowed with --decode\n");
367 if(option_values.format_sample_rate >= 0)
368 return usage_error("ERROR: --sample-rate not allowed with --decode\n");
370 if(!option_values.mode_decode && ((unsigned)option_values.blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)option_values.blocksize > FLAC__MAX_BLOCK_SIZE)) {
371 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);
373 if(option_values.qlp_coeff_precision > 0 && option_values.qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
374 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);
377 if(option_values.sector_align) {
378 if(option_values.mode_decode)
379 return usage_error("ERROR: --sector-align only allowed for encoding\n");
380 if(0 != option_values.skip_specification)
381 return usage_error("ERROR: --sector-align not allowed with --skip\n");
382 if(option_values.format_channels >= 0 && option_values.format_channels != 2)
383 return usage_error("ERROR: --sector-align can only be done with stereo input\n");
384 if(option_values.format_bps >= 0 && option_values.format_bps != 16)
385 return usage_error("ERROR: --sector-align can only be done with 16-bit samples\n");
386 if(option_values.format_sample_rate >= 0 && option_values.format_sample_rate != 44100)
387 return usage_error("ERROR: --sector-align can only be done with a sample rate of 44100\n");
389 if(option_values.replay_gain) {
390 if(option_values.force_to_stdout)
391 return usage_error("ERROR: --replay-gain not allowed with -c/--stdout\n");
392 if(option_values.mode_decode)
393 return usage_error("ERROR: --replay-gain only allowed for encoding\n");
394 if(option_values.format_channels > 2)
395 return usage_error("ERROR: --replay-gain can only be done with mono/stereo input\n");
396 if(option_values.format_sample_rate >= 0 && !grabbag__replaygain_is_valid_sample_frequency(option_values.format_sample_rate))
397 return usage_error("ERROR: invalid sample rate used with --replay-gain\n");
399 * We want to reserve padding space for the ReplayGain
400 * tags that we will set later, to avoid rewriting the
403 if(option_values.padding < 0) {
404 fprintf(stderr, "NOTE: --replay-gain may leave a small PADDING block even with --no-padding\n");
405 option_values.padding = GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED;
408 option_values.padding += GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED;
411 if(option_values.num_files > 1 && option_values.cmdline_forced_outfilename) {
412 return usage_error("ERROR: -o/--output-name cannot be used with multiple files\n");
414 if(option_values.cmdline_forced_outfilename && option_values.output_prefix) {
415 return usage_error("ERROR: --output-prefix conflicts with -o/--output-name\n");
417 if(!option_values.mode_decode && 0 != option_values.cuesheet_filename && option_values.num_files > 1) {
418 return usage_error("ERROR: --cuesheet cannot be used when encoding multiple files\n");
421 if(option_values.verbose) {
422 fprintf(stderr, "\n");
423 fprintf(stderr, "flac %s, Copyright (C) 2000,2001,2002 Josh Coalson\n", FLAC__VERSION_STRING);
424 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
425 fprintf(stderr, "welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
427 if(!option_values.mode_decode) {
429 if(option_values.padding < 0)
432 sprintf(padopt, " %d", option_values.padding);
434 "options:%s%s%s%s -P%s -b %u%s -l %u%s%s%s -q %u -r %u,%u%s\n",
435 option_values.delete_input?" --delete-input-file":"",
436 option_values.sector_align?" --sector-align":"",
437 option_values.use_ogg?" --ogg":"",
438 option_values.lax?" --lax":"",
440 (unsigned)option_values.blocksize,
441 option_values.loose_mid_side?" -M":option_values.do_mid_side?" -m":"",
442 option_values.max_lpc_order,
443 option_values.do_exhaustive_model_search?" -e":"",
444 option_values.do_escape_coding?" -E":"",
445 option_values.do_qlp_coeff_prec_search?" -p":"",
446 option_values.qlp_coeff_precision,
447 (unsigned)option_values.min_residual_partition_order,
448 (unsigned)option_values.max_residual_partition_order,
449 option_values.verify? " -V":""
454 if(option_values.mode_decode) {
455 FLAC__bool first = true;
457 if(option_values.num_files == 0) {
458 retval = decode_file("-");
462 if(option_values.num_files > 1)
463 option_values.cmdline_forced_outfilename = 0;
464 for(i = 0, retval = 0; i < option_values.num_files; i++) {
465 if(0 == strcmp(option_values.filenames[i], "-") && !first)
467 retval |= decode_file(option_values.filenames[i]);
473 FLAC__bool first = true;
475 if(option_values.num_files == 0) {
476 retval = encode_file("-", first, true);
480 if(option_values.num_files > 1)
481 option_values.cmdline_forced_outfilename = 0;
482 for(i = 0, retval = 0; i < option_values.num_files; i++) {
483 if(0 == strcmp(option_values.filenames[i], "-") && !first)
485 retval |= encode_file(option_values.filenames[i], first, i == (option_values.num_files-1));
488 if(option_values.replay_gain && retval == 0) {
489 float album_gain, album_peak;
490 grabbag__replaygain_get_album(&album_gain, &album_peak);
491 for(i = 0; i < option_values.num_files; i++) {
492 const char *error, *outfilename = get_encoded_outfilename(option_values.filenames[i]);
493 if(0 == strcmp(option_values.filenames[i], "-"))
495 if(0 != (error = grabbag__replaygain_store_to_file_album(outfilename, album_gain, album_peak, /*preserve_modtime=*/true))) {
496 fprintf(stderr, "%s: ERROR writing ReplayGain album tags\n", outfilename);
507 FLAC__bool init_options()
509 option_values.show_help = false;
510 option_values.show_explain = false;
511 option_values.mode_decode = false;
512 option_values.verify = false;
513 option_values.verbose = true;
514 option_values.continue_through_decode_errors = false;
515 option_values.lax = false;
516 option_values.test_only = false;
517 option_values.analyze = false;
518 option_values.use_ogg = false;
519 option_values.has_serial_number = false;
520 option_values.serial_number = 0;
521 option_values.do_mid_side = true;
522 option_values.loose_mid_side = false;
523 option_values.do_exhaustive_model_search = false;
524 option_values.do_escape_coding = false;
525 option_values.do_qlp_coeff_prec_search = false;
526 option_values.force_to_stdout = false;
527 option_values.force_raw_format = false;
528 option_values.delete_input = false;
529 option_values.replay_gain = false;
530 option_values.sector_align = false;
531 option_values.cmdline_forced_outfilename = 0;
532 option_values.output_prefix = 0;
533 option_values.aopts.do_residual_text = false;
534 option_values.aopts.do_residual_gnuplot = false;
535 option_values.padding = 4096;
536 option_values.max_lpc_order = 8;
537 option_values.qlp_coeff_precision = 0;
538 option_values.skip_specification = 0;
539 option_values.format_is_big_endian = -1;
540 option_values.format_is_unsigned_samples = -1;
541 option_values.format_channels = -1;
542 option_values.format_bps = -1;
543 option_values.format_sample_rate = -1;
544 option_values.blocksize = -1;
545 option_values.min_residual_partition_order = -1;
546 option_values.max_residual_partition_order = -1;
547 option_values.rice_parameter_search_dist = -1;
548 option_values.requested_seek_points[0] = '\0';
549 option_values.num_requested_seek_points = -1;
550 option_values.cuesheet_filename = 0;
551 option_values.cued_seekpoints = true;
553 option_values.num_files = 0;
554 option_values.filenames = 0;
556 if(0 == (option_values.vorbis_comment = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)))
559 option_values.debug.disable_constant_subframes = false;
560 option_values.debug.disable_fixed_subframes = false;
561 option_values.debug.disable_verbatim_subframes = false;
566 int parse_options(int argc, char *argv[])
569 int option_index = 1;
570 FLAC__bool had_error = false;
571 /*@@@ E and R: are deprecated */
572 const char *short_opts = "0123456789ab:cdeFhHl:mMo:pP:q:r:sS:tT:vV";
574 while ((short_option = share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
575 switch (short_option) {
576 case 0: /* long option with no equivalent short option */
577 had_error |= (parse_option(short_option, long_options_[option_index].name, share__optarg) != 0);
583 default: /* short option */
584 had_error |= (parse_option(short_option, 0, share__optarg) != 0);
593 FLAC__ASSERT(share__optind <= argc);
595 option_values.num_files = argc - share__optind;
597 if(option_values.num_files > 0) {
599 if(0 == (option_values.filenames = malloc(sizeof(char *) * option_values.num_files)))
600 die("out of memory allocating space for file names list");
601 while(share__optind < argc)
602 option_values.filenames[i++] = local_strdup(argv[share__optind++]);
608 int parse_option(int short_option, const char *long_option, const char *option_argument)
612 if(short_option == 0) {
613 FLAC__ASSERT(0 != long_option);
614 if(0 == strcmp(long_option, "delete-input-file")) {
615 option_values.delete_input = true;
617 else if(0 == strcmp(long_option, "output-prefix")) {
618 FLAC__ASSERT(0 != option_argument);
619 option_values.output_prefix = option_argument;
621 else if(0 == strcmp(long_option, "skip")) {
622 FLAC__ASSERT(0 != option_argument);
623 option_values.skip_specification = option_argument;
625 else if(0 == strcmp(long_option, "cuesheet")) {
626 FLAC__ASSERT(0 != option_argument);
627 option_values.cuesheet_filename = option_argument;
629 else if(0 == strcmp(long_option, "no-cued-seekpoints")) {
630 option_values.cued_seekpoints = false;
632 else if(0 == strcmp(long_option, "super-secret-totally-impractical-compression-level")) {
633 option_values.lax = true;
634 option_values.do_exhaustive_model_search = true;
635 option_values.do_escape_coding = true;
636 option_values.do_mid_side = true;
637 option_values.loose_mid_side = false;
638 option_values.do_qlp_coeff_prec_search = true;
639 option_values.min_residual_partition_order = 0;
640 option_values.max_residual_partition_order = 16;
641 option_values.rice_parameter_search_dist = 0;
642 option_values.max_lpc_order = 32;
644 else if(0 == strcmp(long_option, "force-raw-format")) {
645 option_values.force_raw_format = true;
647 else if(0 == strcmp(long_option, "lax")) {
648 option_values.lax = true;
650 else if(0 == strcmp(long_option, "replay-gain")) {
651 option_values.replay_gain = true;
653 else if(0 == strcmp(long_option, "sector-align")) {
654 option_values.sector_align = true;
657 else if(0 == strcmp(long_option, "ogg")) {
658 option_values.use_ogg = true;
660 else if(0 == strcmp(long_option, "serial-number")) {
661 option_values.has_serial_number = true;
662 option_values.serial_number = atol(option_argument);
665 else if(0 == strcmp(long_option, "endian")) {
666 FLAC__ASSERT(0 != option_argument);
667 if(0 == strncmp(option_argument, "big", strlen(option_argument)))
668 option_values.format_is_big_endian = true;
669 else if(0 == strncmp(option_argument, "little", strlen(option_argument)))
670 option_values.format_is_big_endian = false;
672 return usage_error("ERROR: argument to --endian must be \"big\" or \"little\"\n");
674 else if(0 == strcmp(long_option, "channels")) {
675 FLAC__ASSERT(0 != option_argument);
676 option_values.format_channels = atoi(option_argument);
678 else if(0 == strcmp(long_option, "bps")) {
679 FLAC__ASSERT(0 != option_argument);
680 option_values.format_bps = atoi(option_argument);
682 else if(0 == strcmp(long_option, "sample-rate")) {
683 FLAC__ASSERT(0 != option_argument);
684 option_values.format_sample_rate = atoi(option_argument);
686 else if(0 == strcmp(long_option, "sign")) {
687 FLAC__ASSERT(0 != option_argument);
688 if(0 == strncmp(option_argument, "signed", strlen(option_argument)))
689 option_values.format_is_unsigned_samples = false;
690 else if(0 == strncmp(option_argument, "unsigned", strlen(option_argument)))
691 option_values.format_is_unsigned_samples = true;
693 return usage_error("ERROR: argument to --sign must be \"signed\" or \"unsigned\"\n");
695 else if(0 == strcmp(long_option, "residual-gnu-plot")) {
696 option_values.aopts.do_residual_gnuplot = true;
698 else if(0 == strcmp(long_option, "residual-text")) {
699 option_values.aopts.do_residual_text = true;
704 else if(0 == strcmp(long_option, "no-decode-through-errors")) {
705 option_values.continue_through_decode_errors = false;
707 else if(0 == strcmp(long_option, "no-silent")) {
708 option_values.verbose = true;
710 else if(0 == strcmp(long_option, "no-seektable")) {
711 option_values.num_requested_seek_points = 0;
712 option_values.requested_seek_points[0] = '\0';
714 else if(0 == strcmp(long_option, "no-delete-input-file")) {
715 option_values.delete_input = false;
717 else if(0 == strcmp(long_option, "no-replay-gain")) {
718 option_values.replay_gain = false;
720 else if(0 == strcmp(long_option, "no-sector-align")) {
721 option_values.sector_align = false;
723 else if(0 == strcmp(long_option, "no-lax")) {
724 option_values.lax = false;
727 else if(0 == strcmp(long_option, "no-ogg")) {
728 option_values.use_ogg = false;
731 else if(0 == strcmp(long_option, "no-exhaustive-model-search")) {
732 option_values.do_exhaustive_model_search = false;
735 /* @@@ deprecated: */
736 else if(0 == strcmp(long_option, "no-escape-coding")) {
737 option_values.do_escape_coding = false;
740 else if(0 == strcmp(long_option, "no-mid-side")) {
741 option_values.do_mid_side = option_values.loose_mid_side = false;
743 else if(0 == strcmp(long_option, "no-adaptive-mid-side")) {
744 option_values.loose_mid_side = option_values.do_mid_side = false;
746 else if(0 == strcmp(long_option, "no-qlp-coeff-prec-search")) {
747 option_values.do_qlp_coeff_prec_search = false;
749 else if(0 == strcmp(long_option, "no-padding")) {
750 option_values.padding = -1;
752 else if(0 == strcmp(long_option, "no-verify")) {
753 option_values.verify = false;
755 else if(0 == strcmp(long_option, "no-residual-gnuplot")) {
756 option_values.aopts.do_residual_gnuplot = false;
758 else if(0 == strcmp(long_option, "no-residual-text")) {
759 option_values.aopts.do_residual_text = false;
761 else if(0 == strcmp(long_option, "disable-constant-subframes")) {
762 option_values.debug.disable_constant_subframes = true;
764 else if(0 == strcmp(long_option, "disable-fixed-subframes")) {
765 option_values.debug.disable_fixed_subframes = true;
767 else if(0 == strcmp(long_option, "disable-verbatim-subframes")) {
768 option_values.debug.disable_verbatim_subframes = true;
772 const char *violation;
773 switch(short_option) {
775 option_values.show_help = true;
778 option_values.show_explain = true;
781 option_values.show_version = true;
784 option_values.mode_decode = true;
787 option_values.mode_decode = true;
788 option_values.analyze = true;
791 option_values.mode_decode = true;
792 option_values.test_only = true;
795 option_values.force_to_stdout = true;
798 option_values.verbose = false;
801 FLAC__ASSERT(0 != option_argument);
802 option_values.cmdline_forced_outfilename = option_argument;
805 option_values.continue_through_decode_errors = true;
808 FLAC__ASSERT(0 != option_argument);
809 if(!flac__vorbiscomment_add(option_values.vorbis_comment, option_argument, &violation))
810 return usage_error("ERROR: (-T/--tag) %s\n", violation);
813 option_values.do_exhaustive_model_search = false;
814 option_values.do_escape_coding = false;
815 option_values.do_mid_side = false;
816 option_values.loose_mid_side = false;
817 option_values.qlp_coeff_precision = 0;
818 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
819 option_values.rice_parameter_search_dist = 0;
820 option_values.max_lpc_order = 0;
823 option_values.do_exhaustive_model_search = false;
824 option_values.do_escape_coding = false;
825 option_values.do_mid_side = true;
826 option_values.loose_mid_side = true;
827 option_values.qlp_coeff_precision = 0;
828 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
829 option_values.rice_parameter_search_dist = 0;
830 option_values.max_lpc_order = 0;
833 option_values.do_exhaustive_model_search = false;
834 option_values.do_escape_coding = false;
835 option_values.do_mid_side = true;
836 option_values.loose_mid_side = false;
837 option_values.qlp_coeff_precision = 0;
838 option_values.min_residual_partition_order = 0;
839 option_values.max_residual_partition_order = 3;
840 option_values.rice_parameter_search_dist = 0;
841 option_values.max_lpc_order = 0;
844 option_values.do_exhaustive_model_search = false;
845 option_values.do_escape_coding = false;
846 option_values.do_mid_side = false;
847 option_values.loose_mid_side = false;
848 option_values.qlp_coeff_precision = 0;
849 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
850 option_values.rice_parameter_search_dist = 0;
851 option_values.max_lpc_order = 6;
854 option_values.do_exhaustive_model_search = false;
855 option_values.do_escape_coding = false;
856 option_values.do_mid_side = true;
857 option_values.loose_mid_side = true;
858 option_values.qlp_coeff_precision = 0;
859 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
860 option_values.rice_parameter_search_dist = 0;
861 option_values.max_lpc_order = 8;
864 option_values.do_exhaustive_model_search = false;
865 option_values.do_escape_coding = false;
866 option_values.do_mid_side = true;
867 option_values.loose_mid_side = false;
868 option_values.qlp_coeff_precision = 0;
869 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
870 option_values.rice_parameter_search_dist = 0;
871 option_values.max_lpc_order = 8;
874 option_values.do_exhaustive_model_search = false;
875 option_values.do_escape_coding = false;
876 option_values.do_mid_side = true;
877 option_values.loose_mid_side = false;
878 option_values.qlp_coeff_precision = 0;
879 option_values.min_residual_partition_order = 0;
880 option_values.max_residual_partition_order = 4;
881 option_values.rice_parameter_search_dist = 0;
882 option_values.max_lpc_order = 8;
885 option_values.do_exhaustive_model_search = true;
886 option_values.do_escape_coding = false;
887 option_values.do_mid_side = true;
888 option_values.loose_mid_side = false;
889 option_values.qlp_coeff_precision = 0;
890 option_values.min_residual_partition_order = 0;
891 option_values.max_residual_partition_order = 6;
892 option_values.rice_parameter_search_dist = 0;
893 option_values.max_lpc_order = 8;
896 option_values.do_exhaustive_model_search = true;
897 option_values.do_escape_coding = false;
898 option_values.do_mid_side = true;
899 option_values.loose_mid_side = false;
900 option_values.qlp_coeff_precision = 0;
901 option_values.min_residual_partition_order = 0;
902 option_values.max_residual_partition_order = 6;
903 option_values.rice_parameter_search_dist = 0;
904 option_values.max_lpc_order = 12;
907 return usage_error("ERROR: compression level '9' is reserved\n");
909 option_values.verify = true;
912 FLAC__ASSERT(0 != option_argument);
913 if(option_values.num_requested_seek_points < 0)
914 option_values.num_requested_seek_points = 0;
915 option_values.num_requested_seek_points++;
916 if(strlen(option_values.requested_seek_points)+strlen(option_argument)+2 >= sizeof(option_values.requested_seek_points)) {
917 return usage_error("ERROR: too many seekpoints requested\n");
920 strcat(option_values.requested_seek_points, option_argument);
921 strcat(option_values.requested_seek_points, ";");
925 FLAC__ASSERT(0 != option_argument);
926 option_values.padding = atoi(option_argument);
927 if(option_values.padding < 0)
928 return usage_error("ERROR: argument to -P must be >= 0\n");
931 FLAC__ASSERT(0 != option_argument);
932 option_values.blocksize = atoi(option_argument);
935 option_values.do_exhaustive_model_search = true;
938 option_values.do_escape_coding = true;
941 FLAC__ASSERT(0 != option_argument);
942 option_values.max_lpc_order = atoi(option_argument);
945 option_values.do_mid_side = true;
946 option_values.loose_mid_side = false;
949 option_values.loose_mid_side = option_values.do_mid_side = true;
952 option_values.do_qlp_coeff_prec_search = true;
955 FLAC__ASSERT(0 != option_argument);
956 option_values.qlp_coeff_precision = atoi(option_argument);
959 FLAC__ASSERT(0 != option_argument);
960 p = strchr(option_argument, ',');
962 option_values.min_residual_partition_order = 0;
963 option_values.max_residual_partition_order = atoi(option_argument);
966 option_values.min_residual_partition_order = atoi(option_argument);
967 option_values.max_residual_partition_order = atoi(++p);
971 FLAC__ASSERT(0 != option_argument);
972 option_values.rice_parameter_search_dist = atoi(option_argument);
985 if(0 != option_values.filenames) {
986 for(i = 0; i < option_values.num_files; i++) {
987 if(0 != option_values.filenames[i])
988 free(option_values.filenames[i]);
990 free(option_values.filenames);
992 if(0 != option_values.vorbis_comment)
993 FLAC__metadata_object_delete(option_values.vorbis_comment);
996 int usage_error(const char *message, ...)
1000 FLAC__ASSERT(0 != message);
1002 va_start(args, message);
1004 (void) vfprintf(stderr, message, args);
1008 printf("Type \"flac\" for a usage summary or \"flac --help\" for all options\n");
1015 printf("flac %s\n", FLAC__VERSION_STRING);
1018 static void usage_header()
1020 printf("===============================================================================\n");
1021 printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
1022 printf("Copyright (C) 2000,2001,2002 Josh Coalson\n");
1024 printf("This program is free software; you can redistribute it and/or\n");
1025 printf("modify it under the terms of the GNU General Public License\n");
1026 printf("as published by the Free Software Foundation; either version 2\n");
1027 printf("of the License, or (at your option) any later version.\n");
1029 printf("This program is distributed in the hope that it will be useful,\n");
1030 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
1031 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
1032 printf("GNU General Public License for more details.\n");
1034 printf("You should have received a copy of the GNU General Public License\n");
1035 printf("along with this program; if not, write to the Free Software\n");
1036 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
1037 printf("===============================================================================\n");
1040 static void usage_summary()
1044 printf(" Encoding: flac [-s] [--skip #] [<encoding/format-options>] [INPUTFILE [...]]\n");
1045 printf(" Decoding: flac -d [-s] [--skip #] [-F] [<format-options>] [INPUTFILE [...]]\n");
1046 printf(" Testing: flac -t [-s] [INPUTFILE [...]]\n");
1047 printf("Analyzing: flac -a [-s] [--skip #] [<analysis-options>] [INPUTFILE [...]]\n");
1055 printf("This is the short help; for all options use 'flac --help'; for even more\n");
1056 printf("instructions use 'flac --explain'\n");
1058 printf("To encode:\n");
1059 printf(" flac [-#] [INPUTFILE [...]]\n");
1061 printf(" -# is -0 (fastest compression) to -8 (highest compression); -5 is the default\n");
1063 printf("To decode:\n");
1064 printf(" flac -d [INPUTFILE [...]]\n");
1066 printf("To test:\n");
1067 printf(" flac -t [INPUTFILE [...]]\n");
1074 printf("generic options:\n");
1075 printf(" -v, --version Show the flac version number\n");
1076 printf(" -h, --help Show this screen\n");
1077 printf(" -H, --explain Show detailed explanation of usage and options\n");
1078 printf(" -d, --decode Decode (the default behavior is to encode)\n");
1079 printf(" -t, --test Same as -d except no decoded file is written\n");
1080 printf(" -a, --analyze Same as -d except an analysis file is written\n");
1081 printf(" -c, --stdout Write output to stdout\n");
1082 printf(" -s, --silent Do not write runtime encode/decode statistics\n");
1083 printf(" -o, --output-name=FILENAME Force the output file name\n");
1084 printf(" --output-prefix=STRING Prepend STRING to output names\n");
1085 printf(" --delete-input-file Deletes after a successful encode/decode\n");
1086 printf(" --skip={#|mm:ss.ss} Skip the given initial samples for each input\n");
1087 printf("analysis options:\n");
1088 printf(" --residual-text Include residual signal in text output\n");
1089 printf(" --residual-gnuplot Generate gnuplot files of residual distribution\n");
1090 printf("decoding options:\n");
1091 printf(" -F, --decode-through-errors Continue decoding through stream errors\n");
1092 printf("encoding options:\n");
1093 printf(" -V, --verify Verify a correct encoding\n");
1094 #ifdef FLAC__HAS_OGG
1095 printf(" --ogg Use Ogg as transport layer\n");
1096 printf(" --serial-number Serial number to use for the FLAC stream\n");
1098 printf(" --lax Allow encoder to generate non-Subset files\n");
1099 printf(" --sector-align Align multiple files on sector boundaries\n");
1100 printf(" --replay-gain Calculate ReplayGain & store in Vorbis comments\n");
1101 printf(" --cuesheet=FILENAME Import cuesheet and store in CUESHEET block\n");
1102 printf(" -T, --tag=FIELD=VALUE Add a Vorbis comment; may appear multiple times\n");
1103 printf(" -S, --seekpoint={#|X|#x|#s} Add seek point(s)\n");
1104 printf(" -P, --padding=# Write a PADDING block of length #\n");
1105 printf(" -0, --compression-level-0, --fast Synonymous with -l 0 -b 1152 -r 2,2\n");
1106 printf(" -1, --compression-level-1 Synonymous with -l 0 -b 1152 -M -r 2,2\n");
1107 printf(" -2, --compression-level-2 Synonymous with -l 0 -b 1152 -m -r 3\n");
1108 printf(" -3, --compression-level-3 Synonymous with -l 6 -b 4608 -r 3,3\n");
1109 printf(" -4, --compression-level-4 Synonymous with -l 8 -b 4608 -M -r 3,3\n");
1110 printf(" -5, --compression-level-5 Synonymous with -l 8 -b 4608 -m -r 3,3\n");
1111 printf(" -6, --compression-level-6 Synonymous with -l 8 -b 4608 -m -r 4\n");
1112 printf(" -7, --compression-level-7 Synonymous with -l 8 -b 4608 -m -e -r 6\n");
1113 printf(" -8, --compression-level-8, --best Synonymous with -l 12 -b 4608 -m -e -r 6\n");
1114 printf(" -b, --blocksize=# Specify blocksize in samples\n");
1115 printf(" -m, --mid-side Try mid-side coding for each frame\n");
1116 printf(" -M, --adaptive-mid-side Adaptive mid-side coding for all frames\n");
1117 printf(" -e, --exhaustive-model-search Do exhaustive model search (expensive!)\n");
1119 /*@@@ deprecated: */
1120 printf(" -E, --escape-coding Do escape coding in the entropy coder\n");
1122 printf(" -l, --max-lpc-order=# Max LPC order; 0 => only fixed predictors\n");
1123 printf(" -p, --qlp-coeff-precision-search Exhaustively search LP coeff quantization\n");
1124 printf(" -q, --qlp-coeff-precision=# Specify precision in bits\n");
1125 printf(" -r, --rice-partition-order=[#,]# Set [min,]max residual partition order\n");
1127 /*@@@ deprecated: */
1128 printf(" -R, -rice-parameter-search-distance=# Rice parameter search distance\n");
1130 printf("format options:\n");
1131 printf(" --endian={big|little} Set byte order for samples\n");
1132 printf(" --channels=# Number of channels\n");
1133 printf(" --bps=# Number of bits per sample\n");
1134 printf(" --sample-rate=# Sample rate in Hz\n");
1135 printf(" --sign={signed|unsigned} Sign of samples\n");
1136 printf(" --force-raw-format Treat input or output as raw samples\n");
1137 printf("negative options:\n");
1138 printf(" --no-adaptive-mid-side\n");
1139 printf(" --no-decode-through-errors\n");
1140 printf(" --no-delete-input-file\n");
1142 /* @@@ deprecated: */
1143 printf(" --no-escape-coding\n");
1145 printf(" --no-exhaustive-model-search\n");
1146 printf(" --no-lax\n");
1147 printf(" --no-mid-side\n");
1148 #ifdef FLAC__HAS_OGG
1149 printf(" --no-ogg\n");
1151 printf(" --no-padding\n");
1152 printf(" --no-qlp-coeff-prec-search\n");
1153 printf(" --no-replay-gain\n");
1154 printf(" --no-residual-gnuplot\n");
1155 printf(" --no-residual-text\n");
1156 printf(" --no-sector-align\n");
1157 printf(" --no-seektable\n");
1158 printf(" --no-silent\n");
1159 printf(" --no-verify\n");
1166 printf("For encoding:\n");
1167 printf(" The input file(s) may be a PCM RIFF WAVE file, AIFF file, or raw samples.\n");
1168 printf(" The output file(s) will be in native FLAC or Ogg FLAC format\n");
1169 printf("For decoding, the reverse is true.\n");
1171 printf("A single INPUTFILE may be - for stdin. No INPUTFILE implies stdin. Use of\n");
1172 printf("stdin implies -c (write to stdout). Normally you should use:\n");
1173 printf(" flac [options] -o outfilename or flac -d [options] -o outfilename\n");
1174 printf("instead of:\n");
1175 printf(" flac [options] > outfilename or flac -d [options] > outfilename\n");
1176 printf("since the former allows flac to seek backwards to write the STREAMINFO or\n");
1177 printf("WAVE/AIFF header contents when necessary.\n");
1179 printf("flac checks for the presence of a AIFF/WAVE header to decide whether or not to\n");
1180 printf("treat an input file as AIFF/WAVE format or raw samples. If any input file is\n");
1181 printf("raw you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n");
1182 printf("apply to all raw files. You can force AIFF/WAVE files to be treated as raw\n");
1183 printf("files using -fr.\n");
1185 printf("generic options:\n");
1186 printf(" -v, --version Show the flac version number\n");
1187 printf(" -h, --help Show basic usage a list of all options\n");
1188 printf(" -H, --explain Show this screen\n");
1189 printf(" -d, --decode Decode (the default behavior is to encode)\n");
1190 printf(" -t, --test Same as -d except no decoded file is written\n");
1191 printf(" -a, --analyze Same as -d except an analysis file is written\n");
1192 printf(" -c, --stdout Write output to stdout\n");
1193 printf(" -s, --silent Do not write runtime encode/decode statistics\n");
1194 printf(" -o, --output-name=FILENAME Force the output file name; usually flac just\n");
1195 printf(" changes the extension. May only be used when\n");
1196 printf(" encoding a single file. May not be used in\n");
1197 printf(" conjunction with --output-prefix.\n");
1198 printf(" --output-prefix=STRING Prefix each output file name with the given\n");
1199 printf(" STRING. This can be useful for encoding or\n");
1200 printf(" decoding files to a different directory. Make\n");
1201 printf(" sure if your STRING is a path name that it ends\n");
1202 printf(" with a '/' slash.\n");
1203 printf(" --delete-input-file Automatically delete the input file after a\n");
1204 printf(" successful encode or decode. If there was an\n");
1205 printf(" error (including a verify error) the input file\n");
1206 printf(" is left intact.\n");
1207 printf(" --skip={#|mm:ss.ss} Skip the first # samples of each input file; can\n");
1208 printf(" be used both for encoding and decoding. The\n");
1209 printf(" alternative form mm:ss.ss can be used to specify\n");
1210 printf(" minutes, seconds, and fractions of a second.\n");
1211 printf("analysis options:\n");
1212 printf(" --residual-text Include residual signal in text output. This\n");
1213 printf(" will make the file very big, much larger than\n");
1214 printf(" even the decoded file.\n");
1215 printf(" --residual-gnuplot Generate gnuplot files of residual distribution\n");
1216 printf(" of each subframe\n");
1217 printf("decoding options:\n");
1218 printf(" -F, --decode-through-errors By default flac stops decoding with an error\n");
1219 printf(" and removes the partially decoded file if it\n");
1220 printf(" encounters a bitstream error. With -F, errors\n");
1221 printf(" are still printed but flac will continue\n");
1222 printf(" decoding to completion. Note that errors may\n");
1223 printf(" cause the decoded audio to be missing some\n");
1224 printf(" samples or have silent sections.\n");
1225 printf("encoding options:\n");
1226 printf(" -V, --verify Verify a correct encoding by decoding the\n");
1227 printf(" output in parallel and comparing to the\n");
1228 printf(" original\n");
1229 #ifdef FLAC__HAS_OGG
1230 printf(" --ogg When encoding, generate Ogg-FLAC output instead\n");
1231 printf(" of native-FLAC. Ogg-FLAC streams are FLAC\n");
1232 printf(" streams wrapped in an Ogg transport layer. The\n");
1233 printf(" resulting file should have an '.ogg' extension\n");
1234 printf(" and will still be decodable by flac. When\n");
1235 printf(" decoding, force the input to be treated as\n");
1236 printf(" Ogg-FLAC. This is useful when piping input\n");
1237 printf(" from stdin or when the filename does not end in\n");
1238 printf(" '.ogg'.\n");
1239 printf(" --serial-number Serial number to use for the FLAC stream. When\n");
1240 printf(" encoding and no serial number is given, flac\n");
1241 printf(" uses '0'. When decoding and no number is\n");
1242 printf(" given, flac uses the serial number of the first\n");
1245 printf(" --lax Allow encoder to generate non-Subset files\n");
1246 printf(" --sector-align Align encoding of multiple CD format WAVE files\n");
1247 printf(" on sector boundaries.\n");
1248 printf(" --replay-gain Calculate ReplayGain values and store in Vorbis\n");
1249 printf(" comments. Title gains/peaks will be computed\n");
1250 printf(" for each file, and an album gain/peak will be\n");
1251 printf(" computed for all files. All input files must\n");
1252 printf(" have the same resolution, sample rate, and\n");
1253 printf(" number of channels. The sample rate must be\n");
1254 printf(" one of 8, 11.025, 12, 16, 22.05, 24, 32, 44.1,\n");
1255 printf(" or 48 kHz. NOTE: this option may also leave a\n");
1256 printf(" few extra bytes in the PADDING block.\n");
1257 printf(" --cuesheet=FILENAME Import the given cuesheet file and store it in\n");
1258 printf(" a CUESHEET metadata block. This option may only\n");
1259 printf(" be used when encoding a single file. A\n");
1260 printf(" seekpoint will be added for each index point in\n");
1261 printf(" the cuesheet to the SEEKTABLE unless\n");
1262 printf(" --no-cued-seekpoints is specified.\n");
1263 printf(" -T, --tag=FIELD=VALUE Add a Vorbis comment. Make sure to quote the\n");
1264 printf(" comment if necessary. This option may appear\n");
1265 printf(" more than once to add several comments. NOTE:\n");
1266 printf(" all tags will be added to all encoded files.\n");
1267 printf(" -S, --seekpoint={#|X|#x|#s} Include a point or points in a SEEKTABLE\n");
1268 printf(" # : a specific sample number for a seek point\n");
1269 printf(" X : a placeholder point (always goes at the end of the SEEKTABLE)\n");
1270 printf(" #x : # evenly spaced seekpoints, the first being at sample 0\n");
1271 printf(" #s : a seekpoint every # seconds; # does not have to be a whole number\n");
1272 printf(" You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
1273 printf(" ified union of all such values.\n");
1274 printf(" With no -S options, flac defaults to '-S 100x'. Use -S- for no SEEKTABLE.\n");
1275 printf(" Note: -S #x and -S #s will not work if the encoder can't determine the\n");
1276 printf(" input size before starting.\n");
1277 printf(" Note: if you use -S # and # is >= samples in the input, there will be\n");
1278 printf(" either no seek point entered (if the input size is determinable\n");
1279 printf(" before encoding starts) or a placeholder point (if input size is not\n");
1280 printf(" determinable)\n");
1281 printf(" -P, --padding=# Tell the encoder to write a PADDING metadata\n");
1282 printf(" block of the given length (in bytes) after the\n");
1283 printf(" STREAMINFO block. This is useful if you plan\n");
1284 printf(" to tag the file later with an APPLICATION\n");
1285 printf(" block; instead of having to rewrite the entire\n");
1286 printf(" file later just to insert your block, you can\n");
1287 printf(" write directly over the PADDING block. Note\n");
1288 printf(" that the total length of the PADDING block will\n");
1289 printf(" be 4 bytes longer than the length given because\n");
1290 printf(" of the 4 metadata block header bytes. You can\n");
1291 printf(" force no PADDING block at all to be written with\n");
1292 printf(" --no-padding, which is the default.\n");
1293 printf(" -b, --blocksize=# Specify the blocksize in samples; the default is\n");
1294 printf(" 1152 for -l 0, else 4608; must be one of 192,\n");
1295 printf(" 576, 1152, 2304, 4608, 256, 512, 1024, 2048,\n");
1296 printf(" 4096, 8192, 16384, or 32768 (unless --lax is\n");
1298 printf(" -0, --compression-level-0, --fast Synonymous with -l 0 -b 1152 -r 2,2\n");
1299 printf(" -1, --compression-level-1 Synonymous with -l 0 -b 1152 -M -r 2,2\n");
1300 printf(" -2, --compression-level-2 Synonymous with -l 0 -b 1152 -m -r 3\n");
1301 printf(" -3, --compression-level-3 Synonymous with -l 6 -b 4608 -r 3,3\n");
1302 printf(" -4, --compression-level-4 Synonymous with -l 8 -b 4608 -M -r 3,3\n");
1303 printf(" -5, --compression-level-5 Synonymous with -l 8 -b 4608 -m -r 3,3\n");
1304 printf(" -5 is the default setting\n");
1305 printf(" -6, --compression-level-6 Synonymous with -l 8 -b 4608 -m -r 4\n");
1306 printf(" -7, --compression-level-7 Synonymous with -l 8 -b 4608 -m -e -r 6\n");
1307 printf(" -8, --compression-level-8, --best Synonymous with -l 12 -b 4608 -m -e -r 6\n");
1308 printf(" -m, --mid-side Try mid-side coding for each frame\n");
1309 printf(" (stereo only)\n");
1310 printf(" -M, --adaptive-mid-side Adaptive mid-side coding for all frames\n");
1311 printf(" (stereo only)\n");
1312 printf(" -e, --exhaustive-model-search Do exhaustive model search (expensive!)\n");
1314 /*@@@ deprecated: */
1315 printf(" -E, --escape-coding Do escape coding in the entropy coder.\n");
1316 printf(" This causes the encoder to use an\n");
1317 printf(" unencoded representation of the residual\n");
1318 printf(" in a partition if it is smaller. It\n");
1319 printf(" increases the runtime and usually results\n");
1320 printf(" in an improvement of less than 1%.\n");
1322 printf(" -l, --max-lpc-order=# Max LPC order; 0 => only fixed predictors\n");
1323 printf(" -p, --qlp-coeff-precision-search Do exhaustive search of LP coefficient\n");
1324 printf(" quantization (expensive!); overrides -q;\n");
1325 printf(" does nothing if using -l 0\n");
1326 printf(" -q, --qlp-coeff-precision=# Specify precision in bits of quantized\n");
1327 printf(" linear-predictor coefficients; 0 => let\n");
1328 printf(" encoder decide (the minimun is %u, the\n", FLAC__MIN_QLP_COEFF_PRECISION);
1329 printf(" default is -q 0)\n");
1330 printf(" -r, --rice-partition-order=[#,]# Set [min,]max residual partition order\n");
1331 printf(" (# is 0..16; min defaults to 0; the\n");
1332 printf(" default is -r 0; above 4 doesn't usually\n");
1333 printf(" help much)\n");
1335 /*@@@ deprecated: */
1336 printf(" -R, -rice-parameter-search-distance=# Rice parameter search distance\n");
1338 printf("format options:\n");
1339 printf(" --endian={big|little} Set byte order for samples\n");
1340 printf(" --channels=# Number of channels\n");
1341 printf(" --bps=# Number of bits per sample\n");
1342 printf(" --sample-rate=# Sample rate in Hz\n");
1343 printf(" --sign={signed|unsigned} Sign of samples (the default is signed)\n");
1344 printf(" --force-raw-format Force input (when encoding) or output (when\n");
1345 printf(" decoding) to be treated as raw samples\n");
1346 printf("negative options:\n");
1347 printf(" --no-adaptive-mid-side\n");
1348 printf(" --no-decode-through-errors\n");
1349 printf(" --no-delete-input-file\n");
1351 /* @@@ deprecated: */
1352 printf(" --no-escape-coding\n");
1354 printf(" --no-exhaustive-model-search\n");
1355 printf(" --no-lax\n");
1356 printf(" --no-mid-side\n");
1357 #ifdef FLAC__HAS_OGG
1358 printf(" --no-ogg\n");
1360 printf(" --no-padding\n");
1361 printf(" --no-qlp-coeff-prec-search\n");
1362 printf(" --no-residual-gnuplot\n");
1363 printf(" --no-residual-text\n");
1364 printf(" --no-sector-align\n");
1365 printf(" --no-seektable\n");
1366 printf(" --no-silent\n");
1367 printf(" --no-verify\n");
1371 format_mistake(const char *infilename, const char *wrong, const char *right)
1373 fprintf(stderr, "WARNING: %s is not a %s file; treating as a %s file\n", infilename, wrong, right);
1376 int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file)
1378 FILE *encode_infile;
1379 FLAC__byte lookahead[12];
1380 unsigned lookahead_length = 0;
1381 FileFormat fmt= RAW;
1384 encode_options_t common_options;
1385 const char *outfilename = get_encoded_outfilename(infilename);
1387 if(0 == strcmp(infilename, "-")) {
1389 encode_infile = grabbag__file_get_binary_stdin();
1392 infilesize = grabbag__file_get_filesize(infilename);
1393 if(0 == (encode_infile = fopen(infilename, "rb"))) {
1394 fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
1399 if(!option_values.force_raw_format) {
1400 /* first set format based on name */
1401 if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav"))
1403 else if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".aif"))
1405 else if(strlen(infilename) > 4 && 0 == strcasecmp(infilename+(strlen(infilename)-5), ".aiff"))
1408 /* attempt to guess the file type based on the first 12 bytes */
1409 if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
1411 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1415 if(!strncmp(lookahead, "RIFF", 4) && !strncmp(lookahead+8, "WAVE", 4))
1417 else if(!strncmp(lookahead, "FORM", 4) && !strncmp(lookahead+8, "AIFF", 4))
1421 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1427 if(option_values.sector_align && fmt == RAW && infilesize < 0) {
1428 fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n");
1433 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)
1434 return usage_error("ERROR: for encoding a raw file you must specify a value for --endian, --sign, --channels, --bps, and --sample-rate\n");
1437 if(encode_infile == stdin || option_values.force_to_stdout) {
1438 if(option_values.replay_gain)
1439 return usage_error("ERROR: --replay-gain cannot be used when encoding to stdout\n");
1442 if(!flac__utils_parse_skip_until_specification(option_values.skip_specification, &common_options.skip_specification) || common_options.skip_specification.is_relative)
1443 return usage_error("ERROR: invalid value for --skip\n");
1445 common_options.verbose = option_values.verbose;
1446 common_options.verify = option_values.verify;
1447 #ifdef FLAC__HAS_OGG
1448 common_options.use_ogg = option_values.use_ogg;
1449 common_options.has_serial_number = option_values.has_serial_number;
1450 common_options.serial_number = option_values.serial_number;
1452 common_options.lax = option_values.lax;
1453 common_options.do_mid_side = option_values.do_mid_side;
1454 common_options.loose_mid_side = option_values.loose_mid_side;
1455 common_options.do_exhaustive_model_search = option_values.do_exhaustive_model_search;
1456 common_options.do_escape_coding = option_values.do_escape_coding;
1457 common_options.do_qlp_coeff_prec_search = option_values.do_qlp_coeff_prec_search;
1458 common_options.min_residual_partition_order = option_values.min_residual_partition_order;
1459 common_options.max_residual_partition_order = option_values.max_residual_partition_order;
1460 common_options.rice_parameter_search_dist = option_values.rice_parameter_search_dist;
1461 common_options.max_lpc_order = option_values.max_lpc_order;
1462 common_options.blocksize = (unsigned)option_values.blocksize;
1463 common_options.qlp_coeff_precision = option_values.qlp_coeff_precision;
1464 common_options.padding = option_values.padding;
1465 common_options.requested_seek_points = option_values.requested_seek_points;
1466 common_options.num_requested_seek_points = option_values.num_requested_seek_points;
1467 common_options.cuesheet_filename = option_values.cuesheet_filename;
1468 common_options.cued_seekpoints = option_values.cued_seekpoints;
1469 common_options.is_first_file = is_first_file;
1470 common_options.is_last_file = is_last_file;
1471 common_options.align_reservoir = align_reservoir;
1472 common_options.align_reservoir_samples = &align_reservoir_samples;
1473 common_options.replay_gain = option_values.replay_gain;
1474 common_options.sector_align = option_values.sector_align;
1475 common_options.vorbis_comment = option_values.vorbis_comment;
1476 common_options.debug.disable_constant_subframes = option_values.debug.disable_constant_subframes;
1477 common_options.debug.disable_fixed_subframes = option_values.debug.disable_fixed_subframes;
1478 common_options.debug.disable_verbatim_subframes = option_values.debug.disable_verbatim_subframes;
1481 raw_encode_options_t options;
1483 options.common = common_options;
1484 options.is_big_endian = option_values.format_is_big_endian;
1485 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1486 options.channels = option_values.format_channels;
1487 options.bps = option_values.format_bps;
1488 options.sample_rate = option_values.format_sample_rate;
1490 retval = flac__encode_raw(encode_infile, infilesize, infilename, outfilename, lookahead, lookahead_length, options);
1493 wav_encode_options_t options;
1495 options.common = common_options;
1498 retval = flac__encode_aif(encode_infile, infilesize, infilename, outfilename, lookahead, lookahead_length, options);
1500 retval = flac__encode_wav(encode_infile, infilesize, infilename, outfilename, lookahead, lookahead_length, options);
1503 if(retval == 0 && strcmp(infilename, "-")) {
1504 if(strcmp(outfilename, "-")) {
1505 if(option_values.replay_gain) {
1506 float title_gain, title_peak;
1508 grabbag__replaygain_get_title(&title_gain, &title_peak);
1509 if(0 != (error = grabbag__replaygain_store_to_file_title(outfilename, title_gain, title_peak, /*preserve_modtime=*/true))) {
1510 fprintf(stderr, "%s: ERROR writing ReplayGain title tags\n", outfilename);
1513 grabbag__file_copy_metadata(infilename, outfilename);
1515 if(option_values.delete_input)
1522 int decode_file(const char *infilename)
1525 FLAC__bool treat_as_ogg = false;
1526 decode_options_t common_options;
1527 const char *outfilename = get_decoded_outfilename(infilename);
1529 if(!option_values.test_only && !option_values.analyze) {
1530 if(option_values.force_raw_format && (option_values.format_is_big_endian < 0 || option_values.format_is_unsigned_samples < 0))
1531 return usage_error("ERROR: for decoding to a raw file you must specify a value for --endian and --sign\n");
1534 if(option_values.use_ogg)
1535 treat_as_ogg = true;
1536 else if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".ogg"))
1537 treat_as_ogg = true;
1539 treat_as_ogg = false;
1541 #ifndef FLAC__HAS_OGG
1543 fprintf(stderr, "%s: Ogg support has not been built into this copy of flac\n", infilename);
1548 if(!flac__utils_parse_skip_until_specification(option_values.skip_specification, &common_options.skip_specification) || common_options.skip_specification.is_relative)
1549 return usage_error("ERROR: invalid value for --skip\n");
1551 common_options.verbose = option_values.verbose;
1552 common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
1553 #ifdef FLAC__HAS_OGG
1554 common_options.is_ogg = treat_as_ogg;
1555 common_options.use_first_serial_number = !option_values.has_serial_number;
1556 common_options.serial_number = option_values.serial_number;
1559 if(!option_values.force_raw_format) {
1560 wav_decode_options_t options;
1562 options.common = common_options;
1564 retval = flac__decode_wav(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, options);
1567 raw_decode_options_t options;
1569 options.common = common_options;
1570 options.is_big_endian = option_values.format_is_big_endian;
1571 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1573 retval = flac__decode_raw(infilename, option_values.test_only? 0 : outfilename, option_values.analyze, option_values.aopts, options);
1576 if(retval == 0 && strcmp(infilename, "-")) {
1577 if(strcmp(outfilename, "-"))
1578 grabbag__file_copy_metadata(infilename, outfilename);
1579 if(option_values.delete_input && !option_values.test_only && !option_values.analyze)
1586 const char *get_encoded_outfilename(const char *infilename)
1588 if(0 == option_values.cmdline_forced_outfilename) {
1589 static char buffer[4096]; /* @@@ bad MAGIC NUMBER */
1591 if(0 == strcmp(infilename, "-") || option_values.force_to_stdout) {
1592 strcpy(buffer, "-");
1595 const char *suffix = (option_values.use_ogg? ".ogg" : ".flac");
1597 strcpy(buffer, option_values.output_prefix? option_values.output_prefix : "");
1598 strcat(buffer, infilename);
1599 if(0 == (p = strrchr(buffer, '.')))
1600 strcat(buffer, suffix);
1602 if(0 == strcmp(p, suffix)) {
1613 return option_values.cmdline_forced_outfilename;
1616 const char *get_decoded_outfilename(const char *infilename)
1618 if(0 == option_values.cmdline_forced_outfilename) {
1619 static char buffer[4096]; /* @@@ bad MAGIC NUMBER */
1621 if(0 == strcmp(infilename, "-") || option_values.force_to_stdout) {
1622 strcpy(buffer, "-");
1625 static const char *suffixes[] = { ".wav", ".raw", ".ana" };
1626 const char *suffix = suffixes[option_values.analyze? 2 : option_values.force_raw_format? 1 : 0];
1628 strcpy(buffer, option_values.output_prefix? option_values.output_prefix : "");
1629 strcat(buffer, infilename);
1630 if(0 == (p = strrchr(buffer, '.')))
1631 strcat(buffer, suffix);
1633 if(0 == strcmp(p, suffix)) {
1644 return option_values.cmdline_forced_outfilename;
1647 void die(const char *message)
1649 FLAC__ASSERT(0 != message);
1650 fprintf(stderr, "ERROR: %s\n", message);
1654 char *local_strdup(const char *source)
1657 FLAC__ASSERT(0 != source);
1658 if(0 == (ret = strdup(source)))
1659 die("out of memory during strdup()");