update default values for compression levels
[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 "analyze.h"
27 #include "decode.h"
28 #include "encode.h"
29
30 static int usage(const char *message, ...);
31 static int encode_file(const char *infilename, const char *forced_outfilename);
32 static int decode_file(const char *infilename, const char *forced_outfilename);
33
34 bool verify = false, verbose = true, lax = 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 bool force_to_stdout = false;
37 analysis_options aopts = { false, false };
38 unsigned padding = 0;
39 unsigned max_lpc_order = 8;
40 unsigned qlp_coeff_precision = 0;
41 uint64 skip = 0;
42 int format_is_wave = -1, format_is_big_endian = -1, format_is_unsigned_samples = false;
43 int format_channels = -1, format_bps = -1, format_sample_rate = -1;
44 int blocksize = -1, min_residual_partition_order = -1, max_residual_partition_order = -1, rice_parameter_search_dist = -1;
45 char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
46 int num_requested_seek_points = -1; /* -1 => no -S options were given, 0 => -S- was given */
47
48 int main(int argc, char *argv[])
49 {
50         int i, retval = 0;
51         bool mode_decode = false;
52
53         if(argc <= 1)
54                 return usage(0);
55
56         /* get the options */
57         for(i = 1; i < argc; i++) {
58                 if(argv[i][0] != '-' || argv[i][1] == 0)
59                         break;
60                 if(0 == strcmp(argv[i], "-d"))
61                         mode_decode = true;
62                 else if(0 == strcmp(argv[i], "-a")) {
63                         mode_decode = true;
64                         analyze = true;
65                 }
66                 else if(0 == strcmp(argv[i], "-t")) {
67                         mode_decode = true;
68                         test_only = true;
69                 }
70                 else if(0 == strcmp(argv[i], "-c"))
71                         force_to_stdout = true;
72                 else if(0 == strcmp(argv[i], "-s"))
73                         verbose = false;
74                 else if(0 == strcmp(argv[i], "-s-"))
75                         verbose = true;
76                 else if(0 == strcmp(argv[i], "-S")) {
77                         if(num_requested_seek_points < 0)
78                                 num_requested_seek_points = 0;
79                         num_requested_seek_points++;
80                         strcat(requested_seek_points, argv[++i]);
81                         strcat(requested_seek_points, "<");
82                 }
83                 else if(0 == strcmp(argv[i], "-S-")) {
84                         num_requested_seek_points = 0;
85                         requested_seek_points[0] = '\0';
86                 }
87                 else if(0 == strcmp(argv[i], "--skip"))
88                         skip = (uint64)atoi(argv[++i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
89                 else if(0 == strcmp(argv[i], "--lax"))
90                         lax = true;
91                 else if(0 == strcmp(argv[i], "--lax-"))
92                         lax = false;
93                 else if(0 == strcmp(argv[i], "-b"))
94                         blocksize = atoi(argv[++i]);
95                 else if(0 == strcmp(argv[i], "-e"))
96                         do_exhaustive_model_search = true;
97                 else if(0 == strcmp(argv[i], "-e-"))
98                         do_exhaustive_model_search = false;
99                 else if(0 == strcmp(argv[i], "-l"))
100                         max_lpc_order = atoi(argv[++i]);
101                 else if(0 == strcmp(argv[i], "-m"))
102                         do_mid_side = true;
103                 else if(0 == strcmp(argv[i], "-m-"))
104                         do_mid_side = false;
105                 else if(0 == strcmp(argv[i], "-M"))
106                         loose_mid_side = do_mid_side = true;
107                 else if(0 == strcmp(argv[i], "-M-"))
108                         loose_mid_side = do_mid_side = false;
109                 else if(0 == strcmp(argv[i], "-p"))
110                         do_qlp_coeff_prec_search = true;
111                 else if(0 == strcmp(argv[i], "-p-"))
112                         do_qlp_coeff_prec_search = false;
113                 else if(0 == strcmp(argv[i], "-P"))
114                         padding = atoi(argv[++i]);
115                 else if(0 == strcmp(argv[i], "-q"))
116                         qlp_coeff_precision = atoi(argv[++i]);
117                 else if(0 == strcmp(argv[i], "-r")) {
118                         char *p = strchr(argv[++i], ',');
119                         if(0 == p) {
120                                 min_residual_partition_order = 0;
121                                 max_residual_partition_order = atoi(argv[i]);
122                         }
123                         else {
124                                 min_residual_partition_order = atoi(argv[i]);
125                                 max_residual_partition_order = atoi(++p);
126                         }
127                 }
128                 else if(0 == strcmp(argv[i], "-R"))
129                         rice_parameter_search_dist = atoi(argv[++i]);
130                 else if(0 == strcmp(argv[i], "-V"))
131                         verify = true;
132                 else if(0 == strcmp(argv[i], "-V-"))
133                         verify = false;
134                 else if(0 == strcmp(argv[i], "-fb"))
135                         format_is_big_endian = true;
136                 else if(0 == strcmp(argv[i], "-fl"))
137                         format_is_big_endian = false;
138                 else if(0 == strcmp(argv[i], "-fc"))
139                         format_channels = atoi(argv[++i]);
140                 else if(0 == strcmp(argv[i], "-fp"))
141                         format_bps = atoi(argv[++i]);
142                 else if(0 == strcmp(argv[i], "-fs"))
143                         format_sample_rate = atoi(argv[++i]);
144                 else if(0 == strcmp(argv[i], "-fu"))
145                         format_is_unsigned_samples = true;
146                 else if(0 == strcmp(argv[i], "-fr"))
147                         format_is_wave = false;
148                 else if(0 == strcmp(argv[i], "-fw"))
149                         format_is_wave = true;
150                 else if(0 == strcmp(argv[i], "--a-rgp"))
151                         aopts.do_residual_gnuplot = true;
152                 else if(0 == strcmp(argv[i], "--a-rgp-"))
153                         aopts.do_residual_gnuplot = false;
154                 else if(0 == strcmp(argv[i], "--a-rtext"))
155                         aopts.do_residual_text = true;
156                 else if(0 == strcmp(argv[i], "--a-rtext-"))
157                         aopts.do_residual_text = false;
158                 else if(0 == strcmp(argv[i], "-0")) {
159                         do_exhaustive_model_search = false;
160                         do_mid_side = false;
161                         loose_mid_side = false;
162                         qlp_coeff_precision = 0;
163                         min_residual_partition_order = max_residual_partition_order = 0;
164                         rice_parameter_search_dist = 0;
165                         max_lpc_order = 0;
166                 }
167                 else if(0 == strcmp(argv[i], "-1")) {
168                         do_exhaustive_model_search = false;
169                         do_mid_side = true;
170                         loose_mid_side = true;
171                         qlp_coeff_precision = 0;
172                         min_residual_partition_order = max_residual_partition_order = 2;
173                         rice_parameter_search_dist = 0;
174                         max_lpc_order = 0;
175                 }
176                 else if(0 == strcmp(argv[i], "-2")) {
177                         do_exhaustive_model_search = false;
178                         do_mid_side = true;
179                         loose_mid_side = false;
180                         qlp_coeff_precision = 0;
181                         min_residual_partition_order = 0;
182                         min_residual_partition_order = 3;
183                         rice_parameter_search_dist = 0;
184                         max_lpc_order = 0;
185                 }
186                 else if(0 == strcmp(argv[i], "-4")) {
187                         do_exhaustive_model_search = false;
188                         do_mid_side = false;
189                         loose_mid_side = false;
190                         qlp_coeff_precision = 0;
191                         min_residual_partition_order = max_residual_partition_order = 0;
192                         rice_parameter_search_dist = 0;
193                         max_lpc_order = 8;
194                 }
195                 else if(0 == strcmp(argv[i], "-5")) {
196                         do_exhaustive_model_search = false;
197                         do_mid_side = true;
198                         loose_mid_side = true;
199                         qlp_coeff_precision = 0;
200                         min_residual_partition_order = max_residual_partition_order = 3;
201                         rice_parameter_search_dist = 0;
202                         max_lpc_order = 8;
203                 }
204                 else if(0 == strcmp(argv[i], "-6")) {
205                         do_exhaustive_model_search = false;
206                         do_mid_side = true;
207                         loose_mid_side = false;
208                         qlp_coeff_precision = 0;
209                         min_residual_partition_order = 0;
210                         min_residual_partition_order = 4;
211                         rice_parameter_search_dist = 0;
212                         max_lpc_order = 8;
213                 }
214                 else if(0 == strcmp(argv[i], "-8")) {
215                         do_exhaustive_model_search = false;
216                         do_mid_side = true;
217                         loose_mid_side = false;
218                         qlp_coeff_precision = 0;
219                         rice_parameter_search_dist = 0;
220                         max_lpc_order = 32;
221                 }
222                 else if(0 == strcmp(argv[i], "-9")) {
223                         do_exhaustive_model_search = true;
224                         do_mid_side = true;
225                         loose_mid_side = false;
226                         do_qlp_coeff_prec_search = true;
227                         min_residual_partition_order = 0;
228                         max_residual_partition_order = 16;
229                         rice_parameter_search_dist = 32;
230                         max_lpc_order = 32;
231                 }
232                 else if(isdigit((int)(argv[i][1]))) {
233                         return usage("ERROR: compression level '%s' is still reserved\n", argv[i]);
234                 }
235                 else {
236                         return usage("ERROR: invalid option '%s'\n", argv[i]);
237                 }
238         }
239
240         /* tweak options; validate the values */
241         if(!mode_decode) {
242                 if(blocksize < 0) {
243                         if(max_lpc_order == 0)
244                                 blocksize = 1152;
245                         else
246                                 blocksize = 4608;
247                 }
248                 if(min_residual_partition_order < 0) {
249                         if(blocksize <= 1152)
250                                 max_residual_partition_order = 3;
251                         else if(blocksize <= 2304)
252                                 max_residual_partition_order = 4;
253                         else if(blocksize <= 4608)
254                                 max_residual_partition_order = 4;
255                         else
256                                 max_residual_partition_order = 5;
257                         min_residual_partition_order = max_residual_partition_order;
258                 }
259                 if(rice_parameter_search_dist < 0) {
260                         rice_parameter_search_dist = 0;
261                 }
262         }
263         else {
264                 if(test_only) {
265                         if(skip > 0)
266                                 return usage("ERROR: --skip is not allowed in test mode\n");
267                 }
268         }
269
270         assert(blocksize >= 0 || mode_decode);
271
272         if(format_channels >= 0) {
273                 if(format_channels == 0 || (unsigned)format_channels > FLAC__MAX_CHANNELS)
274                         return usage("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", format_channels, FLAC__MAX_CHANNELS);
275         }
276         if(format_bps >= 0) {
277                 if(format_bps != 8 && format_bps != 16 && format_bps != 24)
278                         return usage("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", format_bps);
279         }
280         if(format_sample_rate >= 0) {
281                 if(format_sample_rate == 0 || (unsigned)format_sample_rate > FLAC__MAX_SAMPLE_RATE)
282                         return usage("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", format_sample_rate, FLAC__MAX_SAMPLE_RATE);
283         }
284         if(!mode_decode && ((unsigned)blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)blocksize > FLAC__MAX_BLOCK_SIZE)) {
285                 return usage("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
286         }
287         if(qlp_coeff_precision > 0 && qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
288                 return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
289         }
290
291         if(verbose) {
292                 fprintf(stderr, "\n");
293                 fprintf(stderr, "flac %s, Copyright (C) 2000,2001 Josh Coalson\n", FLAC__VERSION_STRING);
294                 fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
295                 fprintf(stderr, "welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
296
297                 if(!mode_decode) {
298                         fprintf(stderr, "options:%s -P %u -b %u%s -l %u%s%s -q %u -r %u,%u -R %u%s\n",
299                                 lax?" --lax":"", padding, (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order,
300                                 do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"",
301                                 qlp_coeff_precision,
302                                 (unsigned)min_residual_partition_order, (unsigned)max_residual_partition_order, (unsigned)rice_parameter_search_dist,
303                                 verify? " -V":""
304                         );
305                 }
306         }
307
308         if(mode_decode) {
309                 bool first = true;
310                 int save_format;
311
312                 if(i == argc)
313                         retval = decode_file("-", 0);
314                 else if(i + 2 == argc && 0 == strcmp(argv[i], "-"))
315                         retval = decode_file("-", argv[i+1]);
316                 else {
317                         for(retval = 0; i < argc && retval == 0; i++) {
318                                 if(0 == strcmp(argv[i], "-") && !first)
319                                         continue;
320                                 save_format = format_is_wave;
321                                 retval = decode_file(argv[i], 0);
322                                 format_is_wave = save_format;
323                                 first = false;
324                         }
325                 }
326         }
327         else { /* encode */
328                 bool first = true;
329                 int save_format;
330
331                 if(i == argc)
332                         retval = encode_file("-", 0);
333                 else if(i + 2 == argc && 0 == strcmp(argv[i], "-"))
334                         retval = encode_file("-", argv[i+1]);
335                 else {
336                         for(retval = 0; i < argc && retval == 0; i++) {
337                                 if(0 == strcmp(argv[i], "-") && !first)
338                                         continue;
339                                 save_format = format_is_wave;
340                                 retval = encode_file(argv[i], 0);
341                                 format_is_wave = save_format;
342                                 first = false;
343                         }
344                 }
345         }
346
347         return retval;
348 }
349
350 int usage(const char *message, ...)
351 {
352         va_list args;
353
354         if(message) {
355                 va_start(args, message);
356
357                 (void) vfprintf(stderr, message, args);
358
359                 va_end(args);
360
361         }
362         fprintf(stderr, "===============================================================================\n");
363         fprintf(stderr, "flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
364         fprintf(stderr, "Copyright (C) 2000,2001  Josh Coalson\n");
365         fprintf(stderr, "\n");
366         fprintf(stderr, "This program is free software; you can redistribute it and/or\n");
367         fprintf(stderr, "modify it under the terms of the GNU General Public License\n");
368         fprintf(stderr, "as published by the Free Software Foundation; either version 2\n");
369         fprintf(stderr, "of the License, or (at your option) any later version.\n");
370         fprintf(stderr, "\n");
371         fprintf(stderr, "This program is distributed in the hope that it will be useful,\n");
372         fprintf(stderr, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
373         fprintf(stderr, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
374         fprintf(stderr, "GNU General Public License for more details.\n");
375         fprintf(stderr, "\n");
376         fprintf(stderr, "You should have received a copy of the GNU General Public License\n");
377         fprintf(stderr, "along with this program; if not, write to the Free Software\n");
378         fprintf(stderr, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
379         fprintf(stderr, "===============================================================================\n");
380         fprintf(stderr, "Usage:\n");
381         fprintf(stderr, "  flac [options] [infile [...]]\n");
382         fprintf(stderr, "\n");
383         fprintf(stderr, "For encoding:\n");
384         fprintf(stderr, "  the input file(s) may be a PCM RIFF WAVE file or raw samples\n");
385         fprintf(stderr, "  the output file(s) will be in FLAC format\n");
386         fprintf(stderr, "For decoding, the reverse is true\n");
387         fprintf(stderr, "\n");
388         fprintf(stderr, "A single 'infile' may be - for stdin.  No 'infile' implies stdin.  Use of\n");
389         fprintf(stderr, "stdin implies -c (write to stdout).  However, flac allows the special\n");
390         fprintf(stderr, "encoding/decoding forms:\n");
391         fprintf(stderr, "   flac [options] - outfilename   or  flac -d [options] - outfilename\n");
392         fprintf(stderr, "which is better than:\n");
393         fprintf(stderr, "   flac [options] > outfilename   or  flac -d [options] > outfilename\n");
394         fprintf(stderr, "since the former allows flac to seek backwards to write the STREAMINFO or\n");
395         fprintf(stderr, "RIFF WAVE header contents when necessary.\n");
396         fprintf(stderr, "\n");
397         fprintf(stderr, "If the unencoded filename ends with '.wav' or -fw is used, it's assumed to be\n");
398         fprintf(stderr, "RIFF WAVE.  Otherwise, flac will check for the presence of a RIFF header.  If\n");
399         fprintf(stderr, "any infile is raw you must specify the format options {-fb|fl} -fc -fp and -fs,\n");
400         fprintf(stderr, "which will apply to all raw files.  You can force a .wav file to be treated as\n");
401         fprintf(stderr, "a raw file using -fr.\n");
402         fprintf(stderr, "\n");
403         fprintf(stderr, "generic options:\n");
404         fprintf(stderr, "  -d : decode (default behavior is encode)\n");
405         fprintf(stderr, "  -t : test (same as -d except no decoded file is written)\n");
406         fprintf(stderr, "  -a : analyze (same as -d except an analysis file is written)\n");
407         fprintf(stderr, "  -c : write output to stdout\n");
408         fprintf(stderr, "  -s : silent (do not write runtime encode/decode statistics)\n");
409         fprintf(stderr, "  --skip samples : can be used both for encoding and decoding\n");
410         fprintf(stderr, "analyze options:\n");
411         fprintf(stderr, "  --a-rtext : include residual signal in text output\n");
412         fprintf(stderr, "  --a-rgp : generate gnuplot files of residual distribution of each subframe\n");
413         fprintf(stderr, "encoding options:\n");
414         fprintf(stderr, "  --lax : allow encoder to generate non-Subset files\n");
415         fprintf(stderr, "  -S { # | X | #x } : include a point or points in a SEEKTABLE\n");
416         fprintf(stderr, "       #  : a specific sample number for a seek point\n");
417         fprintf(stderr, "       X  : a placeholder point (always goes at the end of the SEEKTABLE)\n");
418         fprintf(stderr, "       #x : # evenly spaced seekpoints, the first being at sample 0\n");
419         fprintf(stderr, "     You may use many -S options; the resulting SEEKTABLE will be the unique-\n");
420         fprintf(stderr, "           ified union of all such values.\n");
421         fprintf(stderr, "     With no -S options, flac defaults to '-S 100x'.  Use -S- for no SEEKTABLE.\n");
422         fprintf(stderr, "     Note: -S #x will not work if the encoder can't determine the input size\n");
423         fprintf(stderr, "           before starting.\n");
424         fprintf(stderr, "     Note: if you use -S # and # is >= samples in the input, there will be\n");
425         fprintf(stderr, "           either no seek point entered (if the input size is determinable\n");
426         fprintf(stderr, "           before encoding starts) or a placeholder point (if input size is not\n");
427         fprintf(stderr, "           determinable)\n");
428         fprintf(stderr, "  -P # : write a PADDING block of # bytes (goes after SEEKTABLE)\n");
429         fprintf(stderr, "         (0 => no PADDING block, default is -P 0)\n");
430         fprintf(stderr, "  -b # : specify blocksize in samples; default is 1152 for -l 0, else 4608;\n");
431         fprintf(stderr, "         must be 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768\n");
432         fprintf(stderr, "         (unless --lax is used)\n");
433         fprintf(stderr, "  -m   : try mid-side coding for each frame (stereo input only)\n");
434         fprintf(stderr, "  -M   : loose mid-side coding for all frames (stereo input only)\n");
435         fprintf(stderr, "  -0 .. -9 : fastest compression .. highest compression, default is -5\n");
436         fprintf(stderr, "             these are synonyms for other options:\n");
437         fprintf(stderr, "  -0   : synonymous with -l 0 -b 1152\n");
438         fprintf(stderr, "  -1   : synonymous with -l 0 -b 1152 -M -r 2,2\n");
439         fprintf(stderr, "  -2   : synonymous with -l 0 -b 1152 -m -r 3\n");
440         fprintf(stderr, "  -3   : reserved\n");
441         fprintf(stderr, "  -4   : synonymous with -l 8 -b 4608\n");
442         fprintf(stderr, "  -5   : synonymous with -l 8 -b 4608 -M -r 3,3\n");
443         fprintf(stderr, "  -6   : synonymous with -l 8 -b 4608 -m -r 4\n");
444         fprintf(stderr, "  -7   : reserved\n");
445         fprintf(stderr, "  -8   : synonymous with -l 32 -b 4608 -m -r 4\n");
446         fprintf(stderr, "  -9   : synonymous with -l 32 -m -e -r 16 -R 32 -p (very slow!)\n");
447         fprintf(stderr, "  -e   : do exhaustive model search (expensive!)\n");
448         fprintf(stderr, "  -l # : specify max LPC order; 0 => use only fixed predictors\n");
449         fprintf(stderr, "  -p   : do exhaustive search of LP coefficient quantization (expensive!);\n");
450         fprintf(stderr, "         overrides -q, does nothing if using -l 0\n");
451         fprintf(stderr, "  -q # : specify precision in bits of quantized linear-predictor coefficients;\n");
452         fprintf(stderr, "         0 => let encoder decide (min is %u, default is -q 0)\n", FLAC__MIN_QLP_COEFF_PRECISION);
453         fprintf(stderr, "  -r [#,]# : [min,]max residual partition order (# is 0..16; min defaults to 0;\n");
454         fprintf(stderr, "         default is -r 0; above 4 doesn't usually help much)\n");
455         fprintf(stderr, "  -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much\n");
456         fprintf(stderr, "  -V   : verify a correct encoding by decoding the output in parallel and\n");
457         fprintf(stderr, "         comparing to the original\n");
458         fprintf(stderr, "  -S-, -m-, -M-, -e-, -p-, -V-, --lax- can all be used to turn off a particular\n");
459         fprintf(stderr, "  option\n");
460         fprintf(stderr, "format options:\n");
461         fprintf(stderr, "  -fb | -fl : big-endian | little-endian byte order\n");
462         fprintf(stderr, "  -fc channels\n");
463         fprintf(stderr, "  -fp bits_per_sample\n");
464         fprintf(stderr, "  -fs sample_rate : in Hz\n");
465         fprintf(stderr, "  -fu : unsigned samples (default is signed)\n");
466         fprintf(stderr, "  -fr : force to raw format (even if filename ends in .wav)\n");
467         fprintf(stderr, "  -fw : force to RIFF WAVE\n");
468         return 1;
469 }
470
471 int encode_file(const char *infilename, const char *forced_outfilename)
472 {
473         FILE *encode_infile;
474         char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
475         char *p;
476
477         if(0 == strcmp(infilename, "-")) {
478                 encode_infile = stdin;
479         }
480         else {
481                 if(0 == (encode_infile = fopen(infilename, "rb"))) {
482                         fprintf(stderr, "ERROR: can't open input file %s\n", infilename);
483                         return 1;
484                 }
485         }
486
487         if(verbose)
488                 fprintf(stderr, "%s:\n", infilename);
489
490         if(format_is_wave < 0) {
491                 /* lamely attempt to guess the file type based on the first 4 bytes (which is all ungetc will guarantee us) */
492                 char head[4];
493                 int h, n;
494                 /* first set format based on name */
495                 if(strstr(infilename, ".wav") == infilename + (strlen(infilename) - strlen(".wav")))
496                         format_is_wave = true;
497                 else
498                         format_is_wave = false;
499                 if((n = fread(head, 1, 4, encode_infile)) < 4) {
500                         if(format_is_wave)
501                                 fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename);
502                         format_is_wave = false;
503                 }
504                 else {
505                         if(strncmp(head, "RIFF", 4)) {
506                                 if(format_is_wave)
507                                         fprintf(stderr, "WARNING: %s is not a WAVE file, treating as a raw file\n", infilename);
508                                 format_is_wave = false;
509                         }
510                         else
511                                 format_is_wave = true;
512                 }
513                 for(h = n-1; h >= 0; h--)
514                         ungetc(head[h], encode_infile);
515         }
516
517         if(!format_is_wave) {
518                 if(format_is_big_endian < 0 || format_channels < 0 || format_bps < 0 || format_sample_rate < 0)
519                         return usage("ERROR: for encoding a raw file you must specify { -fb or -fl }, -fc, -fp, and -fs\n");
520         }
521
522         if(encode_infile == stdin || force_to_stdout)
523                 strcpy(outfilename, "-");
524         else {
525                 strcpy(outfilename, infilename);
526                 if(0 == (p = strrchr(outfilename, '.')))
527                         strcat(outfilename, ".flac");
528                 else {
529                         if(0 == strcmp(p, ".flac"))
530                                 strcpy(p, "_new.flac");
531                         else
532                                 strcpy(p, ".flac");
533                 }
534         }
535         if(0 == forced_outfilename)
536                 forced_outfilename = outfilename;
537
538         if(format_is_wave)
539                 return encode_wav(encode_infile, infilename, forced_outfilename, verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points);
540         else
541                 return encode_raw(encode_infile, infilename, forced_outfilename, verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate);
542 }
543
544 int decode_file(const char *infilename, const char *forced_outfilename)
545 {
546         static const char *suffixes[] = { ".wav", ".raw" };
547         char outfilename[4096]; /* @@@ bad MAGIC NUMBER */
548         char *p;
549
550         if(!test_only && !analyze) {
551                 if(format_is_wave < 0) {
552                         format_is_wave = true;
553                 }
554                 if(!format_is_wave) {
555                         if(format_is_big_endian < 0)
556                                 return usage("ERROR: for decoding to a raw file you must specify -fb or -fl\n");
557                 }
558         }
559
560         if(0 == strcmp(infilename, "-") || force_to_stdout)
561                 strcpy(outfilename, "-");
562         else {
563                 const char *suffix = suffixes[format_is_wave? 0:1];
564                 strcpy(outfilename, infilename);
565                 if(0 == (p = strrchr(outfilename, '.')))
566                         strcat(outfilename, suffix);
567                 else {
568                         if(0 == strcmp(p, suffix)) {
569                                 strcpy(p, "_new");
570                                 strcat(p, suffix);
571                         }
572                         else
573                                 strcpy(p, suffix);
574                 }
575         }
576         if(0 == forced_outfilename)
577                 forced_outfilename = outfilename;
578
579         if(format_is_wave)
580                 return decode_wav(infilename, test_only? 0 : forced_outfilename, analyze, aopts, verbose, skip);
581         else
582                 return decode_raw(infilename, test_only? 0 : forced_outfilename, analyze, aopts, verbose, skip, format_is_big_endian, format_is_unsigned_samples);
583 }