main.c 71.2 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* flac - Command-line FLAC encoder/decoder
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <ctype.h>
Josh Coalson's avatar
Josh Coalson committed
20
#include <locale.h>
Josh Coalson's avatar
Josh Coalson committed
21
22
23
24
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
25
26
27
28
29

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

30
#if !defined _MSC_VER && !defined __MINGW32__
Josh Coalson's avatar
Josh Coalson committed
31
/* unlink is in stdio.h in VC++ */
Josh Coalson's avatar
Josh Coalson committed
32
#include <unistd.h> /* for unlink() */
33
34
#else
#define strcasecmp stricmp
Josh Coalson's avatar
Josh Coalson committed
35
#endif
Josh Coalson's avatar
Josh Coalson committed
36
#include "FLAC/all.h"
Josh Coalson's avatar
Josh Coalson committed
37
#include "share/grabbag.h"
Josh Coalson's avatar
Josh Coalson committed
38
#include "analyze.h"
Josh Coalson's avatar
Josh Coalson committed
39
40
#include "decode.h"
#include "encode.h"
41
#include "local_string_utils.h" /* for flac__strlcat() and flac__strlcpy() */
42
#include "utils.h"
Josh Coalson's avatar
Josh Coalson committed
43
#include "vorbiscomment.h"
Josh Coalson's avatar
Josh Coalson committed
44

45
46
47
48
49
50
51
52
#if 0
/*[JEC] was:#if HAVE_GETOPT_LONG*/
/*[JEC] see flac/include/share/getopt.h as to why the change */
#  include <getopt.h>
#else
#  include "share/getopt.h"
#endif

53
54
typedef enum { RAW, WAV, AIF } FileFormat;

55
56
static int do_it();

Josh Coalson's avatar
Josh Coalson committed
57
static FLAC__bool init_options();
58
59
60
61
62
63
static int parse_options(int argc, char *argv[]);
static int parse_option(int short_option, const char *long_option, const char *option_argument);
static void free_options();

static int usage_error(const char *message, ...);
static void short_usage();
Josh Coalson's avatar
Josh Coalson committed
64
static void show_version();
65
66
static void show_help();
static void show_explain();
67
68
static void format_mistake(const char *infilename, const char *wrong, const char *right);

69
70
71
72
73
static int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file);
static int decode_file(const char *infilename);

static const char *get_encoded_outfilename(const char *infilename);
static const char *get_decoded_outfilename(const char *infilename);
74
static const char *get_outfilename(const char *infilename, const char *suffix);
75

76
77
78
79
80
static void die(const char *message);
static char *local_strdup(const char *source);


/*
Josh Coalson's avatar
Josh Coalson committed
81
 * share__getopt format struct; note that for long options with no
82
83
 * short option equivalent we just set the 'val' field to 0.
 */
Josh Coalson's avatar
Josh Coalson committed
84
static struct share__option long_options_[] = {
85
86
87
	/*
	 * general options
	 */
88
89
90
91
92
93
94
95
96
97
98
99
100
	{ "help"             , share__no_argument, 0, 'h' },
	{ "explain"          , share__no_argument, 0, 'H' },
	{ "version"          , share__no_argument, 0, 'v' },
	{ "decode"           , share__no_argument, 0, 'd' },
	{ "analyze"          , share__no_argument, 0, 'a' },
	{ "test"             , share__no_argument, 0, 't' },
	{ "stdout"           , share__no_argument, 0, 'c' },
	{ "silent"           , share__no_argument, 0, 's' },
	{ "delete-input-file", share__no_argument, 0, 0 },
	{ "output-prefix"    , share__required_argument, 0, 0 },
	{ "output-name"      , share__required_argument, 0, 'o' },
	{ "skip"             , share__required_argument, 0, 0 },
	{ "until"            , share__required_argument, 0, 0 },
101
102
103
104

	/*
	 * decoding options
	 */
105
106
	{ "decode-through-errors", share__no_argument, 0, 'F' },
	{ "apply-replaygain-which-is-not-lossless", share__optional_argument, 0, 0 }, /* undocumented */
107
108
109
110

	/*
	 * encoding options
	 */
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	{ "cuesheet"                  , share__required_argument, 0, 0 },
	{ "no-cued-seekpoints"        , share__no_argument, 0, 0 },
	{ "tag"                       , share__required_argument, 0, 'T' },
	{ "compression-level-0"       , share__no_argument, 0, '0' },
	{ "compression-level-1"       , share__no_argument, 0, '1' },
	{ "compression-level-2"       , share__no_argument, 0, '2' },
	{ "compression-level-3"       , share__no_argument, 0, '3' },
	{ "compression-level-4"       , share__no_argument, 0, '4' },
	{ "compression-level-5"       , share__no_argument, 0, '5' },
	{ "compression-level-6"       , share__no_argument, 0, '6' },
	{ "compression-level-7"       , share__no_argument, 0, '7' },
	{ "compression-level-8"       , share__no_argument, 0, '8' },
	{ "compression-level-9"       , share__no_argument, 0, '9' },
	{ "best"                      , share__no_argument, 0, '8' },
	{ "fast"                      , share__no_argument, 0, '0' },
	{ "super-secret-totally-impractical-compression-level", share__no_argument, 0, 0 },
	{ "verify"                    , share__no_argument, 0, 'V' },
	{ "force-aiff-format"         , share__no_argument, 0, 0 },
	{ "force-raw-format"          , share__no_argument, 0, 0 },
	{ "lax"                       , share__no_argument, 0, 0 },
	{ "replay-gain"               , share__no_argument, 0, 0 },
	{ "sector-align"              , share__no_argument, 0, 0 },
	{ "seekpoint"                 , share__required_argument, 0, 'S' },
	{ "padding"                   , share__required_argument, 0, 'P' },
135
#ifdef FLAC__HAS_OGG
136
137
	{ "ogg"                       , share__no_argument, 0, 0 },
	{ "serial-number"             , share__required_argument, 0, 0 },
138
#endif
139
140
141
142
143
144
145
146
147
148
149
150
151
	{ "blocksize"                 , share__required_argument, 0, 'b' },
	{ "exhaustive-model-search"   , share__no_argument, 0, 'e' },
	{ "max-lpc-order"             , share__required_argument, 0, 'l' },
	{ "mid-side"                  , share__no_argument, 0, 'm' },
	{ "adaptive-mid-side"         , share__no_argument, 0, 'M' },
	{ "qlp-coeff-precision-search", share__no_argument, 0, 'p' },
	{ "qlp-coeff-precision"       , share__required_argument, 0, 'q' },
	{ "rice-partition-order"      , share__required_argument, 0, 'r' },
	{ "endian"                    , share__required_argument, 0, 0 },
	{ "channels"                  , share__required_argument, 0, 0 },
	{ "bps"                       , share__required_argument, 0, 0 },
	{ "sample-rate"               , share__required_argument, 0, 0 },
	{ "sign"                      , share__required_argument, 0, 0 },
152
153
154
155

	/*
	 * analysis options
	 */
156
157
	{ "residual-gnu-plot", share__no_argument, 0, 0 },
	{ "residual-text", share__no_argument, 0, 0 },
158
159
160
161

	/*
	 * negatives
	 */
162
163
164
165
166
167
168
	{ "no-decode-through-errors"  , share__no_argument, 0, 0 },
	{ "no-silent"                 , share__no_argument, 0, 0 },
	{ "no-seektable"              , share__no_argument, 0, 0 },
	{ "no-delete-input-file"      , share__no_argument, 0, 0 },
	{ "no-replay-gain"            , share__no_argument, 0, 0 },
	{ "no-sector-align"           , share__no_argument, 0, 0 },
	{ "no-lax"                    , share__no_argument, 0, 0 },
169
#ifdef FLAC__HAS_OGG
170
	{ "no-ogg"                    , share__no_argument, 0, 0 },
171
#endif
172
173
174
175
176
177
178
179
	{ "no-exhaustive-model-search", share__no_argument, 0, 0 },
	{ "no-mid-side"               , share__no_argument, 0, 0 },
	{ "no-adaptive-mid-side"      , share__no_argument, 0, 0 },
	{ "no-qlp-coeff-prec-search"  , share__no_argument, 0, 0 },
	{ "no-padding"                , share__no_argument, 0, 0 },
	{ "no-verify"                 , share__no_argument, 0, 0 },
	{ "no-residual-gnuplot"       , share__no_argument, 0, 0 },
	{ "no-residual-text"          , share__no_argument, 0, 0 },
180
181
182
	/*
	 * undocumented debugging options for the test suite
	 */
183
184
185
	{ "disable-constant-subframes", share__no_argument, 0, 0 },
	{ "disable-fixed-subframes"   , share__no_argument, 0, 0 },
	{ "disable-verbatim-subframes", share__no_argument, 0, 0 },
186

Josh Coalson's avatar
Josh Coalson committed
187
	{0, 0, 0, 0}
188
189
190
191
192
193
194
195
};


/*
 * global to hold command-line option values
 */

static struct {
196
197
	FLAC__bool show_help;
	FLAC__bool show_explain;
Josh Coalson's avatar
Josh Coalson committed
198
	FLAC__bool show_version;
199
200
201
202
	FLAC__bool mode_decode;
	FLAC__bool verify;
	FLAC__bool verbose;
	FLAC__bool continue_through_decode_errors;
203
	replaygain_synthesis_spec_t replaygain_synthesis_spec;
204
205
206
207
	FLAC__bool lax;
	FLAC__bool test_only;
	FLAC__bool analyze;
	FLAC__bool use_ogg;
208
209
	FLAC__bool has_serial_number; /* true iff --serial-number was used */
	long serial_number; /* this is the Ogg serial number and is unused for native FLAC */
210
211
212
213
214
215
	FLAC__bool do_mid_side;
	FLAC__bool loose_mid_side;
	FLAC__bool do_exhaustive_model_search;
	FLAC__bool do_escape_coding;
	FLAC__bool do_qlp_coeff_prec_search;
	FLAC__bool force_to_stdout;
216
	FLAC__bool force_aiff_format;
217
218
	FLAC__bool force_raw_format;
	FLAC__bool delete_input;
219
	FLAC__bool replay_gain;
220
221
222
223
224
225
226
	FLAC__bool sector_align;
	const char *cmdline_forced_outfilename;
	const char *output_prefix;
	analysis_options aopts;
	int padding;
	unsigned max_lpc_order;
	unsigned qlp_coeff_precision;
227
	const char *skip_specification;
228
	const char *until_specification;
229
230
231
232
233
234
235
236
237
	int format_is_big_endian;
	int format_is_unsigned_samples;
	int format_channels;
	int format_bps;
	int format_sample_rate;
	int blocksize;
	int min_residual_partition_order;
	int max_residual_partition_order;
	int rice_parameter_search_dist;
238
	char requested_seek_points[50000]; /* bad MAGIC NUMBER but buffer overflow is checked */
239
	int num_requested_seek_points; /* -1 => no -S options were given, 0 => -S- was given */
240
241
	const char *cuesheet_filename;
	FLAC__bool cued_seekpoints;
242
243
244

	unsigned num_files;
	char **filenames;
Josh Coalson's avatar
Josh Coalson committed
245
246

	FLAC__StreamMetadata *vorbis_comment;
247
248
249
250
251
252

	struct {
		FLAC__bool disable_constant_subframes;
		FLAC__bool disable_fixed_subframes;
		FLAC__bool disable_verbatim_subframes;
	} debug;
253
254
255
256
257
258
259
260
261
262
} option_values;


/*
 * miscellaneous globals
 */

static FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
static FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
static unsigned align_reservoir_samples = 0; /* 0 .. 587 */
Josh Coalson's avatar
Josh Coalson committed
263

264

Josh Coalson's avatar
Josh Coalson committed
265
266
int main(int argc, char *argv[])
{
267
268
	int retval = 0;

Josh Coalson's avatar
Josh Coalson committed
269
270
271
272
273
274
275
276
277
	setlocale(LC_ALL, "");
	if(!init_options()) {
		fprintf(stderr, "ERROR: allocating memory\n");
		retval = 1;
	}
	else {
		if((retval = parse_options(argc, argv)) == 0)
			retval = do_it();
	}
278
279
280
281
282
283
284
285
286
287

	free_options();

	return retval;
}

int do_it()
{
	int retval = 0;

Josh Coalson's avatar
Josh Coalson committed
288
289
290
291
292
	if(option_values.show_version) {
		show_version();
		return 0;
	}
	else if(option_values.show_explain) {
293
294
295
296
297
		show_explain();
		return 0;
	}
	else if(option_values.show_help) {
		show_help();
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
		return 0;
	}
	else {
		if(option_values.num_files == 0) {
			short_usage();
			return 0;
		}

		/*
		 * tweak options; validate the values
		 */
		if(!option_values.mode_decode) {
			if(option_values.blocksize < 0) {
				if(option_values.max_lpc_order == 0)
					option_values.blocksize = 1152;
				else
					option_values.blocksize = 4608;
315
			}
316
317
318
319
320
321
322
323
324
325
326
327
328
			if(option_values.max_residual_partition_order < 0) {
				if(option_values.blocksize <= 1152)
					option_values.max_residual_partition_order = 2;
				else if(option_values.blocksize <= 2304)
					option_values.max_residual_partition_order = 3;
				else if(option_values.blocksize <= 4608)
					option_values.max_residual_partition_order = 3;
				else
					option_values.max_residual_partition_order = 4;
				option_values.min_residual_partition_order = option_values.max_residual_partition_order;
			}
			if(option_values.rice_parameter_search_dist < 0) {
				option_values.rice_parameter_search_dist = 0;
329
			}
Josh Coalson's avatar
Josh Coalson committed
330
331
		}
		else {
332
			if(option_values.test_only) {
333
				if(0 != option_values.skip_specification)
334
					return usage_error("ERROR: --skip is not allowed in test mode\n");
335
336
				if(0 != option_values.until_specification)
					return usage_error("ERROR: --until is not allowed in test mode\n");
337
			}
Josh Coalson's avatar
Josh Coalson committed
338
339
		}

340
341
342
343
344
		FLAC__ASSERT(option_values.blocksize >= 0 || option_values.mode_decode);

		if(option_values.format_channels >= 0) {
			if(option_values.format_channels == 0 || (unsigned)option_values.format_channels > FLAC__MAX_CHANNELS)
				return usage_error("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", option_values.format_channels, FLAC__MAX_CHANNELS);
345
		}
346
347
348
		if(option_values.format_bps >= 0) {
			if(option_values.format_bps != 8 && option_values.format_bps != 16 && option_values.format_bps != 24)
				return usage_error("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", option_values.format_bps);
Josh Coalson's avatar
Josh Coalson committed
349
		}
350
351
352
353
		if(option_values.format_sample_rate >= 0) {
			if(!FLAC__format_sample_rate_is_valid(option_values.format_sample_rate))
				return usage_error("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", option_values.format_sample_rate, FLAC__MAX_SAMPLE_RATE);
		}
354
355
		if(option_values.force_raw_format && option_values.force_aiff_format)
			return usage_error("ERROR: only one of --force-raw-format and --force-aiff-format allowed\n");
356
357
358
359
360
361
362
363
364
365
366
367
368
369
		if(option_values.mode_decode) {
			if(!option_values.force_raw_format) {
				if(option_values.format_is_big_endian >= 0)
					return usage_error("ERROR: --endian only allowed with --force-raw-format\n");
				if(option_values.format_is_unsigned_samples >= 0)
					return usage_error("ERROR: --sign only allowed with --force-raw-format\n");
			}
			if(option_values.format_channels >= 0)
				return usage_error("ERROR: --channels not allowed with --decode\n");
			if(option_values.format_bps >= 0)
				return usage_error("ERROR: --bps not allowed with --decode\n");
			if(option_values.format_sample_rate >= 0)
				return usage_error("ERROR: --sample-rate not allowed with --decode\n");
		}
370
371
372
373
		if(!option_values.mode_decode && ((unsigned)option_values.blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)option_values.blocksize > FLAC__MAX_BLOCK_SIZE)) {
			return usage_error("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)option_values.blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
		}
		if(option_values.qlp_coeff_precision > 0 && option_values.qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
374
			return usage_error("ERROR: invalid value '%u' for qlp coeff precision, must be 0 or >= %u\n", option_values.qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
Josh Coalson's avatar
Josh Coalson committed
375
376
		}

377
378
379
		if(option_values.sector_align) {
			if(option_values.mode_decode)
				return usage_error("ERROR: --sector-align only allowed for encoding\n");
380
			if(0 != option_values.skip_specification)
381
				return usage_error("ERROR: --sector-align not allowed with --skip\n");
382
383
			if(0 != option_values.until_specification)
				return usage_error("ERROR: --sector-align not allowed with --until\n");
384
			if(option_values.format_channels >= 0 && option_values.format_channels != 2)
385
				return usage_error("ERROR: --sector-align can only be done with stereo input\n");
386
			if(option_values.format_bps >= 0 && option_values.format_bps != 16)
387
				return usage_error("ERROR: --sector-align can only be done with 16-bit samples\n");
388
			if(option_values.format_sample_rate >= 0 && option_values.format_sample_rate != 44100)
389
390
				return usage_error("ERROR: --sector-align can only be done with a sample rate of 44100\n");
		}
391
		if(option_values.replay_gain) {
392
393
			if(option_values.force_to_stdout)
				return usage_error("ERROR: --replay-gain not allowed with -c/--stdout\n");
394
395
396
397
			if(option_values.mode_decode)
				return usage_error("ERROR: --replay-gain only allowed for encoding\n");
			if(option_values.format_channels > 2)
				return usage_error("ERROR: --replay-gain can only be done with mono/stereo input\n");
Josh Coalson's avatar
Josh Coalson committed
398
			if(option_values.format_sample_rate >= 0 && !grabbag__replaygain_is_valid_sample_frequency(option_values.format_sample_rate))
399
				return usage_error("ERROR: invalid sample rate used with --replay-gain\n");
400
401
402
403
404
405
406
			/*
			 * We want to reserve padding space for the ReplayGain
			 * tags that we will set later, to avoid rewriting the
			 * whole file.
			 */
			if(option_values.padding < 0) {
				fprintf(stderr, "NOTE: --replay-gain may leave a small PADDING block even with --no-padding\n");
Josh Coalson's avatar
Josh Coalson committed
407
				option_values.padding = GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED;
408
409
			}
			else {
Josh Coalson's avatar
Josh Coalson committed
410
				option_values.padding += GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED;
411
			}
412
		}
413
		if(option_values.num_files > 1 && option_values.cmdline_forced_outfilename) {
414
			return usage_error("ERROR: -o/--output-name cannot be used with multiple files\n");
415
416
		}
		if(option_values.cmdline_forced_outfilename && option_values.output_prefix) {
417
			return usage_error("ERROR: --output-prefix conflicts with -o/--output-name\n");
418
		}
419
420
421
		if(!option_values.mode_decode && 0 != option_values.cuesheet_filename && option_values.num_files > 1) {
			return usage_error("ERROR: --cuesheet cannot be used when encoding multiple files\n");
		}
Josh Coalson's avatar
Josh Coalson committed
422
	}
423
	if(option_values.verbose) {
424
		fprintf(stderr, "\n");
Josh Coalson's avatar
Josh Coalson committed
425
		fprintf(stderr, "flac %s, Copyright (C) 2000,2001,2002,2003,2004 Josh Coalson\n", FLAC__VERSION_STRING);
426
427
		fprintf(stderr, "flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are\n");
		fprintf(stderr, "welcome to redistribute it under certain conditions.  Type `flac' for details.\n\n");
Josh Coalson's avatar
Josh Coalson committed
428

429
		if(!option_values.mode_decode) {
430
			char padopt[16];
431
			if(option_values.padding < 0)
432
433
				strcpy(padopt, "-");
			else
434
				sprintf(padopt, " %d", option_values.padding);
435
			fprintf(stderr,
436
				"options:%s%s%s%s -P%s -b %u%s -l %u%s%s%s -q %u -r %u,%u%s\n",
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
				option_values.delete_input?" --delete-input-file":"",
				option_values.sector_align?" --sector-align":"",
				option_values.use_ogg?" --ogg":"",
				option_values.lax?" --lax":"",
				padopt,
				(unsigned)option_values.blocksize,
				option_values.loose_mid_side?" -M":option_values.do_mid_side?" -m":"",
				option_values.max_lpc_order,
				option_values.do_exhaustive_model_search?" -e":"",
				option_values.do_escape_coding?" -E":"",
				option_values.do_qlp_coeff_prec_search?" -p":"",
				option_values.qlp_coeff_precision,
				(unsigned)option_values.min_residual_partition_order,
				(unsigned)option_values.max_residual_partition_order,
				option_values.verify? " -V":""
Josh Coalson's avatar
Josh Coalson committed
452
453
454
455
			);
		}
	}

456
	if(option_values.mode_decode) {
Josh Coalson's avatar
Josh Coalson committed
457
		FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
458

459
		if(option_values.num_files == 0) {
460
			retval = decode_file("-");
461
		}
462
		else {
463
464
465
			unsigned i;
			if(option_values.num_files > 1)
				option_values.cmdline_forced_outfilename = 0;
466
			for(i = 0, retval = 0; i < option_values.num_files; i++) {
467
				if(0 == strcmp(option_values.filenames[i], "-") && !first)
468
					continue;
469
				retval |= decode_file(option_values.filenames[i]);
470
471
472
473
474
				first = false;
			}
		}
	}
	else { /* encode */
Josh Coalson's avatar
Josh Coalson committed
475
		FLAC__bool first = true;
476

477
		if(option_values.num_files == 0) {
478
			retval = encode_file("-", first, true);
479
		}
480
		else {
481
482
483
			unsigned i;
			if(option_values.num_files > 1)
				option_values.cmdline_forced_outfilename = 0;
484
			for(i = 0, retval = 0; i < option_values.num_files; i++) {
485
				if(0 == strcmp(option_values.filenames[i], "-") && !first)
486
					continue;
487
				retval |= encode_file(option_values.filenames[i], first, i == (option_values.num_files-1));
488
489
				first = false;
			}
490
491
			if(option_values.replay_gain && retval == 0) {
				float album_gain, album_peak;
Josh Coalson's avatar
Josh Coalson committed
492
				grabbag__replaygain_get_album(&album_gain, &album_peak);
493
494
				for(i = 0; i < option_values.num_files; i++) {
					const char *error, *outfilename = get_encoded_outfilename(option_values.filenames[i]);
495
496
497
498
					if(0 == outfilename) {
						fprintf(stderr, "ERROR: filename too long: %s", option_values.filenames[i]);
						return 1;
					}
Josh Coalson's avatar
Josh Coalson committed
499
					if(0 == strcmp(option_values.filenames[i], "-")) {
500
						FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
501
502
503
504
						/* double protection */
						fprintf(stderr, "internal error\n");
						return 2;
					}
Josh Coalson's avatar
Josh Coalson committed
505
					if(0 != (error = grabbag__replaygain_store_to_file_album(outfilename, album_gain, album_peak, /*preserve_modtime=*/true))) {
506
507
508
509
510
						fprintf(stderr, "%s: ERROR writing ReplayGain album tags\n", outfilename);
						retval = 1;
					}
				}
			}
511
512
513
514
		}
	}

	return retval;
Josh Coalson's avatar
Josh Coalson committed
515
516
}

Josh Coalson's avatar
Josh Coalson committed
517
FLAC__bool init_options()
Josh Coalson's avatar
Josh Coalson committed
518
{
519
520
	option_values.show_help = false;
	option_values.show_explain = false;
521
522
523
524
	option_values.mode_decode = false;
	option_values.verify = false;
	option_values.verbose = true;
	option_values.continue_through_decode_errors = false;
525
526
527
528
529
	option_values.replaygain_synthesis_spec.apply = false;
	option_values.replaygain_synthesis_spec.use_album_gain = true;
	option_values.replaygain_synthesis_spec.limiter = RGSS_LIMIT__HARD;
	option_values.replaygain_synthesis_spec.noise_shaping = NOISE_SHAPING_LOW;
	option_values.replaygain_synthesis_spec.preamp = 0.0;
530
531
532
533
	option_values.lax = false;
	option_values.test_only = false;
	option_values.analyze = false;
	option_values.use_ogg = false;
534
535
	option_values.has_serial_number = false;
	option_values.serial_number = 0;
536
537
538
539
540
541
	option_values.do_mid_side = true;
	option_values.loose_mid_side = false;
	option_values.do_exhaustive_model_search = false;
	option_values.do_escape_coding = false;
	option_values.do_qlp_coeff_prec_search = false;
	option_values.force_to_stdout = false;
542
	option_values.force_aiff_format = false;
543
544
	option_values.force_raw_format = false;
	option_values.delete_input = false;
545
	option_values.replay_gain = false;
546
547
548
549
550
	option_values.sector_align = false;
	option_values.cmdline_forced_outfilename = 0;
	option_values.output_prefix = 0;
	option_values.aopts.do_residual_text = false;
	option_values.aopts.do_residual_gnuplot = false;
551
	option_values.padding = 4096;
552
553
	option_values.max_lpc_order = 8;
	option_values.qlp_coeff_precision = 0;
554
	option_values.skip_specification = 0;
555
	option_values.until_specification = 0;
556
	option_values.format_is_big_endian = -1;
557
	option_values.format_is_unsigned_samples = -1;
558
559
560
561
562
563
564
565
566
	option_values.format_channels = -1;
	option_values.format_bps = -1;
	option_values.format_sample_rate = -1;
	option_values.blocksize = -1;
	option_values.min_residual_partition_order = -1;
	option_values.max_residual_partition_order = -1;
	option_values.rice_parameter_search_dist = -1;
	option_values.requested_seek_points[0] = '\0';
	option_values.num_requested_seek_points = -1;
567
568
	option_values.cuesheet_filename = 0;
	option_values.cued_seekpoints = true;
569
570
571

	option_values.num_files = 0;
	option_values.filenames = 0;
Josh Coalson's avatar
Josh Coalson committed
572
573
574
575

	if(0 == (option_values.vorbis_comment = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)))
		return false;

576
577
578
579
	option_values.debug.disable_constant_subframes = false;
	option_values.debug.disable_fixed_subframes = false;
	option_values.debug.disable_verbatim_subframes = false;

Josh Coalson's avatar
Josh Coalson committed
580
	return true;
581
582
}

583
int parse_options(int argc, char *argv[])
584
{
Josh Coalson's avatar
Josh Coalson committed
585
586
	int short_option;
	int option_index = 1;
587
	FLAC__bool had_error = false;
Josh Coalson's avatar
Josh Coalson committed
588
	const char *short_opts = "0123456789ab:cdeFhHl:mMo:pP:q:r:sS:tT:vV";
589

Josh Coalson's avatar
Josh Coalson committed
590
	while ((short_option = share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
Josh Coalson's avatar
Josh Coalson committed
591
592
		switch (short_option) {
			case 0: /* long option with no equivalent short option */
Josh Coalson's avatar
Josh Coalson committed
593
				had_error |= (parse_option(short_option, long_options_[option_index].name, share__optarg) != 0);
Josh Coalson's avatar
Josh Coalson committed
594
				break;
595
596
			case '?':
			case ':':
Josh Coalson's avatar
Josh Coalson committed
597
598
599
				had_error = true;
				break;
			default: /* short option */
Josh Coalson's avatar
Josh Coalson committed
600
				had_error |= (parse_option(short_option, 0, share__optarg) != 0);
Josh Coalson's avatar
Josh Coalson committed
601
602
603
				break;
		}
	}
604

605
606
607
	if(had_error) {
		return 1;
	}
608

Josh Coalson's avatar
Josh Coalson committed
609
	FLAC__ASSERT(share__optind <= argc);
610

Josh Coalson's avatar
Josh Coalson committed
611
	option_values.num_files = argc - share__optind;
612

613
614
615
616
	if(option_values.num_files > 0) {
		unsigned i = 0;
		if(0 == (option_values.filenames = malloc(sizeof(char *) * option_values.num_files)))
			die("out of memory allocating space for file names list");
Josh Coalson's avatar
Josh Coalson committed
617
618
		while(share__optind < argc)
			option_values.filenames[i++] = local_strdup(argv[share__optind++]);
619
	}
620
621
622
623
624
625
626
627
628
629

	return 0;
}

int parse_option(int short_option, const char *long_option, const char *option_argument)
{
	char *p;

	if(short_option == 0) {
		FLAC__ASSERT(0 != long_option);
Josh Coalson's avatar
Josh Coalson committed
630
		if(0 == strcmp(long_option, "delete-input-file")) {
631
632
			option_values.delete_input = true;
		}
Josh Coalson's avatar
Josh Coalson committed
633
		else if(0 == strcmp(long_option, "output-prefix")) {
634
635
636
			FLAC__ASSERT(0 != option_argument);
			option_values.output_prefix = option_argument;
		}
Josh Coalson's avatar
Josh Coalson committed
637
		else if(0 == strcmp(long_option, "skip")) {
638
			FLAC__ASSERT(0 != option_argument);
639
			option_values.skip_specification = option_argument;
640
		}
641
642
643
644
		else if(0 == strcmp(long_option, "until")) {
			FLAC__ASSERT(0 != option_argument);
			option_values.until_specification = option_argument;
		}
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
		else if(0 == strcmp(long_option, "apply-replaygain-which-is-not-lossless")) {
			option_values.replaygain_synthesis_spec.apply = true;
			if (0 != option_argument) {
				char *p;
				option_values.replaygain_synthesis_spec.limiter = RGSS_LIMIT__NONE;
				option_values.replaygain_synthesis_spec.noise_shaping = NOISE_SHAPING_NONE;
				option_values.replaygain_synthesis_spec.preamp = strtod(option_argument, &p);
				for ( ; *p; p++) {
					if (*p == 'a')
						option_values.replaygain_synthesis_spec.use_album_gain = true;
					else if (*p == 't')
						option_values.replaygain_synthesis_spec.use_album_gain = false;
					else if (*p == 'l')
						option_values.replaygain_synthesis_spec.limiter = RGSS_LIMIT__PEAK;
					else if (*p == 'L')
						option_values.replaygain_synthesis_spec.limiter = RGSS_LIMIT__HARD;
					else if (*p == 'n' && p[1] >= '0' && p[1] <= '3') {
						option_values.replaygain_synthesis_spec.noise_shaping = p[1] - '0';
						p++;
					}
					else
						return usage_error("ERROR: bad specification string \"%s\" for --%s\n", option_argument, long_option);
				}
			}
		}
670
671
672
673
674
675
676
677
678
		else if(0 == strcmp(long_option, "cuesheet")) {
			FLAC__ASSERT(0 != option_argument);
			option_values.cuesheet_filename = option_argument;
		}
		else if(0 == strcmp(long_option, "no-cued-seekpoints")) {
			option_values.cued_seekpoints = false;
		}
		else if(0 == strcmp(long_option, "super-secret-totally-impractical-compression-level")) {
			option_values.lax = true;
679
680
681
682
683
684
685
686
687
688
			option_values.do_exhaustive_model_search = true;
			option_values.do_escape_coding = true;
			option_values.do_mid_side = true;
			option_values.loose_mid_side = false;
			option_values.do_qlp_coeff_prec_search = true;
			option_values.min_residual_partition_order = 0;
			option_values.max_residual_partition_order = 16;
			option_values.rice_parameter_search_dist = 0;
			option_values.max_lpc_order = 32;
		}
689
690
691
		else if(0 == strcmp(long_option, "force-aiff-format")) {
			option_values.force_aiff_format = true;
		}
692
		else if(0 == strcmp(long_option, "force-raw-format")) {
693
694
			option_values.force_raw_format = true;
		}
Josh Coalson's avatar
Josh Coalson committed
695
		else if(0 == strcmp(long_option, "lax")) {
696
697
			option_values.lax = true;
		}
698
699
700
		else if(0 == strcmp(long_option, "replay-gain")) {
			option_values.replay_gain = true;
		}
Josh Coalson's avatar
Josh Coalson committed
701
		else if(0 == strcmp(long_option, "sector-align")) {
702
703
704
			option_values.sector_align = true;
		}
#ifdef FLAC__HAS_OGG
Josh Coalson's avatar
Josh Coalson committed
705
		else if(0 == strcmp(long_option, "ogg")) {
706
707
			option_values.use_ogg = true;
		}
708
709
710
711
		else if(0 == strcmp(long_option, "serial-number")) {
			option_values.has_serial_number = true;
			option_values.serial_number = atol(option_argument);
		}
712
#endif
Josh Coalson's avatar
Josh Coalson committed
713
		else if(0 == strcmp(long_option, "endian")) {
714
715
716
717
718
			FLAC__ASSERT(0 != option_argument);
			if(0 == strncmp(option_argument, "big", strlen(option_argument)))
				option_values.format_is_big_endian = true;
			else if(0 == strncmp(option_argument, "little", strlen(option_argument)))
				option_values.format_is_big_endian = false;
Josh Coalson's avatar
Josh Coalson committed
719
			else
720
721
				return usage_error("ERROR: argument to --endian must be \"big\" or \"little\"\n");
		}
Josh Coalson's avatar
Josh Coalson committed
722
		else if(0 == strcmp(long_option, "channels")) {
723
724
725
			FLAC__ASSERT(0 != option_argument);
			option_values.format_channels = atoi(option_argument);
		}
Josh Coalson's avatar
Josh Coalson committed
726
		else if(0 == strcmp(long_option, "bps")) {
727
728
729
			FLAC__ASSERT(0 != option_argument);
			option_values.format_bps = atoi(option_argument);
		}
Josh Coalson's avatar
Josh Coalson committed
730
		else if(0 == strcmp(long_option, "sample-rate")) {
731
732
733
			FLAC__ASSERT(0 != option_argument);
			option_values.format_sample_rate = atoi(option_argument);
		}
Josh Coalson's avatar
Josh Coalson committed
734
		else if(0 == strcmp(long_option, "sign")) {
735
736
737
738
739
			FLAC__ASSERT(0 != option_argument);
			if(0 == strncmp(option_argument, "signed", strlen(option_argument)))
				option_values.format_is_unsigned_samples = false;
			else if(0 == strncmp(option_argument, "unsigned", strlen(option_argument)))
				option_values.format_is_unsigned_samples = true;
Josh Coalson's avatar
Josh Coalson committed
740
			else
741
742
				return usage_error("ERROR: argument to --sign must be \"signed\" or \"unsigned\"\n");
		}
Josh Coalson's avatar
Josh Coalson committed
743
		else if(0 == strcmp(long_option, "residual-gnu-plot")) {
744
745
			option_values.aopts.do_residual_gnuplot = true;
		}
Josh Coalson's avatar
Josh Coalson committed
746
		else if(0 == strcmp(long_option, "residual-text")) {
747
748
749
750
751
			option_values.aopts.do_residual_text = true;
		}
		/*
		 * negatives
		 */
Josh Coalson's avatar
Josh Coalson committed
752
		else if(0 == strcmp(long_option, "no-decode-through-errors")) {
753
754
			option_values.continue_through_decode_errors = false;
		}
Josh Coalson's avatar
Josh Coalson committed
755
		else if(0 == strcmp(long_option, "no-silent")) {
756
757
			option_values.verbose = true;
		}
Josh Coalson's avatar
Josh Coalson committed
758
		else if(0 == strcmp(long_option, "no-seektable")) {
759
760
761
			option_values.num_requested_seek_points = 0;
			option_values.requested_seek_points[0] = '\0';
		}
Josh Coalson's avatar
Josh Coalson committed
762
		else if(0 == strcmp(long_option, "no-delete-input-file")) {
763
764
			option_values.delete_input = false;
		}
765
766
767
		else if(0 == strcmp(long_option, "no-replay-gain")) {
			option_values.replay_gain = false;
		}
Josh Coalson's avatar
Josh Coalson committed
768
		else if(0 == strcmp(long_option, "no-sector-align")) {
769
770
			option_values.sector_align = false;
		}
Josh Coalson's avatar
Josh Coalson committed
771
		else if(0 == strcmp(long_option, "no-lax")) {
772
773
774
			option_values.lax = false;
		}
#ifdef FLAC__HAS_OGG
Josh Coalson's avatar
Josh Coalson committed
775
		else if(0 == strcmp(long_option, "no-ogg")) {
776
777
778
			option_values.use_ogg = false;
		}
#endif
Josh Coalson's avatar
Josh Coalson committed
779
		else if(0 == strcmp(long_option, "no-exhaustive-model-search")) {
780
781
			option_values.do_exhaustive_model_search = false;
		}
Josh Coalson's avatar
Josh Coalson committed
782
		else if(0 == strcmp(long_option, "no-mid-side")) {
783
784
			option_values.do_mid_side = option_values.loose_mid_side = false;
		}
Josh Coalson's avatar
Josh Coalson committed
785
		else if(0 == strcmp(long_option, "no-adaptive-mid-side")) {
786
787
			option_values.loose_mid_side = option_values.do_mid_side = false;
		}
Josh Coalson's avatar
Josh Coalson committed
788
		else if(0 == strcmp(long_option, "no-qlp-coeff-prec-search")) {
789
790
			option_values.do_qlp_coeff_prec_search = false;
		}
Josh Coalson's avatar
Josh Coalson committed
791
		else if(0 == strcmp(long_option, "no-padding")) {
792
793
			option_values.padding = -1;
		}
Josh Coalson's avatar
Josh Coalson committed
794
		else if(0 == strcmp(long_option, "no-verify")) {
795
796
			option_values.verify = false;
		}
Josh Coalson's avatar
Josh Coalson committed
797
		else if(0 == strcmp(long_option, "no-residual-gnuplot")) {
798
799
			option_values.aopts.do_residual_gnuplot = false;
		}
Josh Coalson's avatar
Josh Coalson committed
800
		else if(0 == strcmp(long_option, "no-residual-text")) {
801
802
			option_values.aopts.do_residual_text = false;
		}
803
804
805
806
807
808
809
810
811
		else if(0 == strcmp(long_option, "disable-constant-subframes")) {
			option_values.debug.disable_constant_subframes = true;
		}
		else if(0 == strcmp(long_option, "disable-fixed-subframes")) {
			option_values.debug.disable_fixed_subframes = true;
		}
		else if(0 == strcmp(long_option, "disable-verbatim-subframes")) {
			option_values.debug.disable_verbatim_subframes = true;
		}
812
813
	}
	else {
Josh Coalson's avatar
Josh Coalson committed
814
		const char *violation;
815
		switch(short_option) {
816
817
818
			case 'h':
				option_values.show_help = true;
				break;
819
			case 'H':
820
				option_values.show_explain = true;
821
				break;
Josh Coalson's avatar
Josh Coalson committed
822
823
824
			case 'v':
				option_values.show_version = true;
				break;
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
			case 'd':
				option_values.mode_decode = true;
				break;
			case 'a':
				option_values.mode_decode = true;
				option_values.analyze = true;
				break;
			case 't':
				option_values.mode_decode = true;
				option_values.test_only = true;
				break;
			case 'c':
				option_values.force_to_stdout = true;
				break;
			case 's':
				option_values.verbose = false;
				break;
			case 'o':
				FLAC__ASSERT(0 != option_argument);
				option_values.cmdline_forced_outfilename = option_argument;
				break;
			case 'F':
				option_values.continue_through_decode_errors = true;
				break;
Josh Coalson's avatar
Josh Coalson committed
849
850
851
852
853
			case 'T':
				FLAC__ASSERT(0 != option_argument);
				if(!flac__vorbiscomment_add(option_values.vorbis_comment, option_argument, &violation))
					return usage_error("ERROR: (-T/--tag) %s\n", violation);
				break;
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
			case '0':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = false;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 0;
				break;
			case '1':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = true;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = option_values.max_residual_partition_order = 2;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 0;
				break;
			case '2':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = 0;
				option_values.max_residual_partition_order = 3;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 0;
				break;
			case '3':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = false;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 6;
				break;
			case '4':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = true;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 8;
				break;
			case '5':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = option_values.max_residual_partition_order = 3;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 8;
				break;
			case '6':
				option_values.do_exhaustive_model_search = false;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = 0;
				option_values.max_residual_partition_order = 4;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 8;
				break;
			case '7':
				option_values.do_exhaustive_model_search = true;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = 0;
				option_values.max_residual_partition_order = 6;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 8;
				break;
			case '8':
				option_values.do_exhaustive_model_search = true;
				option_values.do_escape_coding = false;
				option_values.do_mid_side = true;
				option_values.loose_mid_side = false;
				option_values.qlp_coeff_precision = 0;
				option_values.min_residual_partition_order = 0;
				option_values.max_residual_partition_order = 6;
				option_values.rice_parameter_search_dist = 0;
				option_values.max_lpc_order = 12;
				break;
			case '9':
				return usage_error("ERROR: compression level '9' is reserved\n");
			case 'V':
				option_values.verify = true;
				break;
			case 'S':
				FLAC__ASSERT(0 != option_argument);
				if(option_values.num_requested_seek_points < 0)
					option_values.num_requested_seek_points = 0;
				option_values.num_requested_seek_points++;
958
959
960
961
962
963
964
				if(strlen(option_values.requested_seek_points)+strlen(option_argument)+2 >= sizeof(option_values.requested_seek_points)) {
					return usage_error("ERROR: too many seekpoints requested\n");
				}
				else {
					strcat(option_values.requested_seek_points, option_argument);
					strcat(option_values.requested_seek_points, ";");
				}
965
966
967
968
				break;
			case 'P':
				FLAC__ASSERT(0 != option_argument);
				option_values.padding = atoi(option_argument);
Josh Coalson's avatar
Josh Coalson committed
969
				if(option_values.padding < 0)
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
					return usage_error("ERROR: argument to -P must be >= 0\n");
				break;
			case 'b':
				FLAC__ASSERT(0 != option_argument);
				option_values.blocksize = atoi(option_argument);
				break;
			case 'e':
				option_values.do_exhaustive_model_search = true;
				break;
			case 'E':
				option_values.do_escape_coding = true;
				break;
			case 'l':
				FLAC__ASSERT(0 != option_argument);
				option_values.max_lpc_order = atoi(option_argument);
				break;
			case 'm':
				option_values.do_mid_side = true;
				option_values.loose_mid_side = false;
				break;
			case 'M':
				option_values.loose_mid_side = option_values.do_mid_side = true;
				break;
			case 'p':
				option_values.do_qlp_coeff_prec_search = true;
				break;
			case 'q':
				FLAC__ASSERT(0 != option_argument);
				option_values.qlp_coeff_precision = atoi(option_argument);
				break;
			case 'r':
				FLAC__ASSERT(0 != option_argument);
				p = strchr(option_argument, ',');
				if(0 == p) {
					option_values.min_residual_partition_order = 0;
					option_values.max_residual_partition_order = atoi(option_argument);
				}
				else {
					option_values.min_residual_partition_order = atoi(option_argument);
					option_values.max_residual_partition_order = atoi(++p);
				}
				break;
			case 'R':
				FLAC__ASSERT(0 != option_argument);
				option_values.rice_parameter_search_dist = atoi(option_argument);
				break;
			default:
				FLAC__ASSERT(0);
		}
	}

Josh Coalson's avatar
Josh Coalson committed
1021
	return 0;
1022
1023
}

1024
1025
void free_options()
{
1026
1027
1028
1029
1030
1031
	unsigned i;
	if(0 != option_values.filenames) {
		for(i = 0; i < option_values.num_files; i++) {
			if(0 != option_values.filenames[i])
				free(option_values.filenames[i]);
		}
1032
		free(option_values.filenames);
1033
	}
Josh Coalson's avatar
Josh Coalson committed
1034
1035
	if(0 != option_values.vorbis_comment)
		FLAC__metadata_object_delete(option_values.vorbis_comment);
1036
1037
1038
}

int usage_error(const char *message, ...)
1039
1040
1041
{
	va_list args;

1042
	FLAC__ASSERT(0 != message);
1043

1044
	va_start(args, message);
1045

1046
	(void) vfprintf(stderr, message, args);
1047

1048
1049
1050
1051
1052
1053
1054
	va_end(args);

	printf("Type \"flac\" for a usage summary or \"flac --help\" for all options\n");

	return 1;
}

Josh Coalson's avatar
Josh Coalson committed
1055
1056
1057
1058
1059
void show_version()
{
	printf("flac %s\n", FLAC__VERSION_STRING);
}

1060
1061
1062
1063
static void usage_header()
{
	printf("===============================================================================\n");
	printf("flac - Command-line FLAC encoder/decoder version %s\n", FLAC__VERSION_STRING);
Josh Coalson's avatar
Josh Coalson committed
1064
	printf("Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson\n");
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
	printf("\n");
	printf("This program is free software; you can redistribute it and/or\n");
	printf("modify it under the terms of the GNU General Public License\n");
	printf("as published by the Free Software Foundation; either version 2\n");
	printf("of the License, or (at your option) any later version.\n");
	printf("\n");
	printf("This program is distributed in the hope that it will be useful,\n");
	printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
	printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
	printf("GNU General Public License for more details.\n");
	printf("\n");
	printf("You should have received a copy of the GNU General Public License\n");
	printf("along with this program; if not, write to the Free Software\n");
	printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
	printf("===============================================================================\n");
}

1082
1083
1084
1085
static void usage_summary()
{
	printf("Usage:\n");
	printf("\n");
Josh Coalson's avatar
Josh Coalson committed
1086
1087
1088
1089
	printf(" Encoding: flac [<general-options>] [<encoding/format-options>] [INPUTFILE [...]]\n");
	printf(" Decoding: flac -d [<general-options>] [<format-options>] [FLACFILE [...]]\n");
	printf("  Testing: flac -t [<general-options>] [FLACFILE [...]]\n");
	printf("Analyzing: flac -a [<general-options>] [<analysis-options>] [FLACFILE [...]]\n");
1090
1091
1092
	printf("\n");
}

1093
1094
void short_usage()
{
1095
	usage_header();
1096
	printf("\n");
1097
1098
	printf("This is the short help; for all options use 'flac --help'; for even more\n");
	printf("instructions use 'flac --explain'\n");
1099
1100
	printf("\n");
	printf("To encode:\n");
1101
	printf("  flac [-#] [INPUTFILE [...]]\n");
1102
1103
1104
1105
	printf("\n");
	printf("  -# is -0 (fastest compression) to -8 (highest compression); -5 is the default\n");
	printf("\n");
	printf("To decode:\n");
1106
	printf("  flac -d [INPUTFILE [...]]\n");
1107
1108
	printf("\n");
	printf("To test:\n");
1109
	printf("  flac -t [INPUTFILE [...]]\n");
1110
1111
}

1112
void show_help()
1113
1114
{
	usage_header();
1115
	usage_summary();
Josh Coalson's avatar
Josh Coalson committed
1116
	printf("general options:\n");
Josh Coalson's avatar
Josh Coalson committed
1117
	printf("  -v, --version                Show the flac version number\n");
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
	printf("  -h, --help                   Show this screen\n");
	printf("  -H, --explain                Show detailed explanation of usage and options\n");
	printf("  -d, --decode                 Decode (the default behavior is to encode)\n");
	printf("  -t, --test                   Same as -d except no decoded file is written\n");
	printf("  -a, --analyze                Same as -d except an analysis file is written\n");
	printf("  -c, --stdout                 Write output to stdout\n");
	printf("  -s, --silent                 Do not write runtime encode/decode statistics\n");
	printf("  -o, --output-name=FILENAME   Force the output file name\n");
	printf("      --output-prefix=STRING   Prepend STRING to output names\n");
	printf("      --delete-input-file      Deletes after a successful encode/decode\n");
1128
	printf("      --skip={#|mm:ss.ss}      Skip the given initial samples for each input\n");
1129
	printf("      --until={#|[+|-]mm:ss.ss}  Stop at the given sample for each input file\n");
Josh Coalson's avatar
Josh Coalson committed
1130
1131
1132
1133
#ifdef FLAC__HAS_OGG
	printf("      --ogg                    Use Ogg as transport layer\n");
	printf("      --serial-number          Serial number to use for the FLAC stream\n");
#endif
1134
	printf("analysis options:\n");
1135
1136
	printf("      --residual-text          Include residual signal in text output\n");
	printf("      --residual-gnuplot       Generate gnuplot files of residual distribution\n");
1137
	printf("decoding options:\n");
1138
	printf("  -F, --decode-through-errors  Continue decoding through stream errors\n");
1139
	printf("encoding options:\n");
1140
1141
1142
	printf("  -V, --verify                 Verify a correct encoding\n");
	printf("      --lax                    Allow encoder to generate non-Subset files\n");
	printf("      --sector-align           Align multiple files on sector boundaries\n");
1143
	printf("      --replay-gain            Calculate ReplayGain & store in Vorbis comments\n");
1144
1145
	printf("      --cuesheet=FILENAME      Import cuesheet and store in CUESHEET block\n");
	printf("  -T, --tag=FIELD=VALUE        Add a Vorbis comment; may appear multiple times\n");
Josh Coalson's avatar
Josh Coalson committed
1146
	printf("  -S, --seekpoint={#|X|#x|#s}  Add seek point(s)\n");
1147
1148
1149
1150
</