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