1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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.
36 #if defined _MSC_VER || defined __MINGW32__
37 #include <io.h> /* for _setmode() */
38 #include <fcntl.h> /* for _O_BINARY */
40 #if defined __CYGWIN__ || defined __EMX__
41 #include <io.h> /* for setmode(), O_BINARY */
42 #include <fcntl.h> /* for _O_BINARY */
45 #include <stdlib.h> /* for malloc() */
46 #include <string.h> /* for memset/memcpy() */
47 #include <sys/stat.h> /* for stat() */
48 #include <sys/types.h> /* for off_t */
49 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
50 #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
55 #include "FLAC/assert.h"
56 #include "protected/stream_decoder.h"
57 #include "private/bitreader.h"
58 #include "private/bitmath.h"
59 #include "private/cpu.h"
60 #include "private/crc.h"
61 #include "private/fixed.h"
62 #include "private/format.h"
63 #include "private/lpc.h"
64 #include "private/md5.h"
65 #include "private/memory.h"
70 #define max(a,b) ((a)>(b)?(a):(b))
72 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
74 #define FLAC__U64L(x) x
76 #define FLAC__U64L(x) x##LLU
80 /* technically this should be in an "export.c" but this is convenient enough */
81 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
90 /***********************************************************************
94 ***********************************************************************/
96 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
98 /***********************************************************************
100 * Private class method prototypes
102 ***********************************************************************/
104 static void set_defaults_(FLAC__StreamDecoder *decoder);
105 static FILE *get_binary_stdin_(void);
106 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
107 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
108 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
109 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
110 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
111 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
112 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
113 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
114 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
115 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
116 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
117 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
118 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
119 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
120 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
121 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
122 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
123 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
124 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);
125 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
126 static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
128 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
129 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
131 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
132 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
133 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
135 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
137 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
138 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
139 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
140 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
141 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
143 /***********************************************************************
147 ***********************************************************************/
149 typedef struct FLAC__StreamDecoderPrivate {
153 FLAC__StreamDecoderReadCallback read_callback;
154 FLAC__StreamDecoderSeekCallback seek_callback;
155 FLAC__StreamDecoderTellCallback tell_callback;
156 FLAC__StreamDecoderLengthCallback length_callback;
157 FLAC__StreamDecoderEofCallback eof_callback;
158 FLAC__StreamDecoderWriteCallback write_callback;
159 FLAC__StreamDecoderMetadataCallback metadata_callback;
160 FLAC__StreamDecoderErrorCallback error_callback;
161 /* generic 32-bit datapath: */
162 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[]);
163 /* generic 64-bit datapath: */
164 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[]);
165 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
166 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[]);
167 /* 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: */
168 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[]);
169 FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
171 FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
172 FLAC__BitReader *input;
173 FLAC__int32 *output[FLAC__MAX_CHANNELS];
174 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
175 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
176 unsigned output_capacity, output_channels;
177 FLAC__uint32 fixed_block_size, next_fixed_block_size;
178 FLAC__uint64 samples_decoded;
179 FLAC__bool has_stream_info, has_seek_table;
180 FLAC__StreamMetadata stream_info;
181 FLAC__StreamMetadata seek_table;
182 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
183 FLAC__byte *metadata_filter_ids;
184 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
186 FLAC__bool cached; /* true if there is a byte in lookahead */
187 FLAC__CPUInfo cpuinfo;
188 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
189 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
190 /* unaligned (original) pointers to allocated data */
191 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
192 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 */
193 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
194 FLAC__bool is_seeking;
195 FLAC__MD5Context md5context;
196 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
197 /* (the rest of these are only used for seeking) */
198 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
199 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
200 FLAC__uint64 target_sample;
201 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
203 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
205 } FLAC__StreamDecoderPrivate;
207 /***********************************************************************
209 * Public static class data
211 ***********************************************************************/
213 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
214 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
215 "FLAC__STREAM_DECODER_READ_METADATA",
216 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
217 "FLAC__STREAM_DECODER_READ_FRAME",
218 "FLAC__STREAM_DECODER_END_OF_STREAM",
219 "FLAC__STREAM_DECODER_OGG_ERROR",
220 "FLAC__STREAM_DECODER_SEEK_ERROR",
221 "FLAC__STREAM_DECODER_ABORTED",
222 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
223 "FLAC__STREAM_DECODER_UNINITIALIZED"
226 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
227 "FLAC__STREAM_DECODER_INIT_STATUS_OK",
228 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
229 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
230 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
231 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
232 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
235 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
236 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
237 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
238 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
241 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
242 "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
243 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
244 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
247 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
248 "FLAC__STREAM_DECODER_TELL_STATUS_OK",
249 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
250 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
253 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
254 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
255 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
256 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
259 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
260 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
261 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
264 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
265 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
266 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
267 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
268 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
271 /***********************************************************************
273 * Class constructor/destructor
275 ***********************************************************************/
276 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
278 FLAC__StreamDecoder *decoder;
281 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
283 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
288 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
289 if(decoder->protected_ == 0) {
294 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
295 if(decoder->private_ == 0) {
296 free(decoder->protected_);
301 decoder->private_->input = FLAC__bitreader_new();
302 if(decoder->private_->input == 0) {
303 free(decoder->private_);
304 free(decoder->protected_);
309 decoder->private_->metadata_filter_ids_capacity = 16;
310 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
311 FLAC__bitreader_delete(decoder->private_->input);
312 free(decoder->private_);
313 free(decoder->protected_);
318 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
319 decoder->private_->output[i] = 0;
320 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
323 decoder->private_->output_capacity = 0;
324 decoder->private_->output_channels = 0;
325 decoder->private_->has_seek_table = false;
327 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
328 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
330 decoder->private_->file = 0;
332 set_defaults_(decoder);
334 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
339 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
343 FLAC__ASSERT(0 != decoder);
344 FLAC__ASSERT(0 != decoder->protected_);
345 FLAC__ASSERT(0 != decoder->private_);
346 FLAC__ASSERT(0 != decoder->private_->input);
348 (void)FLAC__stream_decoder_finish(decoder);
350 if(0 != decoder->private_->metadata_filter_ids)
351 free(decoder->private_->metadata_filter_ids);
353 FLAC__bitreader_delete(decoder->private_->input);
355 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
356 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
358 free(decoder->private_);
359 free(decoder->protected_);
363 /***********************************************************************
365 * Public class methods
367 ***********************************************************************/
369 static FLAC__StreamDecoderInitStatus init_stream_internal_(
370 FLAC__StreamDecoder *decoder,
371 FLAC__StreamDecoderReadCallback read_callback,
372 FLAC__StreamDecoderSeekCallback seek_callback,
373 FLAC__StreamDecoderTellCallback tell_callback,
374 FLAC__StreamDecoderLengthCallback length_callback,
375 FLAC__StreamDecoderEofCallback eof_callback,
376 FLAC__StreamDecoderWriteCallback write_callback,
377 FLAC__StreamDecoderMetadataCallback metadata_callback,
378 FLAC__StreamDecoderErrorCallback error_callback,
383 FLAC__ASSERT(0 != decoder);
385 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
386 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
390 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
394 0 == read_callback ||
395 0 == write_callback ||
396 0 == error_callback ||
397 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
399 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
402 decoder->private_->is_ogg = is_ogg;
403 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
404 return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
408 * get the CPU info and set the function pointers
410 FLAC__cpu_info(&decoder->private_->cpuinfo);
411 /* first default to the non-asm routines */
412 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
413 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
414 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
415 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
416 decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
417 /* now override with asm where appropriate */
419 if(decoder->private_->cpuinfo.use_asm) {
420 #ifdef FLAC__CPU_IA32
421 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
422 #ifdef FLAC__HAS_NASM
423 #if 0 /*@@@@@@ OPT: not clearly faster, needs more testing */
424 if(decoder->private_->cpuinfo.data.ia32.bswap)
425 decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap;
427 if(decoder->private_->cpuinfo.data.ia32.mmx) {
428 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
429 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
430 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
433 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
434 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
435 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
438 #elif defined FLAC__CPU_PPC
439 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
440 if(decoder->private_->cpuinfo.data.ppc.altivec) {
441 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
442 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
448 /* from here on, errors are fatal */
450 if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) {
451 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
452 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
455 decoder->private_->read_callback = read_callback;
456 decoder->private_->seek_callback = seek_callback;
457 decoder->private_->tell_callback = tell_callback;
458 decoder->private_->length_callback = length_callback;
459 decoder->private_->eof_callback = eof_callback;
460 decoder->private_->write_callback = write_callback;
461 decoder->private_->metadata_callback = metadata_callback;
462 decoder->private_->error_callback = error_callback;
463 decoder->private_->client_data = client_data;
464 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
465 decoder->private_->samples_decoded = 0;
466 decoder->private_->has_stream_info = false;
467 decoder->private_->cached = false;
469 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
470 decoder->private_->is_seeking = false;
472 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
473 if(!FLAC__stream_decoder_reset(decoder)) {
474 /* above call sets the state for us */
475 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
478 return FLAC__STREAM_DECODER_INIT_STATUS_OK;
481 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
482 FLAC__StreamDecoder *decoder,
483 FLAC__StreamDecoderReadCallback read_callback,
484 FLAC__StreamDecoderSeekCallback seek_callback,
485 FLAC__StreamDecoderTellCallback tell_callback,
486 FLAC__StreamDecoderLengthCallback length_callback,
487 FLAC__StreamDecoderEofCallback eof_callback,
488 FLAC__StreamDecoderWriteCallback write_callback,
489 FLAC__StreamDecoderMetadataCallback metadata_callback,
490 FLAC__StreamDecoderErrorCallback error_callback,
494 return init_stream_internal_(
509 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
510 FLAC__StreamDecoder *decoder,
511 FLAC__StreamDecoderReadCallback read_callback,
512 FLAC__StreamDecoderSeekCallback seek_callback,
513 FLAC__StreamDecoderTellCallback tell_callback,
514 FLAC__StreamDecoderLengthCallback length_callback,
515 FLAC__StreamDecoderEofCallback eof_callback,
516 FLAC__StreamDecoderWriteCallback write_callback,
517 FLAC__StreamDecoderMetadataCallback metadata_callback,
518 FLAC__StreamDecoderErrorCallback error_callback,
522 return init_stream_internal_(
537 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
538 FLAC__StreamDecoder *decoder,
540 FLAC__StreamDecoderWriteCallback write_callback,
541 FLAC__StreamDecoderMetadataCallback metadata_callback,
542 FLAC__StreamDecoderErrorCallback error_callback,
547 FLAC__ASSERT(0 != decoder);
548 FLAC__ASSERT(0 != file);
550 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
551 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
553 if(0 == write_callback || 0 == error_callback)
554 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
557 * To make sure that our file does not go unclosed after an error, we
558 * must assign the FILE pointer before any further error can occur in
562 file = get_binary_stdin_(); /* just to be safe */
564 decoder->private_->file = file;
566 return init_stream_internal_(
569 decoder->private_->file == stdin? 0: file_seek_callback_,
570 decoder->private_->file == stdin? 0: file_tell_callback_,
571 decoder->private_->file == stdin? 0: file_length_callback_,
581 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
582 FLAC__StreamDecoder *decoder,
584 FLAC__StreamDecoderWriteCallback write_callback,
585 FLAC__StreamDecoderMetadataCallback metadata_callback,
586 FLAC__StreamDecoderErrorCallback error_callback,
590 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
593 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
594 FLAC__StreamDecoder *decoder,
596 FLAC__StreamDecoderWriteCallback write_callback,
597 FLAC__StreamDecoderMetadataCallback metadata_callback,
598 FLAC__StreamDecoderErrorCallback error_callback,
602 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
605 static FLAC__StreamDecoderInitStatus init_file_internal_(
606 FLAC__StreamDecoder *decoder,
607 const char *filename,
608 FLAC__StreamDecoderWriteCallback write_callback,
609 FLAC__StreamDecoderMetadataCallback metadata_callback,
610 FLAC__StreamDecoderErrorCallback error_callback,
617 FLAC__ASSERT(0 != decoder);
620 * To make sure that our file does not go unclosed after an error, we
621 * have to do the same entrance checks here that are later performed
622 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
624 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
625 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
627 if(0 == write_callback || 0 == error_callback)
628 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
630 file = filename? fopen(filename, "rb") : stdin;
633 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
635 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
638 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
639 FLAC__StreamDecoder *decoder,
640 const char *filename,
641 FLAC__StreamDecoderWriteCallback write_callback,
642 FLAC__StreamDecoderMetadataCallback metadata_callback,
643 FLAC__StreamDecoderErrorCallback error_callback,
647 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
650 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
651 FLAC__StreamDecoder *decoder,
652 const char *filename,
653 FLAC__StreamDecoderWriteCallback write_callback,
654 FLAC__StreamDecoderMetadataCallback metadata_callback,
655 FLAC__StreamDecoderErrorCallback error_callback,
659 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
662 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
664 FLAC__bool md5_failed = false;
667 FLAC__ASSERT(0 != decoder);
668 FLAC__ASSERT(0 != decoder->private_);
669 FLAC__ASSERT(0 != decoder->protected_);
671 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
674 /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
675 * always call FLAC__MD5Final()
677 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
679 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
680 free(decoder->private_->seek_table.data.seek_table.points);
681 decoder->private_->seek_table.data.seek_table.points = 0;
682 decoder->private_->has_seek_table = false;
684 FLAC__bitreader_free(decoder->private_->input);
685 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
687 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
688 * output arrays have a buffer of up to 3 zeroes in front
689 * (at negative indices) for alignment purposes; we use 4
690 * to keep the data well-aligned.
692 if(0 != decoder->private_->output[i]) {
693 free(decoder->private_->output[i]-4);
694 decoder->private_->output[i] = 0;
696 if(0 != decoder->private_->residual_unaligned[i]) {
697 free(decoder->private_->residual_unaligned[i]);
698 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
701 decoder->private_->output_capacity = 0;
702 decoder->private_->output_channels = 0;
705 if(decoder->private_->is_ogg)
706 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
709 if(0 != decoder->private_->file) {
710 if(decoder->private_->file != stdin)
711 fclose(decoder->private_->file);
712 decoder->private_->file = 0;
715 if(decoder->private_->do_md5_checking) {
716 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
719 decoder->private_->is_seeking = false;
721 set_defaults_(decoder);
723 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
728 FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
730 FLAC__ASSERT(0 != decoder);
731 FLAC__ASSERT(0 != decoder->private_);
732 FLAC__ASSERT(0 != decoder->protected_);
733 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
736 /* can't check decoder->private_->is_ogg since that's not set until init time */
737 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
745 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
747 FLAC__ASSERT(0 != decoder);
748 FLAC__ASSERT(0 != decoder->protected_);
749 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
751 decoder->protected_->md5_checking = value;
755 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
757 FLAC__ASSERT(0 != decoder);
758 FLAC__ASSERT(0 != decoder->private_);
759 FLAC__ASSERT(0 != decoder->protected_);
760 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
761 /* double protection */
762 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
764 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
766 decoder->private_->metadata_filter[type] = true;
767 if(type == FLAC__METADATA_TYPE_APPLICATION)
768 decoder->private_->metadata_filter_ids_count = 0;
772 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
774 FLAC__ASSERT(0 != decoder);
775 FLAC__ASSERT(0 != decoder->private_);
776 FLAC__ASSERT(0 != decoder->protected_);
777 FLAC__ASSERT(0 != id);
778 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
781 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
784 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
786 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
787 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
788 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
791 decoder->private_->metadata_filter_ids_capacity *= 2;
794 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));
795 decoder->private_->metadata_filter_ids_count++;
800 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
803 FLAC__ASSERT(0 != decoder);
804 FLAC__ASSERT(0 != decoder->private_);
805 FLAC__ASSERT(0 != decoder->protected_);
806 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
808 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
809 decoder->private_->metadata_filter[i] = true;
810 decoder->private_->metadata_filter_ids_count = 0;
814 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
816 FLAC__ASSERT(0 != decoder);
817 FLAC__ASSERT(0 != decoder->private_);
818 FLAC__ASSERT(0 != decoder->protected_);
819 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
820 /* double protection */
821 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
823 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
825 decoder->private_->metadata_filter[type] = false;
826 if(type == FLAC__METADATA_TYPE_APPLICATION)
827 decoder->private_->metadata_filter_ids_count = 0;
831 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
833 FLAC__ASSERT(0 != decoder);
834 FLAC__ASSERT(0 != decoder->private_);
835 FLAC__ASSERT(0 != decoder->protected_);
836 FLAC__ASSERT(0 != id);
837 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
840 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
843 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
845 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
846 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
847 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
850 decoder->private_->metadata_filter_ids_capacity *= 2;
853 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));
854 decoder->private_->metadata_filter_ids_count++;
859 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
861 FLAC__ASSERT(0 != decoder);
862 FLAC__ASSERT(0 != decoder->private_);
863 FLAC__ASSERT(0 != decoder->protected_);
864 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
866 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
867 decoder->private_->metadata_filter_ids_count = 0;
871 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
873 FLAC__ASSERT(0 != decoder);
874 FLAC__ASSERT(0 != decoder->protected_);
875 return decoder->protected_->state;
878 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
880 return FLAC__StreamDecoderStateString[decoder->protected_->state];
883 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
885 FLAC__ASSERT(0 != decoder);
886 FLAC__ASSERT(0 != decoder->protected_);
887 return decoder->protected_->md5_checking;
890 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
892 FLAC__ASSERT(0 != decoder);
893 FLAC__ASSERT(0 != decoder->protected_);
894 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
897 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
899 FLAC__ASSERT(0 != decoder);
900 FLAC__ASSERT(0 != decoder->protected_);
901 return decoder->protected_->channels;
904 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
906 FLAC__ASSERT(0 != decoder);
907 FLAC__ASSERT(0 != decoder->protected_);
908 return decoder->protected_->channel_assignment;
911 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
913 FLAC__ASSERT(0 != decoder);
914 FLAC__ASSERT(0 != decoder->protected_);
915 return decoder->protected_->bits_per_sample;
918 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
920 FLAC__ASSERT(0 != decoder);
921 FLAC__ASSERT(0 != decoder->protected_);
922 return decoder->protected_->sample_rate;
925 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
927 FLAC__ASSERT(0 != decoder);
928 FLAC__ASSERT(0 != decoder->protected_);
929 return decoder->protected_->blocksize;
932 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
934 FLAC__ASSERT(0 != decoder);
935 FLAC__ASSERT(0 != decoder->private_);
936 FLAC__ASSERT(0 != position);
939 if(decoder->private_->is_ogg)
942 if(0 == decoder->private_->tell_callback)
944 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
946 /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
947 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
949 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
950 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
954 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
956 FLAC__ASSERT(0 != decoder);
957 FLAC__ASSERT(0 != decoder->private_);
958 FLAC__ASSERT(0 != decoder->protected_);
960 decoder->private_->samples_decoded = 0;
961 decoder->private_->do_md5_checking = false;
964 if(decoder->private_->is_ogg)
965 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
968 if(!FLAC__bitreader_clear(decoder->private_->input)) {
969 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
972 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
977 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
979 FLAC__ASSERT(0 != decoder);
980 FLAC__ASSERT(0 != decoder->private_);
981 FLAC__ASSERT(0 != decoder->protected_);
983 if(!FLAC__stream_decoder_flush(decoder)) {
984 /* above call sets the state for us */
989 /*@@@ could go in !internal_reset_hack block below */
990 if(decoder->private_->is_ogg)
991 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
994 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
995 * (internal_reset_hack) don't try to rewind since we are already at
996 * the beginning of the stream and don't want to fail if the input is
999 if(!decoder->private_->internal_reset_hack) {
1000 if(decoder->private_->file == stdin)
1001 return false; /* can't rewind stdin, reset fails */
1002 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
1003 return false; /* seekable and seek fails, reset fails */
1006 decoder->private_->internal_reset_hack = false;
1008 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
1010 decoder->private_->has_stream_info = false;
1011 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
1012 free(decoder->private_->seek_table.data.seek_table.points);
1013 decoder->private_->seek_table.data.seek_table.points = 0;
1014 decoder->private_->has_seek_table = false;
1016 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
1018 * This goes in reset() and not flush() because according to the spec, a
1019 * fixed-blocksize stream must stay that way through the whole stream.
1021 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
1023 /* We initialize the FLAC__MD5Context even though we may never use it. This
1024 * is because md5 checking may be turned on to start and then turned off if
1025 * a seek occurs. So we init the context here and finalize it in
1026 * FLAC__stream_decoder_finish() to make sure things are always cleaned up
1029 FLAC__MD5Init(&decoder->private_->md5context);
1031 decoder->private_->first_frame_offset = 0;
1032 decoder->private_->unparseable_frame_count = 0;
1037 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1039 FLAC__bool got_a_frame;
1040 FLAC__ASSERT(0 != decoder);
1041 FLAC__ASSERT(0 != decoder->protected_);
1044 switch(decoder->protected_->state) {
1045 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1046 if(!find_metadata_(decoder))
1047 return false; /* above function sets the status for us */
1049 case FLAC__STREAM_DECODER_READ_METADATA:
1050 if(!read_metadata_(decoder))
1051 return false; /* above function sets the status for us */
1054 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1055 if(!frame_sync_(decoder))
1056 return true; /* above function sets the status for us */
1058 case FLAC__STREAM_DECODER_READ_FRAME:
1059 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1060 return false; /* above function sets the status for us */
1062 return true; /* above function sets the status for us */
1064 case FLAC__STREAM_DECODER_END_OF_STREAM:
1065 case FLAC__STREAM_DECODER_ABORTED:
1074 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1076 FLAC__ASSERT(0 != decoder);
1077 FLAC__ASSERT(0 != decoder->protected_);
1080 switch(decoder->protected_->state) {
1081 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1082 if(!find_metadata_(decoder))
1083 return false; /* above function sets the status for us */
1085 case FLAC__STREAM_DECODER_READ_METADATA:
1086 if(!read_metadata_(decoder))
1087 return false; /* above function sets the status for us */
1089 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1090 case FLAC__STREAM_DECODER_READ_FRAME:
1091 case FLAC__STREAM_DECODER_END_OF_STREAM:
1092 case FLAC__STREAM_DECODER_ABORTED:
1101 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1104 FLAC__ASSERT(0 != decoder);
1105 FLAC__ASSERT(0 != decoder->protected_);
1108 switch(decoder->protected_->state) {
1109 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1110 if(!find_metadata_(decoder))
1111 return false; /* above function sets the status for us */
1113 case FLAC__STREAM_DECODER_READ_METADATA:
1114 if(!read_metadata_(decoder))
1115 return false; /* above function sets the status for us */
1117 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1118 if(!frame_sync_(decoder))
1119 return true; /* above function sets the status for us */
1121 case FLAC__STREAM_DECODER_READ_FRAME:
1122 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1123 return false; /* above function sets the status for us */
1125 case FLAC__STREAM_DECODER_END_OF_STREAM:
1126 case FLAC__STREAM_DECODER_ABORTED:
1135 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1137 FLAC__bool got_a_frame;
1138 FLAC__ASSERT(0 != decoder);
1139 FLAC__ASSERT(0 != decoder->protected_);
1142 switch(decoder->protected_->state) {
1143 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1144 case FLAC__STREAM_DECODER_READ_METADATA:
1145 return false; /* above function sets the status for us */
1146 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1147 if(!frame_sync_(decoder))
1148 return true; /* above function sets the status for us */
1150 case FLAC__STREAM_DECODER_READ_FRAME:
1151 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1152 return false; /* above function sets the status for us */
1154 return true; /* above function sets the status for us */
1156 case FLAC__STREAM_DECODER_END_OF_STREAM:
1157 case FLAC__STREAM_DECODER_ABORTED:
1166 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1168 FLAC__uint64 length;
1170 FLAC__ASSERT(0 != decoder);
1173 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1174 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1175 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1176 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1177 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1181 if(0 == decoder->private_->seek_callback)
1184 FLAC__ASSERT(decoder->private_->seek_callback);
1185 FLAC__ASSERT(decoder->private_->tell_callback);
1186 FLAC__ASSERT(decoder->private_->length_callback);
1187 FLAC__ASSERT(decoder->private_->eof_callback);
1189 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1192 decoder->private_->is_seeking = true;
1194 /* turn off md5 checking if a seek is attempted */
1195 decoder->private_->do_md5_checking = false;
1197 /* 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) */
1198 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1199 decoder->private_->is_seeking = false;
1203 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
1205 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1206 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1208 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1209 /* above call sets the state for us */
1210 decoder->private_->is_seeking = false;
1213 /* check this again in case we didn't know total_samples the first time */
1214 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1215 decoder->private_->is_seeking = false;
1221 const FLAC__bool ok =
1223 decoder->private_->is_ogg?
1224 seek_to_absolute_sample_ogg_(decoder, length, sample) :
1226 seek_to_absolute_sample_(decoder, length, sample)
1228 decoder->private_->is_seeking = false;
1233 /***********************************************************************
1235 * Protected class methods
1237 ***********************************************************************/
1239 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1241 FLAC__ASSERT(0 != decoder);
1242 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1243 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
1244 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
1247 /***********************************************************************
1249 * Private class methods
1251 ***********************************************************************/
1253 void set_defaults_(FLAC__StreamDecoder *decoder)
1256 decoder->private_->is_ogg = false;
1258 decoder->private_->read_callback = 0;
1259 decoder->private_->seek_callback = 0;
1260 decoder->private_->tell_callback = 0;
1261 decoder->private_->length_callback = 0;
1262 decoder->private_->eof_callback = 0;
1263 decoder->private_->write_callback = 0;
1264 decoder->private_->metadata_callback = 0;
1265 decoder->private_->error_callback = 0;
1266 decoder->private_->client_data = 0;
1268 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1269 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1270 decoder->private_->metadata_filter_ids_count = 0;
1272 decoder->protected_->md5_checking = false;
1275 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1280 * This will forcibly set stdin to binary mode (for OSes that require it)
1282 FILE *get_binary_stdin_(void)
1284 /* if something breaks here it is probably due to the presence or
1285 * absence of an underscore before the identifiers 'setmode',
1286 * 'fileno', and/or 'O_BINARY'; check your system header files.
1288 #if defined _MSC_VER || defined __MINGW32__
1289 _setmode(_fileno(stdin), _O_BINARY);
1290 #elif defined __CYGWIN__ || defined __EMX__
1291 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
1292 setmode(_fileno(stdin), _O_BINARY);
1298 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1303 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
1306 /* simply using realloc() is not practical because the number of channels may change mid-stream */
1308 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1309 if(0 != decoder->private_->output[i]) {
1310 free(decoder->private_->output[i]-4);
1311 decoder->private_->output[i] = 0;
1313 if(0 != decoder->private_->residual_unaligned[i]) {
1314 free(decoder->private_->residual_unaligned[i]);
1315 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1319 for(i = 0; i < channels; i++) {
1321 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
1322 * output arrays have a buffer of up to 3 zeroes in front
1323 * (at negative indices) for alignment purposes; we use 4
1324 * to keep the data well-aligned.
1326 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
1328 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1331 memset(tmp, 0, sizeof(FLAC__int32)*4);
1332 decoder->private_->output[i] = tmp + 4;
1335 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
1337 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
1338 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1343 decoder->private_->output_capacity = size;
1344 decoder->private_->output_channels = channels;
1349 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1353 FLAC__ASSERT(0 != decoder);
1354 FLAC__ASSERT(0 != decoder->private_);
1356 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1357 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
1363 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1367 FLAC__bool first = true;
1369 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1371 for(i = id = 0; i < 4; ) {
1372 if(decoder->private_->cached) {
1373 x = (FLAC__uint32)decoder->private_->lookahead;
1374 decoder->private_->cached = false;
1377 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1378 return false; /* read_callback_ sets the state for us */
1380 if(x == FLAC__STREAM_SYNC_STRING[i]) {
1386 if(x == ID3V2_TAG_[id]) {
1390 if(!skip_id3v2_tag_(decoder))
1391 return false; /* skip_id3v2_tag_ sets the state for us */
1396 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1397 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1398 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1399 return false; /* read_callback_ sets the state for us */
1401 /* 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 */
1402 /* else we have to check if the second byte is the end of a sync code */
1403 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1404 decoder->private_->lookahead = (FLAC__byte)x;
1405 decoder->private_->cached = true;
1407 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1408 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1409 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1415 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1420 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1424 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1427 FLAC__uint32 i, x, type, length;
1429 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1431 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1432 return false; /* read_callback_ sets the state for us */
1433 is_last = x? true : false;
1435 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
1436 return false; /* read_callback_ sets the state for us */
1438 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1439 return false; /* read_callback_ sets the state for us */
1441 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1442 if(!read_metadata_streaminfo_(decoder, is_last, length))
1445 decoder->private_->has_stream_info = true;
1446 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))
1447 decoder->private_->do_md5_checking = false;
1448 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1449 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1451 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1452 if(!read_metadata_seektable_(decoder, is_last, length))
1455 decoder->private_->has_seek_table = true;
1456 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1457 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1460 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1461 unsigned real_length = length;
1462 FLAC__StreamMetadata block;
1464 block.is_last = is_last;
1465 block.type = (FLAC__MetadataType)type;
1466 block.length = length;
1468 if(type == FLAC__METADATA_TYPE_APPLICATION) {
1469 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1470 return false; /* read_callback_ sets the state for us */
1472 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1474 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1479 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1480 return false; /* read_callback_ sets the state for us */
1484 case FLAC__METADATA_TYPE_PADDING:
1485 /* skip the padding bytes */
1486 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1487 return false; /* read_callback_ sets the state for us */
1489 case FLAC__METADATA_TYPE_APPLICATION:
1490 /* remember, we read the ID already */
1491 if(real_length > 0) {
1492 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
1493 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1496 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1497 return false; /* read_callback_ sets the state for us */
1500 block.data.application.data = 0;
1502 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1503 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
1506 case FLAC__METADATA_TYPE_CUESHEET:
1507 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1510 case FLAC__METADATA_TYPE_PICTURE:
1511 if(!read_metadata_picture_(decoder, &block.data.picture))
1514 case FLAC__METADATA_TYPE_STREAMINFO:
1515 case FLAC__METADATA_TYPE_SEEKTABLE:
1519 if(real_length > 0) {
1520 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
1521 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1524 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1525 return false; /* read_callback_ sets the state for us */
1528 block.data.unknown.data = 0;
1531 if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
1532 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1534 /* now we have to free any malloc'ed data in the block */
1536 case FLAC__METADATA_TYPE_PADDING:
1538 case FLAC__METADATA_TYPE_APPLICATION:
1539 if(0 != block.data.application.data)
1540 free(block.data.application.data);
1542 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1543 if(0 != block.data.vorbis_comment.vendor_string.entry)
1544 free(block.data.vorbis_comment.vendor_string.entry);
1545 if(block.data.vorbis_comment.num_comments > 0)
1546 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1547 if(0 != block.data.vorbis_comment.comments[i].entry)
1548 free(block.data.vorbis_comment.comments[i].entry);
1549 if(0 != block.data.vorbis_comment.comments)
1550 free(block.data.vorbis_comment.comments);
1552 case FLAC__METADATA_TYPE_CUESHEET:
1553 if(block.data.cue_sheet.num_tracks > 0)
1554 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1555 if(0 != block.data.cue_sheet.tracks[i].indices)
1556 free(block.data.cue_sheet.tracks[i].indices);
1557 if(0 != block.data.cue_sheet.tracks)
1558 free(block.data.cue_sheet.tracks);
1560 case FLAC__METADATA_TYPE_PICTURE:
1561 if(0 != block.data.picture.mime_type)
1562 free(block.data.picture.mime_type);
1563 if(0 != block.data.picture.description)
1564 free(block.data.picture.description);
1565 if(0 != block.data.picture.data)
1566 free(block.data.picture.data);
1568 case FLAC__METADATA_TYPE_STREAMINFO:
1569 case FLAC__METADATA_TYPE_SEEKTABLE:
1572 if(0 != block.data.unknown.data)
1573 free(block.data.unknown.data);
1580 /* if this fails, it's OK, it's just a hint for the seek routine */
1581 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1582 decoder->private_->first_frame_offset = 0;
1583 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1589 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1592 unsigned bits, used_bits = 0;
1594 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1596 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1597 decoder->private_->stream_info.is_last = is_last;
1598 decoder->private_->stream_info.length = length;
1600 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1601 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1602 return false; /* read_callback_ sets the state for us */
1603 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1606 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1607 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1608 return false; /* read_callback_ sets the state for us */
1609 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1612 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1613 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1614 return false; /* read_callback_ sets the state for us */
1615 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1618 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1619 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1620 return false; /* read_callback_ sets the state for us */
1621 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1624 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1625 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1626 return false; /* read_callback_ sets the state for us */
1627 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1630 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1631 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1632 return false; /* read_callback_ sets the state for us */
1633 decoder->private_->stream_info.data.stream_info.channels = x+1;
1636 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1637 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1638 return false; /* read_callback_ sets the state for us */
1639 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1642 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1643 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))
1644 return false; /* read_callback_ sets the state for us */
1647 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1648 return false; /* read_callback_ sets the state for us */
1651 /* skip the rest of the block */
1652 FLAC__ASSERT(used_bits % 8 == 0);
1653 length -= (used_bits / 8);
1654 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1655 return false; /* read_callback_ sets the state for us */
1660 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1665 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1667 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1668 decoder->private_->seek_table.is_last = is_last;
1669 decoder->private_->seek_table.length = length;
1671 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1673 /* use realloc since we may pass through here several times (e.g. after seeking) */
1674 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
1675 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1678 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1679 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
1680 return false; /* read_callback_ sets the state for us */
1681 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1683 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
1684 return false; /* read_callback_ sets the state for us */
1685 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1687 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
1688 return false; /* read_callback_ sets the state for us */
1689 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1691 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1692 /* if there is a partial point left, skip over it */
1694 /*@@@ do a send_error_to_client_() here? there's an argument for either way */
1695 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1696 return false; /* read_callback_ sets the state for us */
1702 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1706 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1708 /* read vendor string */
1709 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1710 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
1711 return false; /* read_callback_ sets the state for us */
1712 if(obj->vendor_string.length > 0) {
1713 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
1714 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1717 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
1718 return false; /* read_callback_ sets the state for us */
1719 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
1722 obj->vendor_string.entry = 0;
1724 /* read num comments */
1725 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1726 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
1727 return false; /* read_callback_ sets the state for us */
1730 if(obj->num_comments > 0) {
1731 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1732 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1735 for(i = 0; i < obj->num_comments; i++) {
1736 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1737 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
1738 return false; /* read_callback_ sets the state for us */
1739 if(obj->comments[i].length > 0) {
1740 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
1741 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1744 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
1745 return false; /* read_callback_ sets the state for us */
1746 obj->comments[i].entry[obj->comments[i].length] = '\0';
1749 obj->comments[i].entry = 0;
1759 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1761 FLAC__uint32 i, j, x;
1763 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1765 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1767 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1768 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))
1769 return false; /* read_callback_ sets the state for us */
1771 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
1772 return false; /* read_callback_ sets the state for us */
1774 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
1775 return false; /* read_callback_ sets the state for us */
1776 obj->is_cd = x? true : false;
1778 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_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_NUM_TRACKS_LEN))
1782 return false; /* read_callback_ sets the state for us */
1783 obj->num_tracks = x;
1785 if(obj->num_tracks > 0) {
1786 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1787 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1790 for(i = 0; i < obj->num_tracks; i++) {
1791 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1792 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
1793 return false; /* read_callback_ sets the state for us */
1795 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
1796 return false; /* read_callback_ sets the state for us */
1797 track->number = (FLAC__byte)x;
1799 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1800 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
1801 return false; /* read_callback_ sets the state for us */
1803 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
1804 return false; /* read_callback_ sets the state for us */
1807 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
1808 return false; /* read_callback_ sets the state for us */
1809 track->pre_emphasis = x;
1811 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_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_TRACK_NUM_INDICES_LEN))
1815 return false; /* read_callback_ sets the state for us */
1816 track->num_indices = (FLAC__byte)x;
1818 if(track->num_indices > 0) {
1819 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1820 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1823 for(j = 0; j < track->num_indices; j++) {
1824 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1825 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
1826 return false; /* read_callback_ sets the state for us */
1828 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
1829 return false; /* read_callback_ sets the state for us */
1830 index->number = (FLAC__byte)x;
1832 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
1833 return false; /* read_callback_ sets the state for us */
1842 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
1846 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1849 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
1850 return false; /* read_callback_ sets the state for us */
1853 /* read MIME type */
1854 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
1855 return false; /* read_callback_ sets the state for us */
1856 if(0 == (obj->mime_type = (char*)malloc(x+1))) {
1857 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1861 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
1862 return false; /* read_callback_ sets the state for us */
1864 obj->mime_type[x] = '\0';
1866 /* read description */
1867 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
1868 return false; /* read_callback_ sets the state for us */
1869 if(0 == (obj->description = (FLAC__byte*)malloc(x+1))) {
1870 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1874 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
1875 return false; /* read_callback_ sets the state for us */
1877 obj->description[x] = '\0';
1880 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
1881 return false; /* read_callback_ sets the state for us */
1884 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
1885 return false; /* read_callback_ sets the state for us */
1888 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
1889 return false; /* read_callback_ sets the state for us */
1892 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
1893 return false; /* read_callback_ sets the state for us */
1896 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
1897 return false; /* read_callback_ sets the state for us */
1898 if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
1899 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1902 if(obj->data_length > 0) {
1903 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
1904 return false; /* read_callback_ sets the state for us */
1910 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1915 /* skip the version and flags bytes */
1916 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
1917 return false; /* read_callback_ sets the state for us */
1918 /* get the size (in bytes) to skip */
1920 for(i = 0; i < 4; i++) {
1921 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1922 return false; /* read_callback_ sets the state for us */
1926 /* skip the rest of the tag */
1927 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
1928 return false; /* read_callback_ sets the state for us */
1932 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1935 FLAC__bool first = true;
1937 /* If we know the total number of samples in the stream, stop if we've read that many. */
1938 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1939 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
1940 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1941 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1946 /* make sure we're byte aligned */
1947 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
1948 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
1949 return false; /* read_callback_ sets the state for us */
1953 if(decoder->private_->cached) {
1954 x = (FLAC__uint32)decoder->private_->lookahead;
1955 decoder->private_->cached = false;
1958 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1959 return false; /* read_callback_ sets the state for us */
1961 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1962 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1963 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1964 return false; /* read_callback_ sets the state for us */
1966 /* 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 */
1967 /* else we have to check if the second byte is the end of a sync code */
1968 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1969 decoder->private_->lookahead = (FLAC__byte)x;
1970 decoder->private_->cached = true;
1972 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1973 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1974 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1979 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1987 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
1991 FLAC__int32 mid, side;
1992 unsigned frame_crc; /* the one we calculate from the input stream */
1995 *got_a_frame = false;
1999 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
2000 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
2001 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
2003 if(!read_frame_header_(decoder))
2005 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
2007 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
2009 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2011 * first figure the correct bits-per-sample of the subframe
2013 unsigned bps = decoder->private_->frame.header.bits_per_sample;
2014 switch(decoder->private_->frame.header.channel_assignment) {
2015 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2016 /* no adjustment needed */
2018 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2019 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2023 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2024 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2028 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2029 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2039 if(!read_subframe_(decoder, channel, bps, do_full_decode))
2041 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2044 if(!read_zero_padding_(decoder))
2048 * Read the frame CRC-16 from the footer and check
2050 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2051 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
2052 return false; /* read_callback_ sets the state for us */
2053 if(frame_crc == x) {
2054 if(do_full_decode) {
2055 /* Undo any special channel coding */
2056 switch(decoder->private_->frame.header.channel_assignment) {
2057 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2060 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2061 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2062 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2063 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
2065 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2066 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2067 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2068 decoder->private_->output[0][i] += decoder->private_->output[1][i];
2070 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2071 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2072 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2074 mid = decoder->private_->output[0][i];
2075 side = decoder->private_->output[1][i];
2077 mid |= (side & 1); /* i.e. if 'side' is odd... */
2078 decoder->private_->output[0][i] = (mid + side) >> 1;
2079 decoder->private_->output[1][i] = (mid - side) >> 1;
2081 //@@@@@@ OPT: try without 'side' temp variable
2082 mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */
2083 decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1;
2084 decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1;
2095 /* Bad frame, emit error and zero the output signal */
2096 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2097 if(do_full_decode) {
2098 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2099 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2104 *got_a_frame = true;
2106 /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */
2107 if(decoder->private_->next_fixed_block_size)
2108 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;
2110 /* put the latest values into the public section of the decoder instance */
2111 decoder->protected_->channels = decoder->private_->frame.header.channels;
2112 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2113 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2114 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2115 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2117 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2118 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2121 if(do_full_decode) {
2122 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
2126 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2130 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2134 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
2135 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2136 unsigned raw_header_len;
2137 FLAC__bool is_unparseable = false;
2139 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2141 /* init the raw header with the saved bits from synchronization */
2142 raw_header[0] = decoder->private_->header_warmup[0];
2143 raw_header[1] = decoder->private_->header_warmup[1];
2146 /* check to make sure that reserved bit is 0 */
2147 if(raw_header[1] & 0x02) /* MAGIC NUMBER */
2148 is_unparseable = true;
2151 * Note that along the way as we read the header, we look for a sync
2152 * code inside. If we find one it would indicate that our original
2153 * sync was bad since there cannot be a sync code in a valid header.
2155 * Three kinds of things can go wrong when reading the frame header:
2156 * 1) We may have sync'ed incorrectly and not landed on a frame header.
2157 * If we don't find a sync code, it can end up looking like we read
2158 * a valid but unparseable header, until getting to the frame header
2159 * CRC. Even then we could get a false positive on the CRC.
2160 * 2) We may have sync'ed correctly but on an unparseable frame (from a
2162 * 3) We may be on a damaged frame which appears valid but unparseable.
2164 * For all these reasons, we try and read a complete frame header as
2165 * long as it seems valid, even if unparseable, up until the frame
2170 * read in the raw header as bytes so we can CRC it, and parse it on the way
2172 for(i = 0; i < 2; i++) {
2173 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2174 return false; /* read_callback_ sets the state for us */
2175 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2176 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
2177 decoder->private_->lookahead = (FLAC__byte)x;
2178 decoder->private_->cached = true;
2179 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2180 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2183 raw_header[raw_header_len++] = (FLAC__byte)x;
2186 switch(x = raw_header[2] >> 4) {
2188 is_unparseable = true;
2191 decoder->private_->frame.header.blocksize = 192;
2197 decoder->private_->frame.header.blocksize = 576 << (x-2);
2211 decoder->private_->frame.header.blocksize = 256 << (x-8);
2218 switch(x = raw_header[2] & 0x0f) {
2220 if(decoder->private_->has_stream_info)
2221 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2223 is_unparseable = true;
2226 decoder->private_->frame.header.sample_rate = 88200;
2229 decoder->private_->frame.header.sample_rate = 176400;
2232 decoder->private_->frame.header.sample_rate = 192000;
2235 decoder->private_->frame.header.sample_rate = 8000;
2238 decoder->private_->frame.header.sample_rate = 16000;
2241 decoder->private_->frame.header.sample_rate = 22050;
2244 decoder->private_->frame.header.sample_rate = 24000;
2247 decoder->private_->frame.header.sample_rate = 32000;
2250 decoder->private_->frame.header.sample_rate = 44100;
2253 decoder->private_->frame.header.sample_rate = 48000;
2256 decoder->private_->frame.header.sample_rate = 96000;
2261 sample_rate_hint = x;
2264 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2265 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2271 x = (unsigned)(raw_header[3] >> 4);
2273 decoder->private_->frame.header.channels = 2;
2276 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2279 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2282 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2285 is_unparseable = true;
2290 decoder->private_->frame.header.channels = (unsigned)x + 1;
2291 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2294 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
2296 if(decoder->private_->has_stream_info)
2297 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2299 is_unparseable = true;
2302 decoder->private_->frame.header.bits_per_sample = 8;
2305 decoder->private_->frame.header.bits_per_sample = 12;
2308 decoder->private_->frame.header.bits_per_sample = 16;
2311 decoder->private_->frame.header.bits_per_sample = 20;
2314 decoder->private_->frame.header.bits_per_sample = 24;
2318 is_unparseable = true;
2325 /* check to make sure that reserved bit is 0 */
2326 if(raw_header[3] & 0x01) /* MAGIC NUMBER */
2327 is_unparseable = true;
2329 /* read the frame's starting sample number (or frame number as the case may be) */
2331 raw_header[1] & 0x01 ||
2332 /*@@@ 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 */
2333 (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
2334 ) { /* variable blocksize */
2335 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2336 return false; /* read_callback_ sets the state for us */
2337 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2338 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2339 decoder->private_->cached = true;
2340 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2341 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2344 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2345 decoder->private_->frame.header.number.sample_number = xx;
2347 else { /* fixed blocksize */
2348 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2349 return false; /* read_callback_ sets the state for us */
2350 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2351 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2352 decoder->private_->cached = true;
2353 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2354 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2357 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2358 decoder->private_->frame.header.number.frame_number = x;
2361 if(blocksize_hint) {
2362 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2363 return false; /* read_callback_ sets the state for us */
2364 raw_header[raw_header_len++] = (FLAC__byte)x;
2365 if(blocksize_hint == 7) {
2367 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2368 return false; /* read_callback_ sets the state for us */
2369 raw_header[raw_header_len++] = (FLAC__byte)_x;
2372 decoder->private_->frame.header.blocksize = x+1;
2375 if(sample_rate_hint) {
2376 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2377 return false; /* read_callback_ sets the state for us */
2378 raw_header[raw_header_len++] = (FLAC__byte)x;
2379 if(sample_rate_hint != 12) {
2381 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2382 return false; /* read_callback_ sets the state for us */
2383 raw_header[raw_header_len++] = (FLAC__byte)_x;
2386 if(sample_rate_hint == 12)
2387 decoder->private_->frame.header.sample_rate = x*1000;
2388 else if(sample_rate_hint == 13)
2389 decoder->private_->frame.header.sample_rate = x;
2391 decoder->private_->frame.header.sample_rate = x*10;
2394 /* read the CRC-8 byte */
2395 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2396 return false; /* read_callback_ sets the state for us */
2397 crc8 = (FLAC__byte)x;
2399 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
2400 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2401 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2405 /* calculate the sample number from the frame number if needed */
2406 decoder->private_->next_fixed_block_size = 0;
2407 if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
2408 x = decoder->private_->frame.header.number.frame_number;
2409 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2410 if(decoder->private_->fixed_block_size)
2411 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
2412 else if(decoder->private_->has_stream_info) {
2413 if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
2414 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2415 decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
2418 is_unparseable = true;
2421 decoder->private_->frame.header.number.sample_number = 0;
2422 decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
2425 /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */
2426 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2430 if(is_unparseable) {
2431 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2432 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2439 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2442 FLAC__bool wasted_bits;
2445 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2446 return false; /* read_callback_ sets the state for us */
2448 wasted_bits = (x & 1);
2453 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2454 return false; /* read_callback_ sets the state for us */
2455 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2456 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2459 decoder->private_->frame.subframes[channel].wasted_bits = 0;
2462 * Lots of magic numbers here
2465 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2466 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2470 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2474 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2478 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2479 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2483 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
2485 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2489 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2490 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2494 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
2496 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2500 if(wasted_bits && do_full_decode) {
2501 x = decoder->private_->frame.subframes[channel].wasted_bits;
2502 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2503 decoder->private_->output[channel][i] <<= x;
2509 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2511 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
2514 FLAC__int32 *output = decoder->private_->output[channel];
2516 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2518 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2519 return false; /* read_callback_ sets the state for us */
2521 subframe->value = x;
2523 /* decode the subframe */
2524 if(do_full_decode) {
2525 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2532 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2534 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
2539 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
2541 subframe->residual = decoder->private_->residual[channel];
2542 subframe->order = order;
2544 /* read warm-up samples */
2545 for(u = 0; u < order; u++) {
2546 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2547 return false; /* read_callback_ sets the state for us */
2548 subframe->warmup[u] = i32;
2551 /* read entropy coding method info */
2552 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2553 return false; /* read_callback_ sets the state for us */
2554 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2555 switch(subframe->entropy_coding_method.type) {
2556 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2557 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2558 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2559 return false; /* read_callback_ sets the state for us */
2560 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2561 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2564 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2565 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2570 switch(subframe->entropy_coding_method.type) {
2571 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2572 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2573 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))
2580 /* decode the subframe */
2581 if(do_full_decode) {
2582 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2583 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
2589 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2591 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
2596 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
2598 subframe->residual = decoder->private_->residual[channel];
2599 subframe->order = order;
2601 /* read warm-up samples */
2602 for(u = 0; u < order; u++) {
2603 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2604 return false; /* read_callback_ sets the state for us */
2605 subframe->warmup[u] = i32;
2608 /* read qlp coeff precision */
2609 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
2610 return false; /* read_callback_ sets the state for us */
2611 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
2612 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2613 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2616 subframe->qlp_coeff_precision = u32+1;
2618 /* read qlp shift */
2619 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
2620 return false; /* read_callback_ sets the state for us */
2621 subframe->quantization_level = i32;
2623 /* read quantized lp coefficiencts */
2624 for(u = 0; u < order; u++) {
2625 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
2626 return false; /* read_callback_ sets the state for us */
2627 subframe->qlp_coeff[u] = i32;
2630 /* read entropy coding method info */
2631 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2632 return false; /* read_callback_ sets the state for us */
2633 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2634 switch(subframe->entropy_coding_method.type) {
2635 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2636 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2637 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2638 return false; /* read_callback_ sets the state for us */
2639 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2640 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2643 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2644 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2649 switch(subframe->entropy_coding_method.type) {
2650 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2651 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2652 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))
2659 /* decode the subframe */
2660 if(do_full_decode) {
2661 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2662 /*@@@@@@ technically not pessimistic enough, should be more like
2663 if( (FLAC__uint64)order * ((((FLAC__uint64)1)<<bps)-1) * ((1<<subframe->qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) )
2665 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2666 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
2668 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);
2670 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);
2673 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);
2675 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);
2681 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2683 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2684 FLAC__int32 x, *residual = decoder->private_->residual[channel];
2687 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
2689 subframe->data = residual;
2691 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2692 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2693 return false; /* read_callback_ sets the state for us */
2697 /* decode the subframe */
2699 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2704 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)
2706 FLAC__uint32 rice_parameter;
2708 unsigned partition, sample, u;
2709 const unsigned partitions = 1u << partition_order;
2710 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2711 const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2712 const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2715 if(partition_order == 0) {
2716 if(decoder->private_->frame.header.blocksize < predictor_order) {
2717 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2718 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2723 if(partition_samples < predictor_order) {
2724 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2725 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2730 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
2731 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2736 for(partition = 0; partition < partitions; partition++) {
2737 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
2738 return false; /* read_callback_ sets the state for us */
2739 partitioned_rice_contents->parameters[partition] = rice_parameter;
2740 if(rice_parameter < pesc) {
2741 partitioned_rice_contents->raw_bits[partition] = 0;
2742 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2743 if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
2744 return false; /* read_callback_ sets the state for us */
2748 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
2749 return false; /* read_callback_ sets the state for us */
2750 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2751 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2752 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
2753 return false; /* read_callback_ sets the state for us */
2754 residual[sample] = i;
2762 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2764 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2765 FLAC__uint32 zero = 0;
2766 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2767 return false; /* read_callback_ sets the state for us */
2769 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2770 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2776 FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
2778 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2782 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2783 !decoder->private_->is_ogg &&
2785 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2788 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2791 else if(*bytes > 0) {
2792 /* While seeking, it is possible for our seek to land in the
2793 * middle of audio data that looks exactly like a frame header
2794 * from a future version of an encoder. When that happens, our
2795 * error callback will get an
2796 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
2797 * unparseable_frame_count. But there is a remote possibility
2798 * that it is properly synced at such a "future-codec frame",
2799 * so to make sure, we wait to see many "unparseable" errors in
2800 * a row before bailing out.
2802 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2803 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2807 const FLAC__StreamDecoderReadStatus status =
2809 decoder->private_->is_ogg?
2810 read_callback_ogg_aspect_(decoder, buffer, bytes) :
2812 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2814 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
2815 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2818 else if(*bytes == 0) {
2820 status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
2823 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2824 !decoder->private_->is_ogg &&
2826 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2829 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2840 /* abort to avoid a deadlock */
2841 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2844 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
2845 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2846 * and at the same time hit the end of the stream (for example, seeking
2847 * to a point that is after the beginning of the last Ogg page). There
2848 * is no way to report an Ogg sync loss through the callbacks (see note
2849 * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
2850 * So to keep the decoder from stopping at this point we gate the call
2851 * to the eof_callback and let the Ogg decoder aspect set the
2852 * end-of-stream state when it is needed.
2857 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
2859 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2860 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
2861 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2862 /* we don't really have a way to handle lost sync via read
2863 * callback so we'll let it pass and let the underlying
2864 * FLAC decoder catch the error
2866 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
2867 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2868 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
2869 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2870 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
2871 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
2872 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
2873 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
2874 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
2875 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2878 /* double protection */
2879 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2883 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2885 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2887 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2888 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
2889 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
2890 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
2891 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
2892 case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
2893 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2895 /* double protection: */
2897 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2902 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2904 if(decoder->private_->is_seeking) {
2905 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
2906 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
2907 FLAC__uint64 target_sample = decoder->private_->target_sample;
2909 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2912 decoder->private_->got_a_frame = true;
2914 decoder->private_->last_frame = *frame; /* save the frame */
2915 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
2916 unsigned delta = (unsigned)(target_sample - this_frame_sample);
2917 /* kick out of seek mode */
2918 decoder->private_->is_seeking = false;
2919 /* shift out the samples before target_sample */
2922 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
2923 for(channel = 0; channel < frame->header.channels; channel++)
2924 newbuffer[channel] = buffer[channel] + delta;
2925 decoder->private_->last_frame.header.blocksize -= delta;
2926 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2927 /* write the relevant samples */
2928 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2931 /* write the relevant samples */
2932 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2936 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2941 * If we never got STREAMINFO, turn off MD5 checking to save
2942 * cycles since we don't have a sum to compare to anyway
2944 if(!decoder->private_->has_stream_info)
2945 decoder->private_->do_md5_checking = false;
2946 if(decoder->private_->do_md5_checking) {
2947 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2948 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2950 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2954 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2956 if(!decoder->private_->is_seeking)
2957 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2958 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
2959 decoder->private_->unparseable_frame_count++;
2962 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
2964 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
2965 FLAC__int64 pos = -1;
2967 unsigned approx_bytes_per_frame;
2968 FLAC__bool first_seek = true;
2969 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
2970 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2971 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
2972 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
2973 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
2974 /* take these from the current frame in case they've changed mid-stream */
2975 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2976 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
2977 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
2979 /* use values from stream info if we didn't decode a frame */
2981 channels = decoder->private_->stream_info.data.stream_info.channels;
2983 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2985 /* we are just guessing here */
2986 if(max_framesize > 0)
2987 approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
2989 * Check if it's a known fixed-blocksize stream. Note that though
2990 * the spec doesn't allow zeroes in the STREAMINFO block, we may
2991 * never get a STREAMINFO block when decoding so the value of
2992 * min_blocksize might be zero.
2994 else if(min_blocksize == max_blocksize && min_blocksize > 0) {
2995 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
2996 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
2999 approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
3002 * First, we set an upper and lower bound on where in the
3003 * stream we will search. For now we assume the worst case
3004 * scenario, which is our best guess at the beginning of
3005 * the first frame and end of the stream.
3007 lower_bound = first_frame_offset;
3008 lower_bound_sample = 0;
3009 upper_bound = stream_length;
3010 upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
3013 * Now we refine the bounds if we have a seektable with
3014 * suitable points. Note that according to the spec they
3015 * must be ordered by ascending sample number.
3017 * Note: to protect against invalid seek tables we will ignore points
3018 * that have frame_samples==0 or sample_number>=total_samples
3021 FLAC__uint64 new_lower_bound = lower_bound;
3022 FLAC__uint64 new_upper_bound = upper_bound;
3023 FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
3024 FLAC__uint64 new_upper_bound_sample = upper_bound_sample;
3026 /* find the closest seek point <= target_sample, if it exists */
3027 for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
3029 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3030 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3031 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3032 seek_table->points[i].sample_number <= target_sample
3036 if(i >= 0) { /* i.e. we found a suitable seek point... */
3037 new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
3038 new_lower_bound_sample = seek_table->points[i].sample_number;
3041 /* find the closest seek point > target_sample, if it exists */
3042 for(i = 0; i < (int)seek_table->num_points; i++) {
3044 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3045 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3046 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3047 seek_table->points[i].sample_number > target_sample
3051 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3052 new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3053 new_upper_bound_sample = seek_table->points[i].sample_number;
3055 /* final protection against unsorted seek tables; keep original values if bogus */
3056 if(new_upper_bound >= new_lower_bound) {
3057 lower_bound = new_lower_bound;
3058 upper_bound = new_upper_bound;
3059 lower_bound_sample = new_lower_bound_sample;
3060 upper_bound_sample = new_upper_bound_sample;
3064 FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
3065 /* there are 2 insidious ways that the following equality occurs, which
3067 * 1) total_samples is 0 (unknown) and target_sample is 0
3068 * 2) total_samples is 0 (unknown) and target_sample happens to be
3069 * exactly equal to the last seek point in the seek table; this
3070 * means there is no seek point above it, and upper_bound_samples
3071 * remains equal to the estimate (of target_samples) we made above
3072 * in either case it does not hurt to move upper_bound_sample up by 1
3074 if(upper_bound_sample == lower_bound_sample)
3075 upper_bound_sample++;
3077 decoder->private_->target_sample = target_sample;
3079 /* check if the bounds are still ok */
3080 if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
3081 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3084 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3085 #if defined _MSC_VER || defined __MINGW32__
3086 /* with VC++ you have to spoon feed it the casting */
3087 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;
3089 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;
3092 /* a little less accurate: */
3093 if(upper_bound - lower_bound < 0xffffffff)
3094 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;
3095 else /* @@@ WATCHOUT, ~2TB limit */
3096 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;
3098 if(pos >= (FLAC__int64)upper_bound)
3099 pos = (FLAC__int64)upper_bound - 1;
3100 if(pos < (FLAC__int64)lower_bound)
3101 pos = (FLAC__int64)lower_bound;
3102 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3103 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3106 if(!FLAC__stream_decoder_flush(decoder)) {
3107 /* above call sets the state for us */
3110 /* Now we need to get a frame. First we need to reset our
3111 * unparseable_frame_count; if we get too many unparseable
3112 * frames in a row, the read callback will return
3113 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3114 * FLAC__stream_decoder_process_single() to return false.
3116 decoder->private_->unparseable_frame_count = 0;
3117 if(!FLAC__stream_decoder_process_single(decoder)) {
3118 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3121 /* our write callback will change the state when it gets to the target frame */
3122 /* 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 */
3124 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
3125 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3128 if(!decoder->private_->is_seeking)
3131 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3132 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3134 if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
3135 if (pos == (FLAC__int64)lower_bound) {
3136 /* can't move back any more than the first frame, something is fatally wrong */
3137 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3140 /* our last move backwards wasn't big enough, try again */
3141 approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
3144 /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */
3147 /* make sure we are not seeking in corrupted stream */
3148 if (this_frame_sample < lower_bound_sample) {
3149 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3153 /* we need to narrow the search */
3154 if(target_sample < this_frame_sample) {
3155 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3156 /*@@@@@@ what will decode position be if at end of stream? */
3157 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3158 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3161 approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
3163 else { /* target_sample >= this_frame_sample + this frame's blocksize */
3164 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3165 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3166 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3169 approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
3177 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3179 FLAC__uint64 left_pos = 0, right_pos = stream_length;
3180 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3181 FLAC__uint64 this_frame_sample = 0; /* only initialized to avoid compiler warning */
3182 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3183 FLAC__bool did_a_seek;
3184 unsigned iteration = 0;
3186 /* In the first iterations, we will calculate the target byte position
3187 * by the distance from the target sample to left_sample and
3188 * right_sample (let's call it "proportional search"). After that, we
3189 * will switch to binary search.
3191 unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
3193 /* We will switch to a linear search once our current sample is less
3194 * than this number of samples ahead of the target sample
3196 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3198 /* If the total number of samples is unknown, use a large value, and
3199 * force binary search immediately.
3201 if(right_sample == 0) {
3202 right_sample = (FLAC__uint64)(-1);
3203 BINARY_SEARCH_AFTER_ITERATION = 0;
3206 decoder->private_->target_sample = target_sample;
3207 for( ; ; iteration++) {
3208 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3209 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3210 pos = (right_pos + left_pos) / 2;
3213 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3214 #if defined _MSC_VER || defined __MINGW32__
3215 /* with MSVC you have to spoon feed it the casting */
3216 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));
3218 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
3221 /* a little less accurate: */
3222 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
3223 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
3224 else /* @@@ WATCHOUT, ~2TB limit */
3225 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
3227 /* @@@ TODO: might want to limit pos to some distance
3228 * before EOF, to make sure we land before the last frame,
3229 * thereby getting a this_frame_sample and so having a better
3230 * estimate. @@@@@@DELETE:this would also mostly (or totally if we could
3231 * be sure to land before the last frame) avoid the
3232 * end-of-stream case we have to check later.
3237 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3238 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3241 if(!FLAC__stream_decoder_flush(decoder)) {
3242 /* above call sets the state for us */
3250 decoder->private_->got_a_frame = false;
3251 if(!FLAC__stream_decoder_process_single(decoder)) {
3252 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3255 if(!decoder->private_->got_a_frame) {
3257 /* this can happen if we seek to a point after the last frame; we drop
3258 * to binary search right away in this case to avoid any wasted
3259 * iterations of proportional search.
3262 BINARY_SEARCH_AFTER_ITERATION = 0;
3265 /* this can probably only happen if total_samples is unknown and the
3266 * target_sample is past the end of the stream
3268 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3272 /* our write callback will change the state when it gets to the target frame */
3273 else if(!decoder->private_->is_seeking/*@@@@@@ && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM*/) {
3277 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3278 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3281 if (this_frame_sample <= target_sample) {
3282 /* The 'equal' case should not happen, since
3283 * FLAC__stream_decoder_process_single()
3284 * should recognize that it has hit the
3285 * target sample and we would exit through
3286 * the 'break' above.
3288 FLAC__ASSERT(this_frame_sample != target_sample);
3290 left_sample = this_frame_sample;
3291 /* sanity check to avoid infinite loop */
3292 if (left_pos == pos) {
3293 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3298 else if(this_frame_sample > target_sample) {
3299 right_sample = this_frame_sample;
3300 /* sanity check to avoid infinite loop */
3301 if (right_pos == pos) {
3302 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3315 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3320 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3321 if(ferror(decoder->private_->file))
3322 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3323 else if(*bytes == 0)
3324 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3326 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3329 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
3332 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3336 if(decoder->private_->file == stdin)
3337 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3338 else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3339 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3341 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3344 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3349 if(decoder->private_->file == stdin)
3350 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3351 else if((pos = ftello(decoder->private_->file)) < 0)
3352 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3354 *absolute_byte_offset = (FLAC__uint64)pos;
3355 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3359 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3361 struct stat filestats;
3364 if(decoder->private_->file == stdin)
3365 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3366 else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
3367 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3369 *stream_length = (FLAC__uint64)filestats.st_size;
3370 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3374 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3378 return feof(decoder->private_->file)? true : false;