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