1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 "share/alloc.h"
57 #include "protected/stream_decoder.h"
58 #include "private/bitreader.h"
59 #include "private/bitmath.h"
60 #include "private/cpu.h"
61 #include "private/crc.h"
62 #include "private/fixed.h"
63 #include "private/format.h"
64 #include "private/lpc.h"
65 #include "private/md5.h"
66 #include "private/memory.h"
71 #define max(a,b) ((a)>(b)?(a):(b))
73 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
75 #define FLAC__U64L(x) x
77 #define FLAC__U64L(x) x##LLU
81 /* technically this should be in an "export.c" but this is convenient enough */
82 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
91 /***********************************************************************
95 ***********************************************************************/
97 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
99 /***********************************************************************
101 * Private class method prototypes
103 ***********************************************************************/
105 static void set_defaults_(FLAC__StreamDecoder *decoder);
106 static FILE *get_binary_stdin_(void);
107 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
108 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
109 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
110 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
111 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
112 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
113 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
114 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
115 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
116 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
117 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
118 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
119 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
120 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
121 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
122 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
123 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
124 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
125 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);
126 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
127 static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
129 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
130 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
132 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
133 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
134 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
136 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
138 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
139 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
140 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
141 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
142 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
144 /***********************************************************************
148 ***********************************************************************/
150 typedef struct FLAC__StreamDecoderPrivate {
154 FLAC__StreamDecoderReadCallback read_callback;
155 FLAC__StreamDecoderSeekCallback seek_callback;
156 FLAC__StreamDecoderTellCallback tell_callback;
157 FLAC__StreamDecoderLengthCallback length_callback;
158 FLAC__StreamDecoderEofCallback eof_callback;
159 FLAC__StreamDecoderWriteCallback write_callback;
160 FLAC__StreamDecoderMetadataCallback metadata_callback;
161 FLAC__StreamDecoderErrorCallback error_callback;
162 /* generic 32-bit datapath: */
163 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[]);
164 /* generic 64-bit datapath: */
165 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[]);
166 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
167 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[]);
168 /* 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: */
169 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[]);
170 FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
172 FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
173 FLAC__BitReader *input;
174 FLAC__int32 *output[FLAC__MAX_CHANNELS];
175 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
176 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
177 unsigned output_capacity, output_channels;
178 FLAC__uint32 fixed_block_size, next_fixed_block_size;
179 FLAC__uint64 samples_decoded;
180 FLAC__bool has_stream_info, has_seek_table;
181 FLAC__StreamMetadata stream_info;
182 FLAC__StreamMetadata seek_table;
183 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
184 FLAC__byte *metadata_filter_ids;
185 size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
187 FLAC__bool cached; /* true if there is a byte in lookahead */
188 FLAC__CPUInfo cpuinfo;
189 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
190 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
191 /* unaligned (original) pointers to allocated data */
192 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
193 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 */
194 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
195 FLAC__bool is_seeking;
196 FLAC__MD5Context md5context;
197 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
198 /* (the rest of these are only used for seeking) */
199 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
200 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
201 FLAC__uint64 target_sample;
202 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
204 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
206 } FLAC__StreamDecoderPrivate;
208 /***********************************************************************
210 * Public static class data
212 ***********************************************************************/
214 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
215 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
216 "FLAC__STREAM_DECODER_READ_METADATA",
217 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
218 "FLAC__STREAM_DECODER_READ_FRAME",
219 "FLAC__STREAM_DECODER_END_OF_STREAM",
220 "FLAC__STREAM_DECODER_OGG_ERROR",
221 "FLAC__STREAM_DECODER_SEEK_ERROR",
222 "FLAC__STREAM_DECODER_ABORTED",
223 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
224 "FLAC__STREAM_DECODER_UNINITIALIZED"
227 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
228 "FLAC__STREAM_DECODER_INIT_STATUS_OK",
229 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
230 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
231 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
232 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
233 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
236 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
237 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
238 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
239 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
242 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
243 "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
244 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
245 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
248 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
249 "FLAC__STREAM_DECODER_TELL_STATUS_OK",
250 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
251 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
254 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
255 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
256 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
257 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
260 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
261 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
262 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
265 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
266 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
267 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
268 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
269 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
272 /***********************************************************************
274 * Class constructor/destructor
276 ***********************************************************************/
277 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
279 FLAC__StreamDecoder *decoder;
282 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
284 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
289 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
290 if(decoder->protected_ == 0) {
295 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
296 if(decoder->private_ == 0) {
297 free(decoder->protected_);
302 decoder->private_->input = FLAC__bitreader_new();
303 if(decoder->private_->input == 0) {
304 free(decoder->private_);
305 free(decoder->protected_);
310 decoder->private_->metadata_filter_ids_capacity = 16;
311 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
312 FLAC__bitreader_delete(decoder->private_->input);
313 free(decoder->private_);
314 free(decoder->protected_);
319 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
320 decoder->private_->output[i] = 0;
321 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
324 decoder->private_->output_capacity = 0;
325 decoder->private_->output_channels = 0;
326 decoder->private_->has_seek_table = false;
328 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
329 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
331 decoder->private_->file = 0;
333 set_defaults_(decoder);
335 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
340 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
344 FLAC__ASSERT(0 != decoder);
345 FLAC__ASSERT(0 != decoder->protected_);
346 FLAC__ASSERT(0 != decoder->private_);
347 FLAC__ASSERT(0 != decoder->private_->input);
349 (void)FLAC__stream_decoder_finish(decoder);
351 if(0 != decoder->private_->metadata_filter_ids)
352 free(decoder->private_->metadata_filter_ids);
354 FLAC__bitreader_delete(decoder->private_->input);
356 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
357 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
359 free(decoder->private_);
360 free(decoder->protected_);
364 /***********************************************************************
366 * Public class methods
368 ***********************************************************************/
370 static FLAC__StreamDecoderInitStatus init_stream_internal_(
371 FLAC__StreamDecoder *decoder,
372 FLAC__StreamDecoderReadCallback read_callback,
373 FLAC__StreamDecoderSeekCallback seek_callback,
374 FLAC__StreamDecoderTellCallback tell_callback,
375 FLAC__StreamDecoderLengthCallback length_callback,
376 FLAC__StreamDecoderEofCallback eof_callback,
377 FLAC__StreamDecoderWriteCallback write_callback,
378 FLAC__StreamDecoderMetadataCallback metadata_callback,
379 FLAC__StreamDecoderErrorCallback error_callback,
384 FLAC__ASSERT(0 != decoder);
386 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
387 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
391 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
395 0 == read_callback ||
396 0 == write_callback ||
397 0 == error_callback ||
398 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
400 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
403 decoder->private_->is_ogg = is_ogg;
404 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
405 return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
409 * get the CPU info and set the function pointers
411 FLAC__cpu_info(&decoder->private_->cpuinfo);
412 /* first default to the non-asm routines */
413 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
414 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
415 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
416 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
417 decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
418 /* now override with asm where appropriate */
420 if(decoder->private_->cpuinfo.use_asm) {
421 #ifdef FLAC__CPU_IA32
422 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
423 #ifdef FLAC__HAS_NASM
424 #if 1 /*@@@@@@ OPT: not clearly faster, needs more testing */
425 if(decoder->private_->cpuinfo.data.ia32.bswap)
426 decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap;
428 if(decoder->private_->cpuinfo.data.ia32.mmx) {
429 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
430 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
431 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
434 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
435 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
436 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
439 #elif defined FLAC__CPU_PPC
440 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
441 if(decoder->private_->cpuinfo.data.ppc.altivec) {
442 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
443 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
449 /* from here on, errors are fatal */
451 if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) {
452 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
453 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
456 decoder->private_->read_callback = read_callback;
457 decoder->private_->seek_callback = seek_callback;
458 decoder->private_->tell_callback = tell_callback;
459 decoder->private_->length_callback = length_callback;
460 decoder->private_->eof_callback = eof_callback;
461 decoder->private_->write_callback = write_callback;
462 decoder->private_->metadata_callback = metadata_callback;
463 decoder->private_->error_callback = error_callback;
464 decoder->private_->client_data = client_data;
465 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
466 decoder->private_->samples_decoded = 0;
467 decoder->private_->has_stream_info = false;
468 decoder->private_->cached = false;
470 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
471 decoder->private_->is_seeking = false;
473 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
474 if(!FLAC__stream_decoder_reset(decoder)) {
475 /* above call sets the state for us */
476 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
479 return FLAC__STREAM_DECODER_INIT_STATUS_OK;
482 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
483 FLAC__StreamDecoder *decoder,
484 FLAC__StreamDecoderReadCallback read_callback,
485 FLAC__StreamDecoderSeekCallback seek_callback,
486 FLAC__StreamDecoderTellCallback tell_callback,
487 FLAC__StreamDecoderLengthCallback length_callback,
488 FLAC__StreamDecoderEofCallback eof_callback,
489 FLAC__StreamDecoderWriteCallback write_callback,
490 FLAC__StreamDecoderMetadataCallback metadata_callback,
491 FLAC__StreamDecoderErrorCallback error_callback,
495 return init_stream_internal_(
510 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
511 FLAC__StreamDecoder *decoder,
512 FLAC__StreamDecoderReadCallback read_callback,
513 FLAC__StreamDecoderSeekCallback seek_callback,
514 FLAC__StreamDecoderTellCallback tell_callback,
515 FLAC__StreamDecoderLengthCallback length_callback,
516 FLAC__StreamDecoderEofCallback eof_callback,
517 FLAC__StreamDecoderWriteCallback write_callback,
518 FLAC__StreamDecoderMetadataCallback metadata_callback,
519 FLAC__StreamDecoderErrorCallback error_callback,
523 return init_stream_internal_(
538 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
539 FLAC__StreamDecoder *decoder,
541 FLAC__StreamDecoderWriteCallback write_callback,
542 FLAC__StreamDecoderMetadataCallback metadata_callback,
543 FLAC__StreamDecoderErrorCallback error_callback,
548 FLAC__ASSERT(0 != decoder);
549 FLAC__ASSERT(0 != file);
551 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
552 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
554 if(0 == write_callback || 0 == error_callback)
555 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
558 * To make sure that our file does not go unclosed after an error, we
559 * must assign the FILE pointer before any further error can occur in
563 file = get_binary_stdin_(); /* just to be safe */
565 decoder->private_->file = file;
567 return init_stream_internal_(
570 decoder->private_->file == stdin? 0: file_seek_callback_,
571 decoder->private_->file == stdin? 0: file_tell_callback_,
572 decoder->private_->file == stdin? 0: file_length_callback_,
582 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
583 FLAC__StreamDecoder *decoder,
585 FLAC__StreamDecoderWriteCallback write_callback,
586 FLAC__StreamDecoderMetadataCallback metadata_callback,
587 FLAC__StreamDecoderErrorCallback error_callback,
591 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
594 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
595 FLAC__StreamDecoder *decoder,
597 FLAC__StreamDecoderWriteCallback write_callback,
598 FLAC__StreamDecoderMetadataCallback metadata_callback,
599 FLAC__StreamDecoderErrorCallback error_callback,
603 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
606 static FLAC__StreamDecoderInitStatus init_file_internal_(
607 FLAC__StreamDecoder *decoder,
608 const char *filename,
609 FLAC__StreamDecoderWriteCallback write_callback,
610 FLAC__StreamDecoderMetadataCallback metadata_callback,
611 FLAC__StreamDecoderErrorCallback error_callback,
618 FLAC__ASSERT(0 != decoder);
621 * To make sure that our file does not go unclosed after an error, we
622 * have to do the same entrance checks here that are later performed
623 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
625 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
626 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
628 if(0 == write_callback || 0 == error_callback)
629 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
631 file = filename? fopen(filename, "rb") : stdin;
634 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
636 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
639 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
640 FLAC__StreamDecoder *decoder,
641 const char *filename,
642 FLAC__StreamDecoderWriteCallback write_callback,
643 FLAC__StreamDecoderMetadataCallback metadata_callback,
644 FLAC__StreamDecoderErrorCallback error_callback,
648 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
651 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
652 FLAC__StreamDecoder *decoder,
653 const char *filename,
654 FLAC__StreamDecoderWriteCallback write_callback,
655 FLAC__StreamDecoderMetadataCallback metadata_callback,
656 FLAC__StreamDecoderErrorCallback error_callback,
660 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
663 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
665 FLAC__bool md5_failed = false;
668 FLAC__ASSERT(0 != decoder);
669 FLAC__ASSERT(0 != decoder->private_);
670 FLAC__ASSERT(0 != decoder->protected_);
672 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
675 /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
676 * always call FLAC__MD5Final()
678 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
680 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
681 free(decoder->private_->seek_table.data.seek_table.points);
682 decoder->private_->seek_table.data.seek_table.points = 0;
683 decoder->private_->has_seek_table = false;
685 FLAC__bitreader_free(decoder->private_->input);
686 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
688 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
689 * output arrays have a buffer of up to 3 zeroes in front
690 * (at negative indices) for alignment purposes; we use 4
691 * to keep the data well-aligned.
693 if(0 != decoder->private_->output[i]) {
694 free(decoder->private_->output[i]-4);
695 decoder->private_->output[i] = 0;
697 if(0 != decoder->private_->residual_unaligned[i]) {
698 free(decoder->private_->residual_unaligned[i]);
699 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
702 decoder->private_->output_capacity = 0;
703 decoder->private_->output_channels = 0;
706 if(decoder->private_->is_ogg)
707 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
710 if(0 != decoder->private_->file) {
711 if(decoder->private_->file != stdin)
712 fclose(decoder->private_->file);
713 decoder->private_->file = 0;
716 if(decoder->private_->do_md5_checking) {
717 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
720 decoder->private_->is_seeking = false;
722 set_defaults_(decoder);
724 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
729 FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
731 FLAC__ASSERT(0 != decoder);
732 FLAC__ASSERT(0 != decoder->private_);
733 FLAC__ASSERT(0 != decoder->protected_);
734 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
737 /* can't check decoder->private_->is_ogg since that's not set until init time */
738 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
746 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
748 FLAC__ASSERT(0 != decoder);
749 FLAC__ASSERT(0 != decoder->protected_);
750 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
752 decoder->protected_->md5_checking = value;
756 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
758 FLAC__ASSERT(0 != decoder);
759 FLAC__ASSERT(0 != decoder->private_);
760 FLAC__ASSERT(0 != decoder->protected_);
761 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
762 /* double protection */
763 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
765 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
767 decoder->private_->metadata_filter[type] = true;
768 if(type == FLAC__METADATA_TYPE_APPLICATION)
769 decoder->private_->metadata_filter_ids_count = 0;
773 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
775 FLAC__ASSERT(0 != decoder);
776 FLAC__ASSERT(0 != decoder->private_);
777 FLAC__ASSERT(0 != decoder->protected_);
778 FLAC__ASSERT(0 != id);
779 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
782 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
785 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
787 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
788 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
789 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
792 decoder->private_->metadata_filter_ids_capacity *= 2;
795 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));
796 decoder->private_->metadata_filter_ids_count++;
801 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
804 FLAC__ASSERT(0 != decoder);
805 FLAC__ASSERT(0 != decoder->private_);
806 FLAC__ASSERT(0 != decoder->protected_);
807 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
809 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
810 decoder->private_->metadata_filter[i] = true;
811 decoder->private_->metadata_filter_ids_count = 0;
815 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
817 FLAC__ASSERT(0 != decoder);
818 FLAC__ASSERT(0 != decoder->private_);
819 FLAC__ASSERT(0 != decoder->protected_);
820 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
821 /* double protection */
822 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
824 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
826 decoder->private_->metadata_filter[type] = false;
827 if(type == FLAC__METADATA_TYPE_APPLICATION)
828 decoder->private_->metadata_filter_ids_count = 0;
832 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
834 FLAC__ASSERT(0 != decoder);
835 FLAC__ASSERT(0 != decoder->private_);
836 FLAC__ASSERT(0 != decoder->protected_);
837 FLAC__ASSERT(0 != id);
838 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
841 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
844 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
846 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
847 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
848 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
851 decoder->private_->metadata_filter_ids_capacity *= 2;
854 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));
855 decoder->private_->metadata_filter_ids_count++;
860 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
862 FLAC__ASSERT(0 != decoder);
863 FLAC__ASSERT(0 != decoder->private_);
864 FLAC__ASSERT(0 != decoder->protected_);
865 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
867 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
868 decoder->private_->metadata_filter_ids_count = 0;
872 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
874 FLAC__ASSERT(0 != decoder);
875 FLAC__ASSERT(0 != decoder->protected_);
876 return decoder->protected_->state;
879 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
881 return FLAC__StreamDecoderStateString[decoder->protected_->state];
884 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
886 FLAC__ASSERT(0 != decoder);
887 FLAC__ASSERT(0 != decoder->protected_);
888 return decoder->protected_->md5_checking;
891 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
893 FLAC__ASSERT(0 != decoder);
894 FLAC__ASSERT(0 != decoder->protected_);
895 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
898 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
900 FLAC__ASSERT(0 != decoder);
901 FLAC__ASSERT(0 != decoder->protected_);
902 return decoder->protected_->channels;
905 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
907 FLAC__ASSERT(0 != decoder);
908 FLAC__ASSERT(0 != decoder->protected_);
909 return decoder->protected_->channel_assignment;
912 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
914 FLAC__ASSERT(0 != decoder);
915 FLAC__ASSERT(0 != decoder->protected_);
916 return decoder->protected_->bits_per_sample;
919 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
921 FLAC__ASSERT(0 != decoder);
922 FLAC__ASSERT(0 != decoder->protected_);
923 return decoder->protected_->sample_rate;
926 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
928 FLAC__ASSERT(0 != decoder);
929 FLAC__ASSERT(0 != decoder->protected_);
930 return decoder->protected_->blocksize;
933 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
935 FLAC__ASSERT(0 != decoder);
936 FLAC__ASSERT(0 != decoder->private_);
937 FLAC__ASSERT(0 != position);
940 if(decoder->private_->is_ogg)
943 if(0 == decoder->private_->tell_callback)
945 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
947 /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
948 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
950 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
951 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
955 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
957 FLAC__ASSERT(0 != decoder);
958 FLAC__ASSERT(0 != decoder->private_);
959 FLAC__ASSERT(0 != decoder->protected_);
961 decoder->private_->samples_decoded = 0;
962 decoder->private_->do_md5_checking = false;
965 if(decoder->private_->is_ogg)
966 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
969 if(!FLAC__bitreader_clear(decoder->private_->input)) {
970 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
973 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
978 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
980 FLAC__ASSERT(0 != decoder);
981 FLAC__ASSERT(0 != decoder->private_);
982 FLAC__ASSERT(0 != decoder->protected_);
984 if(!FLAC__stream_decoder_flush(decoder)) {
985 /* above call sets the state for us */
990 /*@@@ could go in !internal_reset_hack block below */
991 if(decoder->private_->is_ogg)
992 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
995 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
996 * (internal_reset_hack) don't try to rewind since we are already at
997 * the beginning of the stream and don't want to fail if the input is
1000 if(!decoder->private_->internal_reset_hack) {
1001 if(decoder->private_->file == stdin)
1002 return false; /* can't rewind stdin, reset fails */
1003 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
1004 return false; /* seekable and seek fails, reset fails */
1007 decoder->private_->internal_reset_hack = false;
1009 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
1011 decoder->private_->has_stream_info = false;
1012 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
1013 free(decoder->private_->seek_table.data.seek_table.points);
1014 decoder->private_->seek_table.data.seek_table.points = 0;
1015 decoder->private_->has_seek_table = false;
1017 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
1019 * This goes in reset() and not flush() because according to the spec, a
1020 * fixed-blocksize stream must stay that way through the whole stream.
1022 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
1024 /* We initialize the FLAC__MD5Context even though we may never use it. This
1025 * is because md5 checking may be turned on to start and then turned off if
1026 * a seek occurs. So we init the context here and finalize it in
1027 * FLAC__stream_decoder_finish() to make sure things are always cleaned up
1030 FLAC__MD5Init(&decoder->private_->md5context);
1032 decoder->private_->first_frame_offset = 0;
1033 decoder->private_->unparseable_frame_count = 0;
1038 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1040 FLAC__bool got_a_frame;
1041 FLAC__ASSERT(0 != decoder);
1042 FLAC__ASSERT(0 != decoder->protected_);
1045 switch(decoder->protected_->state) {
1046 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1047 if(!find_metadata_(decoder))
1048 return false; /* above function sets the status for us */
1050 case FLAC__STREAM_DECODER_READ_METADATA:
1051 if(!read_metadata_(decoder))
1052 return false; /* above function sets the status for us */
1055 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1056 if(!frame_sync_(decoder))
1057 return true; /* above function sets the status for us */
1059 case FLAC__STREAM_DECODER_READ_FRAME:
1060 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1061 return false; /* above function sets the status for us */
1063 return true; /* above function sets the status for us */
1065 case FLAC__STREAM_DECODER_END_OF_STREAM:
1066 case FLAC__STREAM_DECODER_ABORTED:
1075 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1077 FLAC__ASSERT(0 != decoder);
1078 FLAC__ASSERT(0 != decoder->protected_);
1081 switch(decoder->protected_->state) {
1082 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1083 if(!find_metadata_(decoder))
1084 return false; /* above function sets the status for us */
1086 case FLAC__STREAM_DECODER_READ_METADATA:
1087 if(!read_metadata_(decoder))
1088 return false; /* above function sets the status for us */
1090 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1091 case FLAC__STREAM_DECODER_READ_FRAME:
1092 case FLAC__STREAM_DECODER_END_OF_STREAM:
1093 case FLAC__STREAM_DECODER_ABORTED:
1102 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1105 FLAC__ASSERT(0 != decoder);
1106 FLAC__ASSERT(0 != decoder->protected_);
1109 switch(decoder->protected_->state) {
1110 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1111 if(!find_metadata_(decoder))
1112 return false; /* above function sets the status for us */
1114 case FLAC__STREAM_DECODER_READ_METADATA:
1115 if(!read_metadata_(decoder))
1116 return false; /* above function sets the status for us */
1118 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1119 if(!frame_sync_(decoder))
1120 return true; /* above function sets the status for us */
1122 case FLAC__STREAM_DECODER_READ_FRAME:
1123 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1124 return false; /* above function sets the status for us */
1126 case FLAC__STREAM_DECODER_END_OF_STREAM:
1127 case FLAC__STREAM_DECODER_ABORTED:
1136 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1138 FLAC__bool got_a_frame;
1139 FLAC__ASSERT(0 != decoder);
1140 FLAC__ASSERT(0 != decoder->protected_);
1143 switch(decoder->protected_->state) {
1144 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1145 case FLAC__STREAM_DECODER_READ_METADATA:
1146 return false; /* above function sets the status for us */
1147 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1148 if(!frame_sync_(decoder))
1149 return true; /* above function sets the status for us */
1151 case FLAC__STREAM_DECODER_READ_FRAME:
1152 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1153 return false; /* above function sets the status for us */
1155 return true; /* above function sets the status for us */
1157 case FLAC__STREAM_DECODER_END_OF_STREAM:
1158 case FLAC__STREAM_DECODER_ABORTED:
1167 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1169 FLAC__uint64 length;
1171 FLAC__ASSERT(0 != decoder);
1174 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1175 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1176 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1177 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1178 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1182 if(0 == decoder->private_->seek_callback)
1185 FLAC__ASSERT(decoder->private_->seek_callback);
1186 FLAC__ASSERT(decoder->private_->tell_callback);
1187 FLAC__ASSERT(decoder->private_->length_callback);
1188 FLAC__ASSERT(decoder->private_->eof_callback);
1190 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1193 decoder->private_->is_seeking = true;
1195 /* turn off md5 checking if a seek is attempted */
1196 decoder->private_->do_md5_checking = false;
1198 /* 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) */
1199 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1200 decoder->private_->is_seeking = false;
1204 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
1206 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1207 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1209 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1210 /* above call sets the state for us */
1211 decoder->private_->is_seeking = false;
1214 /* check this again in case we didn't know total_samples the first time */
1215 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1216 decoder->private_->is_seeking = false;
1222 const FLAC__bool ok =
1224 decoder->private_->is_ogg?
1225 seek_to_absolute_sample_ogg_(decoder, length, sample) :
1227 seek_to_absolute_sample_(decoder, length, sample)
1229 decoder->private_->is_seeking = false;
1234 /***********************************************************************
1236 * Protected class methods
1238 ***********************************************************************/
1240 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1242 FLAC__ASSERT(0 != decoder);
1243 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1244 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
1245 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
1248 /***********************************************************************
1250 * Private class methods
1252 ***********************************************************************/
1254 void set_defaults_(FLAC__StreamDecoder *decoder)
1257 decoder->private_->is_ogg = false;
1259 decoder->private_->read_callback = 0;
1260 decoder->private_->seek_callback = 0;
1261 decoder->private_->tell_callback = 0;
1262 decoder->private_->length_callback = 0;
1263 decoder->private_->eof_callback = 0;
1264 decoder->private_->write_callback = 0;
1265 decoder->private_->metadata_callback = 0;
1266 decoder->private_->error_callback = 0;
1267 decoder->private_->client_data = 0;
1269 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1270 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1271 decoder->private_->metadata_filter_ids_count = 0;
1273 decoder->protected_->md5_checking = false;
1276 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1281 * This will forcibly set stdin to binary mode (for OSes that require it)
1283 FILE *get_binary_stdin_(void)
1285 /* if something breaks here it is probably due to the presence or
1286 * absence of an underscore before the identifiers 'setmode',
1287 * 'fileno', and/or 'O_BINARY'; check your system header files.
1289 #if defined _MSC_VER || defined __MINGW32__
1290 _setmode(_fileno(stdin), _O_BINARY);
1291 #elif defined __CYGWIN__
1292 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
1293 setmode(_fileno(stdin), _O_BINARY);
1294 #elif defined __EMX__
1295 setmode(fileno(stdin), O_BINARY);
1301 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1306 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
1309 /* simply using realloc() is not practical because the number of channels may change mid-stream */
1311 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1312 if(0 != decoder->private_->output[i]) {
1313 free(decoder->private_->output[i]-4);
1314 decoder->private_->output[i] = 0;
1316 if(0 != decoder->private_->residual_unaligned[i]) {
1317 free(decoder->private_->residual_unaligned[i]);
1318 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1322 for(i = 0; i < channels; i++) {
1324 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
1325 * output arrays have a buffer of up to 3 zeroes in front
1326 * (at negative indices) for alignment purposes; we use 4
1327 * to keep the data well-aligned.
1329 tmp = (FLAC__int32*)safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/);
1331 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1334 memset(tmp, 0, sizeof(FLAC__int32)*4);
1335 decoder->private_->output[i] = tmp + 4;
1338 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
1340 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
1341 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1346 decoder->private_->output_capacity = size;
1347 decoder->private_->output_channels = channels;
1352 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1356 FLAC__ASSERT(0 != decoder);
1357 FLAC__ASSERT(0 != decoder->private_);
1359 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1360 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
1366 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1370 FLAC__bool first = true;
1372 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1374 for(i = id = 0; i < 4; ) {
1375 if(decoder->private_->cached) {
1376 x = (FLAC__uint32)decoder->private_->lookahead;
1377 decoder->private_->cached = false;
1380 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1381 return false; /* read_callback_ sets the state for us */
1383 if(x == FLAC__STREAM_SYNC_STRING[i]) {
1389 if(x == ID3V2_TAG_[id]) {
1393 if(!skip_id3v2_tag_(decoder))
1394 return false; /* skip_id3v2_tag_ sets the state for us */
1399 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1400 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1401 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1402 return false; /* read_callback_ sets the state for us */
1404 /* 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 */
1405 /* else we have to check if the second byte is the end of a sync code */
1406 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1407 decoder->private_->lookahead = (FLAC__byte)x;
1408 decoder->private_->cached = true;
1410 else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
1411 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1412 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1418 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1423 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1427 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1430 FLAC__uint32 i, x, type, length;
1432 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1434 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1435 return false; /* read_callback_ sets the state for us */
1436 is_last = x? true : false;
1438 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
1439 return false; /* read_callback_ sets the state for us */
1441 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1442 return false; /* read_callback_ sets the state for us */
1444 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1445 if(!read_metadata_streaminfo_(decoder, is_last, length))
1448 decoder->private_->has_stream_info = true;
1449 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))
1450 decoder->private_->do_md5_checking = false;
1451 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1452 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1454 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1455 if(!read_metadata_seektable_(decoder, is_last, length))
1458 decoder->private_->has_seek_table = true;
1459 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1460 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1463 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1464 unsigned real_length = length;
1465 FLAC__StreamMetadata block;
1467 block.is_last = is_last;
1468 block.type = (FLAC__MetadataType)type;
1469 block.length = length;
1471 if(type == FLAC__METADATA_TYPE_APPLICATION) {
1472 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1473 return false; /* read_callback_ sets the state for us */
1475 if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */
1476 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
1480 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1482 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1487 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1488 return false; /* read_callback_ sets the state for us */
1492 case FLAC__METADATA_TYPE_PADDING:
1493 /* skip the padding bytes */
1494 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1495 return false; /* read_callback_ sets the state for us */
1497 case FLAC__METADATA_TYPE_APPLICATION:
1498 /* remember, we read the ID already */
1499 if(real_length > 0) {
1500 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
1501 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1504 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1505 return false; /* read_callback_ sets the state for us */
1508 block.data.application.data = 0;
1510 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1511 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
1514 case FLAC__METADATA_TYPE_CUESHEET:
1515 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1518 case FLAC__METADATA_TYPE_PICTURE:
1519 if(!read_metadata_picture_(decoder, &block.data.picture))
1522 case FLAC__METADATA_TYPE_STREAMINFO:
1523 case FLAC__METADATA_TYPE_SEEKTABLE:
1527 if(real_length > 0) {
1528 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
1529 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1532 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1533 return false; /* read_callback_ sets the state for us */
1536 block.data.unknown.data = 0;
1539 if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
1540 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1542 /* now we have to free any malloc()ed data in the block */
1544 case FLAC__METADATA_TYPE_PADDING:
1546 case FLAC__METADATA_TYPE_APPLICATION:
1547 if(0 != block.data.application.data)
1548 free(block.data.application.data);
1550 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1551 if(0 != block.data.vorbis_comment.vendor_string.entry)
1552 free(block.data.vorbis_comment.vendor_string.entry);
1553 if(block.data.vorbis_comment.num_comments > 0)
1554 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1555 if(0 != block.data.vorbis_comment.comments[i].entry)
1556 free(block.data.vorbis_comment.comments[i].entry);
1557 if(0 != block.data.vorbis_comment.comments)
1558 free(block.data.vorbis_comment.comments);
1560 case FLAC__METADATA_TYPE_CUESHEET:
1561 if(block.data.cue_sheet.num_tracks > 0)
1562 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1563 if(0 != block.data.cue_sheet.tracks[i].indices)
1564 free(block.data.cue_sheet.tracks[i].indices);
1565 if(0 != block.data.cue_sheet.tracks)
1566 free(block.data.cue_sheet.tracks);
1568 case FLAC__METADATA_TYPE_PICTURE:
1569 if(0 != block.data.picture.mime_type)
1570 free(block.data.picture.mime_type);
1571 if(0 != block.data.picture.description)
1572 free(block.data.picture.description);
1573 if(0 != block.data.picture.data)
1574 free(block.data.picture.data);
1576 case FLAC__METADATA_TYPE_STREAMINFO:
1577 case FLAC__METADATA_TYPE_SEEKTABLE:
1580 if(0 != block.data.unknown.data)
1581 free(block.data.unknown.data);
1588 /* if this fails, it's OK, it's just a hint for the seek routine */
1589 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1590 decoder->private_->first_frame_offset = 0;
1591 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1597 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1600 unsigned bits, used_bits = 0;
1602 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1604 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1605 decoder->private_->stream_info.is_last = is_last;
1606 decoder->private_->stream_info.length = length;
1608 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1609 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1610 return false; /* read_callback_ sets the state for us */
1611 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1614 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1615 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1616 return false; /* read_callback_ sets the state for us */
1617 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1620 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1621 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1622 return false; /* read_callback_ sets the state for us */
1623 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1626 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1627 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1628 return false; /* read_callback_ sets the state for us */
1629 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1632 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1633 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1634 return false; /* read_callback_ sets the state for us */
1635 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1638 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1639 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1640 return false; /* read_callback_ sets the state for us */
1641 decoder->private_->stream_info.data.stream_info.channels = x+1;
1644 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1645 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1646 return false; /* read_callback_ sets the state for us */
1647 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1650 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1651 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))
1652 return false; /* read_callback_ sets the state for us */
1655 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1656 return false; /* read_callback_ sets the state for us */
1659 /* skip the rest of the block */
1660 FLAC__ASSERT(used_bits % 8 == 0);
1661 length -= (used_bits / 8);
1662 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1663 return false; /* read_callback_ sets the state for us */
1668 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1673 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1675 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1676 decoder->private_->seek_table.is_last = is_last;
1677 decoder->private_->seek_table.length = length;
1679 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1681 /* use realloc since we may pass through here several times (e.g. after seeking) */
1682 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) {
1683 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1686 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1687 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
1688 return false; /* read_callback_ sets the state for us */
1689 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1691 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
1692 return false; /* read_callback_ sets the state for us */
1693 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1695 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
1696 return false; /* read_callback_ sets the state for us */
1697 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1699 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1700 /* if there is a partial point left, skip over it */
1702 /*@@@ do a send_error_to_client_() here? there's an argument for either way */
1703 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1704 return false; /* read_callback_ sets the state for us */
1710 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1714 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1716 /* read vendor string */
1717 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1718 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
1719 return false; /* read_callback_ sets the state for us */
1720 if(obj->vendor_string.length > 0) {
1721 if(0 == (obj->vendor_string.entry = (FLAC__byte*)safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) {
1722 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1725 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
1726 return false; /* read_callback_ sets the state for us */
1727 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
1730 obj->vendor_string.entry = 0;
1732 /* read num comments */
1733 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1734 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
1735 return false; /* read_callback_ sets the state for us */
1738 if(obj->num_comments > 0) {
1739 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)safe_malloc_mul_2op_(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1740 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1743 for(i = 0; i < obj->num_comments; i++) {
1744 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1745 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
1746 return false; /* read_callback_ sets the state for us */
1747 if(obj->comments[i].length > 0) {
1748 if(0 == (obj->comments[i].entry = (FLAC__byte*)safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) {
1749 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1752 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
1753 return false; /* read_callback_ sets the state for us */
1754 obj->comments[i].entry[obj->comments[i].length] = '\0';
1757 obj->comments[i].entry = 0;
1767 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1769 FLAC__uint32 i, j, x;
1771 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1773 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1775 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1776 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))
1777 return false; /* read_callback_ sets the state for us */
1779 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
1780 return false; /* read_callback_ sets the state for us */
1782 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
1783 return false; /* read_callback_ sets the state for us */
1784 obj->is_cd = x? true : false;
1786 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
1787 return false; /* read_callback_ sets the state for us */
1789 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
1790 return false; /* read_callback_ sets the state for us */
1791 obj->num_tracks = x;
1793 if(obj->num_tracks > 0) {
1794 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1795 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1798 for(i = 0; i < obj->num_tracks; i++) {
1799 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1800 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
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_NUMBER_LEN))
1804 return false; /* read_callback_ sets the state for us */
1805 track->number = (FLAC__byte)x;
1807 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1808 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
1809 return false; /* read_callback_ sets the state for us */
1811 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
1812 return false; /* read_callback_ sets the state for us */
1815 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
1816 return false; /* read_callback_ sets the state for us */
1817 track->pre_emphasis = x;
1819 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
1820 return false; /* read_callback_ sets the state for us */
1822 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
1823 return false; /* read_callback_ sets the state for us */
1824 track->num_indices = (FLAC__byte)x;
1826 if(track->num_indices > 0) {
1827 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1828 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1831 for(j = 0; j < track->num_indices; j++) {
1832 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1833 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
1834 return false; /* read_callback_ sets the state for us */
1836 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
1837 return false; /* read_callback_ sets the state for us */
1838 index->number = (FLAC__byte)x;
1840 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
1841 return false; /* read_callback_ sets the state for us */
1850 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
1854 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1857 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
1858 return false; /* read_callback_ sets the state for us */
1861 /* read MIME type */
1862 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
1863 return false; /* read_callback_ sets the state for us */
1864 if(0 == (obj->mime_type = (char*)safe_malloc_add_2op_(x, /*+*/1))) {
1865 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1869 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
1870 return false; /* read_callback_ sets the state for us */
1872 obj->mime_type[x] = '\0';
1874 /* read description */
1875 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
1876 return false; /* read_callback_ sets the state for us */
1877 if(0 == (obj->description = (FLAC__byte*)safe_malloc_add_2op_(x, /*+*/1))) {
1878 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1882 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
1883 return false; /* read_callback_ sets the state for us */
1885 obj->description[x] = '\0';
1888 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
1889 return false; /* read_callback_ sets the state for us */
1892 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
1893 return false; /* read_callback_ sets the state for us */
1896 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
1897 return false; /* read_callback_ sets the state for us */
1900 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
1901 return false; /* read_callback_ sets the state for us */
1904 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
1905 return false; /* read_callback_ sets the state for us */
1906 if(0 == (obj->data = (FLAC__byte*)safe_malloc_(obj->data_length))) {
1907 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1910 if(obj->data_length > 0) {
1911 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
1912 return false; /* read_callback_ sets the state for us */
1918 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1923 /* skip the version and flags bytes */
1924 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
1925 return false; /* read_callback_ sets the state for us */
1926 /* get the size (in bytes) to skip */
1928 for(i = 0; i < 4; i++) {
1929 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1930 return false; /* read_callback_ sets the state for us */
1934 /* skip the rest of the tag */
1935 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
1936 return false; /* read_callback_ sets the state for us */
1940 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1943 FLAC__bool first = true;
1945 /* If we know the total number of samples in the stream, stop if we've read that many. */
1946 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1947 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
1948 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1949 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1954 /* make sure we're byte aligned */
1955 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
1956 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
1957 return false; /* read_callback_ sets the state for us */
1961 if(decoder->private_->cached) {
1962 x = (FLAC__uint32)decoder->private_->lookahead;
1963 decoder->private_->cached = false;
1966 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1967 return false; /* read_callback_ sets the state for us */
1969 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1970 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1971 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1972 return false; /* read_callback_ sets the state for us */
1974 /* 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 */
1975 /* else we have to check if the second byte is the end of a sync code */
1976 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1977 decoder->private_->lookahead = (FLAC__byte)x;
1978 decoder->private_->cached = true;
1980 else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
1981 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1982 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1987 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1995 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
1999 FLAC__int32 mid, side;
2000 unsigned frame_crc; /* the one we calculate from the input stream */
2003 *got_a_frame = false;
2007 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
2008 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
2009 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
2011 if(!read_frame_header_(decoder))
2013 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
2015 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
2017 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2019 * first figure the correct bits-per-sample of the subframe
2021 unsigned bps = decoder->private_->frame.header.bits_per_sample;
2022 switch(decoder->private_->frame.header.channel_assignment) {
2023 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2024 /* no adjustment needed */
2026 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2027 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2031 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2032 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2036 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2037 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2047 if(!read_subframe_(decoder, channel, bps, do_full_decode))
2049 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2052 if(!read_zero_padding_(decoder))
2054 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption (i.e. "zero bits" were not all zeroes) */
2058 * Read the frame CRC-16 from the footer and check
2060 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2061 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
2062 return false; /* read_callback_ sets the state for us */
2063 if(frame_crc == x) {
2064 if(do_full_decode) {
2065 /* Undo any special channel coding */
2066 switch(decoder->private_->frame.header.channel_assignment) {
2067 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2070 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2071 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2072 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2073 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
2075 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2076 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2077 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2078 decoder->private_->output[0][i] += decoder->private_->output[1][i];
2080 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2081 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2082 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2084 mid = decoder->private_->output[0][i];
2085 side = decoder->private_->output[1][i];
2087 mid |= (side & 1); /* i.e. if 'side' is odd... */
2088 decoder->private_->output[0][i] = (mid + side) >> 1;
2089 decoder->private_->output[1][i] = (mid - side) >> 1;
2091 /* OPT: without 'side' temp variable */
2092 mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */
2093 decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1;
2094 decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1;
2105 /* Bad frame, emit error and zero the output signal */
2106 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2107 if(do_full_decode) {
2108 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2109 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2114 *got_a_frame = true;
2116 /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */
2117 if(decoder->private_->next_fixed_block_size)
2118 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;
2120 /* put the latest values into the public section of the decoder instance */
2121 decoder->protected_->channels = decoder->private_->frame.header.channels;
2122 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2123 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2124 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2125 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2127 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2128 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2131 if(do_full_decode) {
2132 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
2136 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2140 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2144 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
2145 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2146 unsigned raw_header_len;
2147 FLAC__bool is_unparseable = false;
2149 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2151 /* init the raw header with the saved bits from synchronization */
2152 raw_header[0] = decoder->private_->header_warmup[0];
2153 raw_header[1] = decoder->private_->header_warmup[1];
2156 /* check to make sure that reserved bit is 0 */
2157 if(raw_header[1] & 0x02) /* MAGIC NUMBER */
2158 is_unparseable = true;
2161 * Note that along the way as we read the header, we look for a sync
2162 * code inside. If we find one it would indicate that our original
2163 * sync was bad since there cannot be a sync code in a valid header.
2165 * Three kinds of things can go wrong when reading the frame header:
2166 * 1) We may have sync'ed incorrectly and not landed on a frame header.
2167 * If we don't find a sync code, it can end up looking like we read
2168 * a valid but unparseable header, until getting to the frame header
2169 * CRC. Even then we could get a false positive on the CRC.
2170 * 2) We may have sync'ed correctly but on an unparseable frame (from a
2172 * 3) We may be on a damaged frame which appears valid but unparseable.
2174 * For all these reasons, we try and read a complete frame header as
2175 * long as it seems valid, even if unparseable, up until the frame
2180 * read in the raw header as bytes so we can CRC it, and parse it on the way
2182 for(i = 0; i < 2; i++) {
2183 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2184 return false; /* read_callback_ sets the state for us */
2185 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2186 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
2187 decoder->private_->lookahead = (FLAC__byte)x;
2188 decoder->private_->cached = true;
2189 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2190 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2193 raw_header[raw_header_len++] = (FLAC__byte)x;
2196 switch(x = raw_header[2] >> 4) {
2198 is_unparseable = true;
2201 decoder->private_->frame.header.blocksize = 192;
2207 decoder->private_->frame.header.blocksize = 576 << (x-2);
2221 decoder->private_->frame.header.blocksize = 256 << (x-8);
2228 switch(x = raw_header[2] & 0x0f) {
2230 if(decoder->private_->has_stream_info)
2231 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2233 is_unparseable = true;
2236 decoder->private_->frame.header.sample_rate = 88200;
2239 decoder->private_->frame.header.sample_rate = 176400;
2242 decoder->private_->frame.header.sample_rate = 192000;
2245 decoder->private_->frame.header.sample_rate = 8000;
2248 decoder->private_->frame.header.sample_rate = 16000;
2251 decoder->private_->frame.header.sample_rate = 22050;
2254 decoder->private_->frame.header.sample_rate = 24000;
2257 decoder->private_->frame.header.sample_rate = 32000;
2260 decoder->private_->frame.header.sample_rate = 44100;
2263 decoder->private_->frame.header.sample_rate = 48000;
2266 decoder->private_->frame.header.sample_rate = 96000;
2271 sample_rate_hint = x;
2274 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2275 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2281 x = (unsigned)(raw_header[3] >> 4);
2283 decoder->private_->frame.header.channels = 2;
2286 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2289 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2292 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2295 is_unparseable = true;
2300 decoder->private_->frame.header.channels = (unsigned)x + 1;
2301 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2304 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
2306 if(decoder->private_->has_stream_info)
2307 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2309 is_unparseable = true;
2312 decoder->private_->frame.header.bits_per_sample = 8;
2315 decoder->private_->frame.header.bits_per_sample = 12;
2318 decoder->private_->frame.header.bits_per_sample = 16;
2321 decoder->private_->frame.header.bits_per_sample = 20;
2324 decoder->private_->frame.header.bits_per_sample = 24;
2328 is_unparseable = true;
2335 /* check to make sure that reserved bit is 0 */
2336 if(raw_header[3] & 0x01) /* MAGIC NUMBER */
2337 is_unparseable = true;
2339 /* read the frame's starting sample number (or frame number as the case may be) */
2341 raw_header[1] & 0x01 ||
2342 /*@@@ 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 */
2343 (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
2344 ) { /* variable blocksize */
2345 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2346 return false; /* read_callback_ sets the state for us */
2347 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2348 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2349 decoder->private_->cached = true;
2350 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2351 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2354 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2355 decoder->private_->frame.header.number.sample_number = xx;
2357 else { /* fixed blocksize */
2358 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2359 return false; /* read_callback_ sets the state for us */
2360 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2361 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2362 decoder->private_->cached = true;
2363 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2364 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2367 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2368 decoder->private_->frame.header.number.frame_number = x;
2371 if(blocksize_hint) {
2372 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2373 return false; /* read_callback_ sets the state for us */
2374 raw_header[raw_header_len++] = (FLAC__byte)x;
2375 if(blocksize_hint == 7) {
2377 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2378 return false; /* read_callback_ sets the state for us */
2379 raw_header[raw_header_len++] = (FLAC__byte)_x;
2382 decoder->private_->frame.header.blocksize = x+1;
2385 if(sample_rate_hint) {
2386 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2387 return false; /* read_callback_ sets the state for us */
2388 raw_header[raw_header_len++] = (FLAC__byte)x;
2389 if(sample_rate_hint != 12) {
2391 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2392 return false; /* read_callback_ sets the state for us */
2393 raw_header[raw_header_len++] = (FLAC__byte)_x;
2396 if(sample_rate_hint == 12)
2397 decoder->private_->frame.header.sample_rate = x*1000;
2398 else if(sample_rate_hint == 13)
2399 decoder->private_->frame.header.sample_rate = x;
2401 decoder->private_->frame.header.sample_rate = x*10;
2404 /* read the CRC-8 byte */
2405 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2406 return false; /* read_callback_ sets the state for us */
2407 crc8 = (FLAC__byte)x;
2409 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
2410 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2411 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2415 /* calculate the sample number from the frame number if needed */
2416 decoder->private_->next_fixed_block_size = 0;
2417 if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
2418 x = decoder->private_->frame.header.number.frame_number;
2419 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2420 if(decoder->private_->fixed_block_size)
2421 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
2422 else if(decoder->private_->has_stream_info) {
2423 if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
2424 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2425 decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
2428 is_unparseable = true;
2431 decoder->private_->frame.header.number.sample_number = 0;
2432 decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
2435 /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */
2436 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2440 if(is_unparseable) {
2441 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2442 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2449 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2452 FLAC__bool wasted_bits;
2455 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2456 return false; /* read_callback_ sets the state for us */
2458 wasted_bits = (x & 1);
2463 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2464 return false; /* read_callback_ sets the state for us */
2465 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2466 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2469 decoder->private_->frame.subframes[channel].wasted_bits = 0;
2472 * Lots of magic numbers here
2475 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2476 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2480 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2484 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2488 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2489 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2493 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
2495 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2499 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2500 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2504 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
2506 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2510 if(wasted_bits && do_full_decode) {
2511 x = decoder->private_->frame.subframes[channel].wasted_bits;
2512 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2513 decoder->private_->output[channel][i] <<= x;
2519 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2521 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
2524 FLAC__int32 *output = decoder->private_->output[channel];
2526 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2528 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2529 return false; /* read_callback_ sets the state for us */
2531 subframe->value = x;
2533 /* decode the subframe */
2534 if(do_full_decode) {
2535 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2542 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2544 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
2549 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
2551 subframe->residual = decoder->private_->residual[channel];
2552 subframe->order = order;
2554 /* read warm-up samples */
2555 for(u = 0; u < order; u++) {
2556 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2557 return false; /* read_callback_ sets the state for us */
2558 subframe->warmup[u] = i32;
2561 /* read entropy coding method info */
2562 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2563 return false; /* read_callback_ sets the state for us */
2564 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2565 switch(subframe->entropy_coding_method.type) {
2566 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2567 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2568 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2569 return false; /* read_callback_ sets the state for us */
2570 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2571 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2574 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2575 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2580 switch(subframe->entropy_coding_method.type) {
2581 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2582 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2583 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))
2590 /* decode the subframe */
2591 if(do_full_decode) {
2592 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2593 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
2599 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2601 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
2606 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
2608 subframe->residual = decoder->private_->residual[channel];
2609 subframe->order = order;
2611 /* read warm-up samples */
2612 for(u = 0; u < order; u++) {
2613 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2614 return false; /* read_callback_ sets the state for us */
2615 subframe->warmup[u] = i32;
2618 /* read qlp coeff precision */
2619 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
2620 return false; /* read_callback_ sets the state for us */
2621 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
2622 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2623 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2626 subframe->qlp_coeff_precision = u32+1;
2628 /* read qlp shift */
2629 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
2630 return false; /* read_callback_ sets the state for us */
2631 subframe->quantization_level = i32;
2633 /* read quantized lp coefficiencts */
2634 for(u = 0; u < order; u++) {
2635 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
2636 return false; /* read_callback_ sets the state for us */
2637 subframe->qlp_coeff[u] = i32;
2640 /* read entropy coding method info */
2641 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2642 return false; /* read_callback_ sets the state for us */
2643 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2644 switch(subframe->entropy_coding_method.type) {
2645 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2646 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2647 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2648 return false; /* read_callback_ sets the state for us */
2649 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2650 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2653 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2654 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2659 switch(subframe->entropy_coding_method.type) {
2660 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2661 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
2662 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))
2669 /* decode the subframe */
2670 if(do_full_decode) {
2671 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2672 /*@@@@@@ technically not pessimistic enough, should be more like
2673 if( (FLAC__uint64)order * ((((FLAC__uint64)1)<<bps)-1) * ((1<<subframe->qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) )
2675 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2676 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
2678 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);
2680 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);
2683 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);
2685 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);
2691 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2693 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2694 FLAC__int32 x, *residual = decoder->private_->residual[channel];
2697 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
2699 subframe->data = residual;
2701 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2702 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2703 return false; /* read_callback_ sets the state for us */
2707 /* decode the subframe */
2709 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2714 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)
2716 FLAC__uint32 rice_parameter;
2718 unsigned partition, sample, u;
2719 const unsigned partitions = 1u << partition_order;
2720 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2721 const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2722 const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
2725 if(partition_order == 0) {
2726 if(decoder->private_->frame.header.blocksize < predictor_order) {
2727 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2728 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2733 if(partition_samples < predictor_order) {
2734 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2735 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2740 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
2741 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2746 for(partition = 0; partition < partitions; partition++) {
2747 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
2748 return false; /* read_callback_ sets the state for us */
2749 partitioned_rice_contents->parameters[partition] = rice_parameter;
2750 if(rice_parameter < pesc) {
2751 partitioned_rice_contents->raw_bits[partition] = 0;
2752 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2753 if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
2754 return false; /* read_callback_ sets the state for us */
2758 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
2759 return false; /* read_callback_ sets the state for us */
2760 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2761 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2762 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
2763 return false; /* read_callback_ sets the state for us */
2764 residual[sample] = i;
2772 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2774 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2775 FLAC__uint32 zero = 0;
2776 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2777 return false; /* read_callback_ sets the state for us */
2779 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2780 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2786 FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
2788 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2792 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2793 !decoder->private_->is_ogg &&
2795 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2798 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2801 else if(*bytes > 0) {
2802 /* While seeking, it is possible for our seek to land in the
2803 * middle of audio data that looks exactly like a frame header
2804 * from a future version of an encoder. When that happens, our
2805 * error callback will get an
2806 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
2807 * unparseable_frame_count. But there is a remote possibility
2808 * that it is properly synced at such a "future-codec frame",
2809 * so to make sure, we wait to see many "unparseable" errors in
2810 * a row before bailing out.
2812 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2813 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2817 const FLAC__StreamDecoderReadStatus status =
2819 decoder->private_->is_ogg?
2820 read_callback_ogg_aspect_(decoder, buffer, bytes) :
2822 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2824 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
2825 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2828 else if(*bytes == 0) {
2830 status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
2833 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2834 !decoder->private_->is_ogg &&
2836 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2839 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2850 /* abort to avoid a deadlock */
2851 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2854 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
2855 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2856 * and at the same time hit the end of the stream (for example, seeking
2857 * to a point that is after the beginning of the last Ogg page). There
2858 * is no way to report an Ogg sync loss through the callbacks (see note
2859 * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
2860 * So to keep the decoder from stopping at this point we gate the call
2861 * to the eof_callback and let the Ogg decoder aspect set the
2862 * end-of-stream state when it is needed.
2867 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
2869 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2870 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
2871 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2872 /* we don't really have a way to handle lost sync via read
2873 * callback so we'll let it pass and let the underlying
2874 * FLAC decoder catch the error
2876 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
2877 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2878 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
2879 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2880 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
2881 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
2882 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
2883 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
2884 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
2885 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2888 /* double protection */
2889 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2893 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2895 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2897 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2898 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
2899 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
2900 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
2901 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
2902 case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
2903 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2905 /* double protection: */
2907 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2912 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2914 if(decoder->private_->is_seeking) {
2915 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
2916 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
2917 FLAC__uint64 target_sample = decoder->private_->target_sample;
2919 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2922 decoder->private_->got_a_frame = true;
2924 decoder->private_->last_frame = *frame; /* save the frame */
2925 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
2926 unsigned delta = (unsigned)(target_sample - this_frame_sample);
2927 /* kick out of seek mode */
2928 decoder->private_->is_seeking = false;
2929 /* shift out the samples before target_sample */
2932 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
2933 for(channel = 0; channel < frame->header.channels; channel++)
2934 newbuffer[channel] = buffer[channel] + delta;
2935 decoder->private_->last_frame.header.blocksize -= delta;
2936 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2937 /* write the relevant samples */
2938 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2941 /* write the relevant samples */
2942 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2946 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2951 * If we never got STREAMINFO, turn off MD5 checking to save
2952 * cycles since we don't have a sum to compare to anyway
2954 if(!decoder->private_->has_stream_info)
2955 decoder->private_->do_md5_checking = false;
2956 if(decoder->private_->do_md5_checking) {
2957 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2958 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2960 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2964 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2966 if(!decoder->private_->is_seeking)
2967 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2968 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
2969 decoder->private_->unparseable_frame_count++;
2972 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
2974 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
2975 FLAC__int64 pos = -1;
2977 unsigned approx_bytes_per_frame;
2978 FLAC__bool first_seek = true;
2979 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
2980 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2981 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
2982 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
2983 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
2984 /* take these from the current frame in case they've changed mid-stream */
2985 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2986 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
2987 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
2989 /* use values from stream info if we didn't decode a frame */
2991 channels = decoder->private_->stream_info.data.stream_info.channels;
2993 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2995 /* we are just guessing here */
2996 if(max_framesize > 0)
2997 approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
2999 * Check if it's a known fixed-blocksize stream. Note that though
3000 * the spec doesn't allow zeroes in the STREAMINFO block, we may
3001 * never get a STREAMINFO block when decoding so the value of
3002 * min_blocksize might be zero.
3004 else if(min_blocksize == max_blocksize && min_blocksize > 0) {
3005 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
3006 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
3009 approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
3012 * First, we set an upper and lower bound on where in the
3013 * stream we will search. For now we assume the worst case
3014 * scenario, which is our best guess at the beginning of
3015 * the first frame and end of the stream.
3017 lower_bound = first_frame_offset;
3018 lower_bound_sample = 0;
3019 upper_bound = stream_length;
3020 upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
3023 * Now we refine the bounds if we have a seektable with
3024 * suitable points. Note that according to the spec they
3025 * must be ordered by ascending sample number.
3027 * Note: to protect against invalid seek tables we will ignore points
3028 * that have frame_samples==0 or sample_number>=total_samples
3031 FLAC__uint64 new_lower_bound = lower_bound;
3032 FLAC__uint64 new_upper_bound = upper_bound;
3033 FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
3034 FLAC__uint64 new_upper_bound_sample = upper_bound_sample;
3036 /* find the closest seek point <= target_sample, if it exists */
3037 for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
3039 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3040 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3041 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3042 seek_table->points[i].sample_number <= target_sample
3046 if(i >= 0) { /* i.e. we found a suitable seek point... */
3047 new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
3048 new_lower_bound_sample = seek_table->points[i].sample_number;
3051 /* find the closest seek point > target_sample, if it exists */
3052 for(i = 0; i < (int)seek_table->num_points; i++) {
3054 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3055 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3056 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3057 seek_table->points[i].sample_number > target_sample
3061 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3062 new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3063 new_upper_bound_sample = seek_table->points[i].sample_number;
3065 /* final protection against unsorted seek tables; keep original values if bogus */
3066 if(new_upper_bound >= new_lower_bound) {
3067 lower_bound = new_lower_bound;
3068 upper_bound = new_upper_bound;
3069 lower_bound_sample = new_lower_bound_sample;
3070 upper_bound_sample = new_upper_bound_sample;
3074 FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
3075 /* there are 2 insidious ways that the following equality occurs, which
3077 * 1) total_samples is 0 (unknown) and target_sample is 0
3078 * 2) total_samples is 0 (unknown) and target_sample happens to be
3079 * exactly equal to the last seek point in the seek table; this
3080 * means there is no seek point above it, and upper_bound_samples
3081 * remains equal to the estimate (of target_samples) we made above
3082 * in either case it does not hurt to move upper_bound_sample up by 1
3084 if(upper_bound_sample == lower_bound_sample)
3085 upper_bound_sample++;
3087 decoder->private_->target_sample = target_sample;
3089 /* check if the bounds are still ok */
3090 if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
3091 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3094 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3095 #if defined _MSC_VER || defined __MINGW32__
3096 /* with VC++ you have to spoon feed it the casting */
3097 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;
3099 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;
3102 /* a little less accurate: */
3103 if(upper_bound - lower_bound < 0xffffffff)
3104 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;
3105 else /* @@@ WATCHOUT, ~2TB limit */
3106 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;
3108 if(pos >= (FLAC__int64)upper_bound)
3109 pos = (FLAC__int64)upper_bound - 1;
3110 if(pos < (FLAC__int64)lower_bound)
3111 pos = (FLAC__int64)lower_bound;
3112 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3113 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3116 if(!FLAC__stream_decoder_flush(decoder)) {
3117 /* above call sets the state for us */
3120 /* Now we need to get a frame. First we need to reset our
3121 * unparseable_frame_count; if we get too many unparseable
3122 * frames in a row, the read callback will return
3123 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3124 * FLAC__stream_decoder_process_single() to return false.
3126 decoder->private_->unparseable_frame_count = 0;
3127 if(!FLAC__stream_decoder_process_single(decoder)) {
3128 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3131 /* our write callback will change the state when it gets to the target frame */
3132 /* 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 */
3134 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
3135 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3138 if(!decoder->private_->is_seeking)
3141 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3142 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3144 if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
3145 if (pos == (FLAC__int64)lower_bound) {
3146 /* can't move back any more than the first frame, something is fatally wrong */
3147 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3150 /* our last move backwards wasn't big enough, try again */
3151 approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
3154 /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */
3157 /* make sure we are not seeking in corrupted stream */
3158 if (this_frame_sample < lower_bound_sample) {
3159 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3163 /* we need to narrow the search */
3164 if(target_sample < this_frame_sample) {
3165 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3166 /*@@@@@@ what will decode position be if at end of stream? */
3167 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3168 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3171 approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
3173 else { /* target_sample >= this_frame_sample + this frame's blocksize */
3174 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3175 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3176 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3179 approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
3187 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3189 FLAC__uint64 left_pos = 0, right_pos = stream_length;
3190 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3191 FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1;
3192 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3193 FLAC__bool did_a_seek;
3194 unsigned iteration = 0;
3196 /* In the first iterations, we will calculate the target byte position
3197 * by the distance from the target sample to left_sample and
3198 * right_sample (let's call it "proportional search"). After that, we
3199 * will switch to binary search.
3201 unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
3203 /* We will switch to a linear search once our current sample is less
3204 * than this number of samples ahead of the target sample
3206 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3208 /* If the total number of samples is unknown, use a large value, and
3209 * force binary search immediately.
3211 if(right_sample == 0) {
3212 right_sample = (FLAC__uint64)(-1);
3213 BINARY_SEARCH_AFTER_ITERATION = 0;
3216 decoder->private_->target_sample = target_sample;
3217 for( ; ; iteration++) {
3218 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3219 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3220 pos = (right_pos + left_pos) / 2;
3223 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3224 #if defined _MSC_VER || defined __MINGW32__
3225 /* with MSVC you have to spoon feed it the casting */
3226 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));
3228 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
3231 /* a little less accurate: */
3232 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
3233 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
3234 else /* @@@ WATCHOUT, ~2TB limit */
3235 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
3237 /* @@@ TODO: might want to limit pos to some distance
3238 * before EOF, to make sure we land before the last frame,
3239 * thereby getting a this_frame_sample and so having a better
3245 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3246 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3249 if(!FLAC__stream_decoder_flush(decoder)) {
3250 /* above call sets the state for us */
3258 decoder->private_->got_a_frame = false;
3259 if(!FLAC__stream_decoder_process_single(decoder)) {
3260 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3263 if(!decoder->private_->got_a_frame) {
3265 /* this can happen if we seek to a point after the last frame; we drop
3266 * to binary search right away in this case to avoid any wasted
3267 * iterations of proportional search.
3270 BINARY_SEARCH_AFTER_ITERATION = 0;
3273 /* this can probably only happen if total_samples is unknown and the
3274 * target_sample is past the end of the stream
3276 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3280 /* our write callback will change the state when it gets to the target frame */
3281 else if(!decoder->private_->is_seeking) {
3285 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3286 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3289 if (this_frame_sample <= target_sample) {
3290 /* The 'equal' case should not happen, since
3291 * FLAC__stream_decoder_process_single()
3292 * should recognize that it has hit the
3293 * target sample and we would exit through
3294 * the 'break' above.
3296 FLAC__ASSERT(this_frame_sample != target_sample);
3298 left_sample = this_frame_sample;
3299 /* sanity check to avoid infinite loop */
3300 if (left_pos == pos) {
3301 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3306 else if(this_frame_sample > target_sample) {
3307 right_sample = this_frame_sample;
3308 /* sanity check to avoid infinite loop */
3309 if (right_pos == pos) {
3310 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3323 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3328 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3329 if(ferror(decoder->private_->file))
3330 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3331 else if(*bytes == 0)
3332 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3334 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3337 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
3340 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3344 if(decoder->private_->file == stdin)
3345 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3346 else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3347 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3349 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3352 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3357 if(decoder->private_->file == stdin)
3358 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3359 else if((pos = ftello(decoder->private_->file)) < 0)
3360 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3362 *absolute_byte_offset = (FLAC__uint64)pos;
3363 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3367 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3369 struct stat filestats;
3372 if(decoder->private_->file == stdin)
3373 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3374 else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
3375 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3377 *stream_length = (FLAC__uint64)filestats.st_size;
3378 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3382 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3386 return feof(decoder->private_->file)? true : false;