106
static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, 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, const char *infilename, const char *outfilename);
112
static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, 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);
107
113
static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
108
static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
114
static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, const char *infilename);
109
115
static FLAC__bool DecoderSession_process(DecoderSession *d);
110
116
static int DecoderSession_finish_ok(DecoderSession *d);
111
117
static int DecoderSession_finish_error(DecoderSession *d);
112
118
static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
113
119
static FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples);
120
static FLAC__bool write_riff_wave_fmt_chunk(FILE *f, FLAC__bool is_waveformatextensible, unsigned bps, unsigned channels, unsigned sample_rate, FLAC__uint32 channel_mask);
121
static FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate);
114
122
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
115
123
static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
116
124
static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
245
268
return DecoderSession_finish_ok(&decoder_session);
248
FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, 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, const char *infilename, const char *outfilename)
271
FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, 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)
250
273
#if FLAC__HAS_OGG
251
274
d->is_ogg = is_ogg;
275
d->use_first_serial_number = use_first_serial_number;
276
d->serial_number = serial_number;
279
(void)use_first_serial_number;
256
283
d->is_aiff_out = is_aiff_out;
484
522
flac__utils_printf(stderr, 2, "\r%s: %s \n", d->inbasefilename, d->test_only? "ok ":d->analysis_mode?"done ":"done");
486
524
DecoderSession_destroy(d, /*error_occurred=*/!ok);
487
if(!d->test_only && (d->is_wave_out || d->is_aiff_out) && (d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))))
488
if(!fixup_iff_headers(d))
525
if(!d->analysis_mode && !d->test_only && (d->is_wave_out || d->is_aiff_out)) {
526
if(d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))) {
527
if(!fixup_iff_headers(d))
530
if(d->foreign_metadata) {
532
if(!flac__foreign_metadata_write_to_iff(d->foreign_metadata, d->infilename, d->outfilename, d->fm_offset1, d->fm_offset2, d->fm_offset3, &error)) {
533
flac__utils_printf(stderr, 1, "ERROR updating foreign metadata from %s to %s: %s\n", d->infilename, d->outfilename, error);
490
538
return ok? 0 : 1;
565
618
decoder_session->iff_headers_need_fixup = true;
568
if(data_size >= 0xFFFFFFDC) {
569
flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file chunk\n", decoder_session->inbasefilename, fmt_desc);
623
FLAC__ASSERT(fm->format_block);
624
FLAC__ASSERT(fm->audio_block);
625
FLAC__ASSERT(fm->format_block < fm->audio_block);
626
/* calc foreign metadata size; for RIFF/AIFF we always skip the first chunk, format chunk, and sound chunk since we write our own */
627
for(i = 1; i < fm->num_blocks; i++) {
628
if(i != fm->format_block && i != fm->audio_block)
629
foreign_metadata_size += fm->blocks[i].size;
633
if(data_size + foreign_metadata_size + 60/*worst-case*/ >= 0xFFFFFFF4) {
634
flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file\n", decoder_session->inbasefilename, fmt_desc);
572
638
if(decoder_session->is_wave_out) {
573
639
if(flac__utils_fwrite("RIFF", 1, 4, f) != 4)
576
if(!write_little_endian_uint32(f, aligned_data_size+(is_waveformatextensible?60:36))) /* filesize-8 */
579
if(flac__utils_fwrite("WAVEfmt ", 1, 8, f) != 8)
582
if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
585
if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
588
if(!write_little_endian_uint16(f, (FLAC__uint16)(decoder_session->channels)))
591
if(!write_little_endian_uint32(f, decoder_session->sample_rate))
594
if(!write_little_endian_uint32(f, decoder_session->sample_rate * decoder_session->channels * ((decoder_session->bps+7) / 8)))
597
if(!write_little_endian_uint16(f, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) /* block align */
600
if(!write_little_endian_uint16(f, (FLAC__uint16)(((decoder_session->bps+7)/8)*8))) /* bits per sample */
603
if(is_waveformatextensible) {
604
if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
607
if(!write_little_endian_uint16(f, (FLAC__uint16)decoder_session->bps)) /* validBitsPerSample */
610
if(!write_little_endian_uint32(f, decoder_session->channel_mask))
613
/* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
614
if(flac__utils_fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
642
if(!write_little_endian_uint32(f, foreign_metadata_size + aligned_data_size + (is_waveformatextensible?60:36))) /* filesize-8 */
645
if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
648
decoder_session->fm_offset1 = ftello(f);
651
/* seek forward to {allocate} or {skip over already-written chunks} before "fmt " */
652
for(i = 1; i < fm->format_block; i++) {
653
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
654
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"fmt \"\n", decoder_session->inbasefilename);
660
if(!write_riff_wave_fmt_chunk(f, is_waveformatextensible, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate, decoder_session->channel_mask))
663
decoder_session->fm_offset2 = ftello(f);
666
/* seek forward to {allocate} or {skip over already-written chunks} after "fmt " but before "data" */
667
for(i = fm->format_block+1; i < fm->audio_block; i++) {
668
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
669
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"fmt \"\n", decoder_session->inbasefilename);
618
675
if(flac__utils_fwrite("data", 1, 4, f) != 4)
621
678
if(!write_little_endian_uint32(f, (FLAC__uint32)data_size)) /* data size */
681
decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
684
FLAC__uint32 ssnd_offset_size = (fm? fm->ssnd_offset_size : 0);
625
686
if(flac__utils_fwrite("FORM", 1, 4, f) != 4)
628
if(!write_big_endian_uint32(f, aligned_data_size+46)) /* filesize-8 */
631
if(flac__utils_fwrite("AIFFCOMM", 1, 8, f) != 8)
634
if(flac__utils_fwrite("\000\000\000\022", 1, 4, f) != 4) /* chunk size = 18 */
637
if(!write_big_endian_uint16(f, (FLAC__uint16)(decoder_session->channels)))
640
if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
643
if(!write_big_endian_uint16(f, (FLAC__uint16)(decoder_session->bps)))
646
if(!write_sane_extended(f, decoder_session->sample_rate))
689
if(!write_big_endian_uint32(f, foreign_metadata_size + aligned_data_size + 46 + ssnd_offset_size)) /* filesize-8 */
692
if(flac__utils_fwrite("AIFF", 1, 4, f) != 4)
695
decoder_session->fm_offset1 = ftello(f);
698
/* seek forward to {allocate} or {skip over already-written chunks} before "COMM" */
699
for(i = 1; i < fm->format_block; i++) {
700
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
701
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"COMM\"\n", decoder_session->inbasefilename);
707
if(!write_aiff_form_comm_chunk(f, samples, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate))
710
decoder_session->fm_offset2 = ftello(f);
713
/* seek forward to {allocate} or {skip over already-written chunks} after "COMM" but before "SSND" */
714
for(i = fm->format_block+1; i < fm->audio_block; i++) {
715
if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
716
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"COMM\"\n", decoder_session->inbasefilename);
649
722
if(flac__utils_fwrite("SSND", 1, 4, f) != 4)
652
if(!write_big_endian_uint32(f, (FLAC__uint32)data_size+8)) /* data size */
725
if(!write_big_endian_uint32(f, (FLAC__uint32)data_size + 8 + ssnd_offset_size)) /* data size */
655
if(!write_big_endian_uint32(f, 0/*offset*/))
728
if(!write_big_endian_uint32(f, ssnd_offset_size))
658
731
if(!write_big_endian_uint32(f, 0/*block_size*/))
734
if(ssnd_offset_size) {
735
/* seek forward to {allocate} or {skip over already-written} SSND offset */
736
if(fseeko(f, ssnd_offset_size, SEEK_CUR) < 0) {
737
flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping \"SSND\" offset\n", decoder_session->inbasefilename);
742
decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
748
FLAC__bool write_riff_wave_fmt_chunk(FILE *f, FLAC__bool is_waveformatextensible, unsigned bps, unsigned channels, unsigned sample_rate, FLAC__uint32 channel_mask)
750
if(flac__utils_fwrite("fmt ", 1, 4, f) != 4)
753
if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
756
if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
759
if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
762
if(!write_little_endian_uint32(f, sample_rate))
765
if(!write_little_endian_uint32(f, sample_rate * channels * ((bps+7) / 8)))
768
if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * ((bps+7) / 8)))) /* block align */
771
if(!write_little_endian_uint16(f, (FLAC__uint16)(((bps+7)/8)*8))) /* bits per sample */
774
if(is_waveformatextensible) {
775
if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
778
if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
781
if(!write_little_endian_uint32(f, channel_mask))
784
/* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
785
if(flac__utils_fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
792
FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate)
794
FLAC__ASSERT(samples <= 0xffffffff);
796
if(flac__utils_fwrite("COMM", 1, 4, f) != 4)
799
if(!write_big_endian_uint32(f, 18)) /* chunk size = 18 */
802
if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
805
if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
808
if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
811
if(!write_sane_extended(f, sample_rate))
904
1056
&decoder_session->replaygain.dither_context
907
else if(bps+shift == 8) {
908
if(is_unsigned_samples) {
909
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
910
for(channel = 0; channel < channels; channel++, sample++)
911
u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
914
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
915
for(channel = 0; channel < channels; channel++, sample++)
916
s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
918
bytes_to_write = sample;
1059
/* first some special code for common cases */
1060
else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 2 && bps+shift == 16) {
1061
FLAC__int16 *buf1_ = s16buffer + 1;
1063
memcpy(s16buffer, ((FLAC__byte*)(buffer[0]))+2, sizeof(FLAC__int32) * wide_samples - 2);
1065
memcpy(s16buffer, buffer[0], sizeof(FLAC__int32) * wide_samples);
1066
for(sample = 0; sample < wide_samples; sample++, buf1_+=2)
1067
*buf1_ = (FLAC__int16)buffer[1][sample];
1068
bytes_to_write = 4 * sample;
1070
else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 1 && bps+shift == 16) {
1071
FLAC__int16 *buf1_ = s16buffer;
1072
for(sample = 0; sample < wide_samples; sample++)
1073
*buf1_++ = (FLAC__int16)buffer[0][sample];
1074
bytes_to_write = 2 * sample;
1076
/* generic code for the rest */
920
1077
else if(bps+shift == 16) {
921
1078
if(is_unsigned_samples) {
922
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
923
for(channel = 0; channel < channels; channel++, sample++)
924
u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
1080
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
1081
u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
1082
u16buffer[sample++] = (FLAC__uint16)(buffer[1][wide_sample] + 0x8000);
1085
else if(channels == 1) {
1086
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1087
u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
1089
else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
1090
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1091
for(channel = 0; channel < channels; channel++, sample++)
1092
u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
927
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
928
for(channel = 0; channel < channels; channel++, sample++)
929
s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
1097
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
1098
s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
1099
s16buffer[sample++] = (FLAC__int16)(buffer[1][wide_sample]);
1102
else if(channels == 1) {
1103
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1104
s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
1106
else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
1107
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1108
for(channel = 0; channel < channels; channel++, sample++)
1109
s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
931
1112
if(is_big_endian != is_big_endian_host_) {
932
1113
unsigned char tmp;