329
if(!FLAC__stream_decoder_reset(decoder))
330
return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
332
return decoder->protected_->state;
335
FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
443
/* from here on, errors are fatal */
445
if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
446
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
447
return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
450
decoder->private_->read_callback = read_callback;
451
decoder->private_->seek_callback = seek_callback;
452
decoder->private_->tell_callback = tell_callback;
453
decoder->private_->length_callback = length_callback;
454
decoder->private_->eof_callback = eof_callback;
455
decoder->private_->write_callback = write_callback;
456
decoder->private_->metadata_callback = metadata_callback;
457
decoder->private_->error_callback = error_callback;
458
decoder->private_->client_data = client_data;
459
decoder->private_->last_frame_number = 0;
460
decoder->private_->last_block_size = 0;
461
decoder->private_->samples_decoded = 0;
462
decoder->private_->has_stream_info = false;
463
decoder->private_->cached = false;
465
decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
466
decoder->private_->is_seeking = false;
468
decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
469
if(!FLAC__stream_decoder_reset(decoder)) {
470
/* above call sets the state for us */
471
return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
474
return FLAC__STREAM_DECODER_INIT_STATUS_OK;
477
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
478
FLAC__StreamDecoder *decoder,
479
FLAC__StreamDecoderReadCallback read_callback,
480
FLAC__StreamDecoderSeekCallback seek_callback,
481
FLAC__StreamDecoderTellCallback tell_callback,
482
FLAC__StreamDecoderLengthCallback length_callback,
483
FLAC__StreamDecoderEofCallback eof_callback,
484
FLAC__StreamDecoderWriteCallback write_callback,
485
FLAC__StreamDecoderMetadataCallback metadata_callback,
486
FLAC__StreamDecoderErrorCallback error_callback,
490
return init_stream_internal_(
505
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
506
FLAC__StreamDecoder *decoder,
507
FLAC__StreamDecoderReadCallback read_callback,
508
FLAC__StreamDecoderSeekCallback seek_callback,
509
FLAC__StreamDecoderTellCallback tell_callback,
510
FLAC__StreamDecoderLengthCallback length_callback,
511
FLAC__StreamDecoderEofCallback eof_callback,
512
FLAC__StreamDecoderWriteCallback write_callback,
513
FLAC__StreamDecoderMetadataCallback metadata_callback,
514
FLAC__StreamDecoderErrorCallback error_callback,
518
return init_stream_internal_(
533
static FLAC__StreamDecoderInitStatus init_FILE_internal_(
534
FLAC__StreamDecoder *decoder,
536
FLAC__StreamDecoderWriteCallback write_callback,
537
FLAC__StreamDecoderMetadataCallback metadata_callback,
538
FLAC__StreamDecoderErrorCallback error_callback,
543
FLAC__ASSERT(0 != decoder);
544
FLAC__ASSERT(0 != file);
546
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
547
return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
549
if(0 == write_callback || 0 == error_callback)
550
return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
553
* To make sure that our file does not go unclosed after an error, we
554
* must assign the FILE pointer before any further error can occur in
558
file = get_binary_stdin_(); /* just to be safe */
560
decoder->private_->file = file;
562
return init_stream_internal_(
565
decoder->private_->file == stdin? 0: file_seek_callback_,
566
decoder->private_->file == stdin? 0: file_tell_callback_,
567
decoder->private_->file == stdin? 0: file_length_callback_,
577
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
578
FLAC__StreamDecoder *decoder,
580
FLAC__StreamDecoderWriteCallback write_callback,
581
FLAC__StreamDecoderMetadataCallback metadata_callback,
582
FLAC__StreamDecoderErrorCallback error_callback,
586
return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
589
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
590
FLAC__StreamDecoder *decoder,
592
FLAC__StreamDecoderWriteCallback write_callback,
593
FLAC__StreamDecoderMetadataCallback metadata_callback,
594
FLAC__StreamDecoderErrorCallback error_callback,
598
return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
601
static FLAC__StreamDecoderInitStatus init_file_internal_(
602
FLAC__StreamDecoder *decoder,
603
const char *filename,
604
FLAC__StreamDecoderWriteCallback write_callback,
605
FLAC__StreamDecoderMetadataCallback metadata_callback,
606
FLAC__StreamDecoderErrorCallback error_callback,
613
FLAC__ASSERT(0 != decoder);
616
* To make sure that our file does not go unclosed after an error, we
617
* have to do the same entrance checks here that are later performed
618
* in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
620
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
621
return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
623
if(0 == write_callback || 0 == error_callback)
624
return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
626
file = filename? fopen(filename, "rb") : stdin;
629
return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
631
return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
634
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
635
FLAC__StreamDecoder *decoder,
636
const char *filename,
637
FLAC__StreamDecoderWriteCallback write_callback,
638
FLAC__StreamDecoderMetadataCallback metadata_callback,
639
FLAC__StreamDecoderErrorCallback error_callback,
643
return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
646
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
647
FLAC__StreamDecoder *decoder,
648
const char *filename,
649
FLAC__StreamDecoderWriteCallback write_callback,
650
FLAC__StreamDecoderMetadataCallback metadata_callback,
651
FLAC__StreamDecoderErrorCallback error_callback,
655
return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
658
FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
660
FLAC__bool md5_failed = false;
338
663
FLAC__ASSERT(0 != decoder);
664
FLAC__ASSERT(0 != decoder->private_);
665
FLAC__ASSERT(0 != decoder->protected_);
339
667
if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
341
if(0 != decoder->private_->seek_table.data.seek_table.points) {
670
/* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
671
* always call FLAC__MD5Final()
673
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
675
if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
342
676
free(decoder->private_->seek_table.data.seek_table.points);
343
677
decoder->private_->seek_table.data.seek_table.points = 0;
344
678
decoder->private_->has_seek_table = false;
346
FLAC__bitbuffer_free(decoder->private_->input);
680
FLAC__bitreader_free(decoder->private_->input);
347
681
for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
349
683
* FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
1052
1584
FLAC__uint32 x;
1053
1585
unsigned bits, used_bits = 0;
1055
FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1587
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1057
1589
decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1058
1590
decoder->private_->stream_info.is_last = is_last;
1059
1591
decoder->private_->stream_info.length = length;
1061
1593
bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1062
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
1063
return false; /* the read_callback_ sets the state for us */
1594
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1595
return false; /* read_callback_ sets the state for us */
1064
1596
decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1065
1597
used_bits += bits;
1067
1599
bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1068
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
1069
return false; /* the read_callback_ sets the state for us */
1600
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1601
return false; /* read_callback_ sets the state for us */
1070
1602
decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1071
1603
used_bits += bits;
1073
1605
bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1074
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
1075
return false; /* the read_callback_ sets the state for us */
1606
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1607
return false; /* read_callback_ sets the state for us */
1076
1608
decoder->private_->stream_info.data.stream_info.min_framesize = x;
1077
1609
used_bits += bits;
1079
1611
bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1080
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1081
return false; /* the read_callback_ sets the state for us */
1612
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1613
return false; /* read_callback_ sets the state for us */
1082
1614
decoder->private_->stream_info.data.stream_info.max_framesize = x;
1083
1615
used_bits += bits;
1085
1617
bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1086
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1087
return false; /* the read_callback_ sets the state for us */
1618
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1619
return false; /* read_callback_ sets the state for us */
1088
1620
decoder->private_->stream_info.data.stream_info.sample_rate = x;
1089
1621
used_bits += bits;
1091
1623
bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1092
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1093
return false; /* the read_callback_ sets the state for us */
1624
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1625
return false; /* read_callback_ sets the state for us */
1094
1626
decoder->private_->stream_info.data.stream_info.channels = x+1;
1095
1627
used_bits += bits;
1097
1629
bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1098
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1099
return false; /* the read_callback_ sets the state for us */
1630
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1631
return false; /* read_callback_ sets the state for us */
1100
1632
decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1101
1633
used_bits += bits;
1103
1635
bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1104
if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
1105
return false; /* the read_callback_ sets the state for us */
1636
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
1637
return false; /* read_callback_ sets the state for us */
1106
1638
used_bits += bits;
1108
if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1109
return false; /* the read_callback_ sets the state for us */
1640
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1641
return false; /* read_callback_ sets the state for us */
1110
1642
used_bits += 16*8;
1112
1644
/* skip the rest of the block */
1113
1645
FLAC__ASSERT(used_bits % 8 == 0);
1114
1646
length -= (used_bits / 8);
1115
if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1116
return false; /* the read_callback_ sets the state for us */
1647
if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1648
return false; /* read_callback_ sets the state for us */
2132
2730
FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2134
if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
2732
if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2135
2733
FLAC__uint32 zero = 0;
2136
if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
2137
return false; /* the read_callback_ sets the state for us */
2734
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2735
return false; /* read_callback_ sets the state for us */
2138
2736
if(zero != 0) {
2139
decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
2737
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2140
2738
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2146
FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
2744
FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
2148
2746
FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2149
FLAC__StreamDecoderReadStatus status;
2151
status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
2152
if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
2750
/* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2751
!decoder->private_->is_ogg &&
2753
decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2153
2756
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2154
else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
2759
else if(*bytes > 0) {
2760
/* While seeking, it is possible for our seek to land in the
2761
* middle of audio data that looks exactly like a frame header
2762
* from a future version of an encoder. When that happens, our
2763
* error callback will get an
2764
* FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
2765
* unparseable_frame_count. But there is a remote possibility
2766
* that it is properly synced at such a "future-codec frame",
2767
* so to make sure, we wait to see many "unparseable" errors in
2768
* a row before bailing out.
2770
if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2771
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2775
const FLAC__StreamDecoderReadStatus status =
2777
decoder->private_->is_ogg?
2778
read_callback_ogg_aspect_(decoder, buffer, bytes) :
2780
decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2782
if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
2783
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2786
else if(*bytes == 0) {
2788
status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
2791
/* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2792
!decoder->private_->is_ogg &&
2794
decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2797
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2808
/* abort to avoid a deadlock */
2155
2809
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2156
return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2812
/* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
2813
* for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2814
* and at the same time hit the end of the stream (for example, seeking
2815
* to a point that is after the beginning of the last Ogg page). There
2816
* is no way to report an Ogg sync loss through the callbacks (see note
2817
* in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
2818
* So to keep the decoder from stopping at this point we gate the call
2819
* to the eof_callback and let the Ogg decoder aspect set the
2820
* end-of-stream state when it is needed.
2825
FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
2827
switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2828
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
2829
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2830
/* we don't really have a way to handle lost sync via read
2831
* callback so we'll let it pass and let the underlying
2832
* FLAC decoder catch the error
2834
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
2835
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2836
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
2837
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2838
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
2839
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
2840
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
2841
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
2842
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
2843
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2846
/* double protection */
2847
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2851
FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2853
FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2855
switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2856
case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
2857
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
2858
case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
2859
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
2860
case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
2861
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2863
/* double protection: */
2865
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2870
FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2872
if(decoder->private_->is_seeking) {
2873
FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
2874
FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
2875
FLAC__uint64 target_sample = decoder->private_->target_sample;
2877
FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2880
decoder->private_->got_a_frame = true;
2882
decoder->private_->last_frame = *frame; /* save the frame */
2883
if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
2884
unsigned delta = (unsigned)(target_sample - this_frame_sample);
2885
/* kick out of seek mode */
2886
decoder->private_->is_seeking = false;
2887
/* shift out the samples before target_sample */
2890
const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
2891
for(channel = 0; channel < frame->header.channels; channel++)
2892
newbuffer[channel] = buffer[channel] + delta;
2893
decoder->private_->last_frame.header.blocksize -= delta;
2894
decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2895
/* write the relevant samples */
2896
return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2899
/* write the relevant samples */
2900
return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2904
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2909
* If we never got STREAMINFO, turn off MD5 checking to save
2910
* cycles since we don't have a sum to compare to anyway
2912
if(!decoder->private_->has_stream_info)
2913
decoder->private_->do_md5_checking = false;
2914
if(decoder->private_->do_md5_checking) {
2915
if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2916
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2918
return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2922
void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2924
if(!decoder->private_->is_seeking)
2925
decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2926
else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
2927
decoder->private_->unparseable_frame_count++;
2930
FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
2932
FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
2933
FLAC__int64 pos = -1;
2935
unsigned approx_bytes_per_frame;
2936
FLAC__bool first_seek = true;
2937
const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
2938
const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2939
const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
2940
const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
2941
const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
2942
/* take these from the current frame in case they've changed mid-stream */
2943
unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2944
unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
2945
const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
2947
/* use values from stream info if we didn't decode a frame */
2949
channels = decoder->private_->stream_info.data.stream_info.channels;
2951
bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2953
/* we are just guessing here */
2954
if(max_framesize > 0)
2955
approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
2958
* Check if it's a known fixed-blocksize stream. Note that though
2959
* the spec doesn't allow zeroes in the STREAMINFO block, we may
2960
* never get a STREAMINFO block when decoding so the value of
2961
* min_blocksize might be zero.
2963
else if(min_blocksize == max_blocksize && min_blocksize > 0) {
2964
/* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
2965
approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
2968
approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
2971
* First, we set an upper and lower bound on where in the
2972
* stream we will search. For now we assume the worst case
2973
* scenario, which is our best guess at the beginning of
2974
* the first frame and end of the stream.
2976
lower_bound = first_frame_offset;
2977
lower_bound_sample = 0;
2978
upper_bound = stream_length;
2979
upper_bound_sample = total_samples > 0 ? total_samples : target_sample;
2980
if(upper_bound_sample == 0)
2981
upper_bound_sample = 1;
2984
* Now we refine the bounds if we have a seektable with
2985
* suitable points. Note that according to the spec they
2986
* must be ordered by ascending sample number.
2989
/* find the closest seek point <= target_sample, if it exists */
2990
for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
2991
if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number <= target_sample)
2994
if(i >= 0) { /* i.e. we found a suitable seek point... */
2995
lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
2996
lower_bound_sample = seek_table->points[i].sample_number;
2999
/* find the closest seek point > target_sample, if it exists */
3000
for(i = 0; i < (int)seek_table->num_points; i++) {
3001
if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number > target_sample)
3004
if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3005
upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3006
upper_bound_sample = seek_table->points[i].sample_number;
3010
decoder->private_->target_sample = target_sample;
3012
/* check if the bounds are still ok */
3013
if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
3014
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3017
#ifndef FLAC__INTEGER_ONLY_LIBRARY
3018
#if defined _MSC_VER || defined __MINGW32__
3019
/* with VC++ you have to spoon feed it the casting */
3020
pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame;
3022
pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
3025
/* a little less accurate: */
3026
if(upper_bound - lower_bound < 0xffffffff)
3027
pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
3028
else /* @@@ WATCHOUT, ~2TB limit */
3029
pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame;
3031
if(pos >= (FLAC__int64)upper_bound)
3032
pos = (FLAC__int64)upper_bound - 1;
3033
if(pos < (FLAC__int64)lower_bound)
3034
pos = (FLAC__int64)lower_bound;
3035
if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3036
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3039
if(!FLAC__stream_decoder_flush(decoder)) {
3040
/* above call sets the state for us */
3043
/* Now we need to get a frame. First we need to reset our
3044
* unparseable_frame_count; if we get too many unparseable
3045
* frames in a row, the read callback will return
3046
* FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3047
* FLAC__stream_decoder_process_single() to return false.
3049
decoder->private_->unparseable_frame_count = 0;
3050
if(!FLAC__stream_decoder_process_single(decoder)) {
3051
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3054
/* our write callback will change the state when it gets to the target frame */
3055
/* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */
3057
/*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
3058
if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3061
if(!decoder->private_->is_seeking) {
3064
this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3066
if (!decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
3067
if (pos == (FLAC__int64)lower_bound) {
3068
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3071
/* our last move backwards wasn't big enough, try again */
3072
approx_bytes_per_frame *= 2;
3075
/* allow one seek over upper bound, required for streams with unknown total_samples */
3078
/* make sure we are not seeking in corrupted stream */
3079
if (this_frame_sample < lower_bound_sample) {
3080
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3084
FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3086
/* we need to narrow the search */
3087
if(target_sample < this_frame_sample) {
3088
upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3089
if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3090
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3093
approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
3096
/* target_sample >= this_frame_sample + this frame's blocksize */
3098
lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3099
if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3100
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3103
approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
3111
FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3113
FLAC__uint64 left_pos = 0, right_pos = stream_length;
3114
FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3115
FLAC__uint64 this_frame_sample = 0; /* only initialized to avoid compiler warning */
3116
FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3117
FLAC__bool did_a_seek;
3118
unsigned iteration = 0;
3120
/* In the first iterations, we will calculate the target byte position
3121
* by the distance from the target sample to left_sample and
3122
* right_sample (let's call it "proportional search"). After that, we
3123
* will switch to binary search.
3125
unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
3127
/* We will switch to a linear search once our current sample is less
3128
* than this number of samples ahead of the target sample
3130
static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3132
/* If the total number of samples is unknown, use a large value, and
3133
* force binary search immediately.
3135
if(right_sample == 0) {
3136
right_sample = (FLAC__uint64)(-1);
3137
BINARY_SEARCH_AFTER_ITERATION = 0;
3140
decoder->private_->target_sample = target_sample;
3141
for( ; ; iteration++) {
3142
if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3143
if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3144
pos = (right_pos + left_pos) / 2;
3147
#ifndef FLAC__INTEGER_ONLY_LIBRARY
3148
#if defined _MSC_VER || defined __MINGW32__
3149
/* with MSVC you have to spoon feed it the casting */
3150
pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos));
3152
pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
3155
/* a little less accurate: */
3156
if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
3157
pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
3158
else /* @@@ WATCHOUT, ~2TB limit */
3159
pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
3161
/* @@@ TODO: might want to limit pos to some distance
3162
* before EOF, to make sure we land before the last frame,
3163
* thereby getting a this_frame_sample and so having a better
3164
* estimate. @@@@@@DELETE:this would also mostly (or totally if we could
3165
* be sure to land before the last frame) avoid the
3166
* end-of-stream case we have to check later.
3171
if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3172
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3175
if(!FLAC__stream_decoder_flush(decoder)) {
3176
/* above call sets the state for us */
3184
decoder->private_->got_a_frame = false;
3185
if(!FLAC__stream_decoder_process_single(decoder)) {
3186
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3189
if(!decoder->private_->got_a_frame) {
3191
/* this can happen if we seek to a point after the last frame; we drop
3192
* to binary search right away in this case to avoid any wasted
3193
* iterations of proportional search.
3196
BINARY_SEARCH_AFTER_ITERATION = 0;
3199
/* this can probably only happen if total_samples is unknown and the
3200
* target_sample is past the end of the stream
3202
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3206
/* our write callback will change the state when it gets to the target frame */
3207
else if(!decoder->private_->is_seeking/*@@@@@@ && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM*/) {
3211
this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3212
FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3215
if (this_frame_sample <= target_sample) {
3216
/* The 'equal' case should not happen, since
3217
* FLAC__stream_decoder_process_single()
3218
* should recognize that it has hit the
3219
* target sample and we would exit through
3220
* the 'break' above.
3222
FLAC__ASSERT(this_frame_sample != target_sample);
3224
left_sample = this_frame_sample;
3225
/* sanity check to avoid infinite loop */
3226
if (left_pos == pos) {
3227
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3232
else if(this_frame_sample > target_sample) {
3233
right_sample = this_frame_sample;
3234
/* sanity check to avoid infinite loop */
3235
if (right_pos == pos) {
3236
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3249
FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3254
*bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3255
if(ferror(decoder->private_->file))
3256
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3257
else if(*bytes == 0)
3258
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3260
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3263
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
3266
FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3270
if(decoder->private_->file == stdin)
3271
return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3272
else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3273
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3275
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3278
FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3283
if(decoder->private_->file == stdin)
3284
return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3285
else if((pos = ftello(decoder->private_->file)) < 0)
3286
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3288
*absolute_byte_offset = (FLAC__uint64)pos;
3289
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3293
FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3295
struct stat filestats;
3298
if(decoder->private_->file == stdin)
3299
return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3300
else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
3301
return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3303
*stream_length = (FLAC__uint64)filestats.st_size;
3304
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3308
FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3312
return feof(decoder->private_->file)? true : false;