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