Lines Matching refs:decoder
82 static void set_defaults_(FLAC__StreamDecoder *decoder);
84 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
85 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
86 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
87 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
88 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
89 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
90 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, unsigned length);
91 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
92 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
93 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
94 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
95 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
96 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
97 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
98 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
99 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
100 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
101 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
102 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended);
103 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
106 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
109 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
110 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
111 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
113 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
115 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
116 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
117 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
118 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
119 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
168 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
253 FLAC__StreamDecoder *decoder;
258 decoder = calloc(1, sizeof(FLAC__StreamDecoder));
259 if(decoder == 0) {
263 decoder->protected_ = calloc(1, sizeof(FLAC__StreamDecoderProtected));
264 if(decoder->protected_ == 0) {
265 free(decoder);
269 decoder->private_ = calloc(1, sizeof(FLAC__StreamDecoderPrivate));
270 if(decoder->private_ == 0) {
271 free(decoder->protected_);
272 free(decoder);
276 decoder->private_->input = FLAC__bitreader_new();
277 if(decoder->private_->input == 0) {
278 free(decoder->private_);
279 free(decoder->protected_);
280 free(decoder);
284 decoder->private_->metadata_filter_ids_capacity = 16;
285 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
286 FLAC__bitreader_delete(decoder->private_->input);
287 free(decoder->private_);
288 free(decoder->protected_);
289 free(decoder);
294 decoder->private_->output[i] = 0;
295 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
298 decoder->private_->output_capacity = 0;
299 decoder->private_->output_channels = 0;
300 decoder->private_->has_seek_table = false;
303 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
305 decoder->private_->file = 0;
307 set_defaults_(decoder);
309 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
311 return decoder;
314 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
318 if (decoder == NULL)
321 FLAC__ASSERT(0 != decoder->protected_);
322 FLAC__ASSERT(0 != decoder->private_);
323 FLAC__ASSERT(0 != decoder->private_->input);
325 (void)FLAC__stream_decoder_finish(decoder);
327 if(0 != decoder->private_->metadata_filter_ids)
328 free(decoder->private_->metadata_filter_ids);
330 FLAC__bitreader_delete(decoder->private_->input);
333 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
335 free(decoder->private_);
336 free(decoder->protected_);
337 free(decoder);
347 FLAC__StreamDecoder *decoder,
360 FLAC__ASSERT(0 != decoder);
362 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
379 decoder->private_->is_ogg = is_ogg;
380 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
381 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
387 FLAC__cpu_info(&decoder->private_->cpuinfo);
389 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
390 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
391 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
394 if(decoder->private_->cpuinfo.use_asm) {
396 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
398 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide_asm_ia32; /* OPT_IA32: was really necessary for GCC < 4.9 */
399 if(decoder->private_->cpuinfo.ia32.mmx) {
400 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
401 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
404 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
405 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
410 if(decoder->private_->cpuinfo.ia32.sse2) {
411 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_16_intrin_sse2;
415 if(decoder->private_->cpuinfo.ia32.sse41) {
416 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide_intrin_sse41;
421 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_X86_64);
429 if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
430 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
434 decoder->private_->read_callback = read_callback;
435 decoder->private_->seek_callback = seek_callback;
436 decoder->private_->tell_callback = tell_callback;
437 decoder->private_->length_callback = length_callback;
438 decoder->private_->eof_callback = eof_callback;
439 decoder->private_->write_callback = write_callback;
440 decoder->private_->metadata_callback = metadata_callback;
441 decoder->private_->error_callback = error_callback;
442 decoder->private_->client_data = client_data;
443 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
444 decoder->private_->samples_decoded = 0;
445 decoder->private_->has_stream_info = false;
446 decoder->private_->cached = false;
448 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
449 decoder->private_->is_seeking = false;
451 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
452 if(!FLAC__stream_decoder_reset(decoder)) {
461 FLAC__StreamDecoder *decoder,
474 decoder,
489 FLAC__StreamDecoder *decoder,
502 decoder,
517 FLAC__StreamDecoder *decoder,
526 FLAC__ASSERT(0 != decoder);
529 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
530 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
533 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
543 decoder->private_->file = file;
546 decoder,
548 decoder->private_->file == stdin? 0: file_seek_callback_,
549 decoder->private_->file == stdin? 0: file_tell_callback_,
550 decoder->private_->file == stdin? 0: file_length_callback_,
561 FLAC__StreamDecoder *decoder,
569 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
573 FLAC__StreamDecoder *decoder,
581 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
585 FLAC__StreamDecoder *decoder,
596 FLAC__ASSERT(0 != decoder);
603 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
604 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
607 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
614 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
618 FLAC__StreamDecoder *decoder,
626 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
630 FLAC__StreamDecoder *decoder,
638 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
641 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
646 FLAC__ASSERT(0 != decoder);
647 FLAC__ASSERT(0 != decoder->private_);
648 FLAC__ASSERT(0 != decoder->protected_);
650 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
656 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
658 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
659 free(decoder->private_->seek_table.data.seek_table.points);
660 decoder->private_->seek_table.data.seek_table.points = 0;
661 decoder->private_->has_seek_table = false;
663 FLAC__bitreader_free(decoder->private_->input);
671 if(0 != decoder->private_->output[i]) {
672 free(decoder->private_->output[i]-4);
673 decoder->private_->output[i] = 0;
675 if(0 != decoder->private_->residual_unaligned[i]) {
676 free(decoder->private_->residual_unaligned[i]);
677 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
680 decoder->private_->output_capacity = 0;
681 decoder->private_->output_channels = 0;
684 if(decoder->private_->is_ogg)
685 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
688 if(0 != decoder->private_->file) {
689 if(decoder->private_->file != stdin)
690 fclose(decoder->private_->file);
691 decoder->private_->file = 0;
694 if(decoder->private_->do_md5_checking) {
695 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
698 decoder->private_->is_seeking = false;
700 set_defaults_(decoder);
702 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
707 FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
709 FLAC__ASSERT(0 != decoder);
710 FLAC__ASSERT(0 != decoder->private_);
711 FLAC__ASSERT(0 != decoder->protected_);
712 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
715 /* can't check decoder->private_->is_ogg since that's not set until init time */
716 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
724 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
726 FLAC__ASSERT(0 != decoder);
727 FLAC__ASSERT(0 != decoder->protected_);
728 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
730 decoder->protected_->md5_checking = value;
734 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
736 FLAC__ASSERT(0 != decoder);
737 FLAC__ASSERT(0 != decoder->private_);
738 FLAC__ASSERT(0 != decoder->protected_);
743 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
745 decoder->private_->metadata_filter[type] = true;
747 decoder->private_->metadata_filter_ids_count = 0;
751 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
753 FLAC__ASSERT(0 != decoder);
754 FLAC__ASSERT(0 != decoder->private_);
755 FLAC__ASSERT(0 != decoder->protected_);
757 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
760 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
763 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
765 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
766 if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
767 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
770 decoder->private_->metadata_filter_ids_capacity *= 2;
773 memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
774 decoder->private_->metadata_filter_ids_count++;
779 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
782 FLAC__ASSERT(0 != decoder);
783 FLAC__ASSERT(0 != decoder->private_);
784 FLAC__ASSERT(0 != decoder->protected_);
785 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
787 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
788 decoder->private_->metadata_filter[i] = true;
789 decoder->private_->metadata_filter_ids_count = 0;
793 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
795 FLAC__ASSERT(0 != decoder);
796 FLAC__ASSERT(0 != decoder->private_);
797 FLAC__ASSERT(0 != decoder->protected_);
802 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
804 decoder->private_->metadata_filter[type] = false;
806 decoder->private_->metadata_filter_ids_count = 0;
810 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
812 FLAC__ASSERT(0 != decoder);
813 FLAC__ASSERT(0 != decoder->private_);
814 FLAC__ASSERT(0 != decoder->protected_);
816 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
819 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
822 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
824 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
825 if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
826 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
829 decoder->private_->metadata_filter_ids_capacity *= 2;
832 memcpy(decoderdecoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
833 decoder->private_->metadata_filter_ids_count++;
838 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
840 FLAC__ASSERT(0 != decoder);
841 FLAC__ASSERT(0 != decoder->private_);
842 FLAC__ASSERT(0 != decoder->protected_);
843 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
845 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
846 decoder->private_->metadata_filter_ids_count = 0;
850 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
852 FLAC__ASSERT(0 != decoder);
853 FLAC__ASSERT(0 != decoder->protected_);
854 return decoder->protected_->state;
857 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
859 return FLAC__StreamDecoderStateString[decoder->protected_->state];
862 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
864 FLAC__ASSERT(0 != decoder);
865 FLAC__ASSERT(0 != decoder->protected_);
866 return decoder->protected_->md5_checking;
869 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
871 FLAC__ASSERT(0 != decoder);
872 FLAC__ASSERT(0 != decoder->protected_);
873 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
876 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
878 FLAC__ASSERT(0 != decoder);
879 FLAC__ASSERT(0 != decoder->protected_);
880 return decoder->protected_->channels;
883 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
885 FLAC__ASSERT(0 != decoder);
886 FLAC__ASSERT(0 != decoder->protected_);
887 return decoder->protected_->channel_assignment;
890 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
892 FLAC__ASSERT(0 != decoder);
893 FLAC__ASSERT(0 != decoder->protected_);
894 return decoder->protected_->bits_per_sample;
897 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
899 FLAC__ASSERT(0 != decoder);
900 FLAC__ASSERT(0 != decoder->protected_);
901 return decoder->protected_->sample_rate;
904 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
906 FLAC__ASSERT(0 != decoder);
907 FLAC__ASSERT(0 != decoder->protected_);
908 return decoder->protected_->blocksize;
911 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
913 FLAC__ASSERT(0 != decoder);
914 FLAC__ASSERT(0 != decoder->private_);
918 if(decoder->private_->is_ogg)
921 if(0 == decoder->private_->tell_callback)
923 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
926 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
928 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
929 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
933 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
935 FLAC__ASSERT(0 != decoder);
936 FLAC__ASSERT(0 != decoder->private_);
937 FLAC__ASSERT(0 != decoder->protected_);
939 if(!decoder->private_->internal_reset_hack && decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
942 decoder->private_->samples_decoded = 0;
943 decoder->private_->do_md5_checking = false;
946 if(decoder->private_->is_ogg)
947 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
950 if(!FLAC__bitreader_clear(decoder->private_->input)) {
951 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
954 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
959 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
961 FLAC__ASSERT(0 != decoder);
962 FLAC__ASSERT(0 != decoder->private_);
963 FLAC__ASSERT(0 != decoder->protected_);
965 if(!FLAC__stream_decoder_flush(decoder)) {
972 if(decoder->private_->is_ogg)
973 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
981 if(!decoder->private_->internal_reset_hack) {
982 if(decoder->private_->file == stdin)
984 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
988 decoder->private_->internal_reset_hack = false;
990 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
992 decoder->private_->has_stream_info = false;
993 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
994 free(decoder->private_->seek_table.data.seek_table.points);
995 decoder->private_->seek_table.data.seek_table.points = 0;
996 decoder->private_->has_seek_table = false;
998 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
1003 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
1011 FLAC__MD5Init(&decoder->private_->md5context);
1013 decoder->private_->first_frame_offset = 0;
1014 decoder->private_->unparseable_frame_count = 0;
1019 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1022 FLAC__ASSERT(0 != decoder);
1023 FLAC__ASSERT(0 != decoder->protected_);
1026 switch(decoder->protected_->state) {
1028 if(!find_metadata_(decoder))
1032 if(!read_metadata_(decoder))
1037 if(!frame_sync_(decoder))
1041 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1056 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1058 FLAC__ASSERT(0 != decoder);
1059 FLAC__ASSERT(0 != decoder->protected_);
1062 switch(decoder->protected_->state) {
1064 if(!find_metadata_(decoder))
1068 if(!read_metadata_(decoder))
1083 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1086 FLAC__ASSERT(0 != decoder);
1087 FLAC__ASSERT(0 != decoder->protected_);
1090 switch(decoder->protected_->state) {
1092 if(!find_metadata_(decoder))
1096 if(!read_metadata_(decoder))
1100 if(!frame_sync_(decoder))
1104 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1117 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1120 FLAC__ASSERT(0 != decoder);
1121 FLAC__ASSERT(0 != decoder->protected_);
1124 switch(decoder->protected_->state) {
1129 if(!frame_sync_(decoder))
1133 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1148 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1152 FLAC__ASSERT(0 != decoder);
1155 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1156 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1157 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1158 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1159 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1163 if(0 == decoder->private_->seek_callback)
1166 FLAC__ASSERT(decoder->private_->seek_callback);
1167 FLAC__ASSERT(decoder->private_->tell_callback);
1168 FLAC__ASSERT(decoder->private_->length_callback);
1169 FLAC__ASSERT(decoder->private_->eof_callback);
1171 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1174 decoder->private_->is_seeking = true;
1177 decoder->private_->do_md5_checking = false;
1180 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1181 decoder->private_->is_seeking = false;
1187 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1188 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1190 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1192 decoder->private_->is_seeking = false;
1196 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1197 decoder->private_->is_seeking = false;
1205 decoder->private_->is_ogg?
1206 seek_to_absolute_sample_ogg_(decoder, length, sample) :
1208 seek_to_absolute_sample_(decoder, length, sample)
1210 decoder->private_->is_seeking = false;
1221 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1223 FLAC__ASSERT(0 != decoder);
1224 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1225 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
1226 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
1235 void set_defaults_(FLAC__StreamDecoder *decoder)
1238 decoder->private_->is_ogg = false;
1240 decoder->private_->read_callback = 0;
1241 decoder->private_->seek_callback = 0;
1242 decoder->private_->tell_callback = 0;
1243 decoder->private_->length_callback = 0;
1244 decoder->private_->eof_callback = 0;
1245 decoder->private_->write_callback = 0;
1246 decoder->private_->metadata_callback = 0;
1247 decoder->private_->error_callback = 0;
1248 decoder->private_->client_data = 0;
1250 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1251 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1252 decoder->private_->metadata_filter_ids_count = 0;
1254 decoder->protected_->md5_checking = false;
1257 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1282 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1287 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
1293 if(0 != decoder->private_->output[i]) {
1294 free(decoder->private_->output[i]-4);
1295 decoder->private_->output[i] = 0;
1297 if(0 != decoder->private_->residual_unaligned[i]) {
1298 free(decoder->private_->residual_unaligned[i]);
1299 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1312 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1316 decoder->private_->output[i] = tmp + 4;
1318 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
1319 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1324 decoder->private_->output_capacity = size;
1325 decoder->private_->output_channels = channels;
1330 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1334 FLAC__ASSERT(0 != decoder);
1335 FLAC__ASSERT(0 != decoder->private_);
1337 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1338 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
1344 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1350 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1353 if(decoder->private_->cached) {
1354 x = (FLAC__uint32)decoder->private_->lookahead;
1355 decoder->private_->cached = false;
1358 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1375 if(!skip_id3v2_tag_(decoder))
1382 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1383 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1389 decoder->private_->lookahead = (FLAC__byte)x;
1390 decoder->private_->cached = true;
1393 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1394 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1400 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1405 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1409 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1414 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1416 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1420 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
1423 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1427 if(!read_metadata_streaminfo_(decoder, is_last, length))
1430 decoder->private_->has_stream_info = true;
1431 if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
1432 decoder->private_->do_md5_checking = false;
1433 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1434 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1437 if(!read_metadata_seektable_(decoder, is_last, length))
1440 decoder->private_->has_seek_table = true;
1441 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1442 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1445 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1455 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1459 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
1465 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1470 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1478 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1485 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1488 else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1495 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment, real_length))
1499 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1503 if(!read_metadata_picture_(decoder, &block.data.picture))
1513 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1516 else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1523 if(ok && !decoder->private_->is_seeking && decoder->private_->metadata_callback)
1524 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1569 if(!ok) /* anything that unsets "ok" should also make sure decoder->protected_->state is updated */
1576 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1577 decoder->private_->first_frame_offset = 0;
1578 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1584 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1589 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1591 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1592 decoder->private_->stream_info.is_last = is_last;
1593 decoder->private_->stream_info.length = length;
1596 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1598 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1602 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1604 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1608 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1610 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1614 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1616 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1620 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1622 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1626 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1628 decoder->private_->stream_info.data.stream_info.channels = x+1;
1632 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1634 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1638 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))
1642 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1649 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1655 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1660 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1662 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1663 decoder->private_->seek_table.is_last = is_last;
1664 decoder->private_->seek_table.length = length;
1666 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1669 if(0 == (decoder->private_->seek_table.data.seek_table.points = safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) {
1670 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1673 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1674 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
1676 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1678 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
1680 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1682 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
1684 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1686 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1690 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1697 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, unsigned length)
1701 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1707 if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
1718 decoder
1721 if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
1730 if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
1742 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1758 if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) {
1770 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1774 if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length)) {
1793 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1800 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1804 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1809 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
1812 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
1815 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
1819 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
1822 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
1828 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1833 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
1836 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
1841 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
1844 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
1848 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
1852 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
1855 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
1861 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1866 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
1869 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
1873 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
1883 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
1887 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1890 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
1895 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
1898 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1902 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
1908 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
1911 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1915 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
1921 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
1925 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
1929 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
1933 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
1937 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
1940 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1944 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
1951 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1957 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
1962 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1968 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
1973 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1980 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
1981 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1982 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1988 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
1989 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
1994 if(decoder->private_->cached) {
1995 x = (FLAC__uint32)decoder->private_->lookahead;
1996 decoder->private_->cached = false;
1999 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2003 decoder->private_->header_warmup[0] = (FLAC__byte)x;
2004 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2010 decoder->private_->lookahead = (FLAC__byte)x;
2011 decoder->private_->cached = true;
2014 decoder->private_->header_warmup[1] = (FLAC__byte)x;
2015 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
2020 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2028 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
2040 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
2041 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
2042 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
2044 if(!read_frame_header_(decoder))
2046 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
2048 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
2050 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2054 unsigned bps = decoder->private_->frame.header.bits_per_sample;
2055 switch(decoder->private_->frame.header.channel_assignment) {
2060 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2065 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2070 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2080 if(!read_subframe_(decoder, channel, bps, do_full_decode))
2082 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2085 if(!read_zero_padding_(decoder))
2087 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption (i.e. "zero bits" were not all zeroes) */
2093 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2094 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
2099 switch(decoder->private_->frame.header.channel_assignment) {
2104 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2105 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2106 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
2109 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2110 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2111 decoder->private_->output[0][i] += decoder->private_->output[1][i];
2114 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2115 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2117 mid = decoder->private_->output[0][i];
2118 side = decoder->private_->output[1][i];
2121 decoder
2122 decoder->private_->output[1][i] = (mid - side) >> 1;
2125 mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */
2126 decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1;
2127 decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1;
2139 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2141 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2142 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2150 if(decoder->private_->next_fixed_block_size)
2151 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;
2153 /* put the latest values into the public section of the decoder instance */
2154 decoder->protected_->channels = decoder->private_->frame.header.channels;
2155 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2156 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2157 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2158 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2160 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2161 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2165 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
2169 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2173 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2182 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2185 raw_header[0] = decoder->private_->header_warmup[0];
2186 raw_header[1] = decoder->private_->header_warmup[1];
2216 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2220 decoder->private_->lookahead = (FLAC__byte)x;
2221 decoder->private_->cached = true;
2222 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2223 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2234 decoder->private_->frame.header.blocksize = 192;
2240 decoder->private_->frame.header.blocksize = 576 << (x-2);
2254 decoder->private_->frame.header.blocksize = 256 << (x-8);
2263 if(decoder->private_->has_stream_info)
2264 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2269 decoder->private_->frame.header.sample_rate = 88200;
2272 decoder->private_->frame.header.sample_rate = 176400;
2275 decoder->private_->frame.header.sample_rate = 192000;
2278 decoder->private_->frame.header.sample_rate = 8000;
2281 decoder->private_->frame.header.sample_rate = 16000;
2284 decoder->private_->frame.header.sample_rate = 22050;
2287 decoder->private_->frame.header.sample_rate = 24000;
2290 decoder->private_->frame.header.sample_rate = 32000;
2293 decoder->private_->frame.header.sample_rate = 44100;
2296 decoder->private_->frame.header.sample_rate = 48000;
2299 decoder->private_->frame.header.sample_rate = 96000;
2307 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2308 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2316 decoder->private_->frame.header.channels = 2;
2319 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2322 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2325 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2333 decoder->private_->frame.header.channels = (unsigned)x + 1;
2334 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2339 if(decoder->private_->has_stream_info)
2340 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2345 decoder->private_->frame.header.bits_per_sample = 8;
2348 decoder->private_->frame.header.bits_per_sample = 12;
2351 decoder->private_->frame.header.bits_per_sample = 16;
2354 decoder->private_->frame.header.bits_per_sample = 20;
2357 decoder->private_->frame.header.bits_per_sample = 24;
2376 (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
2378 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2381 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2382 decoder->private_->cached = true;
2383 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2384 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2387 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2388 decoder->private_->frame.header.number.sample_number = xx;
2391 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2394 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2395 decoder->private_->cached = true;
2396 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2397 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2400 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2401 decoder->private_->frame.header.number.frame_number = x;
2405 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2410 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2415 decoder->private_->frame.header.blocksize = x+1;
2419 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2424 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2430 decoder->private_->frame.header.sample_rate = x*1000;
2432 decoder->private_->frame.header.sample_rate = x;
2434 decoder->private_->frame.header.sample_rate = x*10;
2438 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2443 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2444 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2449 decoder->private_->next_fixed_block_size = 0;
2450 if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
2451 x = decoder->private_->frame.header.number.frame_number;
2452 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2453 if(decoder->private_->fixed_block_size)
2454 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
2455 else if(decoder->private_->has_stream_info) {
2456 if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
2457 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2458 decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
2464 decoder->private_->frame.header.number.sample_number = 0;
2465 decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
2469 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2474 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2475 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2482 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2488 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2496 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2498 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2499 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2502 decoder->private_->frame.subframes[channel].wasted_bits = 0;
2508 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2509 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2513 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2517 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2521 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2522 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2526 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
2528 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2532 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2533 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2537 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
2539 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2544 x = decoder->private_->frame.subframes[channel].wasted_bits;
2545 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2546 uint32_t val = decoder->private_->output[channel][i];
2547 decoder->private_->output[channel][i] = (val << x);
2554 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2556 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
2559 FLAC__int32 *output = decoder->private_->output[channel];
2561 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2563 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2570 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2577 decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2579 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
2584 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
2586 subframe->residual = decoder->private_->residual[channel];
2591 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2597 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2603 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2605 if(decoder->private_->frame.header.blocksize >> u32 < order) {
2606 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2607 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2611 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2614 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2615 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2623 if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
2632 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2633 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
2639 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2641 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
2646 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
2648 subframe->residual = decoder->private_->residual[channel];
2653 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2659 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
2662 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2663 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2669 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
2672 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2673 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2680 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
2686 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2692 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2694 if(decoder->private_->frame.header.blocksize >> u32 < order) {
2695 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2696 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2700 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2703 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2704 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2712 if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
2721 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2724 decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2726 decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2728 decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2734 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2736 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2737 FLAC__int32 x, *residual = decoder->private_->residual[channel];
2740 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
2744 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2745 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2752 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2757 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended)
2763 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2768 FLAC__ASSERT(partition_order > 0? partition_samples >= predictor_order : decoder->private_->frame.header.blocksize >= predictor_order);
2771 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2777 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
2783 if(!FLAC__bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
2788 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
2792 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
2802 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2804 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2806 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2809 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2810 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2818 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2823 !decoder->private_->is_ogg &&
2825 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2828 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2842 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2843 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2849 decoder->private_->is_ogg?
2850 read_callback_ogg_aspect_(decoder, buffer, bytes) :
2852 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2855 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2864 !decoder->private_->is_ogg &&
2866 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2869 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2881 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2885 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2890 * So to keep the decoder from stopping at this point we gate the call
2891 * to the eof_callback and let the Ogg decoder aspect set the
2897 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
2899 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2904 * FLAC decoder catch the error
2925 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2927 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2942 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2944 if(decoder->private_->is_seeking) {
2947 FLAC__uint64 target_sample = decoder->private_->target_sample;
2952 decoder->private_->got_a_frame = true;
2954 decoder->private_->last_frame = *frame; /* save the frame */
2958 decoder->private_->is_seeking = false;
2965 decoder->private_->last_frame.header.blocksize -= delta;
2966 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2968 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2972 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2984 if(!decoder->private_->has_stream_info)
2985 decoder->private_->do_md5_checking = false;
2986 if(decoder->private_->do_md5_checking) {
2987 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2990 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2994 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2996 if(!decoder->private_->is_seeking)
2997 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2999 decoder->private_->unparseable_frame_count++;
3002 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3004 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
3009 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
3010 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
3011 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
3012 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
3013 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
3015 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
3016 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
3017 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
3021 channels = decoder->private_->stream_info.data.stream_info.channels;
3023 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
3117 decoder->private_->target_sample = target_sample;
3121 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3137 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3138 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3141 if(!FLAC__stream_decoder_flush(decoder)) {
3151 decoder->private_->unparseable_frame_count = 0;
3152 if(!FLAC__stream_decoder_process_single(decoder)) {
3153 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3157 /* 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 */
3160 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3163 if(!decoder->private_->is_seeking)
3166 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3167 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3169 if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
3172 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3184 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3190 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3192 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3193 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3199 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3200 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3201 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3212 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3215 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3241 decoder->private_->target_sample = target_sample;
3265 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3266 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3269 if(!FLAC__stream_decoder_flush(decoder)) {
3278 decoder->private_->got_a_frame = false;
3279 if(!FLAC__stream_decoder_process_single(decoder)) {
3280 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3283 if(!decoder->private_->got_a_frame) {
3296 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3301 else if(!decoder->private_->is_seeking) {
3305 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3306 decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3321 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3330 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3343 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3348 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3349 if(ferror(decoder->private_->file))
3360 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3364 if(decoder->private_->file == stdin)
3366 else if(fseeko(decoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
3372 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3377 if(decoder->private_->file == stdin)
3379 else if((pos = ftello(decoder->private_->file)) < 0)
3387 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3392 if(decoder->private_->file == stdin)
3394 else if(flac_fstat(fileno(decoder->private_->file), &filestats) != 0)
3402 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3406 return feof(decoder->private_->file)? true : false;