166429328119565029e4872bf372b5117a9c7bf6
[platform/upstream/flac.git] / src / flac / main.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000,2001  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "FLAC/all.h"
26 #include "decode.h"
27 #include "encode.h"
28
29 static int usage(const char *message, ...);
30
31 int main(int argc, char *argv[])
32 {
33         int i;
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;
38         uint64 skip = 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;
42
43         if(argc <= 1)
44                 return usage(0);
45
46         /* get the options */
47         for(i = 1; i < argc; i++) {
48                 if(argv[i][0] != '-' || argv[i][1] == 0)
49                         break;
50                 if(0 == strcmp(argv[i], "-d"))
51                         mode_decode = true;
52                 else if(0 == strcmp(argv[i], "-a")) {
53                         mode_decode = true;
54                         analyze = true;
55                 }
56                 else if(0 == strcmp(argv[i], "-t")) {
57                         mode_decode = true;
58                         test_only = true;
59                 }
60                 else if(0 == strcmp(argv[i], "-s"))
61                         verbose = false;
62                 else if(0 == strcmp(argv[i], "-s-"))
63                         verbose = true;
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"))
67                         lax = true;
68                 else if(0 == strcmp(argv[i], "--lax-"))
69                         lax = false;
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"))
79                         do_mid_side = true;
80                 else if(0 == strcmp(argv[i], "-m-"))
81                         do_mid_side = false;
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"))
95                         verify = true;
96                 else if(0 == strcmp(argv[i], "-V-"))
97                         verify = false;
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;
116                         do_mid_side = false;
117                         force_mid_side = false;
118                         qlp_coeff_precision = 0;
119                         rice_optimization_level = 0;
120                         max_lpc_order = 0;
121                 }
122                 else if(0 == strcmp(argv[i], "-1")) {
123                         do_exhaustive_model_search = false;
124                         do_mid_side = true;
125                         force_mid_side = true;
126                         qlp_coeff_precision = 0;
127                         rice_optimization_level = 0;
128                         max_lpc_order = 0;
129                 }
130                 else if(0 == strcmp(argv[i], "-2")) {
131                         do_exhaustive_model_search = false;
132                         do_mid_side = true;
133                         force_mid_side = false;
134                         qlp_coeff_precision = 0;
135                         max_lpc_order = 0;
136                 }
137                 else if(0 == strcmp(argv[i], "-4")) {
138                         do_exhaustive_model_search = false;
139                         do_mid_side = false;
140                         force_mid_side = false;
141                         qlp_coeff_precision = 0;
142                         rice_optimization_level = 0;
143                         max_lpc_order = 8;
144                 }
145                 else if(0 == strcmp(argv[i], "-5")) {
146                         do_exhaustive_model_search = false;
147                         do_mid_side = true;
148                         force_mid_side = true;
149                         qlp_coeff_precision = 0;
150                         rice_optimization_level = 0;
151                         max_lpc_order = 8;
152                 }
153                 else if(0 == strcmp(argv[i], "-6")) {
154                         do_exhaustive_model_search = false;
155                         do_mid_side = true;
156                         force_mid_side = false;
157                         qlp_coeff_precision = 0;
158                         max_lpc_order = 8;
159                 }
160                 else if(0 == strcmp(argv[i], "-8")) {
161                         do_exhaustive_model_search = false;
162                         do_mid_side = true;
163                         force_mid_side = false;
164                         qlp_coeff_precision = 0;
165                         max_lpc_order = 32;
166                 }
167                 else if(0 == strcmp(argv[i], "-9")) {
168                         do_exhaustive_model_search = true;
169                         do_mid_side = true;
170                         force_mid_side = false;
171                         do_qlp_coeff_prec_search = true;
172                         rice_optimization_level = 99;
173                         max_lpc_order = 32;
174                 }
175                 else if(isdigit((int)(argv[i][1]))) {
176                         return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
177                 }
178                 else {
179                         return usage("ERROR: invalid option '%s'\n", argv[i]);
180                 }
181         }
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"));
184
185         /* tweak options based on the filenames; validate the values */
186         if(!mode_decode) {
187                 if(format_is_wave < 0) {
188                         if(strstr(argv[i], ".wav") == argv[i] + (strlen(argv[i]) - strlen(".wav")))
189                                 format_is_wave = true;
190                         else
191                                 format_is_wave = false;
192                 }
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");
196                 }
197                 if(blocksize < 0) {
198                         if(max_lpc_order == 0)
199                                 blocksize = 1152;
200                         else
201                                 blocksize = 4608;
202                 }
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;
210                         else
211                                 rice_optimization_level = 5;
212                 }
213         }
214         else {
215                 if(test_only) {
216                         if(skip > 0)
217                                 return usage("ERROR: --skip is not allowed in test mode\n");
218                 }
219                 else if(!analyze) {
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;
223                                 else
224                                         format_is_wave = false;
225                         }
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");
229                         }
230                 }
231         }
232
233         assert(blocksize >= 0 || mode_decode);
234
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);
238         }
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);
242         }
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);
246         }
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);
249         }
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);
252         }
253
254         /* turn off verbosity if the output stream is going to stdout */
255         if(!test_only && 0 == strcmp(argv[i+1], "-"))
256                 verbose = false;
257
258         if(verbose) {
259                 printf("\n");
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");
263
264                 if(!mode_decode) {
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,
269                                 verify? " -V":""
270                         );
271                 }
272         }
273
274         if(mode_decode)
275                 if(format_is_wave)
276                         return decode_wav(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip);
277                 else
278                         return decode_raw(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
279         else
280                 if(format_is_wave)
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);
282                 else
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);
284
285         return 0;
286 }
287
288 int usage(const char *message, ...)
289 {
290         va_list args;
291
292         if(message) {
293                 va_start(args, message);
294
295                 (void) vfprintf(stderr, message, args);
296
297                 va_end(args);
298
299         }
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");
303         printf("\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");
308         printf("\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");
313         printf("\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");
318         printf("Usage:\n");
319         printf("  flac [options] infile outfile\n");
320         printf("\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");
325         printf("\n");
326         printf("infile may be - for stdin, outfile may be - for stdout\n");
327         printf("\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");
332         printf("\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("  -s : silent (do not write runtime encode/decode statistics to stdout)\n");
337         printf("  --skip samples : can be used both for encoding and decoding\n");
338         printf("encoding options:\n");
339         printf("  --lax : allow encoder to generate non-Subset files\n");
340         printf("  -b blocksize : default is 1152 for -l 0, else 4608; should be 192/576/1152/2304/4608 (unless --lax is used)\n");
341         printf("  -m : try mid-side coding for each frame (stereo input only)\n");
342         printf("  -M : force mid-side coding for all frames (stereo input only)\n");
343         printf("  -0 .. -9 : fastest compression .. highest compression, default is -6\n");
344         printf("             these are synonyms for other options:\n");
345         printf("  -0 : synonymous with -l 0\n");
346         printf("  -1 : synonymous with -l 0 -M\n");
347         printf("  -2 : synonymous with -l 0 -m -r # (# is automatically determined by the block size)\n");
348         printf("  -3 : reserved\n");
349         printf("  -4 : synonymous with -l 8\n");
350         printf("  -5 : synonymous with -l 8 -M\n");
351         printf("  -6 : synonymous with -l 8 -m -r # (# is automatically determined by the block size)\n");
352         printf("  -7 : reserved\n");
353         printf("  -8 : synonymous with -l 32 -m -r # (# is automatically determined by the block size)\n");
354         printf("  -9 : synonymous with -l 32 -m -e -r 99 -p (very slow!)\n");
355         printf("  -e : do exhaustive model search (expensive!)\n");
356         printf("  -l max_lpc_order : 0 => use only fixed predictors\n");
357         printf("  -p : do exhaustive search of LP coefficient quantization (expensive!); overrides -q\n");
358         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);
359         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");
360         printf("  -V : verify a correct encoding by decoding the output in parallel and comparing to the original\n");
361         printf("  -m-, -M-, -e-, -p-, -V-, --lax- can all be used to turn off a particular option\n");
362         printf("format options:\n");
363         printf("  -fb | -fl : big-endian | little-endian byte order\n");
364         printf("  -fc channels\n");
365         printf("  -fp bits_per_sample\n");
366         printf("  -fs sample_rate : in Hz\n");
367         printf("  -fu : unsigned samples (default is signed)\n");
368         printf("  -fr : force to raw format (even if filename ends in .wav)\n");
369         printf("  -fw : force to RIFF WAVE\n");
370         return 1;
371 }