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