Skip to content

Latest commit

 

History

History
2274 lines (2143 loc) · 104 KB

File metadata and controls

2274 lines (2143 loc) · 104 KB
 
Oct 12, 2017
Oct 12, 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* flac - Command-line FLAC encoder/decoder
* Copyright (C) 2000-2009 Josh Coalson
* Copyright (C) 2011-2016 Xiph.Org Foundation
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if !defined _MSC_VER && !defined __MINGW32__
/* unlink is in stdio.h in VC++ */
#include <unistd.h> /* for unlink() */
#endif
#include "FLAC/all.h"
#include "share/alloc.h"
#include "share/grabbag.h"
#include "share/compat.h"
#include "share/safe_str.h"
#include "analyze.h"
#include "decode.h"
#include "encode.h"
#include "local_string_utils.h" /* for flac__strlcat() and flac__strlcpy() */
#include "utils.h"
#include "vorbiscomment.h"
#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
static int do_it(void);
static FLAC__bool init_options(void);
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(void);
static void add_compression_setting_bool(compression_setting_type_t type, FLAC__bool value);
static void add_compression_setting_string(compression_setting_type_t type, const char *value);
Nov 2, 2019
Nov 2, 2019
65
static void add_compression_setting_uint32_t(compression_setting_type_t type, uint32_t value);
Oct 12, 2017
Oct 12, 2017
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
static int usage_error(const char *message, ...);
static void short_usage(void);
static void show_version(void);
static void show_help(void);
static void show_explain(void);
static void format_mistake(const char *infilename, FileFormat wrong, FileFormat right);
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);
static const char *get_outfilename(const char *infilename, const char *suffix);
static void die(const char *message);
static int conditional_fclose(FILE *f);
static char *local_strdup(const char *source);
/*
* share__getopt format struct; note that for long options with no
* short option equivalent we just set the 'val' field to 0.
*/
static struct share__option long_options_[] = {
/*
* general options
*/
{ "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' },
{ "totally-silent" , share__no_argument, 0, 0 },
{ "warnings-as-errors" , share__no_argument, 0, 'w' },
{ "force" , share__no_argument, 0, 'f' },
{ "delete-input-file" , share__no_argument, 0, 0 },
{ "preserve-modtime" , share__no_argument, 0, 0 },
{ "keep-foreign-metadata" , 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 },
{ "channel-map" , share__required_argument, 0, 0 }, /* undocumented */
/*
* decoding options
*/
{ "decode-through-errors", share__no_argument, 0, 'F' },
{ "cue" , share__required_argument, 0, 0 },
{ "apply-replaygain-which-is-not-lossless", share__optional_argument, 0, 0 }, /* undocumented */
/*
* encoding options
*/
{ "cuesheet" , share__required_argument, 0, 0 },
{ "no-cued-seekpoints" , share__no_argument, 0, 0 },
{ "picture" , share__required_argument, 0, 0 },
{ "tag" , share__required_argument, 0, 'T' },
{ "tag-from-file" , share__required_argument, 0, 0 },
{ "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' },
{ "verify" , share__no_argument, 0, 'V' },
{ "force-raw-format" , share__no_argument, 0, 0 },
{ "force-aiff-format" , share__no_argument, 0, 0 },
{ "force-rf64-format" , share__no_argument, 0, 0 },
{ "force-wave64-format" , share__no_argument, 0, 0 },
{ "lax" , share__no_argument, 0, 0 },
{ "replay-gain" , share__no_argument, 0, 0 },
{ "ignore-chunk-sizes" , share__no_argument, 0, 0 },
{ "sector-align" , share__no_argument, 0, 0 }, /* DEPRECATED */
{ "seekpoint" , share__required_argument, 0, 'S' },
{ "padding" , share__required_argument, 0, 'P' },
#if FLAC__HAS_OGG
{ "ogg" , share__no_argument, 0, 0 },
{ "serial-number" , share__required_argument, 0, 0 },
#endif
{ "blocksize" , share__required_argument, 0, 'b' },
{ "exhaustive-model-search" , share__no_argument, 0, 'e' },
{ "max-lpc-order" , share__required_argument, 0, 'l' },
{ "apodization" , share__required_argument, 0, 'A' },
{ "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 },
{ "input-size" , share__required_argument, 0, 0 },
{ "error-on-compression-fail" , share__no_argument, 0, 0 },
/*
* analysis options
*/
{ "residual-gnuplot", share__no_argument, 0, 0 },
{ "residual-text", share__no_argument, 0, 0 },
/*
* negatives
*/
{ "no-preserve-modtime" , share__no_argument, 0, 0 },
{ "no-decode-through-errors" , share__no_argument, 0, 0 },
{ "no-silent" , share__no_argument, 0, 0 },
{ "no-force" , share__no_argument, 0, 0 },
{ "no-seektable" , share__no_argument, 0, 0 },
{ "no-delete-input-file" , share__no_argument, 0, 0 },
{ "no-keep-foreign-metadata" , share__no_argument, 0, 0 },
{ "no-replay-gain" , share__no_argument, 0, 0 },
{ "no-ignore-chunk-sizes" , share__no_argument, 0, 0 },
{ "no-sector-align" , share__no_argument, 0, 0 }, /* DEPRECATED */
{ "no-utf8-convert" , share__no_argument, 0, 0 },
{ "no-lax" , share__no_argument, 0, 0 },
#if FLAC__HAS_OGG
{ "no-ogg" , share__no_argument, 0, 0 },
#endif
{ "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-warnings-as-errors" , share__no_argument, 0, 0 },
{ "no-residual-gnuplot" , share__no_argument, 0, 0 },
{ "no-residual-text" , share__no_argument, 0, 0 },
{ "no-error-on-compression-fail", share__no_argument, 0, 0 },
/*
* undocumented debugging options for the test suite
*/
{ "disable-constant-subframes", share__no_argument, 0, 0 },
{ "disable-fixed-subframes" , share__no_argument, 0, 0 },
{ "disable-verbatim-subframes", share__no_argument, 0, 0 },
{ "no-md5-sum" , share__no_argument, 0, 0 },
{0, 0, 0, 0}
};
/*
* global to hold command-line option values
*/
static struct {
FLAC__bool show_help;
FLAC__bool show_explain;
FLAC__bool show_version;
FLAC__bool mode_decode;
FLAC__bool verify;
FLAC__bool treat_warnings_as_errors;
FLAC__bool force_file_overwrite;
FLAC__bool continue_through_decode_errors;
replaygain_synthesis_spec_t replaygain_synthesis_spec;
FLAC__bool lax;
FLAC__bool test_only;
FLAC__bool analyze;
FLAC__bool use_ogg;
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 */
FLAC__bool force_to_stdout;
FLAC__bool force_raw_format;
FLAC__bool force_aiff_format;
FLAC__bool force_rf64_format;
FLAC__bool force_wave64_format;
FLAC__bool delete_input;
FLAC__bool preserve_modtime;
FLAC__bool keep_foreign_metadata;
FLAC__bool replay_gain;
FLAC__bool ignore_chunk_sizes;
FLAC__bool sector_align;
FLAC__bool utf8_convert; /* true by default, to convert tag strings from locale to utf-8, false if --no-utf8-convert used */
const char *cmdline_forced_outfilename;
const char *output_prefix;
analysis_options aopts;
int padding; /* -1 => no -P options were given, 0 => -P- was given, else -P value */
size_t num_compression_settings;
compression_setting_t compression_settings[64]; /* bad MAGIC NUMBER but buffer overflow is checked */
const char *skip_specification;
const char *until_specification;
const char *cue_specification;
int format_is_big_endian;
int format_is_unsigned_samples;
int format_channels;
int format_bps;
int format_sample_rate;
FLAC__off_t format_input_size;
char requested_seek_points[5000]; /* bad MAGIC NUMBER but buffer overflow is checked */
int num_requested_seek_points; /* -1 => no -S options were given, 0 => -S- was given */
const char *cuesheet_filename;
FLAC__bool cued_seekpoints;
FLAC__bool channel_map_none; /* --channel-map=none specified, eventually will expand to take actual channel map */
FLAC__bool error_on_compression_fail;
Nov 2, 2019
Nov 2, 2019
272
uint32_t num_files;
Oct 12, 2017
Oct 12, 2017
273
274
275
276
char **filenames;
FLAC__StreamMetadata *vorbis_comment;
FLAC__StreamMetadata *pictures[64];
Nov 2, 2019
Nov 2, 2019
277
uint32_t num_pictures;
Oct 12, 2017
Oct 12, 2017
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
struct {
FLAC__bool disable_constant_subframes;
FLAC__bool disable_fixed_subframes;
FLAC__bool disable_verbatim_subframes;
FLAC__bool do_md5;
} debug;
} option_values;
/*
* miscellaneous globals
*/
static FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */ /* DEPRECATED */
static FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
Nov 2, 2019
Nov 2, 2019
294
static uint32_t align_reservoir_samples = 0; /* 0 .. 587 */
Oct 12, 2017
Oct 12, 2017
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
int main(int argc, char *argv[])
{
int retval = 0;
#ifdef __EMX__
_response(&argc, &argv);
_wildcard(&argc, &argv);
#endif
#ifdef _WIN32
if (get_utf8_argv(&argc, &argv) != 0) {
fprintf(stderr, "ERROR: failed to convert command line parameters to UTF-8\n");
return 1;
}
#endif
Nov 2, 2019
Nov 2, 2019
312
srand((uint32_t)time(0));
Oct 12, 2017
Oct 12, 2017
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#ifdef _WIN32
{
const char *var;
var = getenv("LC_ALL");
if (!var)
var = getenv("LC_NUMERIC");
if (!var)
var = getenv("LANG");
if (!var || strcmp(var, "C") != 0)
setlocale(LC_ALL, "");
}
#else
setlocale(LC_ALL, "");
#endif
if(!init_options()) {
flac__utils_printf(stderr, 1, "ERROR: allocating memory\n");
retval = 1;
}
else {
if((retval = parse_options(argc, argv)) == 0)
retval = do_it();
}
free_options();
return retval;
}
int do_it(void)
{
int retval = 0;
if(option_values.show_version) {
show_version();
return 0;
}
else if(option_values.show_explain) {
show_explain();
return 0;
}
else if(option_values.show_help) {
show_help();
return 0;
}
else {
if(option_values.num_files == 0) {
if(flac__utils_verbosity_ >= 1)
short_usage();
return 0;
}
/*
* tweak options; validate the values
*/
if(!option_values.mode_decode) {
if(0 != option_values.cue_specification)
return usage_error("ERROR: --cue is not allowed in test mode\n");
}
else {
if(option_values.test_only) {
if(0 != option_values.skip_specification)
return usage_error("ERROR: --skip is not allowed in test mode\n");
if(0 != option_values.until_specification)
return usage_error("ERROR: --until is not allowed in test mode\n");
if(0 != option_values.cue_specification)
return usage_error("ERROR: --cue is not allowed in test mode\n");
if(0 != option_values.analyze)
return usage_error("ERROR: analysis mode (-a/--analyze) and test mode (-t/--test) cannot be used together\n");
}
}
if(0 != option_values.cue_specification && (0 != option_values.skip_specification || 0 != option_values.until_specification))
return usage_error("ERROR: --cue may not be combined with --skip or --until\n");
if(option_values.format_channels >= 0) {
Nov 2, 2019
Nov 2, 2019
388
if(option_values.format_channels == 0 || (uint32_t)option_values.format_channels > FLAC__MAX_CHANNELS)
Oct 12, 2017
Oct 12, 2017
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
return usage_error("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", option_values.format_channels, FLAC__MAX_CHANNELS);
}
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);
}
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);
}
if((option_values.force_raw_format?1:0) + (option_values.force_aiff_format?1:0) + (option_values.force_rf64_format?1:0) + (option_values.force_wave64_format?1:0) > 1)
return usage_error("ERROR: only one of --force-raw-format/--force-aiff-format/--force-rf64-format/--force-wave64-format allowed\n");
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");
}
if(option_values.ignore_chunk_sizes) {
if(option_values.mode_decode)
return usage_error("ERROR: --ignore-chunk-sizes only allowed for encoding\n");
if(0 != option_values.sector_align)
return usage_error("ERROR: --ignore-chunk-sizes not allowed with --sector-align\n");
if(0 != option_values.until_specification)
return usage_error("ERROR: --ignore-chunk-sizes not allowed with --until\n");
if(0 != option_values.cue_specification)
return usage_error("ERROR: --ignore-chunk-sizes not allowed with --cue\n");
if(0 != option_values.cuesheet_filename)
return usage_error("ERROR: --ignore-chunk-sizes not allowed with --cuesheet\n");
}
if(option_values.sector_align) {
if(option_values.mode_decode)
return usage_error("ERROR: --sector-align only allowed for encoding\n");
if(0 != option_values.skip_specification)
return usage_error("ERROR: --sector-align not allowed with --skip\n");
if(0 != option_values.until_specification)
return usage_error("ERROR: --sector-align not allowed with --until\n");
if(0 != option_values.cue_specification)
return usage_error("ERROR: --sector-align not allowed with --cue\n");
if(option_values.format_channels >= 0 && option_values.format_channels != 2)
return usage_error("ERROR: --sector-align can only be done with stereo input\n");
if(option_values.format_bps >= 0 && option_values.format_bps != 16)
return usage_error("ERROR: --sector-align can only be done with 16-bit samples\n");
if(option_values.format_sample_rate >= 0 && option_values.format_sample_rate != 44100)
return usage_error("ERROR: --sector-align can only be done with a sample rate of 44100\n");
}
if(option_values.replay_gain) {
if(option_values.force_to_stdout)
return usage_error("ERROR: --replay-gain not allowed with -c/--stdout\n");
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");
if(option_values.format_sample_rate >= 0 && !grabbag__replaygain_is_valid_sample_frequency(option_values.format_sample_rate))
return usage_error("ERROR: invalid sample rate used with --replay-gain\n");
if(
(option_values.padding >= 0 && option_values.padding < (int)GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED) ||
(option_values.padding < 0 && FLAC_ENCODE__DEFAULT_PADDING < (int)GRABBAG__REPLAYGAIN_MAX_TAG_SPACE_REQUIRED)
) {
flac__utils_printf(stderr, 1, "NOTE: --replay-gain may leave a small PADDING block even with --no-padding\n");
}
}
if(option_values.num_files > 1 && option_values.cmdline_forced_outfilename) {
return usage_error("ERROR: -o/--output-name cannot be used with multiple files\n");
}
if(option_values.cmdline_forced_outfilename && option_values.output_prefix) {
return usage_error("ERROR: --output-prefix conflicts with -o/--output-name\n");
}
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");
}
if(option_values.keep_foreign_metadata) {
/* we're not going to try and support the re-creation of broken WAVE files */
if(option_values.ignore_chunk_sizes)
return usage_error("ERROR: using --keep-foreign-metadata cannot be used with --ignore-chunk-sizes\n");
if(option_values.test_only)
return usage_error("ERROR: --keep-foreign-metadata is not allowed in test mode\n");
if(option_values.analyze)
return usage_error("ERROR: --keep-foreign-metadata is not allowed in analyis mode\n");
flac__utils_printf(stderr, 1, "NOTE: --keep-foreign-metadata is a new feature; make sure to test the output file before deleting the original.\n");
}
}
flac__utils_printf(stderr, 2, "\n");
flac__utils_printf(stderr, 2, "flac %s\n", FLAC__VERSION_STRING);
flac__utils_printf(stderr, 2, "Copyright (C) 2000-2009 Josh Coalson, 2011-2016 Xiph.Org Foundation\n");
flac__utils_printf(stderr, 2, "flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n");
flac__utils_printf(stderr, 2, "welcome to redistribute it under certain conditions. Type `flac' for details.\n\n");
if(option_values.mode_decode) {
FLAC__bool first = true;
if(option_values.num_files == 0) {
retval = decode_file("-");
}
else {
Nov 2, 2019
Nov 2, 2019
494
uint32_t i;
Oct 12, 2017
Oct 12, 2017
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
if(option_values.num_files > 1)
option_values.cmdline_forced_outfilename = 0;
for(i = 0, retval = 0; i < option_values.num_files; i++) {
if(0 == strcmp(option_values.filenames[i], "-") && !first)
continue;
retval |= decode_file(option_values.filenames[i]);
first = false;
}
}
}
else { /* encode */
FLAC__bool first = true;
if(option_values.ignore_chunk_sizes)
flac__utils_printf(stderr, 1, "INFO: Make sure you know what you're doing when using --ignore-chunk-sizes.\n Improper use can cause flac to encode non-audio data as audio.\n");
if(option_values.num_files == 0) {
retval = encode_file("-", first, true);
}
else {
Nov 2, 2019
Nov 2, 2019
515
uint32_t i;
Oct 12, 2017
Oct 12, 2017
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
if(option_values.num_files > 1)
option_values.cmdline_forced_outfilename = 0;
for(i = 0, retval = 0; i < option_values.num_files; i++) {
if(0 == strcmp(option_values.filenames[i], "-") && !first)
continue;
retval |= encode_file(option_values.filenames[i], first, i == (option_values.num_files-1));
first = false;
}
if(option_values.replay_gain && retval == 0) {
float album_gain, album_peak;
grabbag__replaygain_get_album(&album_gain, &album_peak);
for(i = 0; i < option_values.num_files; i++) {
const char *error, *outfilename = get_encoded_outfilename(option_values.filenames[i]);
if(0 == outfilename) {
flac__utils_printf(stderr, 1, "ERROR: filename too long: %s", option_values.filenames[i]);
return 1;
}
if(0 != (error = grabbag__replaygain_store_to_file_album(outfilename, album_gain, album_peak, option_values.preserve_modtime))) {
flac__utils_printf(stderr, 1, "%s: ERROR writing ReplayGain album tags (%s)\n", outfilename, error);
retval = 1;
}
}
}
}
}
return retval;
}
FLAC__bool init_options(void)
{
option_values.show_help = false;
option_values.show_explain = false;
option_values.mode_decode = false;
option_values.verify = false;
option_values.treat_warnings_as_errors = false;
option_values.force_file_overwrite = false;
option_values.continue_through_decode_errors = false;
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;
option_values.lax = false;
option_values.test_only = false;
option_values.analyze = false;
option_values.use_ogg = false;
option_values.has_serial_number = false;
option_values.serial_number = 0;
option_values.force_to_stdout = false;
option_values.force_raw_format = false;
option_values.force_aiff_format = false;
option_values.force_rf64_format = false;
option_values.force_wave64_format = false;
option_values.delete_input = false;
option_values.preserve_modtime = true;
option_values.keep_foreign_metadata = false;
option_values.replay_gain = false;
option_values.ignore_chunk_sizes = false;
option_values.sector_align = false;
option_values.utf8_convert = true;
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;
option_values.padding = -1;
option_values.num_compression_settings = 1;
option_values.compression_settings[0].type = CST_COMPRESSION_LEVEL;
option_values.compression_settings[0].value.t_unsigned = 5;
option_values.skip_specification = 0;
option_values.until_specification = 0;
option_values.cue_specification = 0;
option_values.format_is_big_endian = -1;
option_values.format_is_unsigned_samples = -1;
option_values.format_channels = -1;
option_values.format_bps = -1;
option_values.format_sample_rate = -1;
option_values.format_input_size = (FLAC__off_t)(-1);
option_values.requested_seek_points[0] = '\0';
option_values.num_requested_seek_points = -1;
option_values.cuesheet_filename = 0;
option_values.cued_seekpoints = true;
option_values.channel_map_none = false;
option_values.error_on_compression_fail = false;
option_values.num_files = 0;
option_values.filenames = 0;
if(0 == (option_values.vorbis_comment = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)))
return false;
option_values.num_pictures = 0;
option_values.debug.disable_constant_subframes = false;
option_values.debug.disable_fixed_subframes = false;
option_values.debug.disable_verbatim_subframes = false;
option_values.debug.do_md5 = true;
return true;
}
int parse_options(int argc, char *argv[])
{
int short_option;
int option_index = 1;
FLAC__bool had_error = false;
const char *short_opts = "0123456789aA:b:cdefFhHl:mMo:pP:q:r:sS:tT:vVw";
while ((short_option = share__getopt_long(argc, argv, short_opts, long_options_, &option_index)) != -1) {
switch (short_option) {
case 0: /* long option with no equivalent short option */
had_error |= (parse_option(short_option, long_options_[option_index].name, share__optarg) != 0);
break;
case '?':
case ':':
had_error = true;
break;
default: /* short option */
had_error |= (parse_option(short_option, 0, share__optarg) != 0);
break;
}
}
if(had_error) {
return 1;
}
FLAC__ASSERT(share__optind <= argc);
option_values.num_files = argc - share__optind;
if(option_values.num_files > 0) {
Nov 2, 2019
Nov 2, 2019
647
uint32_t i = 0;
Oct 12, 2017
Oct 12, 2017
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
if(0 == (option_values.filenames = malloc(sizeof(char*) * option_values.num_files)))
die("out of memory allocating space for file names list");
while(share__optind < argc)
option_values.filenames[i++] = local_strdup(argv[share__optind++]);
}
return 0;
}
int parse_option(int short_option, const char *long_option, const char *option_argument)
{
const char *violation;
if(short_option == 0) {
FLAC__ASSERT(0 != long_option);
if(0 == strcmp(long_option, "totally-silent")) {
flac__utils_verbosity_ = 0;
}
else if(0 == strcmp(long_option, "delete-input-file")) {
option_values.delete_input = true;
}
else if(0 == strcmp(long_option, "preserve-modtime")) {
option_values.preserve_modtime = true;
}
else if(0 == strcmp(long_option, "keep-foreign-metadata")) {
option_values.keep_foreign_metadata = true;
}
else if(0 == strcmp(long_option, "output-prefix")) {
FLAC__ASSERT(0 != option_argument);
option_values.output_prefix = option_argument;
}
else if(0 == strcmp(long_option, "skip")) {
FLAC__ASSERT(0 != option_argument);
option_values.skip_specification = option_argument;
}
else if(0 == strcmp(long_option, "until")) {
FLAC__ASSERT(0 != option_argument);
option_values.until_specification = option_argument;
}
else if(0 == strcmp(long_option, "input-size")) {
FLAC__ASSERT(0 != option_argument);
{
char *end;
FLAC__int64 ix;
ix = strtoll(option_argument, &end, 10);
if(0 == strlen(option_argument) || *end)
return usage_error("ERROR: --%s must be a number\n", long_option);
option_values.format_input_size = (FLAC__off_t)ix;
if(option_values.format_input_size != ix) /* check if FLAC__off_t is smaller than long long */
return usage_error("ERROR: --%s too large; this build of flac does not support filesizes over 2GB\n", long_option);
if(option_values.format_input_size <= 0)
return usage_error("ERROR: --%s must be > 0\n", long_option);
}
}
else if(0 == strcmp(long_option, "cue")) {
FLAC__ASSERT(0 != option_argument);
option_values.cue_specification = option_argument;
}
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);
}
}
}
else if(0 == strcmp(long_option, "channel-map")) {
if (0 == option_argument || strcmp(option_argument, "none"))
return usage_error("ERROR: only --channel-map=none currently supported\n");
option_values.channel_map_none = true;
}
else if(0 == strcmp(long_option, "cuesheet")) {
FLAC__ASSERT(0 != option_argument);
option_values.cuesheet_filename = option_argument;
}
else if(0 == strcmp(long_option, "picture")) {
Nov 2, 2019
Nov 2, 2019
741
const uint32_t max_pictures = sizeof(option_values.pictures)/sizeof(option_values.pictures[0]);
Oct 12, 2017
Oct 12, 2017
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
FLAC__ASSERT(0 != option_argument);
if(option_values.num_pictures >= max_pictures)
return usage_error("ERROR: too many --picture arguments, only %u allowed\n", max_pictures);
if(0 == (option_values.pictures[option_values.num_pictures] = grabbag__picture_parse_specification(option_argument, &violation)))
return usage_error("ERROR: (--picture) %s\n", violation);
option_values.num_pictures++;
}
else if(0 == strcmp(long_option, "tag-from-file")) {
FLAC__ASSERT(0 != option_argument);
if(!flac__vorbiscomment_add(option_values.vorbis_comment, option_argument, /*value_from_file=*/true, /*raw=*/!option_values.utf8_convert, &violation))
return usage_error("ERROR: (--tag-from-file) %s\n", violation);
}
else if(0 == strcmp(long_option, "no-cued-seekpoints")) {
option_values.cued_seekpoints = false;
}
else if(0 == strcmp(long_option, "force-raw-format")) {
option_values.force_raw_format = true;
}
else if(0 == strcmp(long_option, "force-aiff-format")) {
option_values.force_aiff_format = true;
}
else if(0 == strcmp(long_option, "force-rf64-format")) {
option_values.force_rf64_format = true;
}
else if(0 == strcmp(long_option, "force-wave64-format")) {
option_values.force_wave64_format = true;
}
else if(0 == strcmp(long_option, "lax")) {
option_values.lax = true;
}
else if(0 == strcmp(long_option, "replay-gain")) {
option_values.replay_gain = true;
}
else if(0 == strcmp(long_option, "ignore-chunk-sizes")) {
option_values.ignore_chunk_sizes = true;
}
else if(0 == strcmp(long_option, "sector-align")) {
flac__utils_printf(stderr, 1, "WARNING: --sector-align is DEPRECATED and may not exist in future versions of flac.\n");
flac__utils_printf(stderr, 1, " shntool provides similar functionality\n");
option_values.sector_align = true;
}
#if FLAC__HAS_OGG
else if(0 == strcmp(long_option, "ogg")) {
option_values.use_ogg = true;
}
else if(0 == strcmp(long_option, "serial-number")) {
option_values.has_serial_number = true;
option_values.serial_number = atol(option_argument);
}
#endif
else if(0 == strcmp(long_option, "endian")) {
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;
else
return usage_error("ERROR: argument to --endian must be \"big\" or \"little\"\n");
}
else if(0 == strcmp(long_option, "channels")) {
FLAC__ASSERT(0 != option_argument);
option_values.format_channels = atoi(option_argument);
}
else if(0 == strcmp(long_option, "bps")) {
FLAC__ASSERT(0 != option_argument);
option_values.format_bps = atoi(option_argument);
}
else if(0 == strcmp(long_option, "sample-rate")) {
FLAC__ASSERT(0 != option_argument);
option_values.format_sample_rate = atoi(option_argument);
}
else if(0 == strcmp(long_option, "sign")) {
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;
else
return usage_error("ERROR: argument to --sign must be \"signed\" or \"unsigned\"\n");
}
else if(0 == strcmp(long_option, "residual-gnuplot")) {
option_values.aopts.do_residual_gnuplot = true;
}
else if(0 == strcmp(long_option, "residual-text")) {
option_values.aopts.do_residual_text = true;
}
/*
* negatives
*/
else if(0 == strcmp(long_option, "no-preserve-modtime")) {
option_values.preserve_modtime = false;
}
else if(0 == strcmp(long_option, "no-decode-through-errors")) {
option_values.continue_through_decode_errors = false;
}
else if(0 == strcmp(long_option, "no-silent")) {
flac__utils_verbosity_ = 2;
}
else if(0 == strcmp(long_option, "no-force")) {
option_values.force_file_overwrite = false;
}
else if(0 == strcmp(long_option, "no-seektable")) {
option_values.num_requested_seek_points = 0;
option_values.requested_seek_points[0] = '\0';
}
else if(0 == strcmp(long_option, "no-delete-input-file")) {
option_values.delete_input = false;
}
else if(0 == strcmp(long_option, "no-keep-foreign-metadata")) {
option_values.keep_foreign_metadata = false;
}
else if(0 == strcmp(long_option, "no-replay-gain")) {
option_values.replay_gain = false;
}
else if(0 == strcmp(long_option, "no-ignore-chunk-sizes")) {
option_values.ignore_chunk_sizes = false;
}
else if(0 == strcmp(long_option, "no-sector-align")) {
option_values.sector_align = false;
}
else if(0 == strcmp(long_option, "no-utf8-convert")) {
option_values.utf8_convert = false;
}
else if(0 == strcmp(long_option, "no-lax")) {
option_values.lax = false;
}
#if FLAC__HAS_OGG
else if(0 == strcmp(long_option, "no-ogg")) {
option_values.use_ogg = false;
}
#endif
else if(0 == strcmp(long_option, "no-exhaustive-model-search")) {
add_compression_setting_bool(CST_DO_EXHAUSTIVE_MODEL_SEARCH, false);
}
else if(0 == strcmp(long_option, "no-mid-side")) {
add_compression_setting_bool(CST_DO_MID_SIDE, false);
add_compression_setting_bool(CST_LOOSE_MID_SIDE, false);
}
else if(0 == strcmp(long_option, "no-adaptive-mid-side")) {
add_compression_setting_bool(CST_DO_MID_SIDE, false);
add_compression_setting_bool(CST_LOOSE_MID_SIDE, false);
}
else if(0 == strcmp(long_option, "no-qlp-coeff-prec-search")) {
add_compression_setting_bool(CST_DO_QLP_COEFF_PREC_SEARCH, false);
}
else if(0 == strcmp(long_option, "no-padding")) {
option_values.padding = 0;
}
else if(0 == strcmp(long_option, "no-verify")) {
option_values.verify = false;
}
else if(0 == strcmp(long_option, "no-warnings-as-errors")) {
option_values.treat_warnings_as_errors = false;
}
else if(0 == strcmp(long_option, "no-residual-gnuplot")) {
option_values.aopts.do_residual_gnuplot = false;
}
else if(0 == strcmp(long_option, "no-residual-text")) {
option_values.aopts.do_residual_text = false;
}
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;
}
else if(0 == strcmp(long_option, "no-md5-sum")) {
option_values.debug.do_md5 = false;
}
else if(0 == strcmp(long_option, "no-error-on-compression-fail")) {
option_values.error_on_compression_fail = false;
}
else if(0 == strcmp(long_option, "error-on-compression-fail")) {
option_values.error_on_compression_fail = true;
}
}
else {
switch(short_option) {
case 'h':
option_values.show_help = true;
break;
case 'H':
option_values.show_explain = true;
break;
case 'v':
option_values.show_version = true;
break;
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':
flac__utils_verbosity_ = 1;
break;
case 'f':
option_values.force_file_overwrite = true;
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;
case 'T':
FLAC__ASSERT(0 != option_argument);
if(!flac__vorbiscomment_add(option_values.vorbis_comment, option_argument, /*value_from_file=*/false, /*raw=*/!option_values.utf8_convert, &violation))
return usage_error("ERROR: (-T/--tag) %s\n", violation);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
Nov 2, 2019
Nov 2, 2019
973
add_compression_setting_uint32_t(CST_COMPRESSION_LEVEL, short_option-'0');
Oct 12, 2017
Oct 12, 2017
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
break;
case '9':
return usage_error("ERROR: compression level '9' is reserved\n");
case 'V':
option_values.verify = true;
break;
case 'w':
option_values.treat_warnings_as_errors = true;
break;
case 'S':
FLAC__ASSERT(0 != option_argument);
if(0 == strcmp(option_argument, "-")) {
option_values.num_requested_seek_points = 0;
option_values.requested_seek_points[0] = '\0';
}
else {
if(option_values.num_requested_seek_points < 0)
option_values.num_requested_seek_points = 0;
option_values.num_requested_seek_points++;
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 {
size_t len = strlen(option_values.requested_seek_points);
flac_snprintf(option_values.requested_seek_points+len, sizeof(option_values.requested_seek_points) - len, "%s;", option_argument);
}
}