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