1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000 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;
35 bool do_mid_side = true, 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], "-t")) {
56 else if(0 == strcmp(argv[i], "-s"))
58 else if(0 == strcmp(argv[i], "-s-"))
60 else if(0 == strcmp(argv[i], "--skip"))
61 skip = (uint64)atoi(argv[++i]); /* takes a pretty damn big file to overflow atoi() here, but it could happen */
62 else if(0 == strcmp(argv[i], "--lax"))
64 else if(0 == strcmp(argv[i], "--lax-"))
66 else if(0 == strcmp(argv[i], "-b"))
67 blocksize = atoi(argv[++i]);
68 else if(0 == strcmp(argv[i], "-e"))
69 do_exhaustive_model_search = true;
70 else if(0 == strcmp(argv[i], "-e-"))
71 do_exhaustive_model_search = false;
72 else if(0 == strcmp(argv[i], "-l"))
73 max_lpc_order = atoi(argv[++i]);
74 else if(0 == strcmp(argv[i], "-m"))
76 else if(0 == strcmp(argv[i], "-m-"))
78 else if(0 == strcmp(argv[i], "-p"))
79 do_qlp_coeff_prec_search = true;
80 else if(0 == strcmp(argv[i], "-p-"))
81 do_qlp_coeff_prec_search = false;
82 else if(0 == strcmp(argv[i], "-q"))
83 qlp_coeff_precision = atoi(argv[++i]);
84 else if(0 == strcmp(argv[i], "-r"))
85 rice_optimization_level = atoi(argv[++i]);
86 else if(0 == strcmp(argv[i], "-V"))
88 else if(0 == strcmp(argv[i], "-V-"))
90 else if(0 == strcmp(argv[i], "-fb"))
91 format_is_big_endian = true;
92 else if(0 == strcmp(argv[i], "-fl"))
93 format_is_big_endian = false;
94 else if(0 == strcmp(argv[i], "-fc"))
95 format_channels = atoi(argv[++i]);
96 else if(0 == strcmp(argv[i], "-fp"))
97 format_bps = atoi(argv[++i]);
98 else if(0 == strcmp(argv[i], "-fs"))
99 format_sample_rate = atoi(argv[++i]);
100 else if(0 == strcmp(argv[i], "-fu"))
101 format_is_unsigned_samples = true;
102 else if(0 == strcmp(argv[i], "-fr"))
103 format_is_wave = false;
104 else if(0 == strcmp(argv[i], "-fw"))
105 format_is_wave = true;
106 else if(0 == strcmp(argv[i], "-0")) {
107 do_exhaustive_model_search = false;
109 qlp_coeff_precision = 0;
110 rice_optimization_level = 0;
113 else if(0 == strcmp(argv[i], "-1")) {
114 do_exhaustive_model_search = false;
116 qlp_coeff_precision = 0;
117 rice_optimization_level = 0;
120 else if(0 == strcmp(argv[i], "-2")) {
121 do_exhaustive_model_search = false;
123 qlp_coeff_precision = 0;
126 else if(0 == strcmp(argv[i], "-4")) {
127 do_exhaustive_model_search = false;
129 qlp_coeff_precision = 0;
130 rice_optimization_level = 0;
133 else if(0 == strcmp(argv[i], "-5")) {
134 do_exhaustive_model_search = false;
136 qlp_coeff_precision = 0;
137 rice_optimization_level = 0;
140 else if(0 == strcmp(argv[i], "-6")) {
141 do_exhaustive_model_search = false;
143 qlp_coeff_precision = 0;
146 else if(0 == strcmp(argv[i], "-8")) {
147 do_exhaustive_model_search = false;
149 qlp_coeff_precision = 0;
152 else if(0 == strcmp(argv[i], "-9")) {
153 do_exhaustive_model_search = true;
155 do_qlp_coeff_prec_search = true;
156 rice_optimization_level = 99;
159 else if(isdigit((int)(argv[i][1]))) {
160 return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
163 return usage("ERROR: invalid option '%s'\n", argv[i]);
166 if(i + (test_only? 1:2) != argc)
167 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (test_only? 1:2), (test_only? "":"s"));
169 /* tweak options based on the filenames; validate the values */
171 if(format_is_wave < 0) {
172 if(strstr(argv[i], ".wav") == argv[i] + (strlen(argv[i]) - strlen(".wav")))
173 format_is_wave = true;
175 format_is_wave = false;
177 if(!format_is_wave) {
178 if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
179 return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
182 if(max_lpc_order == 0)
187 if(rice_optimization_level < 0) {
188 if(blocksize <= 1152)
189 rice_optimization_level = 4;
190 else if(blocksize <= 2304)
191 rice_optimization_level = 4;
192 else if(blocksize <= 4608)
193 rice_optimization_level = 4;
195 rice_optimization_level = 5;
200 if(format_is_wave < 0) {
201 if(strstr(argv[i+1], ".wav") == argv[i+1] + (strlen(argv[i+1]) - strlen(".wav")))
202 format_is_wave = true;
204 format_is_wave = false;
206 if(!format_is_wave) {
207 if(format_is_big_endian < 0)
208 return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
213 assert(blocksize >= 0 || mode_decode);
215 if(format_channels >= 0) {
216 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
217 return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
219 if(format_bps >= 0) {
220 if(format_bps != 8 && format_bps != 16)
221 return usage("ERROR: invalid bits per sample '%u' (must be 8 or 16)\n", format_bps);
223 if(format_sample_rate >= 0) {
224 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
225 return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
227 if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
228 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
230 if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
231 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
234 /* turn off verbosity if the output stream is going to stdout */
235 if(!test_only && 0 == strcmp(argv[i+1], "-"))
240 printf("flac v%u.%u, Copyright (C) 2000 Josh Coalson\n", FLAC__MAJOR_VERSION, FLAC__MINOR_VERSION);
241 printf("flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
242 printf("welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
245 printf("options:%s -b %u%s -l %u%s%s -q %u -r %u%s\n",
246 lax?" --lax":"", (unsigned)blocksize, do_mid_side?" -m":"", max_lpc_order,
247 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
248 qlp_coeff_precision, (unsigned)rice_optimization_level,
256 return decode_wav(argv[i], test_only? 0 : argv[i+1], verbose, skip);
258 return decode_raw(argv[i], test_only? 0 : argv[i+1], verbose, skip, format_is_big_endian, format_is_unsigned_samples);
261 return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision);
263 return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_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);
268 int usage(const char *message, ...)
273 va_start(args, message);
275 (void) vfprintf(stderr, message, args);
280 printf("==============================================================================\n");
281 printf("flac - Command-line FLAC encoder/decoder version %u.%u\n", FLAC__MAJOR_VERSION, FLAC__MINOR_VERSION);
282 printf("Copyright (C) 2000 Josh Coalson\n");
284 printf("This program is free software; you can redistribute it and/or\n");
285 printf("modify it under the terms of the GNU General Public License\n");
286 printf("as published by the Free Software Foundation; either version 2\n");
287 printf("of the License, or (at your option) any later version.\n");
289 printf("This program is distributed in the hope that it will be useful,\n");
290 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
291 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
292 printf("GNU General Public License for more details.\n");
294 printf("You should have received a copy of the GNU General Public License\n");
295 printf("along with this program; if not, write to the Free Software\n");
296 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
297 printf("==============================================================================\n");
299 printf(" flac [options] infile outfile\n");
301 printf("For encoding:\n");
302 printf(" infile may be a PCM RIFF WAVE file or raw samples\n");
303 printf(" outfile will be in FLAC format\n");
304 printf("For decoding, the reverse will be true\n");
306 printf("infile may be - for stdin, outfile may be - for stdout\n");
308 printf("If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
309 printf("RIFF WAVE. Otherwise, it's assumed to be raw samples and you have to specify\n");
310 printf("all the format options. You can force a .wav file to be treated as a raw file\n");
311 printf("using -fr.\n");
313 printf("generic options:\n");
314 printf(" -d : decode (default behavior is encode)\n");
315 printf(" -s : silent (do not write runtime encode/decode statistics to stdout)\n");
316 printf(" --skip samples : can be used both for encoding and decoding\n");
317 printf("encoding options:\n");
318 printf(" --lax : allow encoder to generate non-Subset files\n");
319 printf(" -b blocksize : default is 1152 for -l 0, else 4608; should be 192/576/1152/2304/4608 (unless --lax is used)\n");
320 printf(" -m : try mid-side coding for each frame (stereo input only)\n");
321 printf(" -0 .. -9 : fastest compression .. highest compression, default is -6\n");
322 printf(" these are synonyms for other options:\n");
323 printf(" -0 : synonymous with -l 0\n");
324 printf(" -1 : synonymous with -l 0 -m\n");
325 printf(" -2 : synonymous with -l 0 -m -r # (# is automatically determined by the block size)\n");
326 printf(" -3 : reserved\n");
327 printf(" -4 : synonymous with -l 8\n");
328 printf(" -5 : synonymous with -l 8 -m\n");
329 printf(" -6 : synonymous with -l 8 -m -r # (# is automatically determined by the block size)\n");
330 printf(" -7 : reserved\n");
331 printf(" -8 : synonymous with -l 32 -m -r # (# is automatically determined by the block size)\n");
332 printf(" -9 : synonymous with -l 32 -m -e -r 99 -p (very slow!)\n");
333 printf(" -e : do exhaustive model search (expensive!)\n");
334 printf(" -l max_lpc_order : 0 => use only fixed predictors\n");
335 printf(" -p : do exhaustive search of LP coefficient quantization (expensive!); overrides -q\n");
336 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);
337 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");
338 printf(" -V : verify a correct encoding by decoding the output in parallel and comparing to the original\n");
339 printf(" -m-, -e-, -p-, -V-, --lax- can all be used to turn off a particular option\n");
340 printf("format options:\n");
341 printf(" -fb | -fl : big-endian | little-endian byte order\n");
342 printf(" -fc channels\n");
343 printf(" -fp bits_per_sample\n");
344 printf(" -fs sample_rate : in Hz\n");
345 printf(" -fu : unsigned samples (default is signed)\n");
346 printf(" -fr : force to raw format (even if filename ends in .wav)\n");
347 printf(" -fw : force to RIFF WAVE\n");