1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001 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.
29 static int usage(const char *message, ...);
31 int main(int argc, char *argv[])
34 bool verify = false, verbose = true, lax = false, mode_decode = false, test_only = false, analyze = false;
35 bool do_mid_side = true, force_mid_side = false, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false;
36 unsigned max_lpc_order = 8;
37 unsigned qlp_coeff_precision = 0;
39 int format_is_wave = -1, format_is_big_endian = -1, format_is_unsigned_samples = false;
40 int format_channels = -1, format_bps = -1, format_sample_rate = -1;
41 int blocksize = -1, rice_optimization_level = -1;
47 for(i = 1; i < argc; i++) {
48 if(argv[i][0] != '-' || argv[i][1] == 0)
50 if(0 == strcmp(argv[i], "-d"))
52 else if(0 == strcmp(argv[i], "-a")) {
56 else if(0 == strcmp(argv[i], "-t")) {
60 else if(0 == strcmp(argv[i], "-s"))
62 else if(0 == strcmp(argv[i], "-s-"))
64 else if(0 == strcmp(argv[i], "--skip"))
65 skip = (uint64)atoi(argv[++i]); /* takes a pretty damn big file to overflow atoi() here, but it could happen */
66 else if(0 == strcmp(argv[i], "--lax"))
68 else if(0 == strcmp(argv[i], "--lax-"))
70 else if(0 == strcmp(argv[i], "-b"))
71 blocksize = atoi(argv[++i]);
72 else if(0 == strcmp(argv[i], "-e"))
73 do_exhaustive_model_search = true;
74 else if(0 == strcmp(argv[i], "-e-"))
75 do_exhaustive_model_search = false;
76 else if(0 == strcmp(argv[i], "-l"))
77 max_lpc_order = atoi(argv[++i]);
78 else if(0 == strcmp(argv[i], "-m"))
80 else if(0 == strcmp(argv[i], "-m-"))
82 else if(0 == strcmp(argv[i], "-M"))
83 force_mid_side = do_mid_side = true;
84 else if(0 == strcmp(argv[i], "-M-"))
85 force_mid_side = do_mid_side = false;
86 else if(0 == strcmp(argv[i], "-p"))
87 do_qlp_coeff_prec_search = true;
88 else if(0 == strcmp(argv[i], "-p-"))
89 do_qlp_coeff_prec_search = false;
90 else if(0 == strcmp(argv[i], "-q"))
91 qlp_coeff_precision = atoi(argv[++i]);
92 else if(0 == strcmp(argv[i], "-r"))
93 rice_optimization_level = atoi(argv[++i]);
94 else if(0 == strcmp(argv[i], "-V"))
96 else if(0 == strcmp(argv[i], "-V-"))
98 else if(0 == strcmp(argv[i], "-fb"))
99 format_is_big_endian = true;
100 else if(0 == strcmp(argv[i], "-fl"))
101 format_is_big_endian = false;
102 else if(0 == strcmp(argv[i], "-fc"))
103 format_channels = atoi(argv[++i]);
104 else if(0 == strcmp(argv[i], "-fp"))
105 format_bps = atoi(argv[++i]);
106 else if(0 == strcmp(argv[i], "-fs"))
107 format_sample_rate = atoi(argv[++i]);
108 else if(0 == strcmp(argv[i], "-fu"))
109 format_is_unsigned_samples = true;
110 else if(0 == strcmp(argv[i], "-fr"))
111 format_is_wave = false;
112 else if(0 == strcmp(argv[i], "-fw"))
113 format_is_wave = true;
114 else if(0 == strcmp(argv[i], "-0")) {
115 do_exhaustive_model_search = false;
117 force_mid_side = false;
118 qlp_coeff_precision = 0;
119 rice_optimization_level = 0;
122 else if(0 == strcmp(argv[i], "-1")) {
123 do_exhaustive_model_search = false;
125 force_mid_side = true;
126 qlp_coeff_precision = 0;
127 rice_optimization_level = 0;
130 else if(0 == strcmp(argv[i], "-2")) {
131 do_exhaustive_model_search = false;
133 force_mid_side = false;
134 qlp_coeff_precision = 0;
137 else if(0 == strcmp(argv[i], "-4")) {
138 do_exhaustive_model_search = false;
140 force_mid_side = false;
141 qlp_coeff_precision = 0;
142 rice_optimization_level = 0;
145 else if(0 == strcmp(argv[i], "-5")) {
146 do_exhaustive_model_search = false;
148 force_mid_side = true;
149 qlp_coeff_precision = 0;
150 rice_optimization_level = 0;
153 else if(0 == strcmp(argv[i], "-6")) {
154 do_exhaustive_model_search = false;
156 force_mid_side = false;
157 qlp_coeff_precision = 0;
160 else if(0 == strcmp(argv[i], "-8")) {
161 do_exhaustive_model_search = false;
163 force_mid_side = false;
164 qlp_coeff_precision = 0;
167 else if(0 == strcmp(argv[i], "-9")) {
168 do_exhaustive_model_search = true;
170 force_mid_side = false;
171 do_qlp_coeff_prec_search = true;
172 rice_optimization_level = 99;
175 else if(isdigit((int)(argv[i][1]))) {
176 return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
179 return usage("ERROR: invalid option '%s'\n", argv[i]);
182 if(i + (test_only? 1:2) != argc)
183 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (test_only? 1:2), (test_only? "":"s"));
185 /* tweak options based on the filenames; validate the values */
187 if(format_is_wave < 0) {
188 if(strstr(argv[i], ".wav") == argv[i] + (strlen(argv[i]) - strlen(".wav")))
189 format_is_wave = true;
191 format_is_wave = false;
193 if(!format_is_wave) {
194 if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
195 return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
198 if(max_lpc_order == 0)
203 if(rice_optimization_level < 0) {
204 if(blocksize <= 1152)
205 rice_optimization_level = 4;
206 else if(blocksize <= 2304)
207 rice_optimization_level = 4;
208 else if(blocksize <= 4608)
209 rice_optimization_level = 4;
211 rice_optimization_level = 5;
217 return usage("ERROR: --skip is not allowed in test mode\n");
220 if(format_is_wave < 0) {
221 if(strstr(argv[i+1], ".wav") == argv[i+1] + (strlen(argv[i+1]) - strlen(".wav")))
222 format_is_wave = true;
224 format_is_wave = false;
226 if(!format_is_wave) {
227 if(format_is_big_endian < 0)
228 return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
233 assert(blocksize >= 0 || mode_decode);
235 if(format_channels >= 0) {
236 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
237 return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
239 if(format_bps >= 0) {
240 if(format_bps != 8 && format_bps != 16)
241 return usage("ERROR: invalid bits per sample '%u' (must be 8 or 16)\n", format_bps);
243 if(format_sample_rate >= 0) {
244 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
245 return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
247 if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
248 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
250 if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
251 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
254 /* turn off verbosity if the output stream is going to stdout */
255 if(!test_only && 0 == strcmp(argv[i+1], "-"))
260 printf("flac %s, Copyright (C) 2000,2001 Josh Coalson\n", FLAC__VERSION_STRING);
261 printf("flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
262 printf("welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
265 printf("options:%s -b %u%s -l %u%s%s -q %u -r %u%s\n",
266 lax?" --lax":"", (unsigned)blocksize, force_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
267 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
268 qlp_coeff_precision, (unsigned)rice_optimization_level,
276 return decode_wav(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip);
278 return decode_raw(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
281 return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, force_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision);
283 return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, force_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
288 int usage(const char *message, ...)
293 va_start(args, message);
295 (void) vfprintf(stderr, message, args);
300 printf("==============================================================================\n");
301 printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
302 printf("Copyright (C) 2000,2001 Josh Coalson\n");
304 printf("This program is free software; you can redistribute it and/or\n");
305 printf("modify it under the terms of the GNU General Public License\n");
306 printf("as published by the Free Software Foundation; either version 2\n");
307 printf("of the License, or (at your option) any later version.\n");
309 printf("This program is distributed in the hope that it will be useful,\n");
310 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
311 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
312 printf("GNU General Public License for more details.\n");
314 printf("You should have received a copy of the GNU General Public License\n");
315 printf("along with this program; if not, write to the Free Software\n");
316 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
317 printf("==============================================================================\n");
319 printf(" flac [options] infile outfile\n");
321 printf("For encoding:\n");
322 printf(" infile may be a PCM RIFF WAVE file or raw samples\n");
323 printf(" outfile will be in FLAC format\n");
324 printf("For decoding, the reverse is be true\n");
326 printf("infile may be - for stdin, outfile may be - for stdout\n");
328 printf("If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
329 printf("RIFF WAVE. Otherwise, it's assumed to be raw samples and you have to specify\n");
330 printf("all the format options. You can force a .wav file to be treated as a raw file\n");
331 printf("using -fr.\n");
333 printf("generic options:\n");
334 printf(" -d : decode (default behavior is encode)\n");
335 printf(" -t : test (same as -d except no decoded file is written)\n");
336 printf(" -a : analyze (same as -d except an analysis file is written)\n");
337 printf(" -s : silent (do not write runtime encode/decode statistics to stdout)\n");
338 printf(" --skip samples : can be used both for encoding and decoding\n");
339 printf("encoding options:\n");
340 printf(" --lax : allow encoder to generate non-Subset files\n");
341 printf(" -b blocksize : default is 1152 for -l 0, else 4608; should be 192/576/1152/2304/4608 (unless --lax is used)\n");
342 printf(" -m : try mid-side coding for each frame (stereo input only)\n");
343 printf(" -M : force mid-side coding for all frames (stereo input only)\n");
344 printf(" -0 .. -9 : fastest compression .. highest compression, default is -6\n");
345 printf(" these are synonyms for other options:\n");
346 printf(" -0 : synonymous with -l 0\n");
347 printf(" -1 : synonymous with -l 0 -M\n");
348 printf(" -2 : synonymous with -l 0 -m -r # (# is automatically determined by the block size)\n");
349 printf(" -3 : reserved\n");
350 printf(" -4 : synonymous with -l 8\n");
351 printf(" -5 : synonymous with -l 8 -M\n");
352 printf(" -6 : synonymous with -l 8 -m -r # (# is automatically determined by the block size)\n");
353 printf(" -7 : reserved\n");
354 printf(" -8 : synonymous with -l 32 -m -r # (# is automatically determined by the block size)\n");
355 printf(" -9 : synonymous with -l 32 -m -e -r 99 -p (very slow!)\n");
356 printf(" -e : do exhaustive model search (expensive!)\n");
357 printf(" -l max_lpc_order : 0 => use only fixed predictors\n");
358 printf(" -p : do exhaustive search of LP coefficient quantization (expensive!); overrides -q\n");
359 printf(" -q bits : precision of the quantized linear-predictor coefficients, 0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
360 printf(" -r level : rice parameter optimization level (level is 0..99, 0 => none, default is -r 0, above 4 doesn't usually help much)\n");
361 printf(" -V : verify a correct encoding by decoding the output in parallel and comparing to the original\n");
362 printf(" -m-, -M-, -e-, -p-, -V-, --lax- can all be used to turn off a particular option\n");
363 printf("format options:\n");
364 printf(" -fb | -fl : big-endian | little-endian byte order\n");
365 printf(" -fc channels\n");
366 printf(" -fp bits_per_sample\n");
367 printf(" -fs sample_rate : in Hz\n");
368 printf(" -fu : unsigned samples (default is signed)\n");
369 printf(" -fr : force to raw format (even if filename ends in .wav)\n");
370 printf(" -fw : force to RIFF WAVE\n");