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.
24 #if !defined _MSC_VER && !defined __MINGW32__
25 /* unlink is in stdio.h in VC++ */
26 #include <unistd.h> /* for unlink() */
28 #define strcasecmp stricmp
37 /*[JEC] was:#if HAVE_GETOPT_LONG*/
38 /*[JEC] see flac/include/share/getopt.h as to why the change */
41 # include "share/getopt.h"
44 typedef enum { RAW, WAV, AIF } FileFormat;
48 static void init_options();
49 static int parse_options(int argc, char *argv[]);
50 static int parse_option(int short_option, const char *long_option, const char *option_argument);
51 static void free_options();
53 static int usage_error(const char *message, ...);
54 static void short_usage();
55 static void long_usage();
56 static void format_mistake(const char *infilename, const char *wrong, const char *right);
58 static int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file);
59 static int decode_file(const char *infilename, const char *forced_outfilename);
61 static void die(const char *message);
62 static char *local_strdup(const char *source);
66 * FLAC__share__getopt format struct; note that for long options with no
67 * short option equivalent we just set the 'val' field to 0.
69 static struct FLAC__share__option long_options_[] = {
73 { "--help", 0, 0, 'H' },
74 { "--decode", 0, 0, 'd' },
75 { "--analyze", 0, 0, 'a' },
76 { "--test", 0, 0, 't' },
77 { "--stdout", 0, 0, 'c' },
78 { "--silent", 0, 0, 's' },
79 { "--delete-input-file", 0, 0, 0 },
80 { "--output-prefix", 1, 0, 0 },
81 { "--output-name", 1, 0, 'o' },
82 { "--skip", 1, 0, 0 },
87 { "--decode-through-errors", 0, 0, 'F' },
92 { "--compression-level-0", 0, 0, '0' },
93 { "--compression-level-1", 0, 0, '1' },
94 { "--compression-level-2", 0, 0, '2' },
95 { "--compression-level-3", 0, 0, '3' },
96 { "--compression-level-4", 0, 0, '4' },
97 { "--compression-level-5", 0, 0, '5' },
98 { "--compression-level-6", 0, 0, '6' },
99 { "--compression-level-7", 0, 0, '7' },
100 { "--compression-level-8", 0, 0, '8' },
101 { "--compression-level-9", 0, 0, '9' },
102 { "--best", 0, 0, '8' },
103 { "--fast", 0, 0, '0' },
104 { "--super-secret-impractical-compression-level", 0, 0, 0 },
105 { "--verify", 0, 0, 'V' },
106 { "--force-raw-input", 0, 0, 0 },
107 { "--lax", 0, 0, 0 },
108 { "--sector-align", 0, 0, 0 },
109 { "--seekpoint", 1, 0, 'S' },
110 { "--padding", 1, 0, 'P' },
112 { "--ogg", 0, 0, 0 },
114 { "--blocksize", 1, 0, 'b' },
115 { "--exhaustive-model-search", 0, 0, 'e' },
117 /* @@@ deprecated: */
118 { "--escape-coding", 0, 0, 'E' },
120 { "--max-lpc-order", 1, 0, 'l' },
121 { "--mid-side", 0, 0, 'm' },
122 { "--adaptive-mid-side", 0, 0, 'M' },
123 { "--qlp-coeff-precision-search", 0, 0, 'p' },
124 { "--qlp-coeff-precision", 1, 0, 'q' },
125 { "--rice-partition-order", 1, 0, 'r' },
127 /* @@@ deprecated: */
128 { "--rice-parameter-search-distance", 1, 0, 'R' },
130 { "--endian", 1, 0, 0 },
131 { "--channels", 1, 0, 0 },
132 { "--bps", 1, 0, 0 },
133 { "--sample-rate", 1, 0, 0 },
134 { "--sign", 1, 0, 0 },
139 { "--residual-gnu-plot", 0, 0, 0 },
140 { "--residual-text", 0, 0, 0 },
145 { "--no-decode-through-errors", 0, 0, 0 },
146 { "--no-silent", 0, 0, 0 },
147 { "--no-seektable", 0, 0, 0 },
148 { "--no-delete-input-file", 0, 0, 0 },
149 { "--no-sector-align", 0, 0, 0 },
150 { "--no-lax", 0, 0, 0 },
152 { "--no-ogg", 0, 0, 0 },
154 { "--no-exhaustive-model-search", 0, 0, 0 },
156 /* @@@ deprecated: */
157 { "--no-escape-coding", 0, 0, 0 },
159 { "--no-mid-side", 0, 0, 0 },
160 { "--no-adaptive-mid-side", 0, 0, 0 },
161 { "--no-qlp-coeff-prec-search", 0, 0, 0 },
162 { "--no-padding", 0, 0, 0 },
163 { "--no-verify", 0, 0, 0 },
164 { "--no-residual-gnuplot", 0, 0, 0 },
165 { "--no-residual-text", 0, 0, 0 },
172 * global to hold command-line option values
176 FLAC__bool show_long_help;
177 FLAC__bool mode_decode;
180 FLAC__bool continue_through_decode_errors;
182 FLAC__bool test_only;
185 FLAC__bool do_mid_side;
186 FLAC__bool loose_mid_side;
187 FLAC__bool do_exhaustive_model_search;
188 FLAC__bool do_escape_coding;
189 FLAC__bool do_qlp_coeff_prec_search;
190 FLAC__bool force_to_stdout;
191 FLAC__bool force_raw_format;
192 FLAC__bool delete_input;
193 FLAC__bool sector_align;
194 const char *cmdline_forced_outfilename;
195 const char *output_prefix;
196 analysis_options aopts;
198 unsigned max_lpc_order;
199 unsigned qlp_coeff_precision;
201 int format_is_big_endian;
202 int format_is_unsigned_samples;
205 int format_sample_rate;
207 int min_residual_partition_order;
208 int max_residual_partition_order;
209 int rice_parameter_search_dist;
210 char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
211 int num_requested_seek_points; /* -1 => no -S options were given, 0 => -S- was given */
219 * miscellaneous globals
222 static FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
223 static FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
224 static unsigned align_reservoir_samples = 0; /* 0 .. 587 */
226 static const char *flac_suffix = ".flac", *ogg_suffix = ".ogg";
229 int main(int argc, char *argv[])
235 if((retval = parse_options(argc, argv)) == 0)
247 if(option_values.show_long_help) {
252 if(option_values.num_files == 0) {
258 * tweak options; validate the values
260 if(!option_values.mode_decode) {
261 if(option_values.blocksize < 0) {
262 if(option_values.max_lpc_order == 0)
263 option_values.blocksize = 1152;
265 option_values.blocksize = 4608;
267 if(option_values.max_residual_partition_order < 0) {
268 if(option_values.blocksize <= 1152)
269 option_values.max_residual_partition_order = 2;
270 else if(option_values.blocksize <= 2304)
271 option_values.max_residual_partition_order = 3;
272 else if(option_values.blocksize <= 4608)
273 option_values.max_residual_partition_order = 3;
275 option_values.max_residual_partition_order = 4;
276 option_values.min_residual_partition_order = option_values.max_residual_partition_order;
278 if(option_values.rice_parameter_search_dist < 0) {
279 option_values.rice_parameter_search_dist = 0;
283 if(option_values.test_only) {
284 if(option_values.skip > 0)
285 return usage_error("ERROR: --skip is not allowed in test mode\n");
289 FLAC__ASSERT(option_values.blocksize >= 0 || option_values.mode_decode);
291 if(option_values.format_channels >= 0) {
292 if(option_values.format_channels == 0 || (unsigned)option_values.format_channels > FLAC__MAX_CHANNELS)
293 return usage_error("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", option_values.format_channels, FLAC__MAX_CHANNELS);
295 if(option_values.format_bps >= 0) {
296 if(option_values.format_bps != 8 && option_values.format_bps != 16 && option_values.format_bps != 24)
297 return usage_error("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", option_values.format_bps);
299 if(option_values.format_sample_rate >= 0) {
300 if(!FLAC__format_sample_rate_is_valid(option_values.format_sample_rate))
301 return usage_error("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", option_values.format_sample_rate, FLAC__MAX_SAMPLE_RATE);
303 if(!option_values.mode_decode && ((unsigned)option_values.blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)option_values.blocksize > FLAC__MAX_BLOCK_SIZE)) {
304 return usage_error("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)option_values.blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
306 if(option_values.qlp_coeff_precision > 0 && option_values.qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
307 return usage_error("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", option_values.qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
310 if(option_values.sector_align) {
311 if(option_values.mode_decode)
312 return usage_error("ERROR: --sector-align only allowed for encoding\n");
313 else if(option_values.skip > 0)
314 return usage_error("ERROR: --sector-align not allowed with --skip\n");
315 else if(option_values.format_channels >= 0 && option_values.format_channels != 2)
316 return usage_error("ERROR: --sector-align can only be done with stereo input\n");
317 else if(option_values.format_bps >= 0 && option_values.format_bps != 16)
318 return usage_error("ERROR: --sector-align can only be done with 16-bit samples\n");
319 else if(option_values.format_sample_rate >= 0 && option_values.format_sample_rate != 44100)
320 return usage_error("ERROR: --sector-align can only be done with a sample rate of 44100\n");
322 if(option_values.num_files > 1 && option_values.cmdline_forced_outfilename) {
323 return usage_error("ERROR: -o cannot be used with multiple files\n");
325 if(option_values.cmdline_forced_outfilename && option_values.output_prefix) {
326 return usage_error("ERROR: --output-prefix conflicts with -o\n");
331 if(option_values.verbose) {
332 fprintf(stderr, "\n");
333 fprintf(stderr, "flac %s, Copyright (C) 2000,2001,2002 Josh Coalson\n", FLAC__VERSION_STRING);
334 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
335 fprintf(stderr, "welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
337 if(!option_values.mode_decode) {
339 if(option_values.padding < 0)
342 sprintf(padopt, " %d", option_values.padding);
348 "%s -P%s -b %u%s -l %u%s%s%s -q %u -r %u,%u -R %u%s\n",
349 option_values.delete_input?" --delete-input-file":"",
350 option_values.sector_align?" --sector-align":"",
352 option_values.use_ogg?" --ogg":"",
354 option_values.lax?" --lax":"",
356 (unsigned)option_values.blocksize,
357 option_values.loose_mid_side?" -M":option_values.do_mid_side?" -m":"",
358 option_values.max_lpc_order,
359 option_values.do_exhaustive_model_search?" -e":"",
360 option_values.do_escape_coding?" -E":"",
361 option_values.do_qlp_coeff_prec_search?" -p":"",
362 option_values.qlp_coeff_precision,
363 (unsigned)option_values.min_residual_partition_order,
364 (unsigned)option_values.max_residual_partition_order,
365 (unsigned)option_values.rice_parameter_search_dist,
366 option_values.verify? " -V":""
371 if(option_values.mode_decode) {
372 FLAC__bool first = true;
374 if(option_values.num_files == 0) {
375 retval = decode_file("-", 0);
379 if(option_values.num_files > 1)
380 option_values.cmdline_forced_outfilename = 0;
381 for(i = 0, retval = 0; i < option_values.num_files && retval == 0; i++) {
382 if(0 == strcmp(option_values.filenames[i], "-") && !first)
384 retval = decode_file(option_values.filenames[i], 0);
390 FLAC__bool first = true;
392 if(option_values.num_files == 0) {
393 retval = encode_file("-", 0, true);
397 if(option_values.num_files > 1)
398 option_values.cmdline_forced_outfilename = 0;
399 for(i = 0, retval = 0; i < option_values.num_files && retval == 0; i++) {
400 if(0 == strcmp(option_values.filenames[i], "-") && !first)
402 retval = encode_file(option_values.filenames[i], 0, i == (option_values.num_files-1));
413 option_values.show_long_help = false;
414 option_values.mode_decode = false;
415 option_values.verify = false;
416 option_values.verbose = true;
417 option_values.continue_through_decode_errors = false;
418 option_values.lax = false;
419 option_values.test_only = false;
420 option_values.analyze = false;
421 option_values.use_ogg = false;
422 option_values.do_mid_side = true;
423 option_values.loose_mid_side = false;
424 option_values.do_exhaustive_model_search = false;
425 option_values.do_escape_coding = false;
426 option_values.do_qlp_coeff_prec_search = false;
427 option_values.force_to_stdout = false;
428 option_values.force_raw_format = false;
429 option_values.delete_input = false;
430 option_values.sector_align = false;
431 option_values.cmdline_forced_outfilename = 0;
432 option_values.output_prefix = 0;
433 option_values.aopts.do_residual_text = false;
434 option_values.aopts.do_residual_gnuplot = false;
435 option_values.padding = -1;
436 option_values.max_lpc_order = 8;
437 option_values.qlp_coeff_precision = 0;
438 option_values.skip = 0;
439 option_values.format_is_big_endian = -1;
440 option_values.format_is_unsigned_samples = false;
441 option_values.format_channels = -1;
442 option_values.format_bps = -1;
443 option_values.format_sample_rate = -1;
444 option_values.blocksize = -1;
445 option_values.min_residual_partition_order = -1;
446 option_values.max_residual_partition_order = -1;
447 option_values.rice_parameter_search_dist = -1;
448 option_values.requested_seek_points[0] = '\0';
449 option_values.num_requested_seek_points = -1;
451 option_values.num_files = 0;
452 option_values.filenames = 0;
455 int parse_options(int argc, char *argv[])
458 int option_index = 1;
459 FLAC__bool had_error = false;
460 /*@@@ E and R: are deprecated */
461 const char *short_opts = "0123456789ab:cdeFHl:mMo:pP:q:r:sS:tV";
463 while ((short_option = FLAC__share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
464 switch (short_option) {
465 case 0: /* long option with no equivalent short option */
466 had_error |= (parse_option(short_option, long_options_[option_index].name, FLAC__share__optarg) != 0);
472 default: /* short option */
473 had_error |= (parse_option(short_option, 0, FLAC__share__optarg) != 0);
482 FLAC__ASSERT(FLAC__share__optind <= argc);
484 option_values.num_files = argc - FLAC__share__optind;
486 if(option_values.num_files > 0) {
488 if(0 == (option_values.filenames = malloc(sizeof(char *) * option_values.num_files)))
489 die("out of memory allocating space for file names list");
490 while(FLAC__share__optind < argc)
491 option_values.filenames[i++] = local_strdup(argv[FLAC__share__optind++]);
497 int parse_option(int short_option, const char *long_option, const char *option_argument)
499 FLAC__bool ok = true;
502 if(short_option == 0) {
503 FLAC__ASSERT(0 != long_option);
504 if(0 == strcmp(long_option, "--delete-input-file")) {
505 option_values.delete_input = true;
507 else if(0 == strcmp(long_option, "--output-prefix")) {
508 FLAC__ASSERT(0 != option_argument);
509 option_values.output_prefix = option_argument;
511 else if(0 == strcmp(long_option, "--skip")) {
512 FLAC__ASSERT(0 != option_argument);
513 option_values.skip = (FLAC__uint64)atoi(option_argument); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
515 else if(0 == strcmp(long_option, "--super-secret-impractical-compression-level")) {
516 option_values.do_exhaustive_model_search = true;
517 option_values.do_escape_coding = true;
518 option_values.do_mid_side = true;
519 option_values.loose_mid_side = false;
520 option_values.do_qlp_coeff_prec_search = true;
521 option_values.min_residual_partition_order = 0;
522 option_values.max_residual_partition_order = 16;
523 option_values.rice_parameter_search_dist = 0;
524 option_values.max_lpc_order = 32;
526 else if(0 == strcmp(long_option, "--force-raw-input")) {
527 option_values.force_raw_format = true;
529 else if(0 == strcmp(long_option, "--lax")) {
530 option_values.lax = true;
532 else if(0 == strcmp(long_option, "--sector-align")) {
533 option_values.sector_align = true;
536 else if(0 == strcmp(long_option, "--ogg")) {
537 option_values.use_ogg = true;
540 else if(0 == strcmp(long_option, "--endian")) {
541 FLAC__ASSERT(0 != option_argument);
542 if(0 == strncmp(option_argument, "big", strlen(option_argument)))
543 option_values.format_is_big_endian = true;
544 else if(0 == strncmp(option_argument, "little", strlen(option_argument)))
545 option_values.format_is_big_endian = false;
547 return usage_error("ERROR: argument to --endian must be \"big\" or \"little\"\n");
551 else if(0 == strcmp(long_option, "--channels")) {
552 FLAC__ASSERT(0 != option_argument);
553 option_values.format_channels = atoi(option_argument);
555 else if(0 == strcmp(long_option, "--bps")) {
556 FLAC__ASSERT(0 != option_argument);
557 option_values.format_bps = atoi(option_argument);
559 else if(0 == strcmp(long_option, "--sample-rate")) {
560 FLAC__ASSERT(0 != option_argument);
561 option_values.format_sample_rate = atoi(option_argument);
563 else if(0 == strcmp(long_option, "--sign")) {
564 FLAC__ASSERT(0 != option_argument);
565 if(0 == strncmp(option_argument, "signed", strlen(option_argument)))
566 option_values.format_is_unsigned_samples = false;
567 else if(0 == strncmp(option_argument, "unsigned", strlen(option_argument)))
568 option_values.format_is_unsigned_samples = true;
570 return usage_error("ERROR: argument to --sign must be \"signed\" or \"unsigned\"\n");
574 else if(0 == strcmp(long_option, "--residual-gnu-plot")) {
575 option_values.aopts.do_residual_gnuplot = true;
577 else if(0 == strcmp(long_option, "--residual-text")) {
578 option_values.aopts.do_residual_text = true;
583 else if(0 == strcmp(long_option, "--no-decode-through-errors")) {
584 option_values.continue_through_decode_errors = false;
586 else if(0 == strcmp(long_option, "--no-silent")) {
587 option_values.verbose = true;
589 else if(0 == strcmp(long_option, "--no-seektable")) {
590 option_values.num_requested_seek_points = 0;
591 option_values.requested_seek_points[0] = '\0';
593 else if(0 == strcmp(long_option, "--no-delete-input-file")) {
594 option_values.delete_input = false;
596 else if(0 == strcmp(long_option, "--no-sector-align")) {
597 option_values.sector_align = false;
599 else if(0 == strcmp(long_option, "--no-lax")) {
600 option_values.lax = false;
603 else if(0 == strcmp(long_option, "--no-ogg")) {
604 option_values.use_ogg = false;
607 else if(0 == strcmp(long_option, "--no-exhaustive-model-search")) {
608 option_values.do_exhaustive_model_search = false;
611 /* @@@ deprecated: */
612 else if(0 == strcmp(long_option, "--no-escape-coding")) {
613 option_values.do_escape_coding = false;
616 else if(0 == strcmp(long_option, "--no-mid-side")) {
617 option_values.do_mid_side = option_values.loose_mid_side = false;
619 else if(0 == strcmp(long_option, "--no-adaptive-mid-side")) {
620 option_values.loose_mid_side = option_values.do_mid_side = false;
622 else if(0 == strcmp(long_option, "--no-qlp-coeff-prec-search")) {
623 option_values.do_qlp_coeff_prec_search = false;
625 else if(0 == strcmp(long_option, "--no-padding")) {
626 option_values.padding = -1;
628 else if(0 == strcmp(long_option, "--no-verify")) {
629 option_values.verify = false;
631 else if(0 == strcmp(long_option, "--no-residual-gnuplot")) {
632 option_values.aopts.do_residual_gnuplot = false;
634 else if(0 == strcmp(long_option, "--no-residual-text")) {
635 option_values.aopts.do_residual_text = false;
639 switch(short_option) {
641 option_values.show_long_help = true;
644 option_values.mode_decode = true;
647 option_values.mode_decode = true;
648 option_values.analyze = true;
651 option_values.mode_decode = true;
652 option_values.test_only = true;
655 option_values.force_to_stdout = true;
658 option_values.verbose = false;
661 FLAC__ASSERT(0 != option_argument);
662 option_values.cmdline_forced_outfilename = option_argument;
665 option_values.continue_through_decode_errors = true;
668 option_values.do_exhaustive_model_search = false;
669 option_values.do_escape_coding = false;
670 option_values.do_mid_side = false;
671 option_values.loose_mid_side = false;
672 option_values.qlp_coeff_precision = 0;
673 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
674 option_values.rice_parameter_search_dist = 0;
675 option_values.max_lpc_order = 0;
678 option_values.do_exhaustive_model_search = false;
679 option_values.do_escape_coding = false;
680 option_values.do_mid_side = true;
681 option_values.loose_mid_side = true;
682 option_values.qlp_coeff_precision = 0;
683 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
684 option_values.rice_parameter_search_dist = 0;
685 option_values.max_lpc_order = 0;
688 option_values.do_exhaustive_model_search = false;
689 option_values.do_escape_coding = false;
690 option_values.do_mid_side = true;
691 option_values.loose_mid_side = false;
692 option_values.qlp_coeff_precision = 0;
693 option_values.min_residual_partition_order = 0;
694 option_values.max_residual_partition_order = 3;
695 option_values.rice_parameter_search_dist = 0;
696 option_values.max_lpc_order = 0;
699 option_values.do_exhaustive_model_search = false;
700 option_values.do_escape_coding = false;
701 option_values.do_mid_side = false;
702 option_values.loose_mid_side = false;
703 option_values.qlp_coeff_precision = 0;
704 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
705 option_values.rice_parameter_search_dist = 0;
706 option_values.max_lpc_order = 6;
709 option_values.do_exhaustive_model_search = false;
710 option_values.do_escape_coding = false;
711 option_values.do_mid_side = true;
712 option_values.loose_mid_side = true;
713 option_values.qlp_coeff_precision = 0;
714 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
715 option_values.rice_parameter_search_dist = 0;
716 option_values.max_lpc_order = 8;
719 option_values.do_exhaustive_model_search = false;
720 option_values.do_escape_coding = false;
721 option_values.do_mid_side = true;
722 option_values.loose_mid_side = false;
723 option_values.qlp_coeff_precision = 0;
724 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
725 option_values.rice_parameter_search_dist = 0;
726 option_values.max_lpc_order = 8;
729 option_values.do_exhaustive_model_search = false;
730 option_values.do_escape_coding = false;
731 option_values.do_mid_side = true;
732 option_values.loose_mid_side = false;
733 option_values.qlp_coeff_precision = 0;
734 option_values.min_residual_partition_order = 0;
735 option_values.max_residual_partition_order = 4;
736 option_values.rice_parameter_search_dist = 0;
737 option_values.max_lpc_order = 8;
740 option_values.do_exhaustive_model_search = true;
741 option_values.do_escape_coding = false;
742 option_values.do_mid_side = true;
743 option_values.loose_mid_side = false;
744 option_values.qlp_coeff_precision = 0;
745 option_values.min_residual_partition_order = 0;
746 option_values.max_residual_partition_order = 6;
747 option_values.rice_parameter_search_dist = 0;
748 option_values.max_lpc_order = 8;
751 option_values.do_exhaustive_model_search = true;
752 option_values.do_escape_coding = false;
753 option_values.do_mid_side = true;
754 option_values.loose_mid_side = false;
755 option_values.qlp_coeff_precision = 0;
756 option_values.min_residual_partition_order = 0;
757 option_values.max_residual_partition_order = 6;
758 option_values.rice_parameter_search_dist = 0;
759 option_values.max_lpc_order = 12;
762 return usage_error("ERROR: compression level '9' is reserved\n");
765 option_values.verify = true;
768 FLAC__ASSERT(0 != option_argument);
769 if(option_values.num_requested_seek_points < 0)
770 option_values.num_requested_seek_points = 0;
771 option_values.num_requested_seek_points++;
772 strcat(option_values.requested_seek_points, option_argument);
773 strcat(option_values.requested_seek_points, "<");
776 FLAC__ASSERT(0 != option_argument);
777 option_values.padding = atoi(option_argument);
778 if(option_values.padding < 0) {
779 return usage_error("ERROR: argument to -P must be >= 0\n");
783 FLAC__ASSERT(0 != option_argument);
784 option_values.blocksize = atoi(option_argument);
787 option_values.do_exhaustive_model_search = true;
790 option_values.do_escape_coding = true;
793 FLAC__ASSERT(0 != option_argument);
794 option_values.max_lpc_order = atoi(option_argument);
797 option_values.do_mid_side = true;
798 option_values.loose_mid_side = false;
801 option_values.loose_mid_side = option_values.do_mid_side = true;
804 option_values.do_qlp_coeff_prec_search = true;
807 FLAC__ASSERT(0 != option_argument);
808 option_values.qlp_coeff_precision = atoi(option_argument);
811 FLAC__ASSERT(0 != option_argument);
812 p = strchr(option_argument, ',');
814 option_values.min_residual_partition_order = 0;
815 option_values.max_residual_partition_order = atoi(option_argument);
818 option_values.min_residual_partition_order = atoi(option_argument);
819 option_values.max_residual_partition_order = atoi(++p);
823 FLAC__ASSERT(0 != option_argument);
824 option_values.rice_parameter_search_dist = atoi(option_argument);
836 if(0 != option_values.filenames)
837 free(option_values.filenames);
840 int usage_error(const char *message, ...)
846 va_start(args, message);
848 (void) vfprintf(stderr, message, args);
852 printf("Type \"flac\" for a usage summary or \"flac --help\" for all options\n");
857 static void usage_header()
859 printf("===============================================================================\n");
860 printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
861 printf("Copyright (C) 2000,2001,2002 Josh Coalson\n");
863 printf("This program is free software; you can redistribute it and/or\n");
864 printf("modify it under the terms of the GNU General Public License\n");
865 printf("as published by the Free Software Foundation; either version 2\n");
866 printf("of the License, or (at your option) any later version.\n");
868 printf("This program is distributed in the hope that it will be useful,\n");
869 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
870 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
871 printf("GNU General Public License for more details.\n");
873 printf("You should have received a copy of the GNU General Public License\n");
874 printf("along with this program; if not, write to the Free Software\n");
875 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
876 printf("===============================================================================\n");
883 printf("This is the short help; for full help use 'flac --help'\n");
885 printf("To encode:\n");
886 printf(" flac [-#] [infile [...]]\n");
888 printf(" -# is -0 (fastest compression) to -8 (highest compression); -5 is the default\n");
890 printf("To decode:\n");
891 printf(" flac -d [infile [...]]\n");
893 printf("To test:\n");
894 printf(" flac -t [infile [...]]\n");
901 printf(" flac [options] [infile [...]]\n");
903 printf("For encoding:\n");
904 printf(" the input file(s) may be a PCM RIFF WAVE file, AIFF file, or raw samples\n");
905 printf(" the output file(s) will be in FLAC format\n");
906 printf("For decoding, the reverse is true\n");
908 printf("A single 'infile' may be - for stdin. No 'infile' implies stdin. Use of\n");
909 printf("stdin implies -c (write to stdout). Normally you should use:\n");
910 printf(" flac [options] -o outfilename or flac -d [options] -o outfilename\n");
911 printf("instead of:\n");
912 printf(" flac [options] > outfilename or flac -d [options] > outfilename\n");
913 printf("since the former allows flac to seek backwards to write the STREAMINFO or\n");
914 printf("RIFF WAVE header contents when necessary.\n");
916 printf("flac checks for the presence of a AIFF/RIFF WAVE header to decide whether or\n");
917 printf("not to treat an input file as AIFF/WAVE format or raw samples. If any infile\n");
918 printf("is raw you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n");
919 printf("apply to all raw files. You can force AIFF/WAVE files to be treated as a raw\n");
920 printf("files using -fr.\n");
922 printf("generic options:\n");
923 printf(" -d : decode (default behavior is encode)\n");
924 printf(" -t : test (same as -d except no decoded file is written)\n");
925 printf(" -a : analyze (same as -d except an analysis file is written)\n");
926 printf(" -c : write output to stdout\n");
927 printf(" -s : silent (do not write runtime encode/decode statistics)\n");
928 printf(" -o filename : force the output file name (usually flac just changes the\n");
929 printf(" extension)\n");
930 printf(" --output-prefix string : prefix each output file name with the given string.\n");
931 printf(" This can be useful for encoding/decoding files to a different directory.\n");
932 printf(" Make sure if your string is a path name that it ends with a '/' slash.\n");
933 printf(" --delete-input-file : deletes the input file after a successful encode/decode\n");
934 printf(" --skip samples : can be used both for encoding and decoding\n");
935 printf("analyze options:\n");
936 printf(" --a-rtext : include residual signal in text output\n");
937 printf(" --a-rgp : generate gnuplot files of residual distribution of each subframe\n");
938 printf("decoding options:\n");
939 printf(" -F : force decoder to continue decoding through stream errors\n");
940 printf("encoding options:\n");
942 printf(" --ogg : output Ogg-FLAC stream instead of native FLAC\n");
944 printf(" --lax : allow encoder to generate non-Subset files\n");
945 printf(" --sector-align : align encoding of multiple files on sector boundaries\n");
946 printf(" -S { # | X | #x } : include a point or points in a SEEKTABLE\n");
947 printf(" # : a specific sample number for a seek point\n");
948 printf(" X : a placeholder point (always goes at the end of the SEEKTABLE)\n");
949 printf(" #x : # evenly spaced seekpoints, the first being at sample 0\n");
950 printf(" You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
951 printf(" ified union of all such values.\n");
952 printf(" With no -S options, flac defaults to '-S 100x'. Use -S- for no SEEKTABLE.\n");
953 printf(" Note: -S #x will not work if the encoder can't determine the input size\n");
954 printf(" before starting.\n");
955 printf(" Note: if you use -S # and # is >= samples in the input, there will be\n");
956 printf(" either no seek point entered (if the input size is determinable\n");
957 printf(" before encoding starts) or a placeholder point (if input size is not\n");
958 printf(" determinable)\n");
959 printf(" -P # : write a PADDING block of length # (goes after SEEKTABLE)\n");
960 printf(" (# must be >= 0; default is -P-). Note that the overall size in bytes\n");
961 printf(" of the PADDING block will be # + 4 because of the metadata header.\n");
962 printf(" -b # : specify blocksize in samples; default is 1152 for -l 0, else 4608;\n");
963 printf(" must be 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768\n");
964 printf(" (unless --lax is used)\n");
965 printf(" -m : try mid-side coding for each frame (stereo input only)\n");
966 printf(" -M : adaptive mid-side coding for all frames (stereo input only)\n");
967 printf(" -0 .. -8 : fastest compression .. highest compression, default is -5\n");
968 printf(" these are synonyms for other options:\n");
969 printf(" -0 : synonymous with -l 0 -b 1152 -r 2,2\n");
970 printf(" -1 : synonymous with -l 0 -b 1152 -M -r 2,2\n");
971 printf(" -2 : synonymous with -l 0 -b 1152 -m -r 3\n");
972 printf(" -3 : synonymous with -l 6 -b 4608 -r 3,3\n");
973 printf(" -4 : synonymous with -l 8 -b 4608 -M -r 3,3\n");
974 printf(" -5 : synonymous with -l 8 -b 4608 -m -r 3,3\n");
975 printf(" -6 : synonymous with -l 8 -b 4608 -m -r 4\n");
976 printf(" -7 : synonymous with -l 8 -b 4608 -m -e -r 6\n");
977 printf(" -8 : synonymous with -l 12 -b 4608 -m -e -r 6\n");
978 printf(" --fast, --best : synonymous with -0 and -8 respectively\n");
979 printf(" -e : do exhaustive model search (expensive!)\n");
980 printf(" -E : include escape coding in the entropy coder\n");
981 printf(" -l # : specify max LPC order; 0 => use only fixed predictors\n");
982 printf(" -p : do exhaustive search of LP coefficient quantization (expensive!);\n");
983 printf(" overrides -q, does nothing if using -l 0\n");
984 printf(" -q # : specify precision in bits of quantized linear-predictor coefficients;\n");
985 printf(" 0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
986 printf(" -r [#,]# : [min,]max residual partition order (# is 0..16; min defaults to 0;\n");
987 printf(" default is -r 0; above 4 doesn't usually help much)\n");
989 @@@ removed because it doesnt work yet and is too dangerous for users
990 printf(" -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much)\n");
992 printf(" -V : verify a correct encoding by decoding the output in parallel and\n");
993 printf(" comparing to the original\n");
994 printf(" -S-, -P-, -m-, -M-, -e-, -E-, -p-, -V-, --delete-input-file-,%s --lax-,\n",
1001 printf(" --sector-align- can all be used to turn off a particular option\n");
1002 printf("format options:\n");
1003 printf(" -fb | -fl : big-endian | little-endian byte order\n");
1004 printf(" -fc channels\n");
1005 printf(" -fp bits_per_sample\n");
1006 printf(" -fs sample_rate : in Hz\n");
1007 printf(" -fu : unsigned samples (default is signed)\n");
1008 printf(" -fr : force input to be treated as raw samples\n");
1012 format_mistake(const char *infilename, const char *wrong, const char *right)
1014 fprintf(stderr, "WARNING: %s is not a %s file; treating as a %s file\n", infilename, wrong, right);
1017 int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file)
1019 FILE *encode_infile;
1020 char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
1022 FLAC__byte lookahead[12];
1023 unsigned lookahead_length = 0;
1024 FileFormat fmt= RAW;
1027 encode_options_t common_options;
1029 if(0 == strcmp(infilename, "-")) {
1031 encode_infile = file__get_binary_stdin();
1034 infilesize = flac__file_get_filesize(infilename);
1035 if(0 == (encode_infile = fopen(infilename, "rb"))) {
1036 fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
1041 if(!option_values.force_raw_format) {
1042 /* first set format based on name */
1043 if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav"))
1045 else if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".aif"))
1047 else if(strlen(infilename) > 4 && 0 == strcasecmp(infilename+(strlen(infilename)-5), ".aiff"))
1050 /* attempt to guess the file type based on the first 12 bytes */
1051 if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
1053 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1057 if(!strncmp(lookahead, "RIFF", 4) && !strncmp(lookahead+8, "WAVE", 4))
1059 else if(!strncmp(lookahead, "FORM", 4) && !strncmp(lookahead+8, "AIFF", 4))
1063 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1069 if(option_values.sector_align && fmt == RAW && infilesize < 0) {
1070 fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n");
1075 if(option_values.format_is_big_endian < 0 || option_values.format_channels < 0 || option_values.format_bps < 0 || option_values.format_sample_rate < 0)
1076 return usage_error("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
1079 if(encode_infile == stdin || option_values.force_to_stdout)
1080 strcpy(outfilename, "-");
1082 const char *suffix = (option_values.use_ogg? ogg_suffix : flac_suffix);
1083 strcpy(outfilename, option_values.output_prefix? option_values.output_prefix : "");
1084 strcat(outfilename, infilename);
1085 if(0 == (p = strrchr(outfilename, '.')))
1086 strcat(outfilename, suffix);
1088 if(0 == strcmp(p, suffix)) {
1096 if(0 == forced_outfilename)
1097 forced_outfilename = outfilename;
1098 if(0 != option_values.cmdline_forced_outfilename)
1099 forced_outfilename = option_values.cmdline_forced_outfilename;
1101 common_options.verbose = option_values.verbose;
1102 common_options.skip = option_values.skip;
1103 common_options.verify = option_values.verify;
1104 #ifdef FLAC__HAS_OGG
1105 common_options.use_ogg = option_values.use_ogg;
1107 common_options.lax = option_values.lax;
1108 common_options.do_mid_side = option_values.do_mid_side;
1109 common_options.loose_mid_side = option_values.loose_mid_side;
1110 common_options.do_exhaustive_model_search = option_values.do_exhaustive_model_search;
1111 common_options.do_escape_coding = option_values.do_escape_coding;
1112 common_options.do_qlp_coeff_prec_search = option_values.do_qlp_coeff_prec_search;
1113 common_options.min_residual_partition_order = option_values.min_residual_partition_order;
1114 common_options.max_residual_partition_order = option_values.max_residual_partition_order;
1115 common_options.rice_parameter_search_dist = option_values.rice_parameter_search_dist;
1116 common_options.max_lpc_order = option_values.max_lpc_order;
1117 common_options.blocksize = (unsigned)option_values.blocksize;
1118 common_options.qlp_coeff_precision = option_values.qlp_coeff_precision;
1119 common_options.padding = option_values.padding;
1120 common_options.requested_seek_points = option_values.requested_seek_points;
1121 common_options.num_requested_seek_points = option_values.num_requested_seek_points;
1122 common_options.is_last_file = is_last_file;
1123 common_options.align_reservoir = align_reservoir;
1124 common_options.align_reservoir_samples = &align_reservoir_samples;
1125 common_options.sector_align = option_values.sector_align;
1128 raw_encode_options_t options;
1130 options.common = common_options;
1131 options.is_big_endian = option_values.format_is_big_endian;
1132 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1133 options.channels = option_values.format_channels;
1134 options.bps = option_values.format_bps;
1135 options.sample_rate = option_values.format_sample_rate;
1137 retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1140 wav_encode_options_t options;
1142 options.common = common_options;
1145 retval = flac__encode_aif(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1147 retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1150 if(retval == 0 && strcmp(infilename, "-")) {
1151 if(strcmp(forced_outfilename, "-"))
1152 flac__file_copy_metadata(infilename, forced_outfilename);
1153 if(option_values.delete_input)
1160 int decode_file(const char *infilename, const char *forced_outfilename)
1162 static const char *suffixes[] = { ".wav", ".raw", ".ana" };
1163 char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
1166 FLAC__bool treat_as_ogg = false;
1167 decode_options_t common_options;
1169 if(!option_values.test_only && !option_values.analyze) {
1170 if(option_values.force_raw_format && option_values.format_is_big_endian < 0)
1171 return usage_error("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
1174 if(0 == strcmp(infilename, "-") || option_values.force_to_stdout)
1175 strcpy(outfilename, "-");
1177 const char *suffix = suffixes[option_values.analyze? 2 : option_values.force_raw_format? 1 : 0];
1178 strcpy(outfilename, option_values.output_prefix? option_values.output_prefix : "");
1179 strcat(outfilename, infilename);
1180 if(0 == (p = strrchr(outfilename, '.')))
1181 strcat(outfilename, suffix);
1183 if(0 == strcmp(p, suffix)) {
1191 if(0 == forced_outfilename)
1192 forced_outfilename = outfilename;
1193 if(0 != option_values.cmdline_forced_outfilename)
1194 forced_outfilename = option_values.cmdline_forced_outfilename;
1196 if(option_values.use_ogg)
1197 treat_as_ogg = true;
1198 else if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".ogg"))
1199 treat_as_ogg = true;
1201 treat_as_ogg = false;
1203 #ifndef FLAC__HAS_OGG
1205 fprintf(stderr, "%s: Ogg support has not been built into this copy of flac\n", infilename);
1210 common_options.verbose = option_values.verbose;
1211 common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
1212 #ifdef FLAC__HAS_OGG
1213 common_options.is_ogg = treat_as_ogg;
1215 common_options.skip = option_values.skip;
1217 if(!option_values.force_raw_format) {
1218 wav_decode_options_t options;
1220 options.common = common_options;
1222 retval = flac__decode_wav(infilename, option_values.test_only? 0 : forced_outfilename, option_values.analyze, option_values.aopts, options);
1225 raw_decode_options_t options;
1227 options.common = common_options;
1228 options.is_big_endian = option_values.format_is_big_endian;
1229 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1231 retval = flac__decode_raw(infilename, option_values.test_only? 0 : forced_outfilename, option_values.analyze, option_values.aopts, options);
1234 if(retval == 0 && strcmp(infilename, "-")) {
1235 if(strcmp(forced_outfilename, "-"))
1236 flac__file_copy_metadata(infilename, forced_outfilename);
1237 if(option_values.delete_input)
1244 void die(const char *message)
1246 FLAC__ASSERT(0 != message);
1247 fprintf(stderr, "ERROR: %s\n", message);
1251 char *local_strdup(const char *source)
1254 FLAC__ASSERT(0 != source);
1255 if(0 == (ret = strdup(source)))
1256 die("out of memory during strdup()");