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
36 static int short_usage(const char *message, ...);
37 static int long_usage(const char *message, ...);
38 static int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file);
39 static int decode_file(const char *infilename, const char *forced_outfilename);
41 FLAC__bool verify = false, verbose = true, lax = false, test_only = false, analyze = false, use_ogg = false;
42 FLAC__bool do_mid_side = true, loose_mid_side = false, do_exhaustive_model_search = false, do_escape_coding = false, do_qlp_coeff_prec_search = false;
43 FLAC__bool force_to_stdout = false, force_raw_format = false, delete_input = false, sector_align = false;
44 const char *cmdline_forced_outfilename = 0, *output_prefix = 0;
45 analysis_options aopts = { false, false };
47 unsigned max_lpc_order = 8;
48 unsigned qlp_coeff_precision = 0;
49 FLAC__uint64 skip = 0;
50 int format_is_big_endian = -1, format_is_unsigned_samples = false;
51 int format_channels = -1, format_bps = -1, format_sample_rate = -1;
52 int blocksize = -1, min_residual_partition_order = -1, max_residual_partition_order = -1, rice_parameter_search_dist = -1;
53 char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
54 int num_requested_seek_points = -1; /* -1 => no -S options were given, 0 => -S- was given */
55 FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
56 FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
57 unsigned align_reservoir_samples = 0; /* 0 .. 587 */
59 static const char *flac_suffix = ".flac", *ogg_suffix = ".ogg";
61 int main(int argc, char *argv[])
64 FLAC__bool mode_decode = false;
67 return short_usage(0);
70 for(i = 1; i < argc; i++) {
71 if(argv[i][0] != '-' || argv[i][1] == 0)
73 if(0 == strcmp(argv[i], "-H") || 0 == strcmp(argv[i], "--help"))
75 else if(0 == strcmp(argv[i], "-d"))
77 else if(0 == strcmp(argv[i], "-a")) {
81 else if(0 == strcmp(argv[i], "-t")) {
85 else if(0 == strcmp(argv[i], "-c"))
86 force_to_stdout = true;
87 else if(0 == strcmp(argv[i], "-s"))
89 else if(0 == strcmp(argv[i], "-s-"))
91 else if(0 == strcmp(argv[i], "-S")) {
92 if(num_requested_seek_points < 0)
93 num_requested_seek_points = 0;
94 num_requested_seek_points++;
95 strcat(requested_seek_points, argv[++i]);
96 strcat(requested_seek_points, "<");
98 else if(0 == strcmp(argv[i], "-S-")) {
99 num_requested_seek_points = 0;
100 requested_seek_points[0] = '\0';
102 else if(0 == strcmp(argv[i], "--delete-input-file"))
104 else if(0 == strcmp(argv[i], "--delete-input-file-"))
105 delete_input = false;
106 else if(0 == strcmp(argv[i], "--output-prefix"))
107 output_prefix = argv[++i];
108 else if(0 == strcmp(argv[i], "--sector-align"))
110 else if(0 == strcmp(argv[i], "--sector-align-"))
111 sector_align = false;
112 else if(0 == strcmp(argv[i], "--skip"))
113 skip = (FLAC__uint64)atoi(argv[++i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
114 else if(0 == strcmp(argv[i], "--lax"))
116 else if(0 == strcmp(argv[i], "--lax-"))
119 else if(0 == strcmp(argv[i], "--ogg"))
121 else if(0 == strcmp(argv[i], "--ogg-"))
124 else if(0 == strcmp(argv[i], "-b"))
125 blocksize = atoi(argv[++i]);
126 else if(0 == strcmp(argv[i], "-e"))
127 do_exhaustive_model_search = true;
128 else if(0 == strcmp(argv[i], "-e-"))
129 do_exhaustive_model_search = false;
130 else if(0 == strcmp(argv[i], "-E"))
131 do_escape_coding = true;
132 else if(0 == strcmp(argv[i], "-E-"))
133 do_escape_coding = false;
134 else if(0 == strcmp(argv[i], "-l"))
135 max_lpc_order = atoi(argv[++i]);
136 else if(0 == strcmp(argv[i], "-m")) {
138 loose_mid_side = false;
140 else if(0 == strcmp(argv[i], "-m-"))
141 do_mid_side = loose_mid_side = false;
142 else if(0 == strcmp(argv[i], "-M"))
143 loose_mid_side = do_mid_side = true;
144 else if(0 == strcmp(argv[i], "-M-"))
145 loose_mid_side = do_mid_side = false;
146 else if(0 == strcmp(argv[i], "-o"))
147 cmdline_forced_outfilename = argv[++i];
148 else if(0 == strcmp(argv[i], "-p"))
149 do_qlp_coeff_prec_search = true;
150 else if(0 == strcmp(argv[i], "-p-"))
151 do_qlp_coeff_prec_search = false;
152 else if(0 == strcmp(argv[i], "-P"))
153 padding = atoi(argv[++i]);
154 else if(0 == strcmp(argv[i], "-q"))
155 qlp_coeff_precision = atoi(argv[++i]);
156 else if(0 == strcmp(argv[i], "-r")) {
157 char *p = strchr(argv[++i], ',');
159 min_residual_partition_order = 0;
160 max_residual_partition_order = atoi(argv[i]);
163 min_residual_partition_order = atoi(argv[i]);
164 max_residual_partition_order = atoi(++p);
167 else if(0 == strcmp(argv[i], "-R"))
168 rice_parameter_search_dist = atoi(argv[++i]);
169 else if(0 == strcmp(argv[i], "-V"))
171 else if(0 == strcmp(argv[i], "-V-"))
173 else if(0 == strcmp(argv[i], "-fb"))
174 format_is_big_endian = true;
175 else if(0 == strcmp(argv[i], "-fl"))
176 format_is_big_endian = false;
177 else if(0 == strcmp(argv[i], "-fc"))
178 format_channels = atoi(argv[++i]);
179 else if(0 == strcmp(argv[i], "-fp"))
180 format_bps = atoi(argv[++i]);
181 else if(0 == strcmp(argv[i], "-fs"))
182 format_sample_rate = atoi(argv[++i]);
183 else if(0 == strcmp(argv[i], "-fu"))
184 format_is_unsigned_samples = true;
185 else if(0 == strcmp(argv[i], "-fr"))
186 force_raw_format = true;
187 else if(0 == strcmp(argv[i], "--a-rgp"))
188 aopts.do_residual_gnuplot = true;
189 else if(0 == strcmp(argv[i], "--a-rgp-"))
190 aopts.do_residual_gnuplot = false;
191 else if(0 == strcmp(argv[i], "--a-rtext"))
192 aopts.do_residual_text = true;
193 else if(0 == strcmp(argv[i], "--a-rtext-"))
194 aopts.do_residual_text = false;
195 else if(0 == strcmp(argv[i], "-0")) {
196 do_exhaustive_model_search = false;
197 do_escape_coding = false;
199 loose_mid_side = false;
200 qlp_coeff_precision = 0;
201 min_residual_partition_order = max_residual_partition_order = 2;
202 rice_parameter_search_dist = 0;
205 else if(0 == strcmp(argv[i], "-1")) {
206 do_exhaustive_model_search = false;
207 do_escape_coding = false;
209 loose_mid_side = true;
210 qlp_coeff_precision = 0;
211 min_residual_partition_order = max_residual_partition_order = 2;
212 rice_parameter_search_dist = 0;
215 else if(0 == strcmp(argv[i], "-2")) {
216 do_exhaustive_model_search = false;
217 do_escape_coding = false;
219 loose_mid_side = false;
220 qlp_coeff_precision = 0;
221 min_residual_partition_order = 0;
222 max_residual_partition_order = 3;
223 rice_parameter_search_dist = 0;
226 else if(0 == strcmp(argv[i], "-3")) {
227 do_exhaustive_model_search = false;
228 do_escape_coding = false;
230 loose_mid_side = false;
231 qlp_coeff_precision = 0;
232 min_residual_partition_order = max_residual_partition_order = 3;
233 rice_parameter_search_dist = 0;
236 else if(0 == strcmp(argv[i], "-4")) {
237 do_exhaustive_model_search = false;
238 do_escape_coding = false;
240 loose_mid_side = true;
241 qlp_coeff_precision = 0;
242 min_residual_partition_order = max_residual_partition_order = 3;
243 rice_parameter_search_dist = 0;
246 else if(0 == strcmp(argv[i], "-5")) {
247 do_exhaustive_model_search = false;
248 do_escape_coding = false;
250 loose_mid_side = false;
251 qlp_coeff_precision = 0;
252 min_residual_partition_order = max_residual_partition_order = 3;
253 rice_parameter_search_dist = 0;
256 else if(0 == strcmp(argv[i], "-6")) {
257 do_exhaustive_model_search = false;
258 do_escape_coding = false;
260 loose_mid_side = false;
261 qlp_coeff_precision = 0;
262 min_residual_partition_order = 0;
263 max_residual_partition_order = 4;
264 rice_parameter_search_dist = 0;
267 else if(0 == strcmp(argv[i], "-7")) {
268 do_exhaustive_model_search = true;
269 do_escape_coding = false;
271 loose_mid_side = false;
272 qlp_coeff_precision = 0;
273 min_residual_partition_order = 0;
274 max_residual_partition_order = 6;
275 rice_parameter_search_dist = 0;
278 else if(0 == strcmp(argv[i], "-8")) {
279 do_exhaustive_model_search = true;
280 do_escape_coding = false;
282 loose_mid_side = false;
283 qlp_coeff_precision = 0;
284 min_residual_partition_order = 0;
285 max_residual_partition_order = 6;
286 rice_parameter_search_dist = 0;
289 else if(0 == strcmp(argv[i], "--super-secret-impractical-compression-level")) {
290 do_exhaustive_model_search = true;
291 do_escape_coding = true;
293 loose_mid_side = false;
294 do_qlp_coeff_prec_search = true;
295 min_residual_partition_order = 0;
296 max_residual_partition_order = 16;
297 rice_parameter_search_dist = 0;
300 else if(isdigit((int)(argv[i][1]))) {
301 return long_usage("ERROR: compression level '%s' is reserved\n", argv[i]);
304 return long_usage("ERROR: invalid option '%s'\n", argv[i]);
308 /* tweak options; validate the values */
311 if(max_lpc_order == 0)
316 if(max_residual_partition_order < 0) {
317 if(blocksize <= 1152)
318 max_residual_partition_order = 2;
319 else if(blocksize <= 2304)
320 max_residual_partition_order = 3;
321 else if(blocksize <= 4608)
322 max_residual_partition_order = 3;
324 max_residual_partition_order = 4;
325 min_residual_partition_order = max_residual_partition_order;
327 if(rice_parameter_search_dist < 0) {
328 rice_parameter_search_dist = 0;
334 return long_usage("ERROR: --skip is not allowed in test mode\n");
338 FLAC__ASSERT(blocksize >= 0 || mode_decode);
340 if(format_channels >= 0) {
341 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
342 return long_usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
344 if(format_bps >= 0) {
345 if(format_bps != 8 && format_bps != 16 && format_bps != 24)
346 return long_usage("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", format_bps);
348 if(format_sample_rate >= 0) {
349 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
350 return long_usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
352 if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
353 return long_usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
355 if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
356 return long_usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
361 return long_usage("ERROR: --sector-align only allowed for encoding\n");
363 return long_usage("ERROR: --sector-align not allowed with --skip\n");
364 else if(format_channels >= 0 && format_channels != 2)
365 return long_usage("ERROR: --sector-align can only be done with stereo input\n");
366 else if(format_sample_rate >= 0 && format_sample_rate != 2)
367 return long_usage("ERROR: --sector-align can only be done with sample rate of 44100\n");
369 if(argc - i > 1 && cmdline_forced_outfilename) {
370 return long_usage("ERROR: -o cannot be used with multiple files\n");
372 if(cmdline_forced_outfilename && output_prefix) {
373 return long_usage("ERROR: --output-prefix conflicts with -o\n");
377 fprintf(stderr, "\n");
378 fprintf(stderr, "flac %s, Copyright (C) 2000,2001,2002 Josh Coalson\n", FLAC__VERSION_STRING);
379 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
380 fprintf(stderr, "welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
388 "%s -P %u -b %u%s -l %u%s%s%s -q %u -r %u,%u -R %u%s\n",
389 delete_input?" --delete-input-file":"", sector_align?" --sector-align":"",
394 padding, (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
395 do_exhaustive_model_search?" -e":"", do_escape_coding?" -E":"", do_qlp_coeff_prec_search?" -p":"",
397 (unsigned)min_residual_partition_order, (unsigned)max_residual_partition_order, (unsigned)rice_parameter_search_dist,
404 FLAC__bool first = true;
407 retval = decode_file("-", 0);
411 cmdline_forced_outfilename = 0;
412 for(retval = 0; i < argc && retval == 0; i++) {
413 if(0 == strcmp(argv[i], "-") && !first)
415 retval = decode_file(argv[i], 0);
421 FLAC__bool first = true;
424 retval = encode_file("-", 0, true);
428 cmdline_forced_outfilename = 0;
429 for(retval = 0; i < argc && retval == 0; i++) {
430 if(0 == strcmp(argv[i], "-") && !first)
432 retval = encode_file(argv[i], 0, i == (argc-1));
441 static void usage_header()
443 fprintf(stderr, "===============================================================================\n");
444 fprintf(stderr, "flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
445 fprintf(stderr, "Copyright (C) 2000,2001,2002 Josh Coalson\n");
446 fprintf(stderr, "\n");
447 fprintf(stderr, "This program is free software; you can redistribute it and/or\n");
448 fprintf(stderr, "modify it under the terms of the GNU General Public License\n");
449 fprintf(stderr, "as published by the Free Software Foundation; either version 2\n");
450 fprintf(stderr, "of the License, or (at your option) any later version.\n");
451 fprintf(stderr, "\n");
452 fprintf(stderr, "This program is distributed in the hope that it will be useful,\n");
453 fprintf(stderr, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
454 fprintf(stderr, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
455 fprintf(stderr, "GNU General Public License for more details.\n");
456 fprintf(stderr, "\n");
457 fprintf(stderr, "You should have received a copy of the GNU General Public License\n");
458 fprintf(stderr, "along with this program; if not, write to the Free Software\n");
459 fprintf(stderr, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
460 fprintf(stderr, "===============================================================================\n");
463 int short_usage(const char *message, ...)
468 va_start(args, message);
470 (void) vfprintf(stderr, message, args);
476 fprintf(stderr, "\n");
477 fprintf(stderr, "This is the short help; for full help use 'flac --help'\n");
478 fprintf(stderr, "\n");
479 fprintf(stderr, "To encode:\n");
480 fprintf(stderr, " flac [-#] [infile [...]]\n");
481 fprintf(stderr, "\n");
482 fprintf(stderr, " -# is -0 (fastest compression) to -8 (highest compression); -5 is the default\n");
483 fprintf(stderr, "\n");
484 fprintf(stderr, "To decode:\n");
485 fprintf(stderr, " flac -d [infile [...]]\n");
486 fprintf(stderr, "\n");
487 fprintf(stderr, "To test:\n");
488 fprintf(stderr, " flac -t [infile [...]]\n");
490 return message? 1 : 0;
493 int long_usage(const char *message, ...)
495 FILE *out = (message? stderr : stdout);
499 va_start(args, message);
501 (void) vfprintf(stderr, message, args);
507 fprintf(out, "Usage:\n");
508 fprintf(out, " flac [options] [infile [...]]\n");
510 fprintf(out, "For encoding:\n");
511 fprintf(out, " the input file(s) may be a PCM RIFF WAVE file or raw samples\n");
512 fprintf(out, " the output file(s) will be in FLAC format\n");
513 fprintf(out, "For decoding, the reverse is true\n");
515 fprintf(out, "A single 'infile' may be - for stdin. No 'infile' implies stdin. Use of\n");
516 fprintf(out, "stdin implies -c (write to stdout). Normally you should use:\n");
517 fprintf(out, " flac [options] -o outfilename or flac -d [options] -o outfilename\n");
518 fprintf(out, "instead of:\n");
519 fprintf(out, " flac [options] > outfilename or flac -d [options] > outfilename\n");
520 fprintf(out, "since the former allows flac to seek backwards to write the STREAMINFO or\n");
521 fprintf(out, "RIFF WAVE header contents when necessary.\n");
523 fprintf(out, "flac checks for the presence of a RIFF WAVE header to decide whether or not\n");
524 fprintf(out, "to treat an input file as WAVE format or raw samples. If any infile is raw\n");
525 fprintf(out, "you must specify the format options {-fb|fl} -fc -fp and -fs, which will\n");
526 fprintf(out, "apply to all raw files. You can force WAVE files to be treated as a raw files\n");
527 fprintf(out, "using -fr.\n");
529 fprintf(out, "generic options:\n");
530 fprintf(out, " -d : decode (default behavior is encode)\n");
531 fprintf(out, " -t : test (same as -d except no decoded file is written)\n");
532 fprintf(out, " -a : analyze (same as -d except an analysis file is written)\n");
533 fprintf(out, " -c : write output to stdout\n");
534 fprintf(out, " -s : silent (do not write runtime encode/decode statistics)\n");
535 fprintf(out, " -o filename : force the output file name (usually flac just changes the\n");
536 fprintf(out, " extension)\n");
537 fprintf(out, " --delete-input-file : deletes the input file after a successful encode/decode\n");
538 fprintf(out, " --skip samples : can be used both for encoding and decoding\n");
539 fprintf(out, "analyze options:\n");
540 fprintf(out, " --a-rtext : include residual signal in text output\n");
541 fprintf(out, " --a-rgp : generate gnuplot files of residual distribution of each subframe\n");
542 fprintf(out, "encoding options:\n");
544 fprintf(out, " --ogg : output Ogg-FLAC stream instead of native FLAC\n");
546 fprintf(out, " --lax : allow encoder to generate non-Subset files\n");
547 fprintf(out, " --sector-align : align encoding of multiple files on sector boundaries\n");
548 fprintf(out, " -S { # | X | #x } : include a point or points in a SEEKTABLE\n");
549 fprintf(out, " # : a specific sample number for a seek point\n");
550 fprintf(out, " X : a placeholder point (always goes at the end of the SEEKTABLE)\n");
551 fprintf(out, " #x : # evenly spaced seekpoints, the first being at sample 0\n");
552 fprintf(out, " You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
553 fprintf(out, " ified union of all such values.\n");
554 fprintf(out, " With no -S options, flac defaults to '-S 100x'. Use -S- for no SEEKTABLE.\n");
555 fprintf(out, " Note: -S #x will not work if the encoder can't determine the input size\n");
556 fprintf(out, " before starting.\n");
557 fprintf(out, " Note: if you use -S # and # is >= samples in the input, there will be\n");
558 fprintf(out, " either no seek point entered (if the input size is determinable\n");
559 fprintf(out, " before encoding starts) or a placeholder point (if input size is not\n");
560 fprintf(out, " determinable)\n");
561 fprintf(out, " -P # : write a PADDING block of # bytes (goes after SEEKTABLE)\n");
562 fprintf(out, " (0 => no PADDING block, default is -P 0)\n");
563 fprintf(out, " -b # : specify blocksize in samples; default is 1152 for -l 0, else 4608;\n");
564 fprintf(out, " must be 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768\n");
565 fprintf(out, " (unless --lax is used)\n");
566 fprintf(out, " -m : try mid-side coding for each frame (stereo input only)\n");
567 fprintf(out, " -M : adaptive mid-side coding for all frames (stereo input only)\n");
568 fprintf(out, " -0 .. -8 : fastest compression .. highest compression, default is -5\n");
569 fprintf(out, " these are synonyms for other options:\n");
570 fprintf(out, " -0 : synonymous with -l 0 -b 1152 -r 2,2\n");
571 fprintf(out, " -1 : synonymous with -l 0 -b 1152 -M -r 2,2\n");
572 fprintf(out, " -2 : synonymous with -l 0 -b 1152 -m -r 3\n");
573 fprintf(out, " -3 : synonymous with -l 6 -b 4608 -r 3,3\n");
574 fprintf(out, " -4 : synonymous with -l 8 -b 4608 -M -r 3,3\n");
575 fprintf(out, " -5 : synonymous with -l 8 -b 4608 -m -r 3,3\n");
576 fprintf(out, " -6 : synonymous with -l 8 -b 4608 -m -r 4\n");
577 fprintf(out, " -7 : synonymous with -l 8 -b 4608 -m -e -r 6\n");
578 fprintf(out, " -8 : synonymous with -l 12 -b 4608 -m -e -r 6\n");
579 fprintf(out, " -e : do exhaustive model search (expensive!)\n");
580 fprintf(out, " -E : include escape coding in the entropy coder\n");
581 fprintf(out, " -l # : specify max LPC order; 0 => use only fixed predictors\n");
582 fprintf(out, " -p : do exhaustive search of LP coefficient quantization (expensive!);\n");
583 fprintf(out, " overrides -q, does nothing if using -l 0\n");
584 fprintf(out, " -q # : specify precision in bits of quantized linear-predictor coefficients;\n");
585 fprintf(out, " 0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
586 fprintf(out, " -r [#,]# : [min,]max residual partition order (# is 0..16; min defaults to 0;\n");
587 fprintf(out, " default is -r 0; above 4 doesn't usually help much)\n");
588 fprintf(out, " -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much)\n");
589 fprintf(out, " -V : verify a correct encoding by decoding the output in parallel and\n");
590 fprintf(out, " comparing to the original\n");
591 fprintf(out, " -S-, -m-, -M-, -e-, -E-, -p-, -V-, --delete-input-file-,%s --lax-, --sector-align-\n",
598 fprintf(out, " can all be used to turn off a particular option\n");
599 fprintf(out, "format options:\n");
600 fprintf(out, " -fb | -fl : big-endian | little-endian byte order\n");
601 fprintf(out, " -fc channels\n");
602 fprintf(out, " -fp bits_per_sample\n");
603 fprintf(out, " -fs sample_rate : in Hz\n");
604 fprintf(out, " -fu : unsigned samples (default is signed)\n");
605 fprintf(out, " -fr : force input to be treated as raw samples\n");
607 return message? 1 : 0;
610 int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file)
613 char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
615 FLAC__byte lookahead[12];
616 unsigned lookahead_length = 0;
617 FLAC__bool treat_as_wave = false;
620 encode_options_t common_options;
622 if(0 == strcmp(infilename, "-")) {
624 encode_infile = stdin;
627 infilesize = flac__file_get_filesize(infilename);
628 if(0 == (encode_infile = fopen(infilename, "rb"))) {
629 fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
634 if(!force_raw_format) {
635 /* first set format based on name */
636 if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav"))
637 treat_as_wave = true;
639 treat_as_wave = false;
641 /* attempt to guess the file type based on the first 12 bytes */
642 if((lookahead_length = fread(lookahead, 1, 12, encode_infile)) < 12) {
644 fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename);
645 treat_as_wave = false;
648 if(strncmp(lookahead, "RIFF", 4) || strncmp(lookahead+8, "WAVE", 4)) {
650 fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename);
651 treat_as_wave = false;
654 treat_as_wave = true;
658 if(sector_align && !treat_as_wave && infilesize < 0) {
659 fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n");
664 if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
665 return long_usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
668 if(encode_infile == stdin || force_to_stdout)
669 strcpy(outfilename, "-");
671 const char *suffix = (use_ogg? ogg_suffix : flac_suffix);
672 strcpy(outfilename, output_prefix? output_prefix : "");
673 strcat(outfilename, infilename);
674 if(0 == (p = strrchr(outfilename, '.')))
675 strcat(outfilename, suffix);
677 if(0 == strcmp(p, suffix)) {
685 if(0 == forced_outfilename)
686 forced_outfilename = outfilename;
687 if(0 != cmdline_forced_outfilename)
688 forced_outfilename = cmdline_forced_outfilename;
690 common_options.verbose = verbose;
691 common_options.skip = skip;
692 common_options.verify = verify;
694 common_options.use_ogg = use_ogg;
696 common_options.lax = lax;
697 common_options.do_mid_side = do_mid_side;
698 common_options.loose_mid_side = loose_mid_side;
699 common_options.do_exhaustive_model_search = do_exhaustive_model_search;
700 common_options.do_escape_coding = do_escape_coding;
701 common_options.do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
702 common_options.min_residual_partition_order = min_residual_partition_order;
703 common_options.max_residual_partition_order = max_residual_partition_order;
704 common_options.rice_parameter_search_dist = rice_parameter_search_dist;
705 common_options.max_lpc_order = max_lpc_order;
706 common_options.blocksize = (unsigned)blocksize;
707 common_options.qlp_coeff_precision = qlp_coeff_precision;
708 common_options.padding = padding;
709 common_options.requested_seek_points = requested_seek_points;
710 common_options.num_requested_seek_points = num_requested_seek_points;
713 wav_encode_options_t options;
715 options.common = common_options;
716 options.is_last_file = is_last_file;
717 options.align_reservoir = align_reservoir;
718 options.align_reservoir_samples = &align_reservoir_samples;
719 options.sector_align = sector_align;
721 retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
724 raw_encode_options_t options;
726 options.common = common_options;
727 options.is_big_endian = format_is_big_endian;
728 options.is_unsigned_samples = format_is_unsigned_samples;
729 options.channels = format_channels;
730 options.bps = format_bps;
731 options.sample_rate = format_sample_rate;
733 retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, options);
736 if(retval == 0 && strcmp(infilename, "-")) {
737 if(strcmp(forced_outfilename, "-"))
738 flac__file_copy_metadata(infilename, forced_outfilename);
746 int decode_file(const char *infilename, const char *forced_outfilename)
748 static const char *suffixes[] = { ".wav", ".raw", ".ana" };
749 char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
752 FLAC__bool treat_as_ogg = false;
753 decode_options_t common_options;
755 if(!test_only && !analyze) {
756 if(force_raw_format && format_is_big_endian < 0)
757 return long_usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
760 if(0 == strcmp(infilename, "-") || force_to_stdout)
761 strcpy(outfilename, "-");
763 const char *suffix = suffixes[analyze? 2 : force_raw_format? 1 : 0];
764 strcpy(outfilename, output_prefix? output_prefix : "");
765 strcat(outfilename, infilename);
766 if(0 == (p = strrchr(outfilename, '.')))
767 strcat(outfilename, suffix);
769 if(0 == strcmp(p, suffix)) {
777 if(0 == forced_outfilename)
778 forced_outfilename = outfilename;
779 if(0 != cmdline_forced_outfilename)
780 forced_outfilename = cmdline_forced_outfilename;
784 else if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".ogg"))
787 treat_as_ogg = false;
789 #ifndef FLAC__HAS_OGG
791 fprintf(stderr, "%s: Ogg support has not been built into this copy of flac\n", infilename);
796 common_options.verbose = verbose;
798 common_options.is_ogg = treat_as_ogg;
800 common_options.skip = skip;
802 if(!force_raw_format) {
803 wav_decode_options_t options;
805 options.common = common_options;
807 retval = flac__decode_wav(infilename, test_only? 0 : forced_outfilename, analyze, aopts, options);
810 raw_decode_options_t options;
812 options.common = common_options;
813 options.is_big_endian = format_is_big_endian;
814 options.is_unsigned_samples = format_is_unsigned_samples;
816 retval = flac__decode_raw(infilename, test_only? 0 : forced_outfilename, analyze, aopts, options);
819 if(retval == 0 && strcmp(infilename, "-")) {
820 if(strcmp(forced_outfilename, "-"))
821 flac__file_copy_metadata(infilename, forced_outfilename);