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' },
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 },
108 { "sector-align", 0, 0, 0 },
109 { "seekpoint", 1, 0, 'S' },
110 { "padding", 1, 0, 'P' },
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 },
133 { "sample-rate", 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");
329 if(option_values.verbose) {
330 fprintf(stderr, "\n");
331 fprintf(stderr, "flac %s, Copyright (C) 2000,2001,2002 Josh Coalson\n", FLAC__VERSION_STRING);
332 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
333 fprintf(stderr, "welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
335 if(!option_values.mode_decode) {
337 if(option_values.padding < 0)
340 sprintf(padopt, " %d", option_values.padding);
346 "%s -P%s -b %u%s -l %u%s%s%s -q %u -r %u,%u -R %u%s\n",
347 option_values.delete_input?" --delete-input-file":"",
348 option_values.sector_align?" --sector-align":"",
350 option_values.use_ogg?" --ogg":"",
352 option_values.lax?" --lax":"",
354 (unsigned)option_values.blocksize,
355 option_values.loose_mid_side?" -M":option_values.do_mid_side?" -m":"",
356 option_values.max_lpc_order,
357 option_values.do_exhaustive_model_search?" -e":"",
358 option_values.do_escape_coding?" -E":"",
359 option_values.do_qlp_coeff_prec_search?" -p":"",
360 option_values.qlp_coeff_precision,
361 (unsigned)option_values.min_residual_partition_order,
362 (unsigned)option_values.max_residual_partition_order,
363 (unsigned)option_values.rice_parameter_search_dist,
364 option_values.verify? " -V":""
369 if(option_values.mode_decode) {
370 FLAC__bool first = true;
372 if(option_values.num_files == 0) {
373 retval = decode_file("-", 0);
377 if(option_values.num_files > 1)
378 option_values.cmdline_forced_outfilename = 0;
379 for(i = 0, retval = 0; i < option_values.num_files && retval == 0; i++) {
380 if(0 == strcmp(option_values.filenames[i], "-") && !first)
382 retval = decode_file(option_values.filenames[i], 0);
388 FLAC__bool first = true;
390 if(option_values.num_files == 0) {
391 retval = encode_file("-", 0, true);
395 if(option_values.num_files > 1)
396 option_values.cmdline_forced_outfilename = 0;
397 for(i = 0, retval = 0; i < option_values.num_files && retval == 0; i++) {
398 if(0 == strcmp(option_values.filenames[i], "-") && !first)
400 retval = encode_file(option_values.filenames[i], 0, i == (option_values.num_files-1));
411 option_values.show_long_help = false;
412 option_values.mode_decode = false;
413 option_values.verify = false;
414 option_values.verbose = true;
415 option_values.continue_through_decode_errors = false;
416 option_values.lax = false;
417 option_values.test_only = false;
418 option_values.analyze = false;
419 option_values.use_ogg = false;
420 option_values.do_mid_side = true;
421 option_values.loose_mid_side = false;
422 option_values.do_exhaustive_model_search = false;
423 option_values.do_escape_coding = false;
424 option_values.do_qlp_coeff_prec_search = false;
425 option_values.force_to_stdout = false;
426 option_values.force_raw_format = false;
427 option_values.delete_input = false;
428 option_values.sector_align = false;
429 option_values.cmdline_forced_outfilename = 0;
430 option_values.output_prefix = 0;
431 option_values.aopts.do_residual_text = false;
432 option_values.aopts.do_residual_gnuplot = false;
433 option_values.padding = -1;
434 option_values.max_lpc_order = 8;
435 option_values.qlp_coeff_precision = 0;
436 option_values.skip = 0;
437 option_values.format_is_big_endian = -1;
438 option_values.format_is_unsigned_samples = false;
439 option_values.format_channels = -1;
440 option_values.format_bps = -1;
441 option_values.format_sample_rate = -1;
442 option_values.blocksize = -1;
443 option_values.min_residual_partition_order = -1;
444 option_values.max_residual_partition_order = -1;
445 option_values.rice_parameter_search_dist = -1;
446 option_values.requested_seek_points[0] = '\0';
447 option_values.num_requested_seek_points = -1;
449 option_values.num_files = 0;
450 option_values.filenames = 0;
453 int parse_options(int argc, char *argv[])
456 int option_index = 1;
457 FLAC__bool had_error = false;
458 /*@@@ E and R: are deprecated */
459 const char *short_opts = "0123456789ab:cdeFHl:mMo:pP:q:r:sS:tV";
461 while ((short_option = FLAC__share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
462 switch (short_option) {
463 case 0: /* long option with no equivalent short option */
464 had_error |= (parse_option(short_option, long_options_[option_index].name, FLAC__share__optarg) != 0);
470 default: /* short option */
471 had_error |= (parse_option(short_option, 0, FLAC__share__optarg) != 0);
480 FLAC__ASSERT(FLAC__share__optind <= argc);
482 option_values.num_files = argc - FLAC__share__optind;
484 if(option_values.num_files > 0) {
486 if(0 == (option_values.filenames = malloc(sizeof(char *) * option_values.num_files)))
487 die("out of memory allocating space for file names list");
488 while(FLAC__share__optind < argc)
489 option_values.filenames[i++] = local_strdup(argv[FLAC__share__optind++]);
495 int parse_option(int short_option, const char *long_option, const char *option_argument)
499 if(short_option == 0) {
500 FLAC__ASSERT(0 != long_option);
501 if(0 == strcmp(long_option, "delete-input-file")) {
502 option_values.delete_input = true;
504 else if(0 == strcmp(long_option, "output-prefix")) {
505 FLAC__ASSERT(0 != option_argument);
506 option_values.output_prefix = option_argument;
508 else if(0 == strcmp(long_option, "skip")) {
509 FLAC__ASSERT(0 != option_argument);
510 option_values.skip = (FLAC__uint64)atoi(option_argument); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
512 else if(0 == strcmp(long_option, "super-secret-impractical-compression-level")) {
513 option_values.do_exhaustive_model_search = true;
514 option_values.do_escape_coding = true;
515 option_values.do_mid_side = true;
516 option_values.loose_mid_side = false;
517 option_values.do_qlp_coeff_prec_search = true;
518 option_values.min_residual_partition_order = 0;
519 option_values.max_residual_partition_order = 16;
520 option_values.rice_parameter_search_dist = 0;
521 option_values.max_lpc_order = 32;
523 else if(0 == strcmp(long_option, "force-raw-input")) {
524 option_values.force_raw_format = true;
526 else if(0 == strcmp(long_option, "lax")) {
527 option_values.lax = true;
529 else if(0 == strcmp(long_option, "sector-align")) {
530 option_values.sector_align = true;
533 else if(0 == strcmp(long_option, "ogg")) {
534 option_values.use_ogg = true;
537 else if(0 == strcmp(long_option, "endian")) {
538 FLAC__ASSERT(0 != option_argument);
539 if(0 == strncmp(option_argument, "big", strlen(option_argument)))
540 option_values.format_is_big_endian = true;
541 else if(0 == strncmp(option_argument, "little", strlen(option_argument)))
542 option_values.format_is_big_endian = false;
544 return usage_error("ERROR: argument to --endian must be \"big\" or \"little\"\n");
546 else if(0 == strcmp(long_option, "channels")) {
547 FLAC__ASSERT(0 != option_argument);
548 option_values.format_channels = atoi(option_argument);
550 else if(0 == strcmp(long_option, "bps")) {
551 FLAC__ASSERT(0 != option_argument);
552 option_values.format_bps = atoi(option_argument);
554 else if(0 == strcmp(long_option, "sample-rate")) {
555 FLAC__ASSERT(0 != option_argument);
556 option_values.format_sample_rate = atoi(option_argument);
558 else if(0 == strcmp(long_option, "sign")) {
559 FLAC__ASSERT(0 != option_argument);
560 if(0 == strncmp(option_argument, "signed", strlen(option_argument)))
561 option_values.format_is_unsigned_samples = false;
562 else if(0 == strncmp(option_argument, "unsigned", strlen(option_argument)))
563 option_values.format_is_unsigned_samples = true;
565 return usage_error("ERROR: argument to --sign must be \"signed\" or \"unsigned\"\n");
567 else if(0 == strcmp(long_option, "residual-gnu-plot")) {
568 option_values.aopts.do_residual_gnuplot = true;
570 else if(0 == strcmp(long_option, "residual-text")) {
571 option_values.aopts.do_residual_text = true;
576 else if(0 == strcmp(long_option, "no-decode-through-errors")) {
577 option_values.continue_through_decode_errors = false;
579 else if(0 == strcmp(long_option, "no-silent")) {
580 option_values.verbose = true;
582 else if(0 == strcmp(long_option, "no-seektable")) {
583 option_values.num_requested_seek_points = 0;
584 option_values.requested_seek_points[0] = '\0';
586 else if(0 == strcmp(long_option, "no-delete-input-file")) {
587 option_values.delete_input = false;
589 else if(0 == strcmp(long_option, "no-sector-align")) {
590 option_values.sector_align = false;
592 else if(0 == strcmp(long_option, "no-lax")) {
593 option_values.lax = false;
596 else if(0 == strcmp(long_option, "no-ogg")) {
597 option_values.use_ogg = false;
600 else if(0 == strcmp(long_option, "no-exhaustive-model-search")) {
601 option_values.do_exhaustive_model_search = false;
604 /* @@@ deprecated: */
605 else if(0 == strcmp(long_option, "no-escape-coding")) {
606 option_values.do_escape_coding = false;
609 else if(0 == strcmp(long_option, "no-mid-side")) {
610 option_values.do_mid_side = option_values.loose_mid_side = false;
612 else if(0 == strcmp(long_option, "no-adaptive-mid-side")) {
613 option_values.loose_mid_side = option_values.do_mid_side = false;
615 else if(0 == strcmp(long_option, "no-qlp-coeff-prec-search")) {
616 option_values.do_qlp_coeff_prec_search = false;
618 else if(0 == strcmp(long_option, "no-padding")) {
619 option_values.padding = -1;
621 else if(0 == strcmp(long_option, "no-verify")) {
622 option_values.verify = false;
624 else if(0 == strcmp(long_option, "no-residual-gnuplot")) {
625 option_values.aopts.do_residual_gnuplot = false;
627 else if(0 == strcmp(long_option, "no-residual-text")) {
628 option_values.aopts.do_residual_text = false;
632 switch(short_option) {
634 option_values.show_long_help = true;
637 option_values.mode_decode = true;
640 option_values.mode_decode = true;
641 option_values.analyze = true;
644 option_values.mode_decode = true;
645 option_values.test_only = true;
648 option_values.force_to_stdout = true;
651 option_values.verbose = false;
654 FLAC__ASSERT(0 != option_argument);
655 option_values.cmdline_forced_outfilename = option_argument;
658 option_values.continue_through_decode_errors = true;
661 option_values.do_exhaustive_model_search = false;
662 option_values.do_escape_coding = false;
663 option_values.do_mid_side = false;
664 option_values.loose_mid_side = false;
665 option_values.qlp_coeff_precision = 0;
666 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
667 option_values.rice_parameter_search_dist = 0;
668 option_values.max_lpc_order = 0;
671 option_values.do_exhaustive_model_search = false;
672 option_values.do_escape_coding = false;
673 option_values.do_mid_side = true;
674 option_values.loose_mid_side = true;
675 option_values.qlp_coeff_precision = 0;
676 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
677 option_values.rice_parameter_search_dist = 0;
678 option_values.max_lpc_order = 0;
681 option_values.do_exhaustive_model_search = false;
682 option_values.do_escape_coding = false;
683 option_values.do_mid_side = true;
684 option_values.loose_mid_side = false;
685 option_values.qlp_coeff_precision = 0;
686 option_values.min_residual_partition_order = 0;
687 option_values.max_residual_partition_order = 3;
688 option_values.rice_parameter_search_dist = 0;
689 option_values.max_lpc_order = 0;
692 option_values.do_exhaustive_model_search = false;
693 option_values.do_escape_coding = false;
694 option_values.do_mid_side = false;
695 option_values.loose_mid_side = false;
696 option_values.qlp_coeff_precision = 0;
697 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
698 option_values.rice_parameter_search_dist = 0;
699 option_values.max_lpc_order = 6;
702 option_values.do_exhaustive_model_search = false;
703 option_values.do_escape_coding = false;
704 option_values.do_mid_side = true;
705 option_values.loose_mid_side = true;
706 option_values.qlp_coeff_precision = 0;
707 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
708 option_values.rice_parameter_search_dist = 0;
709 option_values.max_lpc_order = 8;
712 option_values.do_exhaustive_model_search = false;
713 option_values.do_escape_coding = false;
714 option_values.do_mid_side = true;
715 option_values.loose_mid_side = false;
716 option_values.qlp_coeff_precision = 0;
717 option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
718 option_values.rice_parameter_search_dist = 0;
719 option_values.max_lpc_order = 8;
722 option_values.do_exhaustive_model_search = false;
723 option_values.do_escape_coding = false;
724 option_values.do_mid_side = true;
725 option_values.loose_mid_side = false;
726 option_values.qlp_coeff_precision = 0;
727 option_values.min_residual_partition_order = 0;
728 option_values.max_residual_partition_order = 4;
729 option_values.rice_parameter_search_dist = 0;
730 option_values.max_lpc_order = 8;
733 option_values.do_exhaustive_model_search = true;
734 option_values.do_escape_coding = false;
735 option_values.do_mid_side = true;
736 option_values.loose_mid_side = false;
737 option_values.qlp_coeff_precision = 0;
738 option_values.min_residual_partition_order = 0;
739 option_values.max_residual_partition_order = 6;
740 option_values.rice_parameter_search_dist = 0;
741 option_values.max_lpc_order = 8;
744 option_values.do_exhaustive_model_search = true;
745 option_values.do_escape_coding = false;
746 option_values.do_mid_side = true;
747 option_values.loose_mid_side = false;
748 option_values.qlp_coeff_precision = 0;
749 option_values.min_residual_partition_order = 0;
750 option_values.max_residual_partition_order = 6;
751 option_values.rice_parameter_search_dist = 0;
752 option_values.max_lpc_order = 12;
755 return usage_error("ERROR: compression level '9' is reserved\n");
757 option_values.verify = true;
760 FLAC__ASSERT(0 != option_argument);
761 if(option_values.num_requested_seek_points < 0)
762 option_values.num_requested_seek_points = 0;
763 option_values.num_requested_seek_points++;
764 strcat(option_values.requested_seek_points, option_argument);
765 strcat(option_values.requested_seek_points, "<");
768 FLAC__ASSERT(0 != option_argument);
769 option_values.padding = atoi(option_argument);
770 if(option_values.padding < 0)
771 return usage_error("ERROR: argument to -P must be >= 0\n");
774 FLAC__ASSERT(0 != option_argument);
775 option_values.blocksize = atoi(option_argument);
778 option_values.do_exhaustive_model_search = true;
781 option_values.do_escape_coding = true;
784 FLAC__ASSERT(0 != option_argument);
785 option_values.max_lpc_order = atoi(option_argument);
788 option_values.do_mid_side = true;
789 option_values.loose_mid_side = false;
792 option_values.loose_mid_side = option_values.do_mid_side = true;
795 option_values.do_qlp_coeff_prec_search = true;
798 FLAC__ASSERT(0 != option_argument);
799 option_values.qlp_coeff_precision = atoi(option_argument);
802 FLAC__ASSERT(0 != option_argument);
803 p = strchr(option_argument, ',');
805 option_values.min_residual_partition_order = 0;
806 option_values.max_residual_partition_order = atoi(option_argument);
809 option_values.min_residual_partition_order = atoi(option_argument);
810 option_values.max_residual_partition_order = atoi(++p);
814 FLAC__ASSERT(0 != option_argument);
815 option_values.rice_parameter_search_dist = atoi(option_argument);
827 if(0 != option_values.filenames)
828 free(option_values.filenames);
831 int usage_error(const char *message, ...)
837 va_start(args, message);
839 (void) vfprintf(stderr, message, args);
843 printf("Type \"flac\" for a usage summary or \"flac --help\" for all options\n");
848 static void usage_header()
850 printf("===============================================================================\n");
851 printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
852 printf("Copyright (C) 2000,2001,2002 Josh Coalson\n");
854 printf("This program is free software; you can redistribute it and/or\n");
855 printf("modify it under the terms of the GNU General Public License\n");
856 printf("as published by the Free Software Foundation; either version 2\n");
857 printf("of the License, or (at your option) any later version.\n");
859 printf("This program is distributed in the hope that it will be useful,\n");
860 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
861 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
862 printf("GNU General Public License for more details.\n");
864 printf("You should have received a copy of the GNU General Public License\n");
865 printf("along with this program; if not, write to the Free Software\n");
866 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
867 printf("===============================================================================\n");
874 printf("This is the short help; for full help use 'flac --help'\n");
876 printf("To encode:\n");
877 printf(" flac [-#] [infile [...]]\n");
879 printf(" -# is -0 (fastest compression) to -8 (highest compression); -5 is the default\n");
881 printf("To decode:\n");
882 printf(" flac -d [infile [...]]\n");
884 printf("To test:\n");
885 printf(" flac -t [infile [...]]\n");
892 printf(" flac [options] [infile [...]]\n");
894 printf("For encoding:\n");
895 printf(" the input file(s) may be a PCM RIFF WAVE file, AIFF file, or raw samples\n");
896 printf(" the output file(s) will be in FLAC format\n");
897 printf("For decoding, the reverse is true\n");
899 printf("A single 'infile' may be - for stdin. No 'infile' implies stdin. Use of\n");
900 printf("stdin implies -c (write to stdout). Normally you should use:\n");
901 printf(" flac [options] -o outfilename or flac -d [options] -o outfilename\n");
902 printf("instead of:\n");
903 printf(" flac [options] > outfilename or flac -d [options] > outfilename\n");
904 printf("since the former allows flac to seek backwards to write the STREAMINFO or\n");
905 printf("RIFF WAVE header contents when necessary.\n");
907 printf("flac checks for the presence of a AIFF/RIFF WAVE header to decide whether or\n");
908 printf("not to treat an input file as AIFF/WAVE format or raw samples. If any infile\n");
909 printf("is raw you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n");
910 printf("apply to all raw files. You can force AIFF/WAVE files to be treated as a raw\n");
911 printf("files using -fr.\n");
913 printf("generic options:\n");
914 printf(" -d : decode (default behavior is encode)\n");
915 printf(" -t : test (same as -d except no decoded file is written)\n");
916 printf(" -a : analyze (same as -d except an analysis file is written)\n");
917 printf(" -c : write output to stdout\n");
918 printf(" -s : silent (do not write runtime encode/decode statistics)\n");
919 printf(" -o filename : force the output file name (usually flac just changes the\n");
920 printf(" extension)\n");
921 printf(" --output-prefix string : prefix each output file name with the given string.\n");
922 printf(" This can be useful for encoding/decoding files to a different directory.\n");
923 printf(" Make sure if your string is a path name that it ends with a '/' slash.\n");
924 printf(" --delete-input-file : deletes the input file after a successful encode/decode\n");
925 printf(" --skip samples : can be used both for encoding and decoding\n");
926 printf("analyze options:\n");
927 printf(" --a-rtext : include residual signal in text output\n");
928 printf(" --a-rgp : generate gnuplot files of residual distribution of each subframe\n");
929 printf("decoding options:\n");
930 printf(" -F : force decoder to continue decoding through stream errors\n");
931 printf("encoding options:\n");
933 printf(" --ogg : output Ogg-FLAC stream instead of native FLAC\n");
935 printf(" --lax : allow encoder to generate non-Subset files\n");
936 printf(" --sector-align : align encoding of multiple files on sector boundaries\n");
937 printf(" -S { # | X | #x } : include a point or points in a SEEKTABLE\n");
938 printf(" # : a specific sample number for a seek point\n");
939 printf(" X : a placeholder point (always goes at the end of the SEEKTABLE)\n");
940 printf(" #x : # evenly spaced seekpoints, the first being at sample 0\n");
941 printf(" You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
942 printf(" ified union of all such values.\n");
943 printf(" With no -S options, flac defaults to '-S 100x'. Use -S- for no SEEKTABLE.\n");
944 printf(" Note: -S #x will not work if the encoder can't determine the input size\n");
945 printf(" before starting.\n");
946 printf(" Note: if you use -S # and # is >= samples in the input, there will be\n");
947 printf(" either no seek point entered (if the input size is determinable\n");
948 printf(" before encoding starts) or a placeholder point (if input size is not\n");
949 printf(" determinable)\n");
950 printf(" -P # : write a PADDING block of length # (goes after SEEKTABLE)\n");
951 printf(" (# must be >= 0; default is -P-). Note that the overall size in bytes\n");
952 printf(" of the PADDING block will be # + 4 because of the metadata header.\n");
953 printf(" -b # : specify blocksize in samples; default is 1152 for -l 0, else 4608;\n");
954 printf(" must be 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768\n");
955 printf(" (unless --lax is used)\n");
956 printf(" -m : try mid-side coding for each frame (stereo input only)\n");
957 printf(" -M : adaptive mid-side coding for all frames (stereo input only)\n");
958 printf(" -0 .. -8 : fastest compression .. highest compression, default is -5\n");
959 printf(" these are synonyms for other options:\n");
960 printf(" -0 : synonymous with -l 0 -b 1152 -r 2,2\n");
961 printf(" -1 : synonymous with -l 0 -b 1152 -M -r 2,2\n");
962 printf(" -2 : synonymous with -l 0 -b 1152 -m -r 3\n");
963 printf(" -3 : synonymous with -l 6 -b 4608 -r 3,3\n");
964 printf(" -4 : synonymous with -l 8 -b 4608 -M -r 3,3\n");
965 printf(" -5 : synonymous with -l 8 -b 4608 -m -r 3,3\n");
966 printf(" -6 : synonymous with -l 8 -b 4608 -m -r 4\n");
967 printf(" -7 : synonymous with -l 8 -b 4608 -m -e -r 6\n");
968 printf(" -8 : synonymous with -l 12 -b 4608 -m -e -r 6\n");
969 printf(" --fast, --best : synonymous with -0 and -8 respectively\n");
970 printf(" -e : do exhaustive model search (expensive!)\n");
971 printf(" -E : include escape coding in the entropy coder\n");
972 printf(" -l # : specify max LPC order; 0 => use only fixed predictors\n");
973 printf(" -p : do exhaustive search of LP coefficient quantization (expensive!);\n");
974 printf(" overrides -q, does nothing if using -l 0\n");
975 printf(" -q # : specify precision in bits of quantized linear-predictor coefficients;\n");
976 printf(" 0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
977 printf(" -r [#,]# : [min,]max residual partition order (# is 0..16; min defaults to 0;\n");
978 printf(" default is -r 0; above 4 doesn't usually help much)\n");
980 @@@ removed because it doesnt work yet and is too dangerous for users
981 printf(" -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much)\n");
983 printf(" -V : verify a correct encoding by decoding the output in parallel and\n");
984 printf(" comparing to the original\n");
985 printf(" -S-, -P-, -m-, -M-, -e-, -E-, -p-, -V-, --delete-input-file-,%s --lax-,\n",
992 printf(" --sector-align- can all be used to turn off a particular option\n");
993 printf("format options:\n");
994 printf(" -fb | -fl : big-endian | little-endian byte order\n");
995 printf(" -fc channels\n");
996 printf(" -fp bits_per_sample\n");
997 printf(" -fs sample_rate : in Hz\n");
998 printf(" -fu : unsigned samples (default is signed)\n");
999 printf(" -fr : force input to be treated as raw samples\n");
1003 format_mistake(const char *infilename, const char *wrong, const char *right)
1005 fprintf(stderr, "WARNING: %s is not a %s file; treating as a %s file\n", infilename, wrong, right);
1008 int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file)
1010 FILE *encode_infile;
1011 char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
1013 FLAC__byte lookahead[12];
1014 unsigned lookahead_length = 0;
1015 FileFormat fmt= RAW;
1018 encode_options_t common_options;
1020 if(0 == strcmp(infilename, "-")) {
1022 encode_infile = file__get_binary_stdin();
1025 infilesize = flac__file_get_filesize(infilename);
1026 if(0 == (encode_infile = fopen(infilename, "rb"))) {
1027 fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
1032 if(!option_values.force_raw_format) {
1033 /* first set format based on name */
1034 if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav"))
1036 else if(strlen(infilename) > 3 && 0 == strcasecmp(infilename+(strlen(infilename)-4), ".aif"))
1038 else if(strlen(infilename) > 4 && 0 == strcasecmp(infilename+(strlen(infilename)-5), ".aiff"))
1041 /* attempt to guess the file type based on the first 12 bytes */
1042 if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
1044 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1048 if(!strncmp(lookahead, "RIFF", 4) && !strncmp(lookahead+8, "WAVE", 4))
1050 else if(!strncmp(lookahead, "FORM", 4) && !strncmp(lookahead+8, "AIFF", 4))
1054 format_mistake(infilename, fmt == AIF ? "AIFF" : "WAVE", "raw");
1060 if(option_values.sector_align && fmt == RAW && infilesize < 0) {
1061 fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n");
1066 if(option_values.format_is_big_endian < 0 || option_values.format_channels < 0 || option_values.format_bps < 0 || option_values.format_sample_rate < 0)
1067 return usage_error("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
1070 if(encode_infile == stdin || option_values.force_to_stdout)
1071 strcpy(outfilename, "-");
1073 const char *suffix = (option_values.use_ogg? ogg_suffix : flac_suffix);
1074 strcpy(outfilename, option_values.output_prefix? option_values.output_prefix : "");
1075 strcat(outfilename, infilename);
1076 if(0 == (p = strrchr(outfilename, '.')))
1077 strcat(outfilename, suffix);
1079 if(0 == strcmp(p, suffix)) {
1087 if(0 == forced_outfilename)
1088 forced_outfilename = outfilename;
1089 if(0 != option_values.cmdline_forced_outfilename)
1090 forced_outfilename = option_values.cmdline_forced_outfilename;
1092 common_options.verbose = option_values.verbose;
1093 common_options.skip = option_values.skip;
1094 common_options.verify = option_values.verify;
1095 #ifdef FLAC__HAS_OGG
1096 common_options.use_ogg = option_values.use_ogg;
1098 common_options.lax = option_values.lax;
1099 common_options.do_mid_side = option_values.do_mid_side;
1100 common_options.loose_mid_side = option_values.loose_mid_side;
1101 common_options.do_exhaustive_model_search = option_values.do_exhaustive_model_search;
1102 common_options.do_escape_coding = option_values.do_escape_coding;
1103 common_options.do_qlp_coeff_prec_search = option_values.do_qlp_coeff_prec_search;
1104 common_options.min_residual_partition_order = option_values.min_residual_partition_order;
1105 common_options.max_residual_partition_order = option_values.max_residual_partition_order;
1106 common_options.rice_parameter_search_dist = option_values.rice_parameter_search_dist;
1107 common_options.max_lpc_order = option_values.max_lpc_order;
1108 common_options.blocksize = (unsigned)option_values.blocksize;
1109 common_options.qlp_coeff_precision = option_values.qlp_coeff_precision;
1110 common_options.padding = option_values.padding;
1111 common_options.requested_seek_points = option_values.requested_seek_points;
1112 common_options.num_requested_seek_points = option_values.num_requested_seek_points;
1113 common_options.is_last_file = is_last_file;
1114 common_options.align_reservoir = align_reservoir;
1115 common_options.align_reservoir_samples = &align_reservoir_samples;
1116 common_options.sector_align = option_values.sector_align;
1119 raw_encode_options_t options;
1121 options.common = common_options;
1122 options.is_big_endian = option_values.format_is_big_endian;
1123 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1124 options.channels = option_values.format_channels;
1125 options.bps = option_values.format_bps;
1126 options.sample_rate = option_values.format_sample_rate;
1128 retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1131 wav_encode_options_t options;
1133 options.common = common_options;
1136 retval = flac__encode_aif(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1138 retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
1141 if(retval == 0 && strcmp(infilename, "-")) {
1142 if(strcmp(forced_outfilename, "-"))
1143 flac__file_copy_metadata(infilename, forced_outfilename);
1144 if(option_values.delete_input)
1151 int decode_file(const char *infilename, const char *forced_outfilename)
1153 static const char *suffixes[] = { ".wav", ".raw", ".ana" };
1154 char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
1157 FLAC__bool treat_as_ogg = false;
1158 decode_options_t common_options;
1160 if(!option_values.test_only && !option_values.analyze) {
1161 if(option_values.force_raw_format && option_values.format_is_big_endian < 0)
1162 return usage_error("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
1165 if(0 == strcmp(infilename, "-") || option_values.force_to_stdout)
1166 strcpy(outfilename, "-");
1168 const char *suffix = suffixes[option_values.analyze? 2 : option_values.force_raw_format? 1 : 0];
1169 strcpy(outfilename, option_values.output_prefix? option_values.output_prefix : "");
1170 strcat(outfilename, infilename);
1171 if(0 == (p = strrchr(outfilename, '.')))
1172 strcat(outfilename, suffix);
1174 if(0 == strcmp(p, suffix)) {
1182 if(0 == forced_outfilename)
1183 forced_outfilename = outfilename;
1184 if(0 != option_values.cmdline_forced_outfilename)
1185 forced_outfilename = option_values.cmdline_forced_outfilename;
1187 if(option_values.use_ogg)
1188 treat_as_ogg = true;
1189 else if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".ogg"))
1190 treat_as_ogg = true;
1192 treat_as_ogg = false;
1194 #ifndef FLAC__HAS_OGG
1196 fprintf(stderr, "%s: Ogg support has not been built into this copy of flac\n", infilename);
1201 common_options.verbose = option_values.verbose;
1202 common_options.continue_through_decode_errors = option_values.continue_through_decode_errors;
1203 #ifdef FLAC__HAS_OGG
1204 common_options.is_ogg = treat_as_ogg;
1206 common_options.skip = option_values.skip;
1208 if(!option_values.force_raw_format) {
1209 wav_decode_options_t options;
1211 options.common = common_options;
1213 retval = flac__decode_wav(infilename, option_values.test_only? 0 : forced_outfilename, option_values.analyze, option_values.aopts, options);
1216 raw_decode_options_t options;
1218 options.common = common_options;
1219 options.is_big_endian = option_values.format_is_big_endian;
1220 options.is_unsigned_samples = option_values.format_is_unsigned_samples;
1222 retval = flac__decode_raw(infilename, option_values.test_only? 0 : forced_outfilename, option_values.analyze, option_values.aopts, options);
1225 if(retval == 0 && strcmp(infilename, "-")) {
1226 if(strcmp(forced_outfilename, "-"))
1227 flac__file_copy_metadata(infilename, forced_outfilename);
1228 if(option_values.delete_input)
1235 void die(const char *message)
1237 FLAC__ASSERT(0 != message);
1238 fprintf(stderr, "ERROR: %s\n", message);
1242 char *local_strdup(const char *source)
1245 FLAC__ASSERT(0 != source);
1246 if(0 == (ret = strdup(source)))
1247 die("out of memory during strdup()");