Skip to content

Latest commit

 

History

History
1501 lines (1325 loc) · 57.1 KB

decode.c

File metadata and controls

1501 lines (1325 loc) · 57.1 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 <errno.h>
#include <math.h> /* for floor() */
#include <stdio.h> /* for FILE etc. */
#include <string.h> /* for strcmp(), strerror() */
#include "FLAC/all.h"
#include "share/grabbag.h"
#include "share/replaygain_synthesis.h"
#include "share/compat.h"
#include "decode.h"
typedef struct {
#if FLAC__HAS_OGG
FLAC__bool is_ogg;
FLAC__bool use_first_serial_number;
long serial_number;
#endif
FileFormat format;
FLAC__bool treat_warnings_as_errors;
FLAC__bool continue_through_decode_errors;
FLAC__bool channel_map_none;
struct {
replaygain_synthesis_spec_t spec;
FLAC__bool apply; /* 'spec.apply' is just a request; this 'apply' means we actually parsed the RG tags and are ready to go */
double scale;
DitherContext dither_context;
} replaygain;
FLAC__bool test_only;
FLAC__bool analysis_mode;
analysis_options aopts;
utils__SkipUntilSpecification *skip_specification;
utils__SkipUntilSpecification *until_specification; /* a canonicalized value of 0 mean end-of-stream (i.e. --until=-0) */
utils__CueSpecification *cue_specification;
const char *inbasefilename;
const char *infilename;
const char *outfilename;
FLAC__uint64 samples_processed;
Nov 2, 2019
Nov 2, 2019
65
uint32_t frame_counter;
Oct 12, 2017
Oct 12, 2017
66
67
68
69
70
71
72
73
74
75
76
77
FLAC__bool abort_flag;
FLAC__bool aborting_due_to_until; /* true if we intentionally abort decoding prematurely because we hit the --until point */
FLAC__bool aborting_due_to_unparseable; /* true if we abort decoding because we hit an unparseable frame */
FLAC__bool error_callback_suppress_messages; /* turn on to prevent repeating messages from the error callback */
FLAC__bool iff_headers_need_fixup;
FLAC__bool is_big_endian;
FLAC__bool is_unsigned_samples;
FLAC__bool got_stream_info;
FLAC__bool has_md5sum;
FLAC__uint64 total_samples;
Nov 2, 2019
Nov 2, 2019
78
79
80
uint32_t bps;
uint32_t channels;
uint32_t sample_rate;
Oct 12, 2017
Oct 12, 2017
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
FLAC__uint32 channel_mask;
/* these are used only in analyze mode */
FLAC__uint64 decode_position;
FLAC__StreamDecoder *decoder;
FILE *fout;
foreign_metadata_t *foreign_metadata; /* NULL unless --keep-foreign-metadata requested */
FLAC__off_t fm_offset1, fm_offset2, fm_offset3;
} DecoderSession;
static FLAC__bool is_big_endian_host_;
/*
* local routines
*/
static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FileFormat format, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, foreign_metadata_t *foreign_metadata, const char *infilename, const char *outfilename);
static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, const char *infilename);
static FLAC__bool DecoderSession_process(DecoderSession *d);
static int DecoderSession_finish_ok(DecoderSession *d);
static int DecoderSession_finish_error(DecoderSession *d);
Nov 2, 2019
Nov 2, 2019
107
static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, uint32_t sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
Oct 12, 2017
Oct 12, 2017
108
static FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples);
Nov 2, 2019
Nov 2, 2019
109
110
static FLAC__bool write_riff_wave_fmt_chunk_body(FILE *f, FLAC__bool is_waveformatextensible, uint32_t bps, uint32_t channels, uint32_t sample_rate, FLAC__uint32 channel_mask);
static FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, uint32_t bps, uint32_t channels, uint32_t sample_rate);
Oct 12, 2017
Oct 12, 2017
111
112
113
114
115
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
static FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 val);
static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val);
Nov 2, 2019
Nov 2, 2019
116
static FLAC__bool write_sane_extended(FILE *f, uint32_t val);
Oct 12, 2017
Oct 12, 2017
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
static FLAC__bool fixup_iff_headers(DecoderSession *d);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_error_with_init_status(const DecoderSession *d, const char *message, FLAC__StreamDecoderInitStatus init_status);
static void print_error_with_state(const DecoderSession *d, const char *message);
static void print_stats(const DecoderSession *decoder_session);
/*
* public routines
*/
int flac__decode_file(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, decode_options_t options)
{
DecoderSession decoder_session;
FLAC__ASSERT(
options.format == FORMAT_WAVE ||
options.format == FORMAT_WAVE64 ||
options.format == FORMAT_RF64 ||
options.format == FORMAT_AIFF ||
options.format == FORMAT_AIFF_C ||
options.format == FORMAT_RAW
);
if(options.format == FORMAT_RAW) {
decoder_session.is_big_endian = options.format_options.raw.is_big_endian;
decoder_session.is_unsigned_samples = options.format_options.raw.is_unsigned_samples;
}
if(!
DecoderSession_construct(
&decoder_session,
#if FLAC__HAS_OGG
options.is_ogg,
options.use_first_serial_number,
options.serial_number,
#else
/*is_ogg=*/false,
/*use_first_serial_number=*/false,
/*serial_number=*/0,
#endif
options.format,
options.treat_warnings_as_errors,
options.continue_through_decode_errors,
options.channel_map_none,
options.replaygain_synthesis_spec,
analysis_mode,
aopts,
&options.skip_specification,
&options.until_specification,
options.has_cue_specification? &options.cue_specification : 0,
options.format == FORMAT_RAW? NULL : options.format_options.iff.foreign_metadata,
infilename,
outfilename
)
)
return 1;
stats_new_file();
if(!DecoderSession_init_decoder(&decoder_session, infilename))
return DecoderSession_finish_error(&decoder_session);
if(!DecoderSession_process(&decoder_session))
return DecoderSession_finish_error(&decoder_session);
return DecoderSession_finish_ok(&decoder_session);
}
FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FileFormat format, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, foreign_metadata_t *foreign_metadata, const char *infilename, const char *outfilename)
{
#if FLAC__HAS_OGG
d->is_ogg = is_ogg;
d->use_first_serial_number = use_first_serial_number;
d->serial_number = serial_number;
#else
(void)is_ogg;
(void)use_first_serial_number;
(void)serial_number;
#endif
d->format = format;
d->treat_warnings_as_errors = treat_warnings_as_errors;
d->continue_through_decode_errors = continue_through_decode_errors;
d->channel_map_none = channel_map_none;
d->replaygain.spec = replaygain_synthesis_spec;
d->replaygain.apply = false;
d->replaygain.scale = 0.0;
/* d->replaygain.dither_context gets initialized later once we know the sample resolution */
d->test_only = (0 == outfilename);
d->analysis_mode = analysis_mode;
d->aopts = aopts;
d->skip_specification = skip_specification;
d->until_specification = until_specification;
d->cue_specification = cue_specification;
d->inbasefilename = grabbag__file_get_basename(infilename);
d->infilename = infilename;
d->outfilename = outfilename;
d->samples_processed = 0;
d->frame_counter = 0;
d->abort_flag = false;
d->aborting_due_to_until = false;
d->aborting_due_to_unparseable = false;
d->error_callback_suppress_messages = false;
d->iff_headers_need_fixup = false;
d->total_samples = 0;
d->got_stream_info = false;
d->has_md5sum = false;
d->bps = 0;
d->channels = 0;
d->sample_rate = 0;
d->channel_mask = 0;
d->decode_position = 0;
d->decoder = 0;
d->fout = 0; /* initialized with an open file later if necessary */
d->foreign_metadata = foreign_metadata;
FLAC__ASSERT(!(d->test_only && d->analysis_mode));
if(!d->test_only) {
if(0 == strcmp(outfilename, "-")) {
d->fout = grabbag__file_get_binary_stdout();
}
else {
if(0 == (d->fout = flac_fopen(outfilename, "wb"))) {
flac__utils_printf(stderr, 1, "%s: ERROR: can't open output file %s: %s\n", d->inbasefilename, outfilename, strerror(errno));
DecoderSession_destroy(d, /*error_occurred=*/true);
return false;
}
}
}
if(analysis_mode)
flac__analyze_init(aopts);
return true;
}
void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
{
if(0 != d->fout && d->fout != stdout) {
Nov 2, 2019
Nov 2, 2019
266
#if defined _WIN32 && !defined __CYGWIN__
Oct 12, 2017
Oct 12, 2017
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
if(!error_occurred) {
FLAC__off_t written_size = ftello(d->fout);
if(written_size > 0) {
HANDLE fh = CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(fh != INVALID_HANDLE_VALUE) {
if(GetFileType(fh) == FILE_TYPE_DISK) {
LARGE_INTEGER size;
size.QuadPart = written_size;
if(SetFilePointerEx(fh, size, NULL, FILE_CURRENT)) /* correct the file size */
SetEndOfFile(fh);
}
CloseHandle(fh);
}
}
}
#endif
fclose(d->fout);
if(error_occurred)
flac_unlink(d->outfilename);
}
}
FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, const char *infilename)
{
FLAC__StreamDecoderInitStatus init_status;
FLAC__uint32 test = 1;
is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
if(!decoder_session->analysis_mode && !decoder_session->test_only && decoder_session->foreign_metadata) {
const char *error;
if(!flac__foreign_metadata_read_from_flac(decoder_session->foreign_metadata, infilename, &error)) {
flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", decoder_session->inbasefilename, error);
return false;
}
}
decoder_session->decoder = FLAC__stream_decoder_new();
if(0 == decoder_session->decoder) {
flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
return false;
}
FLAC__stream_decoder_set_md5_checking(decoder_session->decoder, true);
if (0 != decoder_session->cue_specification)
FLAC__stream_decoder_set_metadata_respond(decoder_session->decoder, FLAC__METADATA_TYPE_CUESHEET);
if (decoder_session->replaygain.spec.apply || !decoder_session->channel_map_none)
FLAC__stream_decoder_set_metadata_respond(decoder_session->decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
#if FLAC__HAS_OGG
if(decoder_session->is_ogg) {
if(!decoder_session->use_first_serial_number)
FLAC__stream_decoder_set_ogg_serial_number(decoder_session->decoder, decoder_session->serial_number);
init_status = FLAC__stream_decoder_init_ogg_file(decoder_session->decoder, strcmp(infilename, "-")? infilename : 0, write_callback, metadata_callback, error_callback, /*client_data=*/decoder_session);
}
else
#endif
{
init_status = FLAC__stream_decoder_init_file(decoder_session->decoder, strcmp(infilename, "-")? infilename : 0, write_callback, metadata_callback, error_callback, /*client_data=*/decoder_session);
}
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
print_error_with_init_status(decoder_session, "ERROR initializing decoder", init_status);
return false;
}
return true;
}
FLAC__bool DecoderSession_process(DecoderSession *d)
{
if(!FLAC__stream_decoder_process_until_end_of_metadata(d->decoder)) {
flac__utils_printf(stderr, 2, "\n");
print_error_with_state(d, "ERROR while decoding metadata");
return false;
}
if(FLAC__stream_decoder_get_state(d->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM) {
flac__utils_printf(stderr, 2, "\n");
print_error_with_state(d, "ERROR during metadata decoding");
if(!d->continue_through_decode_errors)
return false;
}
if(d->abort_flag)
return false;
/* set channel mapping */
/* currently FLAC order matches SMPTE/WAVEFORMATEXTENSIBLE order, so no reordering is necessary; see encode.c */
/* only the channel mask must be set if it was not already picked up from the WAVEFORMATEXTENSIBLE_CHANNEL_MASK tag */
if(!d->channel_map_none && d->channel_mask == 0) {
if(d->channels == 1) {
d->channel_mask = 0x0004;
}
else if(d->channels == 2) {
d->channel_mask = 0x0003;
}
else if(d->channels == 3) {
d->channel_mask = 0x0007;
}
else if(d->channels == 4) {
d->channel_mask = 0x0033;
}
else if(d->channels == 5) {
d->channel_mask = 0x0607;
}
else if(d->channels == 6) {
d->channel_mask = 0x060f;
}
else if(d->channels == 7) {
d->channel_mask = 0x070f;
}
else if(d->channels == 8) {
d->channel_mask = 0x063f;
}
}
Nov 2, 2019
Nov 2, 2019
384
#if defined _WIN32 && !defined __CYGWIN__
Oct 12, 2017
Oct 12, 2017
385
386
387
388
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
if(!d->analysis_mode && !d->test_only && d->total_samples > 0 && d->fout != stdout) {
HANDLE fh = CreateFile_utf8(d->outfilename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(fh != INVALID_HANDLE_VALUE) {
if (GetFileType(fh) == FILE_TYPE_DISK) {
LARGE_INTEGER size;
size.QuadPart = d->total_samples * d->channels * ((d->bps+7)/8);
if(d->format != FORMAT_RAW) {
size.QuadPart += 512;
if(d->foreign_metadata) {
size_t i;
for(i = d->format==FORMAT_RF64?2:1; i < d->foreign_metadata->num_blocks; i++) {
if(i != d->foreign_metadata->format_block && i != d->foreign_metadata->audio_block)
size.QuadPart += d->foreign_metadata->blocks[i].size;
}
}
}
if(SetFilePointerEx(fh, size, NULL, FILE_CURRENT)) /* tell filesystem the expected filesize to eliminate fragmentation */
SetEndOfFile(fh);
}
CloseHandle(fh);
}
}
#endif
/* write the WAVE/AIFF headers if necessary */
if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
if(!write_iff_headers(d->fout, d, d->total_samples)) {
d->abort_flag = true;
return false;
}
}
if(d->skip_specification->value.samples > 0) {
const FLAC__uint64 skip = (FLAC__uint64)d->skip_specification->value.samples;
if(!FLAC__stream_decoder_seek_absolute(d->decoder, skip)) {
print_error_with_state(d, "ERROR seeking while skipping bytes");
return false;
}
}
if(!FLAC__stream_decoder_process_until_end_of_stream(d->decoder) && !d->aborting_due_to_until) {
flac__utils_printf(stderr, 2, "\n");
print_error_with_state(d, "ERROR while decoding data");
if(!d->continue_through_decode_errors)
return false;
}
if(
(d->abort_flag && !(d->aborting_due_to_until || d->continue_through_decode_errors)) ||
(FLAC__stream_decoder_get_state(d->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM && !d->aborting_due_to_until)
) {
flac__utils_printf(stderr, 2, "\n");
print_error_with_state(d, "ERROR during decoding");
return false;
}
/* write padding bytes for alignment if necessary */
if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
const FLAC__uint64 data_size = d->total_samples * d->channels * ((d->bps+7)/8);
Nov 2, 2019
Nov 2, 2019
444
uint32_t padding;
Oct 12, 2017
Oct 12, 2017
445
if(d->format != FORMAT_WAVE64) {
Nov 2, 2019
Nov 2, 2019
446
padding = (uint32_t)(data_size & 1);
Oct 12, 2017
Oct 12, 2017
447
448
449
}
else {
/* 8-byte alignment for Wave64 */
Nov 2, 2019
Nov 2, 2019
450
padding = (8 - (uint32_t)(data_size & 7)) & 7;
Oct 12, 2017
Oct 12, 2017
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
}
for( ; padding > 0; --padding) {
if(flac__utils_fwrite("\000", 1, 1, d->fout) != 1) {
print_error_with_state(
d,
d->format == FORMAT_WAVE? "ERROR writing pad byte to WAVE data chunk" :
d->format == FORMAT_WAVE64? "ERROR writing pad bytes to WAVE64 data chunk" :
d->format == FORMAT_RF64? "ERROR writing pad byte to RF64 data chunk" :
"ERROR writing pad byte to AIFF SSND chunk"
);
return false;
}
}
}
return true;
}
int DecoderSession_finish_ok(DecoderSession *d)
{
FLAC__bool ok = true, md5_failure = false;
if(d->decoder) {
md5_failure = !FLAC__stream_decoder_finish(d->decoder) && !d->aborting_due_to_until;
print_stats(d);
FLAC__stream_decoder_delete(d->decoder);
}
if(d->analysis_mode)
flac__analyze_finish(d->aopts);
if(md5_failure) {
stats_print_name(1, d->inbasefilename);
flac__utils_printf(stderr, 1, "ERROR, MD5 signature mismatch\n");
ok = d->continue_through_decode_errors;
}
else {
if(!d->got_stream_info) {
stats_print_name(1, d->inbasefilename);
flac__utils_printf(stderr, 1, "WARNING, cannot check MD5 signature since there was no STREAMINFO\n");
ok = !d->treat_warnings_as_errors;
}
else if(!d->has_md5sum) {
stats_print_name(1, d->inbasefilename);
flac__utils_printf(stderr, 1, "WARNING, cannot check MD5 signature since it was unset in the STREAMINFO\n");
ok = !d->treat_warnings_as_errors;
}
stats_print_name(2, d->inbasefilename);
flac__utils_printf(stderr, 2, "%s \n", d->test_only? "ok ":d->analysis_mode?"done ":"done");
}
DecoderSession_destroy(d, /*error_occurred=*/!ok);
if(!d->analysis_mode && !d->test_only && d->format != FORMAT_RAW) {
if(d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))) {
if(!fixup_iff_headers(d))
return 1;
}
if(d->foreign_metadata) {
const char *error;
if(!flac__foreign_metadata_write_to_iff(d->foreign_metadata, d->infilename, d->outfilename, d->fm_offset1, d->fm_offset2, d->fm_offset3, &error)) {
flac__utils_printf(stderr, 1, "ERROR updating foreign metadata from %s to %s: %s\n", d->infilename, d->outfilename, error);
return 1;
}
}
}
return ok? 0 : 1;
}
int DecoderSession_finish_error(DecoderSession *d)
{
if(d->decoder) {
(void)FLAC__stream_decoder_finish(d->decoder);
FLAC__stream_decoder_delete(d->decoder);
}
if(d->analysis_mode)
flac__analyze_finish(d->aopts);
DecoderSession_destroy(d, /*error_occurred=*/true);
return 1;
}
Nov 2, 2019
Nov 2, 2019
528
FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, uint32_t sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
Oct 12, 2017
Oct 12, 2017
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
{
/* convert from mm:ss.sss to sample number if necessary */
flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
/* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
if(spec->is_relative && spec->value.samples == 0) {
spec->is_relative = false;
return true;
}
/* in any other case the total samples in the input must be known */
if(total_samples_in_input == 0) {
flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0\n", inbasefilename);
return false;
}
FLAC__ASSERT(spec->value_is_samples);
/* convert relative specifications to absolute */
if(spec->is_relative) {
if(spec->value.samples <= 0)
spec->value.samples += (FLAC__int64)total_samples_in_input;
else
spec->value.samples += skip;
spec->is_relative = false;
}
/* error check */
if(spec->value.samples < 0) {
flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of input\n", inbasefilename);
return false;
}
if((FLAC__uint64)spec->value.samples <= skip) {
flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
return false;
}
if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of input\n", inbasefilename);
return false;
}
return true;
}
FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples)
{
const FileFormat format = decoder_session->format;
const char *fmt_desc =
format==FORMAT_WAVE? "WAVE" :
format==FORMAT_WAVE64? "Wave64" :
format==FORMAT_RF64? "RF64" :
"AIFF";
const FLAC__bool is_waveformatextensible =
(format == FORMAT_WAVE || format == FORMAT_WAVE64 || format == FORMAT_RF64) &&
(
(decoder_session->channel_mask != 0 && decoder_session->channel_mask != 0x0004 && decoder_session->channel_mask != 0x0003) ||
Nov 2, 2019
Nov 2, 2019
585
(decoder_session->bps != 8 && decoder_session->bps != 16) ||
Oct 12, 2017
Oct 12, 2017
586
587
588
589
590
591
592
593
594
decoder_session->channels > 2
);
const FLAC__uint64 data_size = samples * decoder_session->channels * ((decoder_session->bps+7)/8);
const FLAC__uint64 aligned_data_size =
format == FORMAT_WAVE64?
(data_size+7) & (~(FLAC__uint64)7) :
(data_size+1) & (~(FLAC__uint64)1);
FLAC__uint64 iff_size;
Nov 2, 2019
Nov 2, 2019
595
uint32_t foreign_metadata_size = 0; /* size of all non-audio non-fmt/COMM foreign metadata chunks */
Oct 12, 2017
Oct 12, 2017
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
647
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
741
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
foreign_metadata_t *fm = decoder_session->foreign_metadata;
size_t i;
FLAC__ASSERT(
format == FORMAT_WAVE ||
format == FORMAT_WAVE64 ||
format == FORMAT_RF64 ||
format == FORMAT_AIFF ||
format == FORMAT_AIFF_C
);
if(samples == 0) {
if(f == stdout) {
flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
flac__utils_printf(stderr, 1, " Generated %s file will have a data chunk size of 0. Try\n", fmt_desc);
flac__utils_printf(stderr, 1, " decoding directly to a file instead.\n");
if(decoder_session->treat_warnings_as_errors)
return false;
}
else {
decoder_session->iff_headers_need_fixup = true;
}
}
if(fm) {
FLAC__ASSERT(fm->format_block);
FLAC__ASSERT(fm->audio_block);
FLAC__ASSERT(fm->format_block < fm->audio_block);
/* calc foreign metadata size; we always skip the first chunk, ds64 chunk, format chunk, and sound chunk since we write our own */
for(i = format==FORMAT_RF64?2:1; i < fm->num_blocks; i++) {
if(i != fm->format_block && i != fm->audio_block)
foreign_metadata_size += fm->blocks[i].size;
}
}
if(samples == 0)
iff_size = 0;
else if(format == FORMAT_WAVE || format == FORMAT_RF64)
/* 4 for WAVE form bytes */
/* +{36,0} for ds64 chunk */
/* +8+{40,16} for fmt chunk header and body */
/* +8 for data chunk header */
iff_size = 4 + (format==FORMAT_RF64?36:0) + 8+(is_waveformatextensible?40:16) + 8 + foreign_metadata_size + aligned_data_size;
else if(format == FORMAT_WAVE64)
/* 16+8 for RIFF GUID and size field */
/* +16 for WAVE GUID */
/* +16+8+{40,16} for fmt chunk header (GUID and size field) and body */
/* +16+8 for data chunk header (GUID and size field) */
iff_size = 16+8 + 16 + 16+8+(is_waveformatextensible?40:16) + 16+8 + foreign_metadata_size + aligned_data_size;
else /* AIFF */
iff_size = 46 + foreign_metadata_size + aligned_data_size;
if(format != FORMAT_WAVE64 && format != FORMAT_RF64 && iff_size >= 0xFFFFFFF4) {
flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file\n", decoder_session->inbasefilename, fmt_desc);
return false;
}
if(format == FORMAT_WAVE || format == FORMAT_WAVE64 || format == FORMAT_RF64) {
/* RIFF header */
switch(format) {
case FORMAT_WAVE:
if(flac__utils_fwrite("RIFF", 1, 4, f) != 4)
return false;
if(!write_little_endian_uint32(f, (FLAC__uint32)iff_size)) /* filesize-8 */
return false;
if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
return false;
break;
case FORMAT_WAVE64:
/* RIFF GUID 66666972-912E-11CF-A5D6-28DB04C10000 */
if(flac__utils_fwrite("\x72\x69\x66\x66\x2E\x91\xCF\x11\xA5\xD6\x28\xDB\x04\xC1\x00\x00", 1, 16, f) != 16)
return false;
if(!write_little_endian_uint64(f, iff_size))
return false;
/* WAVE GUID 65766177-ACF3-11D3-8CD1-00C04F8EDB8A */
if(flac__utils_fwrite("\x77\x61\x76\x65\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
return false;
break;
case FORMAT_RF64:
if(flac__utils_fwrite("RF64", 1, 4, f) != 4)
return false;
if(!write_little_endian_uint32(f, 0xffffffff))
return false;
if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
return false;
break;
default:
return false;
}
/* ds64 chunk for RF64 */
if(format == FORMAT_RF64) {
if(flac__utils_fwrite("ds64", 1, 4, f) != 4)
return false;
if(!write_little_endian_uint32(f, 28)) /* chunk size */
return false;
if(!write_little_endian_uint64(f, iff_size))
return false;
if(!write_little_endian_uint64(f, data_size))
return false;
if(!write_little_endian_uint64(f, samples)) /*@@@@@@ correct? */
return false;
if(!write_little_endian_uint32(f, 0)) /* table size */
return false;
}
decoder_session->fm_offset1 = ftello(f);
if(fm) {
/* seek forward to {allocate} or {skip over already-written chunks} before "fmt " */
for(i = format==FORMAT_RF64?2:1; i < fm->format_block; i++) {
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"fmt \"\n", decoder_session->inbasefilename);
return false;
}
}
}
if(format != FORMAT_WAVE64) {
if(flac__utils_fwrite("fmt ", 1, 4, f) != 4)
return false;
if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
return false;
}
else { /* Wave64 */
/* fmt GUID 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
if(flac__utils_fwrite("\x66\x6D\x74\x20\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
return false;
/* chunk size (+16+8 for GUID and size fields) */
if(!write_little_endian_uint64(f, 16+8+(is_waveformatextensible?40:16)))
return false;
}
if(!write_riff_wave_fmt_chunk_body(f, is_waveformatextensible, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate, decoder_session->channel_mask))
return false;
decoder_session->fm_offset2 = ftello(f);
if(fm) {
/* seek forward to {allocate} or {skip over already-written chunks} after "fmt " but before "data" */
for(i = fm->format_block+1; i < fm->audio_block; i++) {
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"fmt \"\n", decoder_session->inbasefilename);
return false;
}
}
}
if(format != FORMAT_WAVE64) {
if(flac__utils_fwrite("data", 1, 4, f) != 4)
return false;
if(!write_little_endian_uint32(f, format==FORMAT_RF64? 0xffffffff : (FLAC__uint32)data_size))
return false;
}
else { /* Wave64 */
/* data GUID 61746164-ACF3-11D3-8CD1-00C04F8EDB8A */
if(flac__utils_fwrite("\x64\x61\x74\x61\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
return false;
/* +16+8 for GUID and size fields */
if(!write_little_endian_uint64(f, 16+8 + data_size))
return false;
}
decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
}
else {
if(flac__utils_fwrite("FORM", 1, 4, f) != 4)
return false;
if(!write_big_endian_uint32(f, (FLAC__uint32)iff_size)) /* filesize-8 */
return false;
if(flac__utils_fwrite("AIFF", 1, 4, f) != 4)
return false;
decoder_session->fm_offset1 = ftello(f);
if(fm) {
/* seek forward to {allocate} or {skip over already-written chunks} before "COMM" */
for(i = 1; i < fm->format_block; i++) {
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"COMM\"\n", decoder_session->inbasefilename);
return false;
}
}
}
if(!write_aiff_form_comm_chunk(f, samples, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate))
return false;
decoder_session->fm_offset2 = ftello(f);
if(fm) {
/* seek forward to {allocate} or {skip over already-written chunks} after "COMM" but before "SSND" */
for(i = fm->format_block+1; i < fm->audio_block; i++) {
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"COMM\"\n", decoder_session->inbasefilename);
return false;
}
}
}
if(flac__utils_fwrite("SSND", 1, 4, f) != 4)
return false;
if(!write_big_endian_uint32(f, (FLAC__uint32)data_size + 8)) /* data size */
return false;
if(!write_big_endian_uint32(f, 0/*offset_size*/))
return false;
if(!write_big_endian_uint32(f, 0/*block_size*/))
return false;
decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
}
return true;
}
Nov 2, 2019
Nov 2, 2019
821
FLAC__bool write_riff_wave_fmt_chunk_body(FILE *f, FLAC__bool is_waveformatextensible, uint32_t bps, uint32_t channels, uint32_t sample_rate, FLAC__uint32 channel_mask)
Oct 12, 2017
Oct 12, 2017
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
{
if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
return false;
if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
return false;
if(!write_little_endian_uint32(f, sample_rate))
return false;
if(!write_little_endian_uint32(f, sample_rate * channels * ((bps+7) / 8)))
return false;
if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * ((bps+7) / 8)))) /* block align */
return false;
if(!write_little_endian_uint16(f, (FLAC__uint16)(((bps+7)/8)*8))) /* bits per sample */
return false;
if(is_waveformatextensible) {
if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
return false;
if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
return false;
if(!write_little_endian_uint32(f, channel_mask))
return false;
/* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
if(flac__utils_fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
return false;
}
return true;
}
Nov 2, 2019
Nov 2, 2019
859
FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, uint32_t bps, uint32_t channels, uint32_t sample_rate)
Oct 12, 2017
Oct 12, 2017
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
{
FLAC__ASSERT(samples <= 0xffffffff);
if(flac__utils_fwrite("COMM", 1, 4, f) != 4)
return false;
if(!write_big_endian_uint32(f, 18)) /* chunk size = 18 */
return false;
if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
return false;
if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
return false;
if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
return false;
if(!write_sane_extended(f, sample_rate))
return false;
return true;
}
FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
{
FLAC__byte *b = (FLAC__byte*)(&val);
if(is_big_endian_host_) {
FLAC__byte tmp;
tmp = b[1]; b[1] = b[0]; b[0] = tmp;
}
return flac__utils_fwrite(b, 1, 2, f) == 2;
}
FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
{
FLAC__byte *b = (FLAC__byte*)(&val);
if(is_big_endian_host_) {
FLAC__byte tmp;
tmp = b[3]; b[3] = b[0]; b[0] = tmp;
tmp = b[2]; b[2] = b[1]; b[1] = tmp;
}
return flac__utils_fwrite(b, 1, 4, f) == 4;
}
FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 val)
{
FLAC__byte *b = (FLAC__byte*)(&val);
if(is_big_endian_host_) {
FLAC__byte tmp;
tmp = b[7]; b[7] = b[0]; b[0] = tmp;
tmp = b[6]; b[6] = b[1]; b[1] = tmp;
tmp = b[5]; b[5] = b[2]; b[2] = tmp;
tmp = b[4]; b[4] = b[3]; b[3] = tmp;
}
return flac__utils_fwrite(b, 1, 8, f) == 8;
}
FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
{
FLAC__byte *b = (FLAC__byte*)(&val);
if(!is_big_endian_host_) {
FLAC__byte tmp;
tmp = b[1]; b[1] = b[0]; b[0] = tmp;
}
return flac__utils_fwrite(b, 1, 2, f) == 2;
}
FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
{
FLAC__byte *b = (FLAC__byte*)(&val);
if(!is_big_endian_host_) {
FLAC__byte tmp;
tmp = b[3]; b[3] = b[0]; b[0] = tmp;
tmp = b[2]; b[2] = b[1]; b[1] = tmp;
}
return flac__utils_fwrite(b, 1, 4, f) == 4;
}
Nov 2, 2019
Nov 2, 2019
939
FLAC__bool write_sane_extended(FILE *f, uint32_t val)
Oct 12, 2017
Oct 12, 2017
940
941
942
943
944
945
946
947
948
949
950
/* Write to 'f' a SANE extended representation of 'val'. Return false if
* the write succeeds; return true otherwise.
*
* SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
* of exponent, and 64 bits of significand (mantissa). Unlike most IEEE-754
* representations, it does not imply a 1 above the MSB of the significand.
*
* Preconditions:
* val!=0U
*/
{
Nov 2, 2019
Nov 2, 2019
951
uint32_t shift, exponent;
Oct 12, 2017
Oct 12, 2017
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
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
FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
for(shift= 0U; (val>>(31-shift))==0U; ++shift)
;
val<<= shift;
exponent= 63U-(shift+32U); /* add 32 for unused second word */
if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
return false;
if(!write_big_endian_uint32(f, val))
return false;
if(!write_big_endian_uint32(f, 0)) /* unused second word */
return false;
return true;
}
FLAC__bool fixup_iff_headers(DecoderSession *d)
{
const char *fmt_desc =
d->format==FORMAT_WAVE? "WAVE" :
d->format==FORMAT_WAVE64? "Wave64" :
d->format==FORMAT_RF64? "RF64" :
"AIFF";
FILE *f = flac_fopen(d->outfilename, "r+b"); /* stream is positioned at beginning of file */
if(0 == f) {
flac__utils_printf(stderr, 1, "ERROR, couldn't open file %s while fixing up %s chunk size: %s\n", d->outfilename, fmt_desc, strerror(errno));
return false;
}
if(!write_iff_headers(f, d, d->samples_processed)) {
fclose(f);
return false;
}
fclose(f);
return true;
}
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
DecoderSession *decoder_session = (DecoderSession*)client_data;
FILE *fout = decoder_session->fout;
Nov 2, 2019
Nov 2, 2019
997
998
const uint32_t bps = frame->header.bits_per_sample, channels = frame->header.channels;
const uint32_t shift = (decoder_session->format != FORMAT_RAW && (bps%8))? 8-(bps%8): 0;
Oct 12, 2017
Oct 12, 2017
999
1000
FLAC__bool is_big_endian = (
decoder_session->format == FORMAT_AIFF || decoder_session->format == FORMAT_AIFF_C ? true : (