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