1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <stdlib.h> /* for malloc() */
38 #include <string.h> /* for memset/memcpy() */
39 #include <sys/stat.h> /* for stat() */
40 #include <sys/types.h> /* for off_t */
41 #include "share/compat.h"
42 #include "FLAC/assert.h"
43 #include "share/alloc.h"
44 #include "protected/stream_decoder.h"
45 #include "private/bitreader.h"
46 #include "private/bitmath.h"
47 #include "private/cpu.h"
48 #include "private/crc.h"
49 #include "private/fixed.h"
50 #include "private/format.h"
51 #include "private/lpc.h"
52 #include "private/md5.h"
53 #include "private/memory.h"
54 #include "private/macros.h"
57 /* technically this should be in an "export.c" but this is convenient enough */
58 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
67 /***********************************************************************
71 ***********************************************************************/
73 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
75 /***********************************************************************
77 * Private class method prototypes
79 ***********************************************************************/
81 static void set_defaults_(FLAC__StreamDecoder *decoder);
82 static FILE *get_binary_stdin_(void);
83 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
84 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
85 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
86 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
87 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
88 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
89 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
90 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
91 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
92 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
93 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
94 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
95 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
96 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
97 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
98 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
99 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
100 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
101 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);
102 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
103 static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
105 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
106 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
108 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
109 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
110 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
112 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
114 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
115 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
116 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
117 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
118 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
120 /***********************************************************************
124 ***********************************************************************/
126 typedef struct FLAC__StreamDecoderPrivate {
130 FLAC__StreamDecoderReadCallback read_callback;
131 FLAC__StreamDecoderSeekCallback seek_callback;
132 FLAC__StreamDecoderTellCallback tell_callback;
133 FLAC__StreamDecoderLengthCallback length_callback;
134 FLAC__StreamDecoderEofCallback eof_callback;
135 FLAC__StreamDecoderWriteCallback write_callback;
136 FLAC__StreamDecoderMetadataCallback metadata_callback;
137 FLAC__StreamDecoderErrorCallback error_callback;
138 /* generic 32-bit datapath: */
139 void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
140 /* generic 64-bit datapath: */
141 void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
142 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
143 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
144 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
145 void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
146 FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
148 FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
149 FLAC__BitReader *input;
150 FLAC__int32 *output[FLAC__MAX_CHANNELS];
151 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
152 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
153 unsigned output_capacity, output_channels;
154 FLAC__uint32 fixed_block_size, next_fixed_block_size;
155 FLAC__uint64 samples_decoded;
156 FLAC__bool has_stream_info, has_seek_table;
157 FLAC__StreamMetadata stream_info;
158 FLAC__StreamMetadata seek_table;
159 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
160 FLAC__byte *metadata_filter_ids;
161 size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
163 FLAC__bool cached; /* true if there is a byte in lookahead */
164 FLAC__CPUInfo cpuinfo;
165 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
166 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
167 /* unaligned (original) pointers to allocated data */
168 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
169 FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
170 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
171 FLAC__bool is_seeking;
172 FLAC__MD5Context md5context;
173 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
174 /* (the rest of these are only used for seeking) */
175 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
176 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
177 FLAC__uint64 target_sample;
178 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
180 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
182 } FLAC__StreamDecoderPrivate;
184 /***********************************************************************
186 * Public static class data
188 ***********************************************************************/
190 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
191 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
192 "FLAC__STREAM_DECODER_READ_METADATA",
193 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
194 "FLAC__STREAM_DECODER_READ_FRAME",
195 "FLAC__STREAM_DECODER_END_OF_STREAM",
196 "FLAC__STREAM_DECODER_OGG_ERROR",
197 "FLAC__STREAM_DECODER_SEEK_ERROR",
198 "FLAC__STREAM_DECODER_ABORTED",
199 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
200 "FLAC__STREAM_DECODER_UNINITIALIZED"
203 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
204 "FLAC__STREAM_DECODER_INIT_STATUS_OK",
205 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
206 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
207 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
208 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
209 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
212 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
213 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
214 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
215 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
218 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
219 "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
220 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
221 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
224 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
225 "FLAC__STREAM_DECODER_TELL_STATUS_OK",
226 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
227 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
230 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
231 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
232 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
233 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
236 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
237 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
238 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
241 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
242 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
243 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
244 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
245 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
248 /***********************************************************************
250 * Class constructor/destructor
252 ***********************************************************************/
253 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
255 FLAC__StreamDecoder *decoder;
258 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
260 decoder = calloc(1, sizeof(FLAC__StreamDecoder));
265 decoder->protected_ = calloc(1, sizeof(FLAC__StreamDecoderProtected));
266 if(decoder->protected_ == 0) {
271 decoder->private_ = calloc(1, sizeof(FLAC__StreamDecoderPrivate));
272 if(decoder->private_ == 0) {
273 free(decoder->protected_);
278 decoder->private_->input = FLAC__bitreader_new();
279 if(decoder->private_->input == 0) {
280 free(decoder->private_);
281 free(decoder->protected_);
286 decoder->private_->metadata_filter_ids_capacity = 16;
287 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
288 FLAC__bitreader_delete(decoder->private_->input);
289 free(decoder->private_);
290 free(decoder->protected_);
295 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
296 decoder->private_->output[i] = 0;
297 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
300 decoder->private_->output_capacity = 0;
301 decoder->private_->output_channels = 0;
302 decoder->private_->has_seek_table = false;
304 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
305 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
307 decoder->private_->file = 0;
309 set_defaults_(decoder);
311 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
316 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
323 FLAC__ASSERT(0 != decoder->protected_);
324 FLAC__ASSERT(0 != decoder->private_);
325 FLAC__ASSERT(0 != decoder->private_->input);
327 (void)FLAC__stream_decoder_finish(decoder);
329 if(0 != decoder->private_->metadata_filter_ids)
330 free(decoder->private_->metadata_filter_ids);
332 FLAC__bitreader_delete(decoder->private_->input);
334 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
335 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
337 free(decoder->private_);
338 free(decoder->protected_);
342 /***********************************************************************
344 * Public class methods
346 ***********************************************************************/
348 static FLAC__StreamDecoderInitStatus init_stream_internal_(
349 FLAC__StreamDecoder *decoder,
350 FLAC__StreamDecoderReadCallback read_callback,
351 FLAC__StreamDecoderSeekCallback seek_callback,
352 FLAC__StreamDecoderTellCallback tell_callback,
353 FLAC__StreamDecoderLengthCallback length_callback,
354 FLAC__StreamDecoderEofCallback eof_callback,
355 FLAC__StreamDecoderWriteCallback write_callback,
356 FLAC__StreamDecoderMetadataCallback metadata_callback,
357 FLAC__StreamDecoderErrorCallback error_callback,
362 FLAC__ASSERT(0 != decoder);
364 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
365 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
369 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
373 0 == read_callback ||
374 0 == write_callback ||
375 0 == error_callback ||
376 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
378 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
381 decoder->private_->is_ogg = is_ogg;
382 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
383 return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
387 * get the CPU info and set the function pointers
389 FLAC__cpu_info(&decoder->private_->cpuinfo);
390 /* first default to the non-asm routines */
391 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
392 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
393 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
394 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
395 decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
396 /* now override with asm where appropriate */
398 if(decoder->private_->cpuinfo.use_asm) {
399 #ifdef FLAC__CPU_IA32
400 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
401 #ifdef FLAC__HAS_NASM
402 #if 1 /*@@@@@@ OPT: not clearly faster, needs more testing */
403 if(decoder->private_->cpuinfo.data.ia32.bswap)
404 decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap;
406 if(decoder->private_->cpuinfo.data.ia32.mmx) {
407 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
408 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
409 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
412 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
413 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
414 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
417 #elif defined FLAC__CPU_PPC
418 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
419 if(decoder->private_->cpuinfo.data.ppc.altivec) {
420 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
421 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
427 /* from here on, errors are fatal */
429 if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) {
430 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
431 return FLAC__STREAM_DECODER_INIT_STATUS_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)) {
453 /* above call sets the state for us */
454 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
457 return FLAC__STREAM_DECODER_INIT_STATUS_OK;
460 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
461 FLAC__StreamDecoder *decoder,
462 FLAC__StreamDecoderReadCallback read_callback,
463 FLAC__StreamDecoderSeekCallback seek_callback,
464 FLAC__StreamDecoderTellCallback tell_callback,
465 FLAC__StreamDecoderLengthCallback length_callback,
466 FLAC__StreamDecoderEofCallback eof_callback,
467 FLAC__StreamDecoderWriteCallback write_callback,
468 FLAC__StreamDecoderMetadataCallback metadata_callback,
469 FLAC__StreamDecoderErrorCallback error_callback,
473 return init_stream_internal_(
488 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
489 FLAC__StreamDecoder *decoder,
490 FLAC__StreamDecoderReadCallback read_callback,
491 FLAC__StreamDecoderSeekCallback seek_callback,
492 FLAC__StreamDecoderTellCallback tell_callback,
493 FLAC__StreamDecoderLengthCallback length_callback,
494 FLAC__StreamDecoderEofCallback eof_callback,
495 FLAC__StreamDecoderWriteCallback write_callback,
496 FLAC__StreamDecoderMetadataCallback metadata_callback,
497 FLAC__StreamDecoderErrorCallback error_callback,
501 return init_stream_internal_(
516 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
517 FLAC__StreamDecoder *decoder,
519 FLAC__StreamDecoderWriteCallback write_callback,
520 FLAC__StreamDecoderMetadataCallback metadata_callback,
521 FLAC__StreamDecoderErrorCallback error_callback,
526 FLAC__ASSERT(0 != decoder);
527 FLAC__ASSERT(0 != file);
529 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
530 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
532 if(0 == write_callback || 0 == error_callback)
533 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
536 * To make sure that our file does not go unclosed after an error, we
537 * must assign the FILE pointer before any further error can occur in
541 file = get_binary_stdin_(); /* just to be safe */
543 decoder->private_->file = file;
545 return init_stream_internal_(
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_,
560 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
561 FLAC__StreamDecoder *decoder,
563 FLAC__StreamDecoderWriteCallback write_callback,
564 FLAC__StreamDecoderMetadataCallback metadata_callback,
565 FLAC__StreamDecoderErrorCallback error_callback,
569 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
572 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
573 FLAC__StreamDecoder *decoder,
575 FLAC__StreamDecoderWriteCallback write_callback,
576 FLAC__StreamDecoderMetadataCallback metadata_callback,
577 FLAC__StreamDecoderErrorCallback error_callback,
581 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
584 static FLAC__StreamDecoderInitStatus init_file_internal_(
585 FLAC__StreamDecoder *decoder,
586 const char *filename,
587 FLAC__StreamDecoderWriteCallback write_callback,
588 FLAC__StreamDecoderMetadataCallback metadata_callback,
589 FLAC__StreamDecoderErrorCallback error_callback,
596 FLAC__ASSERT(0 != decoder);
599 * To make sure that our file does not go unclosed after an error, we
600 * have to do the same entrance checks here that are later performed
601 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
603 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
604 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
606 if(0 == write_callback || 0 == error_callback)
607 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
609 file = filename? flac_fopen(filename, "rb") : stdin;
612 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
614 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
617 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
618 FLAC__StreamDecoder *decoder,
619 const char *filename,
620 FLAC__StreamDecoderWriteCallback write_callback,
621 FLAC__StreamDecoderMetadataCallback metadata_callback,
622 FLAC__StreamDecoderErrorCallback error_callback,
626 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
629 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
630 FLAC__StreamDecoder *decoder,
631 const char *filename,
632 FLAC__StreamDecoderWriteCallback write_callback,
633 FLAC__StreamDecoderMetadataCallback metadata_callback,
634 FLAC__StreamDecoderErrorCallback error_callback,
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)
643 FLAC__bool md5_failed = false;
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)
653 /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
654 * always call FLAC__MD5Final()
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);
664 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
666 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
667 * output arrays have a buffer of up to 3 zeroes in front
668 * (at negative indices) for alignment purposes; we use 4
669 * to keep the data well-aligned.
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_);
739 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
740 /* double protection */
741 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
743 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
745 decoder->private_->metadata_filter[type] = true;
746 if(type == FLAC__METADATA_TYPE_APPLICATION)
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_);
756 FLAC__ASSERT(0 != id);
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_);
798 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
799 /* double protection */
800 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
802 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
804 decoder->private_->metadata_filter[type] = false;
805 if(type == FLAC__METADATA_TYPE_APPLICATION)
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_);
815 FLAC__ASSERT(0 != id);
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(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));
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_);
915 FLAC__ASSERT(0 != position);
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)
925 /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
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 decoder->private_->samples_decoded = 0;
940 decoder->private_->do_md5_checking = false;
943 if(decoder->private_->is_ogg)
944 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
947 if(!FLAC__bitreader_clear(decoder->private_->input)) {
948 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
951 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
956 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
958 FLAC__ASSERT(0 != decoder);
959 FLAC__ASSERT(0 != decoder->private_);
960 FLAC__ASSERT(0 != decoder->protected_);
962 if(!FLAC__stream_decoder_flush(decoder)) {
963 /* above call sets the state for us */
968 /*@@@ could go in !internal_reset_hack block below */
969 if(decoder->private_->is_ogg)
970 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
973 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
974 * (internal_reset_hack) don't try to rewind since we are already at
975 * the beginning of the stream and don't want to fail if the input is
978 if(!decoder->private_->internal_reset_hack) {
979 if(decoder->private_->file == stdin)
980 return false; /* can't rewind stdin, reset fails */
981 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
982 return false; /* seekable and seek fails, reset fails */
985 decoder->private_->internal_reset_hack = false;
987 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
989 decoder->private_->has_stream_info = false;
990 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
991 free(decoder->private_->seek_table.data.seek_table.points);
992 decoder->private_->seek_table.data.seek_table.points = 0;
993 decoder->private_->has_seek_table = false;
995 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
997 * This goes in reset() and not flush() because according to the spec, a
998 * fixed-blocksize stream must stay that way through the whole stream.
1000 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
1002 /* We initialize the FLAC__MD5Context even though we may never use it. This
1003 * is because md5 checking may be turned on to start and then turned off if
1004 * a seek occurs. So we init the context here and finalize it in
1005 * FLAC__stream_decoder_finish() to make sure things are always cleaned up
1008 FLAC__MD5Init(&decoder->private_->md5context);
1010 decoder->private_->first_frame_offset = 0;
1011 decoder->private_->unparseable_frame_count = 0;
1016 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1018 FLAC__bool got_a_frame;
1019 FLAC__ASSERT(0 != decoder);
1020 FLAC__ASSERT(0 != decoder->protected_);
1023 switch(decoder->protected_->state) {
1024 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1025 if(!find_metadata_(decoder))
1026 return false; /* above function sets the status for us */
1028 case FLAC__STREAM_DECODER_READ_METADATA:
1029 if(!read_metadata_(decoder))
1030 return false; /* above function sets the status for us */
1033 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1034 if(!frame_sync_(decoder))
1035 return true; /* above function sets the status for us */
1037 case FLAC__STREAM_DECODER_READ_FRAME:
1038 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1039 return false; /* above function sets the status for us */
1041 return true; /* above function sets the status for us */
1043 case FLAC__STREAM_DECODER_END_OF_STREAM:
1044 case FLAC__STREAM_DECODER_ABORTED:
1053 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1055 FLAC__ASSERT(0 != decoder);
1056 FLAC__ASSERT(0 != decoder->protected_);
1059 switch(decoder->protected_->state) {
1060 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1061 if(!find_metadata_(decoder))
1062 return false; /* above function sets the status for us */
1064 case FLAC__STREAM_DECODER_READ_METADATA:
1065 if(!read_metadata_(decoder))
1066 return false; /* above function sets the status for us */
1068 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1069 case FLAC__STREAM_DECODER_READ_FRAME:
1070 case FLAC__STREAM_DECODER_END_OF_STREAM:
1071 case FLAC__STREAM_DECODER_ABORTED:
1080 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1083 FLAC__ASSERT(0 != decoder);
1084 FLAC__ASSERT(0 != decoder->protected_);
1087 switch(decoder->protected_->state) {
1088 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1089 if(!find_metadata_(decoder))
1090 return false; /* above function sets the status for us */
1092 case FLAC__STREAM_DECODER_READ_METADATA:
1093 if(!read_metadata_(decoder))
1094 return false; /* above function sets the status for us */
1096 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1097 if(!frame_sync_(decoder))
1098 return true; /* above function sets the status for us */
1100 case FLAC__STREAM_DECODER_READ_FRAME:
1101 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1102 return false; /* above function sets the status for us */
1104 case FLAC__STREAM_DECODER_END_OF_STREAM:
1105 case FLAC__STREAM_DECODER_ABORTED:
1114 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1116 FLAC__bool got_a_frame;
1117 FLAC__ASSERT(0 != decoder);
1118 FLAC__ASSERT(0 != decoder->protected_);
1121 switch(decoder->protected_->state) {
1122 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1123 case FLAC__STREAM_DECODER_READ_METADATA:
1124 return false; /* above function sets the status for us */
1125 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1126 if(!frame_sync_(decoder))
1127 return true; /* above function sets the status for us */
1129 case FLAC__STREAM_DECODER_READ_FRAME:
1130 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1131 return false; /* above function sets the status for us */
1133 return true; /* above function sets the status for us */
1135 case FLAC__STREAM_DECODER_END_OF_STREAM:
1136 case FLAC__STREAM_DECODER_ABORTED:
1145 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1147 FLAC__uint64 length;
1149 FLAC__ASSERT(0 != decoder);
1152 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1153 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1154 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1155 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1156 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1160 if(0 == decoder->private_->seek_callback)
1163 FLAC__ASSERT(decoder->private_->seek_callback);
1164 FLAC__ASSERT(decoder->private_->tell_callback);
1165 FLAC__ASSERT(decoder->private_->length_callback);
1166 FLAC__ASSERT(decoder->private_->eof_callback);
1168 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1171 decoder->private_->is_seeking = true;
1173 /* turn off md5 checking if a seek is attempted */
1174 decoder->private_->do_md5_checking = false;
1176 /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */
1177 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1178 decoder->private_->is_seeking = false;
1182 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
1184 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1185 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1187 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1188 /* above call sets the state for us */
1189 decoder->private_->is_seeking = false;
1192 /* check this again in case we didn't know total_samples the first time */
1193 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1194 decoder->private_->is_seeking = false;
1200 const FLAC__bool ok =
1202 decoder->private_->is_ogg?
1203 seek_to_absolute_sample_ogg_(decoder, length, sample) :
1205 seek_to_absolute_sample_(decoder, length, sample)
1207 decoder->private_->is_seeking = false;
1212 /***********************************************************************
1214 * Protected class methods
1216 ***********************************************************************/
1218 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1220 FLAC__ASSERT(0 != decoder);
1221 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1222 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
1223 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
1226 /***********************************************************************
1228 * Private class methods
1230 ***********************************************************************/
1232 void set_defaults_(FLAC__StreamDecoder *decoder)
1235 decoder->private_->is_ogg = false;
1237 decoder->private_->read_callback = 0;
1238 decoder->private_->seek_callback = 0;
1239 decoder->private_->tell_callback = 0;
1240 decoder->private_->length_callback = 0;
1241 decoder->private_->eof_callback = 0;
1242 decoder->private_->write_callback = 0;
1243 decoder->private_->metadata_callback = 0;
1244 decoder->private_->error_callback = 0;
1245 decoder->private_->client_data = 0;
1247 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1248 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1249 decoder->private_->metadata_filter_ids_count = 0;
1251 decoder->protected_->md5_checking = false;
1254 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1259 * This will forcibly set stdin to binary mode (for OSes that require it)
1261 FILE *get_binary_stdin_(void)
1263 /* if something breaks here it is probably due to the presence or
1264 * absence of an underscore before the identifiers 'setmode',
1265 * 'fileno', and/or 'O_BINARY'; check your system header files.
1267 #if defined _MSC_VER || defined __MINGW32__
1268 _setmode(_fileno(stdin), _O_BINARY);
1269 #elif defined __CYGWIN__
1270 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
1271 setmode(_fileno(stdin), _O_BINARY);
1272 #elif defined __EMX__
1273 setmode(fileno(stdin), O_BINARY);
1279 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1284 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
1287 /* simply using realloc() is not practical because the number of channels may change mid-stream */
1289 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1290 if(0 != decoder->private_->output[i]) {
1291 free(decoder->private_->output[i]-4);
1292 decoder->private_->output[i] = 0;
1294 if(0 != decoder->private_->residual_unaligned[i]) {
1295 free(decoder->private_->residual_unaligned[i]);
1296 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1300 for(i = 0; i < channels; i++) {
1302 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
1303 * output arrays have a buffer of up to 3 zeroes in front
1304 * (at negative indices) for alignment purposes; we use 4
1305 * to keep the data well-aligned.
1307 tmp = safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/);
1309 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1312 memset(tmp, 0, sizeof(FLAC__int32)*4);
1313 decoder->private_->output[i] = tmp + 4;
1316 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
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)
1348 FLAC__bool first = true;
1350 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1352 for(i = id = 0; i < 4; ) {
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))
1359 return false; /* read_callback_ sets the state for us */
1361 if(x == FLAC__STREAM_SYNC_STRING[i]) {
1367 if(x == ID3V2_TAG_[id]) {
1371 if(!skip_id3v2_tag_(decoder))
1372 return false; /* skip_id3v2_tag_ sets the state for us */
1377 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1378 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1379 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1380 return false; /* read_callback_ sets the state for us */
1382 /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
1383 /* else we have to check if the second byte is the end of a sync code */
1384 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1385 decoder->private_->lookahead = (FLAC__byte)x;
1386 decoder->private_->cached = true;
1388 else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
1389 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1390 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1396 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1401 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1405 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1408 FLAC__uint32 i, x, type, length;
1410 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1412 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1413 return false; /* read_callback_ sets the state for us */
1414 is_last = x? true : false;
1416 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
1417 return false; /* read_callback_ sets the state for us */
1419 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1420 return false; /* read_callback_ sets the state for us */
1422 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1423 if(!read_metadata_streaminfo_(decoder, is_last, length))
1426 decoder->private_->has_stream_info = true;
1427 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))
1428 decoder->private_->do_md5_checking = false;
1429 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1430 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1432 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1433 if(!read_metadata_seektable_(decoder, is_last, length))
1436 decoder->private_->has_seek_table = true;
1437 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1438 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1441 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1442 unsigned real_length = length;
1443 FLAC__StreamMetadata block;
1445 block.is_last = is_last;
1446 block.type = (FLAC__MetadataType)type;
1447 block.length = length;
1449 if(type == FLAC__METADATA_TYPE_APPLICATION) {
1450 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1451 return false; /* read_callback_ sets the state for us */
1453 if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */
1454 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
1458 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1460 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1465 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1466 return false; /* read_callback_ sets the state for us */
1470 case FLAC__METADATA_TYPE_PADDING:
1471 /* skip the padding bytes */
1472 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1473 return false; /* read_callback_ sets the state for us */
1475 case FLAC__METADATA_TYPE_APPLICATION:
1476 /* remember, we read the ID already */
1477 if(real_length > 0) {
1478 if(0 == (block.data.application.data = malloc(real_length))) {
1479 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1482 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1483 return false; /* read_callback_ sets the state for us */
1486 block.data.application.data = 0;
1488 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1489 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
1492 case FLAC__METADATA_TYPE_CUESHEET:
1493 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1496 case FLAC__METADATA_TYPE_PICTURE:
1497 if(!read_metadata_picture_(decoder, &block.data.picture))
1500 case FLAC__METADATA_TYPE_STREAMINFO:
1501 case FLAC__METADATA_TYPE_SEEKTABLE:
1505 if(real_length > 0) {
1506 if(0 == (block.data.unknown.data = malloc(real_length))) {
1507 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1510 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1511 return false; /* read_callback_ sets the state for us */
1514 block.data.unknown.data = 0;
1517 if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
1518 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1520 /* now we have to free any malloc()ed data in the block */
1522 case FLAC__METADATA_TYPE_PADDING:
1524 case FLAC__METADATA_TYPE_APPLICATION:
1525 if(0 != block.data.application.data)
1526 free(block.data.application.data);
1528 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1529 if(0 != block.data.vorbis_comment.vendor_string.entry)
1530 free(block.data.vorbis_comment.vendor_string.entry);
1531 if(block.data.vorbis_comment.num_comments > 0)
1532 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1533 if(0 != block.data.vorbis_comment.comments[i].entry)
1534 free(block.data.vorbis_comment.comments[i].entry);
1535 if(0 != block.data.vorbis_comment.comments)
1536 free(block.data.vorbis_comment.comments);
1538 case FLAC__METADATA_TYPE_CUESHEET:
1539 if(block.data.cue_sheet.num_tracks > 0)
1540 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1541 if(0 != block.data.cue_sheet.tracks[i].indices)
1542 free(block.data.cue_sheet.tracks[i].indices);
1543 if(0 != block.data.cue_sheet.tracks)
1544 free(block.data.cue_sheet.tracks);
1546 case FLAC__METADATA_TYPE_PICTURE:
1547 if(0 != block.data.picture.mime_type)
1548 free(block.data.picture.mime_type);
1549 if(0 != block.data.picture.description)
1550 free(block.data.picture.description);
1551 if(0 != block.data.picture.data)
1552 free(block.data.picture.data);
1554 case FLAC__METADATA_TYPE_STREAMINFO:
1555 case FLAC__METADATA_TYPE_SEEKTABLE:
1558 if(0 != block.data.unknown.data)
1559 free(block.data.unknown.data);
1566 /* if this fails, it's OK, it's just a hint for the seek routine */
1567 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1568 decoder->private_->first_frame_offset = 0;
1569 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1575 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1578 unsigned bits, used_bits = 0;
1580 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1582 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1583 decoder->private_->stream_info.is_last = is_last;
1584 decoder->private_->stream_info.length = length;
1586 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1587 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1588 return false; /* read_callback_ sets the state for us */
1589 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1592 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1593 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1594 return false; /* read_callback_ sets the state for us */
1595 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1598 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1599 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1600 return false; /* read_callback_ sets the state for us */
1601 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1604 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1605 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1606 return false; /* read_callback_ sets the state for us */
1607 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1610 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1611 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1612 return false; /* read_callback_ sets the state for us */
1613 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1616 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1617 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1618 return false; /* read_callback_ sets the state for us */
1619 decoder->private_->stream_info.data.stream_info.channels = x+1;
1622 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1623 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1624 return false; /* read_callback_ sets the state for us */
1625 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1628 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1629 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))
1630 return false; /* read_callback_ sets the state for us */
1633 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1634 return false; /* read_callback_ sets the state for us */
1637 /* skip the rest of the block */
1638 FLAC__ASSERT(used_bits % 8 == 0);
1639 length -= (used_bits / 8);
1640 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1641 return false; /* read_callback_ sets the state for us */
1646 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1651 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1653 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1654 decoder->private_->seek_table.is_last = is_last;
1655 decoder->private_->seek_table.length = length;
1657 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1659 /* use realloc since we may pass through here several times (e.g. after seeking) */
1660 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)))) {
1661 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1664 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1665 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
1666 return false; /* read_callback_ sets the state for us */
1667 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1669 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
1670 return false; /* read_callback_ sets the state for us */
1671 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1673 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
1674 return false; /* read_callback_ sets the state for us */
1675 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1677 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1678 /* if there is a partial point left, skip over it */
1680 /*@@@ do a send_error_to_client_() here? there's an argument for either way */
1681 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1682 return false; /* read_callback_ sets the state for us */
1688 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1692 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1694 /* read vendor string */
1695 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1696 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
1697 return false; /* read_callback_ sets the state for us */
1698 if(obj->vendor_string.length > 0) {
1699 if(0 == (obj->vendor_string.entry = safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) {
1700 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1703 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
1704 return false; /* read_callback_ sets the state for us */
1705 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
1708 obj->vendor_string.entry = 0;
1710 /* read num comments */
1711 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1712 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
1713 return false; /* read_callback_ sets the state for us */
1716 if(obj->num_comments > 0) {
1717 if(0 == (obj->comments = safe_malloc_mul_2op_p(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1718 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1721 for(i = 0; i < obj->num_comments; i++) {
1722 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1723 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
1724 return false; /* read_callback_ sets the state for us */
1725 if(obj->comments[i].length > 0) {
1726 if(0 == (obj->comments[i].entry = safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) {
1727 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1730 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
1731 return false; /* read_callback_ sets the state for us */
1732 obj->comments[i].entry[obj->comments[i].length] = '\0';
1735 obj->comments[i].entry = 0;
1745 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1747 FLAC__uint32 i, j, x;
1749 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1751 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1753 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1754 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))
1755 return false; /* read_callback_ sets the state for us */
1757 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
1758 return false; /* read_callback_ sets the state for us */
1760 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
1761 return false; /* read_callback_ sets the state for us */
1762 obj->is_cd = x? true : false;
1764 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
1765 return false; /* read_callback_ sets the state for us */
1767 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
1768 return false; /* read_callback_ sets the state for us */
1769 obj->num_tracks = x;
1771 if(obj->num_tracks > 0) {
1772 if(0 == (obj->tracks = safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1773 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1776 for(i = 0; i < obj->num_tracks; i++) {
1777 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1778 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
1779 return false; /* read_callback_ sets the state for us */
1781 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
1782 return false; /* read_callback_ sets the state for us */
1783 track->number = (FLAC__byte)x;
1785 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1786 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
1787 return false; /* read_callback_ sets the state for us */
1789 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
1790 return false; /* read_callback_ sets the state for us */
1793 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
1794 return false; /* read_callback_ sets the state for us */
1795 track->pre_emphasis = x;
1797 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
1798 return false; /* read_callback_ sets the state for us */
1800 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
1801 return false; /* read_callback_ sets the state for us */
1802 track->num_indices = (FLAC__byte)x;
1804 if(track->num_indices > 0) {
1805 if(0 == (track->indices = safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1806 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1809 for(j = 0; j < track->num_indices; j++) {
1810 FLAC__StreamMetadata_CueSheet_Index *indx = &track->indices[j];
1811 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
1812 return false; /* read_callback_ sets the state for us */
1814 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
1815 return false; /* read_callback_ sets the state for us */
1816 indx->number = (FLAC__byte)x;
1818 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
1819 return false; /* read_callback_ sets the state for us */
1828 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
1832 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1835 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
1836 return false; /* read_callback_ sets the state for us */
1839 /* read MIME type */
1840 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
1841 return false; /* read_callback_ sets the state for us */
1842 if(0 == (obj->mime_type = safe_malloc_add_2op_(x, /*+*/1))) {
1843 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1847 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
1848 return false; /* read_callback_ sets the state for us */
1850 obj->mime_type[x] = '\0';
1852 /* read description */
1853 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
1854 return false; /* read_callback_ sets the state for us */
1855 if(0 == (obj->description = safe_malloc_add_2op_(x, /*+*/1))) {
1856 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1860 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
1861 return false; /* read_callback_ sets the state for us */
1863 obj->description[x] = '\0';
1866 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
1867 return false; /* read_callback_ sets the state for us */
1870 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
1871 return false; /* read_callback_ sets the state for us */
1874 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
1875 return false; /* read_callback_ sets the state for us */
1878 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
1879 return false; /* read_callback_ sets the state for us */
1882 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
1883 return false; /* read_callback_ sets the state for us */
1884 if(0 == (obj->data = safe_malloc_(obj->data_length))) {
1885 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1888 if(obj->data_length > 0) {
1889 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
1890 return false; /* read_callback_ sets the state for us */
1896 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1901 /* skip the version and flags bytes */
1902 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
1903 return false; /* read_callback_ sets the state for us */
1904 /* get the size (in bytes) to skip */
1906 for(i = 0; i < 4; i++) {
1907 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1908 return false; /* read_callback_ sets the state for us */
1912 /* skip the rest of the tag */
1913 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
1914 return false; /* read_callback_ sets the state for us */
1918 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1921 FLAC__bool first = true;
1923 /* If we know the total number of samples in the stream, stop if we've read that many. */
1924 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1925 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
1926 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1927 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1932 /* make sure we're byte aligned */
1933 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
1934 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
1935 return false; /* read_callback_ sets the state for us */
1939 if(decoder->private_->cached) {
1940 x = (FLAC__uint32)decoder->private_->lookahead;
1941 decoder->private_->cached = false;
1944 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1945 return false; /* read_callback_ sets the state for us */
1947 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1948 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1949 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1950 return false; /* read_callback_ sets the state for us */
1952 /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
1953 /* else we have to check if the second byte is the end of a sync code */
1954 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1955 decoder->private_->lookahead = (FLAC__byte)x;
1956 decoder->private_->cached = true;
1958 else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
1959 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1960 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1965 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1973 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
1977 FLAC__int32 mid, side;
1978 unsigned frame_crc; /* the one we calculate from the input stream */
1981 *got_a_frame = false;
1985 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1986 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1987 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
1989 if(!read_frame_header_(decoder))
1991 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
1993 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1995 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1997 * first figure the correct bits-per-sample of the subframe
1999 unsigned bps = decoder->private_->frame.header.bits_per_sample;
2000 switch(decoder->private_->frame.header.channel_assignment) {
2001 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2002 /* no adjustment needed */
2004 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2005 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2009 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2010 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2014 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2015 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2025 if(!read_subframe_(decoder, channel, bps, do_full_decode))
2027 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2030 if(!read_zero_padding_(decoder))
2032 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) */
2036 * Read the frame CRC-16 from the footer and check
2038 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2039 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
2040 return false; /* read_callback_ sets the state for us */
2041 if(frame_crc == x) {
2042 if(do_full_decode) {
2043 /* Undo any special channel coding */
2044 switch(decoder->private_->frame.header.channel_assignment) {
2045 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2048 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2049 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2050 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2051 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
2053 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2054 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2055 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2056 decoder->private_->output[0][i] += decoder->private_->output[1][i];
2058 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2059 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2060 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2062 mid = decoder->private_->output[0][i];
2063 side = decoder->private_->output[1][i];
2065 mid |= (side & 1); /* i.e. if 'side' is odd... */
2066 decoder->private_->output[0][i] = (mid + side) >> 1;
2067 decoder->private_->output[1][i] = (mid - side) >> 1;
2069 /* OPT: without 'side' temp variable */
2070 mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */
2071 decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1;
2072 decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1;
2083 /* Bad frame, emit error and zero the output signal */
2084 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2085 if(do_full_decode) {
2086 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2087 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2092 *got_a_frame = true;
2094 /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */
2095 if(decoder->private_->next_fixed_block_size)
2096 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;
2098 /* put the latest values into the public section of the decoder instance */
2099 decoder->protected_->channels = decoder->private_->frame.header.channels;
2100 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2101 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2102 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2103 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2105 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2106 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2109 if(do_full_decode) {
2110 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
2114 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2118 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2122 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
2123 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2124 unsigned raw_header_len;
2125 FLAC__bool is_unparseable = false;
2127 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2129 /* init the raw header with the saved bits from synchronization */
2130 raw_header[0] = decoder->private_->header_warmup[0];
2131 raw_header[1] = decoder->private_->header_warmup[1];
2134 /* check to make sure that reserved bit is 0 */
2135 if(raw_header[1] & 0x02) /* MAGIC NUMBER */
2136 is_unparseable = true;
2139 * Note that along the way as we read the header, we look for a sync
2140 * code inside. If we find one it would indicate that our original
2141 * sync was bad since there cannot be a sync code in a valid header.
2143 * Three kinds of things can go wrong when reading the frame header:
2144 * 1) We may have sync'ed incorrectly and not landed on a frame header.
2145 * If we don't find a sync code, it can end up looking like we read
2146 * a valid but unparseable header, until getting to the frame header
2147 * CRC. Even then we could get a false positive on the CRC.
2148 * 2) We may have sync'ed correctly but on an unparseable frame (from a
2150 * 3) We may be on a damaged frame which appears valid but unparseable.
2152 * For all these reasons, we try and read a complete frame header as
2153 * long as it seems valid, even if unparseable, up until the frame
2158 * read in the raw header as bytes so we can CRC it, and parse it on the way
2160 for(i = 0; i < 2; i++) {
2161 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2162 return false; /* read_callback_ sets the state for us */
2163 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2164 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
2165 decoder->private_->lookahead = (FLAC__byte)x;
2166 decoder->private_->cached = true;
2167 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2168 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2171 raw_header[raw_header_len++] = (FLAC__byte)x;
2174 switch(x = raw_header[2] >> 4) {
2176 is_unparseable = true;
2179 decoder->private_->frame.header.blocksize = 192;
2185 decoder->private_->frame.header.blocksize = 576 << (x-2);
2199 decoder->private_->frame.header.blocksize = 256 << (x-8);
2206 switch(x = raw_header[2] & 0x0f) {
2208 if(decoder->private_->has_stream_info)
2209 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2211 is_unparseable = true;
2214 decoder->private_->frame.header.sample_rate = 88200;
2217 decoder->private_->frame.header.sample_rate = 176400;
2220 decoder->private_->frame.header.sample_rate = 192000;
2223 decoder->private_->frame.header.sample_rate = 8000;
2226 decoder->private_->frame.header.sample_rate = 16000;
2229 decoder->private_->frame.header.sample_rate = 22050;
2232 decoder->private_->frame.header.sample_rate = 24000;
2235 decoder->private_->frame.header.sample_rate = 32000;
2238 decoder->private_->frame.header.sample_rate = 44100;
2241 decoder->private_->frame.header.sample_rate = 48000;
2244 decoder->private_->frame.header.sample_rate = 96000;
2249 sample_rate_hint = x;
2252 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2253 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2259 x = (unsigned)(raw_header[3] >> 4);
2261 decoder->private_->frame.header.channels = 2;
2264 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2267 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2270 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2273 is_unparseable = true;
2278 decoder->private_->frame.header.channels = (unsigned)x + 1;
2279 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2282 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
2284 if(decoder->private_->has_stream_info)
2285 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2287 is_unparseable = true;
2290 decoder->private_->frame.header.bits_per_sample = 8;
2293 decoder->private_->frame.header.bits_per_sample = 12;
2296 decoder->private_->frame.header.bits_per_sample = 16;
2299 decoder->private_->frame.header.bits_per_sample = 20;
2302 decoder->private_->frame.header.bits_per_sample = 24;
2306 is_unparseable = true;
2313 /* check to make sure that reserved bit is 0 */
2314 if(raw_header[3] & 0x01) /* MAGIC NUMBER */
2315 is_unparseable = true;
2317 /* read the frame's starting sample number (or frame number as the case may be) */
2319 raw_header[1] & 0x01 ||
2320 /*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */
2321 (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
2322 ) { /* variable blocksize */
2323 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2324 return false; /* read_callback_ sets the state for us */
2325 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2326 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2327 decoder->private_->cached = true;
2328 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2329 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2332 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2333 decoder->private_->frame.header.number.sample_number = xx;
2335 else { /* fixed blocksize */
2336 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2337 return false; /* read_callback_ sets the state for us */
2338 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2339 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2340 decoder->private_->cached = true;
2341 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2342 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2345 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2346 decoder->private_->frame.header.number.frame_number = x;
2349 if(blocksize_hint) {
2350 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2351 return false; /* read_callback_ sets the state for us */
2352 raw_header[raw_header_len++] = (FLAC__byte)x;
2353 if(blocksize_hint == 7) {
2355 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2356 return false; /* read_callback_ sets the state for us */
2357 raw_header[raw_header_len++] = (FLAC__byte)_x;
2360 decoder->private_->frame.header.blocksize = x+1;
2363 if(sample_rate_hint) {
2364 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2365 return false; /* read_callback_ sets the state for us */
2366 raw_header[raw_header_len++] = (FLAC__byte)x;
2367 if(sample_rate_hint != 12) {
2369 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2370 return false; /* read_callback_ sets the state for us */
2371 raw_header[raw_header_len++] = (FLAC__byte)_x;
2374 if(sample_rate_hint == 12)
2375 decoder->private_->frame.header.sample_rate = x*1000;
2376 else if(sample_rate_hint == 13)
2377 decoder->private_->frame.header.sample_rate = x;
2379 decoder->private_->frame.header.sample_rate = x*10;
2382 /* read the CRC-8 byte */
2383 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2384 return false; /* read_callback_ sets the state for us */
2385 crc8 = (FLAC__byte)x;
2387 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
2388 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2389 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2393 /* calculate the sample number from the frame number if needed */
2394 decoder->private_->next_fixed_block_size = 0;
2395 if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
2396 x = decoder->private_->frame.header.number.frame_number;
2397 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2398 if(decoder->private_->fixed_block_size)
2399 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
2400 else if(decoder->private_->has_stream_info) {
2401 if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
2402 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2403 decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
2406 is_unparseable = true;
2409 decoder->private_->frame.header.number.sample_number = 0;
2410 decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
2413 /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */
2414 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2418 if(is_unparseable) {
2419 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2420 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2427 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2430 FLAC__bool wasted_bits;
2433 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2434 return false; /* read_callback_ sets the state for us */
2436 wasted_bits = (x & 1);
2441 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2442 return false; /* read_callback_ sets the state for us */
2443 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2444 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2447 decoder->private_->frame.subframes[channel].wasted_bits = 0;
2450 * Lots of magic numbers here
2453 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2454 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2458 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2462 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2466 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2467 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2471 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
2473 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2477 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2478 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2482 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
2484 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2488 if(wasted_bits && do_full_decode) {
2489 x = decoder->private_->frame.subframes[channel].wasted_bits;
2490 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2491 decoder->private_->output[channel][i] <<= x;
2497 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2499 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
2502 FLAC__int32 *output = decoder->private_->output[channel];
2504 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2506 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2507 return false; /* read_callback_ sets the state for us */
2509 subframe->value = x;
2511 /* decode the subframe */
2512 if(do_full_decode) {
2513 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2520 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2522 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
2527 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
2529 subframe->residual = decoder->private_->residual[channel];
2530 subframe->order = order;
2532 /* read warm-up samples */
2533 for(u = 0; u < order; u++) {
2534 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2535 return false; /* read_callback_ sets the state for us */
2536 subframe->warmup[u] = i32;
2539 /* read entropy coding method info */
2540 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2541 return false; /* read_callback_ sets the state for us */
2542 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2543 switch(subframe->entropy_coding_method.type) {
2544 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2545 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2546 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2547 return false; /* read_callback_ sets the state for us */
2548 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2549 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2552 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2553 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2558 switch(subframe->entropy_coding_method.type) {
2559 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2560 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2561 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))
2568 /* decode the subframe */
2569 if(do_full_decode) {
2570 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2571 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
2577 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2579 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
2584 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
2586 subframe->residual = decoder->private_->residual[channel];
2587 subframe->order = order;
2589 /* read warm-up samples */
2590 for(u = 0; u < order; u++) {
2591 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2592 return false; /* read_callback_ sets the state for us */
2593 subframe->warmup[u] = i32;
2596 /* read qlp coeff precision */
2597 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
2598 return false; /* read_callback_ sets the state for us */
2599 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
2600 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2601 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2604 subframe->qlp_coeff_precision = u32+1;
2606 /* read qlp shift */
2607 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
2608 return false; /* read_callback_ sets the state for us */
2609 subframe->quantization_level = i32;
2611 /* read quantized lp coefficiencts */
2612 for(u = 0; u < order; u++) {
2613 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
2614 return false; /* read_callback_ sets the state for us */
2615 subframe->qlp_coeff[u] = i32;
2618 /* read entropy coding method info */
2619 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2620 return false; /* read_callback_ sets the state for us */
2621 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2622 switch(subframe->entropy_coding_method.type) {
2623 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2624 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2625 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2626 return false; /* read_callback_ sets the state for us */
2627 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2628 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2631 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2632 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2637 switch(subframe->entropy_coding_method.type) {
2638 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2639 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2640 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))
2647 /* decode the subframe */
2648 if(do_full_decode) {
2649 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2650 /*@@@@@@ technically not pessimistic enough, should be more like
2651 if( (FLAC__uint64)order * ((((FLAC__uint64)1)<<bps)-1) * ((1<<subframe->qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) )
2653 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2654 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
2656 decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2658 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);
2661 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);
2663 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);
2669 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2671 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2672 FLAC__int32 x, *residual = decoder->private_->residual[channel];
2675 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
2677 subframe->data = residual;
2679 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2680 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2681 return false; /* read_callback_ sets the state for us */
2685 /* decode the subframe */
2687 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2692 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)
2694 FLAC__uint32 rice_parameter;
2696 unsigned partition, sample, u;
2697 const unsigned partitions = 1u << partition_order;
2698 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2699 const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2700 const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2703 if(partition_order == 0) {
2704 if(decoder->private_->frame.header.blocksize < predictor_order) {
2705 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2706 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2711 if(partition_samples < predictor_order) {
2712 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2713 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2718 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order))) {
2719 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2724 for(partition = 0; partition < partitions; partition++) {
2725 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
2726 return false; /* read_callback_ sets the state for us */
2727 partitioned_rice_contents->parameters[partition] = rice_parameter;
2728 if(rice_parameter < pesc) {
2729 partitioned_rice_contents->raw_bits[partition] = 0;
2730 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2731 if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
2732 return false; /* read_callback_ sets the state for us */
2736 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
2737 return false; /* read_callback_ sets the state for us */
2738 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2739 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2740 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
2741 return false; /* read_callback_ sets the state for us */
2742 residual[sample] = i;
2750 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2752 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2753 FLAC__uint32 zero = 0;
2754 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2755 return false; /* read_callback_ sets the state for us */
2757 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2758 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2764 FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
2766 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2770 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2771 !decoder->private_->is_ogg &&
2773 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2776 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2779 else if(*bytes > 0) {
2780 /* While seeking, it is possible for our seek to land in the
2781 * middle of audio data that looks exactly like a frame header
2782 * from a future version of an encoder. When that happens, our
2783 * error callback will get an
2784 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
2785 * unparseable_frame_count. But there is a remote possibility
2786 * that it is properly synced at such a "future-codec frame",
2787 * so to make sure, we wait to see many "unparseable" errors in
2788 * a row before bailing out.
2790 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2791 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2795 const FLAC__StreamDecoderReadStatus status =
2797 decoder->private_->is_ogg?
2798 read_callback_ogg_aspect_(decoder, buffer, bytes) :
2800 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2802 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
2803 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2806 else if(*bytes == 0) {
2808 status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
2811 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2812 !decoder->private_->is_ogg &&
2814 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2817 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2828 /* abort to avoid a deadlock */
2829 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2832 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
2833 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2834 * and at the same time hit the end of the stream (for example, seeking
2835 * to a point that is after the beginning of the last Ogg page). There
2836 * is no way to report an Ogg sync loss through the callbacks (see note
2837 * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
2838 * So to keep the decoder from stopping at this point we gate the call
2839 * to the eof_callback and let the Ogg decoder aspect set the
2840 * end-of-stream state when it is needed.
2845 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
2847 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2848 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
2849 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2850 /* we don't really have a way to handle lost sync via read
2851 * callback so we'll let it pass and let the underlying
2852 * FLAC decoder catch the error
2854 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
2855 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2856 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
2857 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2858 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
2859 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
2860 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
2861 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
2862 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
2863 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2866 /* double protection */
2867 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2871 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2873 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2875 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2876 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
2877 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
2878 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
2879 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
2880 case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
2881 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2883 /* double protection: */
2885 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2890 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2892 if(decoder->private_->is_seeking) {
2893 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
2894 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
2895 FLAC__uint64 target_sample = decoder->private_->target_sample;
2897 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2900 decoder->private_->got_a_frame = true;
2902 decoder->private_->last_frame = *frame; /* save the frame */
2903 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
2904 unsigned delta = (unsigned)(target_sample - this_frame_sample);
2905 /* kick out of seek mode */
2906 decoder->private_->is_seeking = false;
2907 /* shift out the samples before target_sample */
2910 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
2911 for(channel = 0; channel < frame->header.channels; channel++)
2912 newbuffer[channel] = buffer[channel] + delta;
2913 decoder->private_->last_frame.header.blocksize -= delta;
2914 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2915 /* write the relevant samples */
2916 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2919 /* write the relevant samples */
2920 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2924 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2929 * If we never got STREAMINFO, turn off MD5 checking to save
2930 * cycles since we don't have a sum to compare to anyway
2932 if(!decoder->private_->has_stream_info)
2933 decoder->private_->do_md5_checking = false;
2934 if(decoder->private_->do_md5_checking) {
2935 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2936 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2938 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2942 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2944 if(!decoder->private_->is_seeking)
2945 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2946 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
2947 decoder->private_->unparseable_frame_count++;
2950 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
2952 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
2953 FLAC__int64 pos = -1;
2955 unsigned approx_bytes_per_frame;
2956 FLAC__bool first_seek = true;
2957 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
2958 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2959 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
2960 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
2961 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
2962 /* take these from the current frame in case they've changed mid-stream */
2963 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2964 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
2965 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
2967 /* use values from stream info if we didn't decode a frame */
2969 channels = decoder->private_->stream_info.data.stream_info.channels;
2971 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2973 /* we are just guessing here */
2974 if(max_framesize > 0)
2975 approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
2977 * Check if it's a known fixed-blocksize stream. Note that though
2978 * the spec doesn't allow zeroes in the STREAMINFO block, we may
2979 * never get a STREAMINFO block when decoding so the value of
2980 * min_blocksize might be zero.
2982 else if(min_blocksize == max_blocksize && min_blocksize > 0) {
2983 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
2984 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
2987 approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
2990 * First, we set an upper and lower bound on where in the
2991 * stream we will search. For now we assume the worst case
2992 * scenario, which is our best guess at the beginning of
2993 * the first frame and end of the stream.
2995 lower_bound = first_frame_offset;
2996 lower_bound_sample = 0;
2997 upper_bound = stream_length;
2998 upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
3001 * Now we refine the bounds if we have a seektable with
3002 * suitable points. Note that according to the spec they
3003 * must be ordered by ascending sample number.
3005 * Note: to protect against invalid seek tables we will ignore points
3006 * that have frame_samples==0 or sample_number>=total_samples
3009 FLAC__uint64 new_lower_bound = lower_bound;
3010 FLAC__uint64 new_upper_bound = upper_bound;
3011 FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
3012 FLAC__uint64 new_upper_bound_sample = upper_bound_sample;
3014 /* find the closest seek point <= target_sample, if it exists */
3015 for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
3017 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3018 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3019 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3020 seek_table->points[i].sample_number <= target_sample
3024 if(i >= 0) { /* i.e. we found a suitable seek point... */
3025 new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
3026 new_lower_bound_sample = seek_table->points[i].sample_number;
3029 /* find the closest seek point > target_sample, if it exists */
3030 for(i = 0; i < (int)seek_table->num_points; i++) {
3032 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3033 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3034 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3035 seek_table->points[i].sample_number > target_sample
3039 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3040 new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3041 new_upper_bound_sample = seek_table->points[i].sample_number;
3043 /* final protection against unsorted seek tables; keep original values if bogus */
3044 if(new_upper_bound >= new_lower_bound) {
3045 lower_bound = new_lower_bound;
3046 upper_bound = new_upper_bound;
3047 lower_bound_sample = new_lower_bound_sample;
3048 upper_bound_sample = new_upper_bound_sample;
3052 FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
3053 /* there are 2 insidious ways that the following equality occurs, which
3055 * 1) total_samples is 0 (unknown) and target_sample is 0
3056 * 2) total_samples is 0 (unknown) and target_sample happens to be
3057 * exactly equal to the last seek point in the seek table; this
3058 * means there is no seek point above it, and upper_bound_samples
3059 * remains equal to the estimate (of target_samples) we made above
3060 * in either case it does not hurt to move upper_bound_sample up by 1
3062 if(upper_bound_sample == lower_bound_sample)
3063 upper_bound_sample++;
3065 decoder->private_->target_sample = target_sample;
3067 /* check if the bounds are still ok */
3068 if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
3069 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3072 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3073 #if defined _MSC_VER || defined __MINGW32__
3074 /* with VC++ you have to spoon feed it the casting */
3075 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;
3077 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;
3080 /* a little less accurate: */
3081 if(upper_bound - lower_bound < 0xffffffff)
3082 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;
3083 else /* @@@ WATCHOUT, ~2TB limit */
3084 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;
3086 if(pos >= (FLAC__int64)upper_bound)
3087 pos = (FLAC__int64)upper_bound - 1;
3088 if(pos < (FLAC__int64)lower_bound)
3089 pos = (FLAC__int64)lower_bound;
3090 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3091 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3094 if(!FLAC__stream_decoder_flush(decoder)) {
3095 /* above call sets the state for us */
3098 /* Now we need to get a frame. First we need to reset our
3099 * unparseable_frame_count; if we get too many unparseable
3100 * frames in a row, the read callback will return
3101 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3102 * FLAC__stream_decoder_process_single() to return false.
3104 decoder->private_->unparseable_frame_count = 0;
3105 if(!FLAC__stream_decoder_process_single(decoder)) {
3106 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3109 /* our write callback will change the state when it gets to the target frame */
3110 /* 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 */
3112 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
3113 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3116 if(!decoder->private_->is_seeking)
3119 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3120 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3122 if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
3123 if (pos == (FLAC__int64)lower_bound) {
3124 /* can't move back any more than the first frame, something is fatally wrong */
3125 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3128 /* our last move backwards wasn't big enough, try again */
3129 approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
3132 /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */
3135 /* make sure we are not seeking in corrupted stream */
3136 if (this_frame_sample < lower_bound_sample) {
3137 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3141 /* we need to narrow the search */
3142 if(target_sample < this_frame_sample) {
3143 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3144 /*@@@@@@ what will decode position be if at end of stream? */
3145 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3146 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3149 approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
3151 else { /* target_sample >= this_frame_sample + this frame's blocksize */
3152 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3153 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3154 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3157 approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
3165 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3167 FLAC__uint64 left_pos = 0, right_pos = stream_length;
3168 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3169 FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1;
3170 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3171 FLAC__bool did_a_seek;
3172 unsigned iteration = 0;
3174 /* In the first iterations, we will calculate the target byte position
3175 * by the distance from the target sample to left_sample and
3176 * right_sample (let's call it "proportional search"). After that, we
3177 * will switch to binary search.
3179 unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
3181 /* We will switch to a linear search once our current sample is less
3182 * than this number of samples ahead of the target sample
3184 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3186 /* If the total number of samples is unknown, use a large value, and
3187 * force binary search immediately.
3189 if(right_sample == 0) {
3190 right_sample = (FLAC__uint64)(-1);
3191 BINARY_SEARCH_AFTER_ITERATION = 0;
3194 decoder->private_->target_sample = target_sample;
3195 for( ; ; iteration++) {
3196 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3197 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3198 pos = (right_pos + left_pos) / 2;
3201 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3202 #if defined _MSC_VER || defined __MINGW32__
3203 /* with MSVC you have to spoon feed it the casting */
3204 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));
3206 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
3209 /* a little less accurate: */
3210 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
3211 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
3212 else /* @@@ WATCHOUT, ~2TB limit */
3213 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
3215 /* @@@ TODO: might want to limit pos to some distance
3216 * before EOF, to make sure we land before the last frame,
3217 * thereby getting a this_frame_sample and so having a better
3223 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3224 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3227 if(!FLAC__stream_decoder_flush(decoder)) {
3228 /* above call sets the state for us */
3236 decoder->private_->got_a_frame = false;
3237 if(!FLAC__stream_decoder_process_single(decoder)) {
3238 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3241 if(!decoder->private_->got_a_frame) {
3243 /* this can happen if we seek to a point after the last frame; we drop
3244 * to binary search right away in this case to avoid any wasted
3245 * iterations of proportional search.
3248 BINARY_SEARCH_AFTER_ITERATION = 0;
3251 /* this can probably only happen if total_samples is unknown and the
3252 * target_sample is past the end of the stream
3254 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3258 /* our write callback will change the state when it gets to the target frame */
3259 else if(!decoder->private_->is_seeking) {
3263 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3264 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3267 if (this_frame_sample <= target_sample) {
3268 /* The 'equal' case should not happen, since
3269 * FLAC__stream_decoder_process_single()
3270 * should recognize that it has hit the
3271 * target sample and we would exit through
3272 * the 'break' above.
3274 FLAC__ASSERT(this_frame_sample != target_sample);
3276 left_sample = this_frame_sample;
3277 /* sanity check to avoid infinite loop */
3278 if (left_pos == pos) {
3279 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3284 else if(this_frame_sample > target_sample) {
3285 right_sample = this_frame_sample;
3286 /* sanity check to avoid infinite loop */
3287 if (right_pos == pos) {
3288 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3301 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3306 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3307 if(ferror(decoder->private_->file))
3308 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3309 else if(*bytes == 0)
3310 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3312 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3315 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
3318 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3322 if(decoder->private_->file == stdin)
3323 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3324 else if(fseeko(decoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
3325 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3327 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3330 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3335 if(decoder->private_->file == stdin)
3336 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3337 else if((pos = ftello(decoder->private_->file)) < 0)
3338 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3340 *absolute_byte_offset = (FLAC__uint64)pos;
3341 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3345 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3347 struct flac_stat_s filestats;
3350 if(decoder->private_->file == stdin)
3351 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3352 else if(flac_fstat(fileno(decoder->private_->file), &filestats) != 0)
3353 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3355 *stream_length = (FLAC__uint64)filestats.st_size;
3356 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3360 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3364 return feof(decoder->private_->file)? true : false;