add support for 24-bit input
[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, loose_mid_side = false, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false;
36         unsigned padding = 0;
37         unsigned max_lpc_order = 8;
38         unsigned qlp_coeff_precision = 0;
39         uint64 skip = 0;
40         int format_is_wave = -1, format_is_big_endian = -1, format_is_unsigned_samples = false;
41         int format_channels = -1, format_bps = -1, format_sample_rate = -1;
42         int blocksize = -1, rice_optimization_level = -1;
43
44         if(argc <= 1)
45                 return usage(0);
46
47         /* get the options */
48         for(i = 1; i < argc; i++) {
49                 if(argv[i][0] != '-' || argv[i][1] == 0)
50                         break;
51                 if(0 == strcmp(argv[i], "-d"))
52                         mode_decode = true;
53                 else if(0 == strcmp(argv[i], "-a")) {
54                         mode_decode = true;
55                         analyze = true;
56                 }
57                 else if(0 == strcmp(argv[i], "-t")) {
58                         mode_decode = true;
59                         test_only = true;
60                 }
61                 else if(0 == strcmp(argv[i], "-s"))
62                         verbose = false;
63                 else if(0 == strcmp(argv[i], "-s-"))
64                         verbose = true;
65                 else if(0 == strcmp(argv[i], "--skip"))
66                         skip = (uint64)atoi(argv[++i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
67                 else if(0 == strcmp(argv[i], "--lax"))
68                         lax = true;
69                 else if(0 == strcmp(argv[i], "--lax-"))
70                         lax = false;
71                 else if(0 == strcmp(argv[i], "-b"))
72                         blocksize = atoi(argv[++i]);
73                 else if(0 == strcmp(argv[i], "-e"))
74                         do_exhaustive_model_search = true;
75                 else if(0 == strcmp(argv[i], "-e-"))
76                         do_exhaustive_model_search = false;
77                 else if(0 == strcmp(argv[i], "-l"))
78                         max_lpc_order = atoi(argv[++i]);
79                 else if(0 == strcmp(argv[i], "-m"))
80                         do_mid_side = true;
81                 else if(0 == strcmp(argv[i], "-m-"))
82                         do_mid_side = false;
83                 else if(0 == strcmp(argv[i], "-M"))
84                         loose_mid_side = do_mid_side = true;
85                 else if(0 == strcmp(argv[i], "-M-"))
86                         loose_mid_side = do_mid_side = false;
87                 else if(0 == strcmp(argv[i], "-p"))
88                         do_qlp_coeff_prec_search = true;
89                 else if(0 == strcmp(argv[i], "-p-"))
90                         do_qlp_coeff_prec_search = false;
91                 else if(0 == strcmp(argv[i], "-P"))
92                         padding = atoi(argv[++i]);
93                 else if(0 == strcmp(argv[i], "-q"))
94                         qlp_coeff_precision = atoi(argv[++i]);
95                 else if(0 == strcmp(argv[i], "-r"))
96                         rice_optimization_level = atoi(argv[++i]);
97                 else if(0 == strcmp(argv[i], "-V"))
98                         verify = true;
99                 else if(0 == strcmp(argv[i], "-V-"))
100                         verify = false;
101                 else if(0 == strcmp(argv[i], "-fb"))
102                         format_is_big_endian = true;
103                 else if(0 == strcmp(argv[i], "-fl"))
104                         format_is_big_endian = false;
105                 else if(0 == strcmp(argv[i], "-fc"))
106                         format_channels = atoi(argv[++i]);
107                 else if(0 == strcmp(argv[i], "-fp"))
108                         format_bps = atoi(argv[++i]);
109                 else if(0 == strcmp(argv[i], "-fs"))
110                         format_sample_rate = atoi(argv[++i]);
111                 else if(0 == strcmp(argv[i], "-fu"))
112                         format_is_unsigned_samples = true;
113                 else if(0 == strcmp(argv[i], "-fr"))
114                         format_is_wave = false;
115                 else if(0 == strcmp(argv[i], "-fw"))
116                         format_is_wave = true;
117                 else if(0 == strcmp(argv[i], "-0")) {
118                         do_exhaustive_model_search = false;
119                         do_mid_side = false;
120                         loose_mid_side = false;
121                         qlp_coeff_precision = 0;
122                         rice_optimization_level = 0;
123                         max_lpc_order = 0;
124                 }
125                 else if(0 == strcmp(argv[i], "-1")) {
126                         do_exhaustive_model_search = false;
127                         do_mid_side = true;
128                         loose_mid_side = true;
129                         qlp_coeff_precision = 0;
130                         rice_optimization_level = 0;
131                         max_lpc_order = 0;
132                 }
133                 else if(0 == strcmp(argv[i], "-2")) {
134                         do_exhaustive_model_search = false;
135                         do_mid_side = true;
136                         loose_mid_side = false;
137                         qlp_coeff_precision = 0;
138                         max_lpc_order = 0;
139                 }
140                 else if(0 == strcmp(argv[i], "-4")) {
141                         do_exhaustive_model_search = false;
142                         do_mid_side = false;
143                         loose_mid_side = false;
144                         qlp_coeff_precision = 0;
145                         rice_optimization_level = 0;
146                         max_lpc_order = 8;
147                 }
148                 else if(0 == strcmp(argv[i], "-5")) {
149                         do_exhaustive_model_search = false;
150                         do_mid_side = true;
151                         loose_mid_side = true;
152                         qlp_coeff_precision = 0;
153                         rice_optimization_level = 0;
154                         max_lpc_order = 8;
155                 }
156                 else if(0 == strcmp(argv[i], "-6")) {
157                         do_exhaustive_model_search = false;
158                         do_mid_side = true;
159                         loose_mid_side = false;
160                         qlp_coeff_precision = 0;
161                         max_lpc_order = 8;
162                 }
163                 else if(0 == strcmp(argv[i], "-8")) {
164                         do_exhaustive_model_search = false;
165                         do_mid_side = true;
166                         loose_mid_side = false;
167                         qlp_coeff_precision = 0;
168                         max_lpc_order = 32;
169                 }
170                 else if(0 == strcmp(argv[i], "-9")) {
171                         do_exhaustive_model_search = true;
172                         do_mid_side = true;
173                         loose_mid_side = false;
174                         do_qlp_coeff_prec_search = true;
175                         rice_optimization_level = 99;
176                         max_lpc_order = 32;
177                 }
178                 else if(isdigit((int)(argv[i][1]))) {
179                         return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
180                 }
181                 else {
182                         return usage("ERROR: invalid option '%s'\n", argv[i]);
183                 }
184         }
185         if(i + (test_only? 1:2) != argc)
186                 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (test_only? 1:2), (test_only? "":"s"));
187
188         /* tweak options based on the filenames; validate the values */
189         if(!mode_decode) {
190                 if(format_is_wave < 0) {
191                         if(strstr(argv[i], ".wav") == argv[i] + (strlen(argv[i]) - strlen(".wav")))
192                                 format_is_wave = true;
193                         else
194                                 format_is_wave = false;
195                 }
196                 if(!format_is_wave) {
197                         if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
198                                 return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
199                 }
200                 if(blocksize < 0) {
201                         if(max_lpc_order == 0)
202                                 blocksize = 1152;
203                         else
204                                 blocksize = 4608;
205                 }
206                 if(rice_optimization_level < 0) {
207                         if(blocksize <= 1152)
208                                 rice_optimization_level = 4;
209                         else if(blocksize <= 2304)
210                                 rice_optimization_level = 4;
211                         else if(blocksize <= 4608)
212                                 rice_optimization_level = 4;
213                         else
214                                 rice_optimization_level = 5;
215                 }
216         }
217         else {
218                 if(test_only) {
219                         if(skip > 0)
220                                 return usage("ERROR: --skip is not allowed in test mode\n");
221                 }
222                 else if(!analyze) {
223                         if(format_is_wave < 0) {
224                                 if(strstr(argv[i+1], ".wav") == argv[i+1] + (strlen(argv[i+1]) - strlen(".wav")))
225                                         format_is_wave = true;
226                                 else
227                                         format_is_wave = false;
228                         }
229                         if(!format_is_wave) {
230                                 if(format_is_big_endian < 0)
231                                         return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
232                         }
233                 }
234         }
235
236         assert(blocksize >= 0 || mode_decode);
237
238         if(format_channels >= 0) {
239                 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
240                         return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
241         }
242         if(format_bps >= 0) {
243                 if(format_bps != 8 && format_bps != 16 && format_bps != 24)
244                         return usage("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", format_bps);
245         }
246         if(format_sample_rate >= 0) {
247                 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
248                         return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
249         }
250         if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
251                 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
252         }
253         if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
254                 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
255         }
256
257         /* turn off verbosity if the output stream is going to stdout */
258         if(!test_only && 0 == strcmp(argv[i+1], "-"))
259                 verbose = false;
260
261         if(verbose) {
262                 printf("\n");
263                 printf("flac %s, Copyright (C) 2000,2001 Josh Coalson\n", FLAC__VERSION_STRING);
264                 printf("flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
265                 printf("welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
266
267                 if(!mode_decode) {
268                         printf("options:%s -P %u -b %u%s -l %u%s%s -q %u -r %u%s\n",
269                                 lax?" --lax":"", padding, (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
270                                 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
271                                 qlp_coeff_precision, (unsigned)rice_optimization_level,
272                                 verify? " -V":""
273                         );
274                 }
275         }
276
277         if(mode_decode)
278                 if(format_is_wave)
279                         return decode_wav(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip);
280                 else
281                         return decode_raw(argv[i], test_only? 0 : argv[i+1], analyze, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
282         else
283                 if(format_is_wave)
284                         return encode_wav(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding);
285                 else
286                         return encode_raw(argv[i], argv[i+1], verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
287
288         return 0;
289 }
290
291 int usage(const char *message, ...)
292 {
293         va_list args;
294
295         if(message) {
296                 va_start(args, message);
297
298                 (void) vfprintf(stderr, message, args);
299
300                 va_end(args);
301
302         }
303         printf("==============================================================================\n");
304         printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
305         printf("Copyright (C) 2000,2001  Josh Coalson\n");
306         printf("\n");
307         printf("This program is free software; you can redistribute it and/or\n");
308         printf("modify it under the terms of the GNU General Public License\n");
309         printf("as published by the Free Software Foundation; either version 2\n");
310         printf("of the License, or (at your option) any later version.\n");
311         printf("\n");
312         printf("This program is distributed in the hope that it will be useful,\n");
313         printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
314         printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
315         printf("GNU General Public License for more details.\n");
316         printf("\n");
317         printf("You should have received a copy of the GNU General Public License\n");
318         printf("along with this program; if not, write to the Free Software\n");
319         printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
320         printf("==============================================================================\n");
321         printf("Usage:\n");
322         printf("  flac [options] infile outfile\n");
323         printf("\n");
324         printf("For encoding:\n");
325         printf("  infile may be a PCM RIFF WAVE file or raw samples\n");
326         printf("  outfile will be in FLAC format\n");
327         printf("For decoding, the reverse is be true\n");
328         printf("\n");
329         printf("infile may be - for stdin, outfile may be - for stdout\n");
330         printf("\n");
331         printf("If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
332         printf("RIFF WAVE.  Otherwise, it's assumed to be raw samples and you have to specify\n");
333         printf("all the format options.  You can force a .wav file to be treated as a raw file\n");
334         printf("using -fr.\n");
335         printf("\n");
336         printf("generic options:\n");
337         printf("  -d : decode (default behavior is encode)\n");
338         printf("  -t : test (same as -d except no decoded file is written)\n");
339         printf("  -a : analyze (same as -d except an analysis file is written)\n");
340         printf("  -s : silent (do not write runtime encode/decode statistics to stdout)\n");
341         printf("  --skip samples : can be used both for encoding and decoding\n");
342         printf("encoding options:\n");
343         printf("  --lax : allow encoder to generate non-Subset files\n");
344         printf("  -P bytes : write a PADDING block of the given length (0 => no PADDING block, default is -P 0)\n");
345         printf("  -b blocksize : default is 1152 for -l 0, else 4608; should be 192/576/1152/2304/4608 (unless --lax is used)\n");
346         printf("  -m : try mid-side coding for each frame (stereo input only)\n");
347         printf("  -M : loose mid-side coding for all frames (stereo input only)\n");
348         printf("  -0 .. -9 : fastest compression .. highest compression, default is -6\n");
349         printf("             these are synonyms for other options:\n");
350         printf("  -0 : synonymous with -l 0\n");
351         printf("  -1 : synonymous with -l 0 -M\n");
352         printf("  -2 : synonymous with -l 0 -m -r # (# is automatically determined by the block size)\n");
353         printf("  -3 : reserved\n");
354         printf("  -4 : synonymous with -l 8\n");
355         printf("  -5 : synonymous with -l 8 -M\n");
356         printf("  -6 : synonymous with -l 8 -m -r # (# is automatically determined by the block size)\n");
357         printf("  -7 : reserved\n");
358         printf("  -8 : synonymous with -l 32 -m -r # (# is automatically determined by the block size)\n");
359         printf("  -9 : synonymous with -l 32 -m -e -r 99 -p (very slow!)\n");
360         printf("  -e : do exhaustive model search (expensive!)\n");
361         printf("  -l max_lpc_order : 0 => use only fixed predictors\n");
362         printf("  -p : do exhaustive search of LP coefficient quantization (expensive!); overrides -q, does nothing if using -l 0\n");
363         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);
364         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");
365         printf("  -V : verify a correct encoding by decoding the output in parallel and comparing to the original\n");
366         printf("  -m-, -M-, -e-, -p-, -V-, --lax- can all be used to turn off a particular option\n");
367         printf("format options:\n");
368         printf("  -fb | -fl : big-endian | little-endian byte order\n");
369         printf("  -fc channels\n");
370         printf("  -fp bits_per_sample\n");
371         printf("  -fs sample_rate : in Hz\n");
372         printf("  -fu : unsigned samples (default is signed)\n");
373         printf("  -fr : force to raw format (even if filename ends in .wav)\n");
374         printf("  -fw : force to RIFF WAVE\n");
375         return 1;
376 }