1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 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 __MINGW32__
50 #if _MSC_VER <= 1200 /* @@@ [2G limit] */
55 #include "FLAC/assert.h"
56 #include "protected/stream_decoder.h"
57 #include "private/bitbuffer.h"
58 #include "private/bitmath.h"
59 #include "private/cpu.h"
60 #include "private/crc.h"
61 #include "private/fixed.h"
62 #include "private/format.h"
63 #include "private/lpc.h"
64 #include "private/md5.h"
65 #include "private/memory.h"
70 #define max(a,b) ((a)>(b)?(a):(b))
72 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
74 #define FLAC__U64L(x) x
76 #define FLAC__U64L(x) x##LLU
80 /* technically this should be in an "export.c" but this is convenient enough */
81 int FLAC_API_SUPPORTS_OGG_FLAC =
90 /***********************************************************************
94 ***********************************************************************/
96 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
98 /***********************************************************************
100 * Private class method prototypes
102 ***********************************************************************/
104 static void set_defaults_(FLAC__StreamDecoder *decoder);
105 static FILE *get_binary_stdin_();
106 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
107 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
108 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
109 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
110 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
111 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
112 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
113 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
114 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
115 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
116 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
117 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
118 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
119 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
120 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
121 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
122 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
123 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
124 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
125 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
126 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
128 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes);
129 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
131 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
132 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
133 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
135 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
137 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
138 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
139 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
140 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
141 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
143 /***********************************************************************
147 ***********************************************************************/
149 typedef struct FLAC__StreamDecoderPrivate {
153 FLAC__StreamDecoderReadCallback read_callback;
154 FLAC__StreamDecoderSeekCallback seek_callback;
155 FLAC__StreamDecoderTellCallback tell_callback;
156 FLAC__StreamDecoderLengthCallback length_callback;
157 FLAC__StreamDecoderEofCallback eof_callback;
158 FLAC__StreamDecoderWriteCallback write_callback;
159 FLAC__StreamDecoderMetadataCallback metadata_callback;
160 FLAC__StreamDecoderErrorCallback error_callback;
161 /* generic 32-bit datapath: */
162 void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
163 /* generic 64-bit datapath: */
164 void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
165 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
166 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
167 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
168 void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
170 FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
171 FLAC__BitBuffer *input;
172 FLAC__int32 *output[FLAC__MAX_CHANNELS];
173 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
174 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
175 unsigned output_capacity, output_channels;
176 FLAC__uint32 last_frame_number;
177 FLAC__uint32 last_block_size;
178 FLAC__uint64 samples_decoded;
179 FLAC__bool has_stream_info, has_seek_table;
180 FLAC__StreamMetadata stream_info;
181 FLAC__StreamMetadata seek_table;
182 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
183 FLAC__byte *metadata_filter_ids;
184 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
186 FLAC__bool cached; /* true if there is a byte in lookahead */
187 FLAC__CPUInfo cpuinfo;
188 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
189 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
190 /* unaligned (original) pointers to allocated data */
191 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
192 FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
193 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
194 FLAC__bool is_seeking;
195 struct FLAC__MD5Context md5context;
196 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
197 /* (the rest of these are only used for seeking) */
198 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
199 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
200 FLAC__uint64 target_sample;
201 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
203 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
205 } FLAC__StreamDecoderPrivate;
207 /***********************************************************************
209 * Public static class data
211 ***********************************************************************/
213 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
214 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
215 "FLAC__STREAM_DECODER_READ_METADATA",
216 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
217 "FLAC__STREAM_DECODER_READ_FRAME",
218 "FLAC__STREAM_DECODER_END_OF_STREAM",
219 "FLAC__STREAM_DECODER_OGG_ERROR",
220 "FLAC__STREAM_DECODER_SEEK_ERROR",
221 "FLAC__STREAM_DECODER_ABORTED",
222 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
223 "FLAC__STREAM_DECODER_UNINITIALIZED"
226 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
227 "FLAC__STREAM_DECODER_INIT_STATUS_OK",
228 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
229 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
230 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
231 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
232 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
235 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
236 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
237 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
238 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
241 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
242 "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
243 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
244 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
247 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
248 "FLAC__STREAM_DECODER_TELL_STATUS_OK",
249 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
250 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
253 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
254 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
255 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
256 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
259 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
260 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
261 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
264 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
265 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
266 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
267 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
268 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
271 /***********************************************************************
273 * Class constructor/destructor
275 ***********************************************************************/
276 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
278 FLAC__StreamDecoder *decoder;
281 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
283 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
288 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
289 if(decoder->protected_ == 0) {
294 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
295 if(decoder->private_ == 0) {
296 free(decoder->protected_);
301 decoder->private_->input = FLAC__bitbuffer_new();
302 if(decoder->private_->input == 0) {
303 free(decoder->private_);
304 free(decoder->protected_);
309 decoder->private_->metadata_filter_ids_capacity = 16;
310 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
311 FLAC__bitbuffer_delete(decoder->private_->input);
312 free(decoder->private_);
313 free(decoder->protected_);
318 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
319 decoder->private_->output[i] = 0;
320 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
323 decoder->private_->output_capacity = 0;
324 decoder->private_->output_channels = 0;
325 decoder->private_->has_seek_table = false;
327 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
328 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
330 decoder->private_->file = 0;
332 set_defaults_(decoder);
334 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
339 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
343 FLAC__ASSERT(0 != decoder);
344 FLAC__ASSERT(0 != decoder->protected_);
345 FLAC__ASSERT(0 != decoder->private_);
346 FLAC__ASSERT(0 != decoder->private_->input);
348 FLAC__stream_decoder_finish(decoder);
350 if(0 != decoder->private_->metadata_filter_ids)
351 free(decoder->private_->metadata_filter_ids);
353 FLAC__bitbuffer_delete(decoder->private_->input);
355 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
356 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
358 free(decoder->private_);
359 free(decoder->protected_);
363 /***********************************************************************
365 * Public class methods
367 ***********************************************************************/
369 static FLAC__StreamDecoderInitStatus init_stream_internal_(
370 FLAC__StreamDecoder *decoder,
371 FLAC__StreamDecoderReadCallback read_callback,
372 FLAC__StreamDecoderSeekCallback seek_callback,
373 FLAC__StreamDecoderTellCallback tell_callback,
374 FLAC__StreamDecoderLengthCallback length_callback,
375 FLAC__StreamDecoderEofCallback eof_callback,
376 FLAC__StreamDecoderWriteCallback write_callback,
377 FLAC__StreamDecoderMetadataCallback metadata_callback,
378 FLAC__StreamDecoderErrorCallback error_callback,
383 FLAC__ASSERT(0 != decoder);
385 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
386 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
388 #ifndef FLAC__HAS_OGG
390 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
394 0 == read_callback ||
395 0 == write_callback ||
396 0 == error_callback ||
397 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
399 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
402 decoder->private_->is_ogg = is_ogg;
403 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
404 return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
408 * get the CPU info and set the function pointers
410 FLAC__cpu_info(&decoder->private_->cpuinfo);
411 /* first default to the non-asm routines */
412 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
413 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
414 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
415 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
416 /* now override with asm where appropriate */
418 if(decoder->private_->cpuinfo.use_asm) {
419 #ifdef FLAC__CPU_IA32
420 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
421 #ifdef FLAC__HAS_NASM
422 if(decoder->private_->cpuinfo.data.ia32.mmx) {
423 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
424 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
425 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
428 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
429 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
430 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
433 #elif defined FLAC__CPU_PPC
434 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
435 if(decoder->private_->cpuinfo.data.ppc.altivec) {
436 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
437 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
443 /* from here on, errors are fatal */
445 if(!FLAC__bitbuffer_init(decoder->private_->input)) {
446 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
447 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
450 decoder->private_->read_callback = read_callback;
451 decoder->private_->seek_callback = seek_callback;
452 decoder->private_->tell_callback = tell_callback;
453 decoder->private_->length_callback = length_callback;
454 decoder->private_->eof_callback = eof_callback;
455 decoder->private_->write_callback = write_callback;
456 decoder->private_->metadata_callback = metadata_callback;
457 decoder->private_->error_callback = error_callback;
458 decoder->private_->client_data = client_data;
459 decoder->private_->last_frame_number = 0;
460 decoder->private_->last_block_size = 0;
461 decoder->private_->samples_decoded = 0;
462 decoder->private_->has_stream_info = false;
463 decoder->private_->cached = false;
465 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
466 decoder->private_->is_seeking = false;
468 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
469 if(!FLAC__stream_decoder_reset(decoder)) {
470 /* above call sets the state for us */
471 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
474 return FLAC__STREAM_DECODER_INIT_STATUS_OK;
477 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
478 FLAC__StreamDecoder *decoder,
479 FLAC__StreamDecoderReadCallback read_callback,
480 FLAC__StreamDecoderSeekCallback seek_callback,
481 FLAC__StreamDecoderTellCallback tell_callback,
482 FLAC__StreamDecoderLengthCallback length_callback,
483 FLAC__StreamDecoderEofCallback eof_callback,
484 FLAC__StreamDecoderWriteCallback write_callback,
485 FLAC__StreamDecoderMetadataCallback metadata_callback,
486 FLAC__StreamDecoderErrorCallback error_callback,
490 return init_stream_internal_(
505 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
506 FLAC__StreamDecoder *decoder,
507 FLAC__StreamDecoderReadCallback read_callback,
508 FLAC__StreamDecoderSeekCallback seek_callback,
509 FLAC__StreamDecoderTellCallback tell_callback,
510 FLAC__StreamDecoderLengthCallback length_callback,
511 FLAC__StreamDecoderEofCallback eof_callback,
512 FLAC__StreamDecoderWriteCallback write_callback,
513 FLAC__StreamDecoderMetadataCallback metadata_callback,
514 FLAC__StreamDecoderErrorCallback error_callback,
518 return init_stream_internal_(
533 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
534 FLAC__StreamDecoder *decoder,
536 FLAC__StreamDecoderWriteCallback write_callback,
537 FLAC__StreamDecoderMetadataCallback metadata_callback,
538 FLAC__StreamDecoderErrorCallback error_callback,
543 FLAC__ASSERT(0 != decoder);
544 FLAC__ASSERT(0 != file);
546 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
547 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
549 if(0 == write_callback || 0 == error_callback)
550 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
553 * To make sure that our file does not go unclosed after an error, we
554 * must assign the FILE pointer before any further error can occur in
558 file = get_binary_stdin_(); /* just to be safe */
560 decoder->private_->file = file;
562 return init_stream_internal_(
565 decoder->private_->file == stdin? 0: file_seek_callback_,
566 decoder->private_->file == stdin? 0: file_tell_callback_,
567 decoder->private_->file == stdin? 0: file_length_callback_,
577 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
578 FLAC__StreamDecoder *decoder,
580 FLAC__StreamDecoderWriteCallback write_callback,
581 FLAC__StreamDecoderMetadataCallback metadata_callback,
582 FLAC__StreamDecoderErrorCallback error_callback,
586 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
589 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
590 FLAC__StreamDecoder *decoder,
592 FLAC__StreamDecoderWriteCallback write_callback,
593 FLAC__StreamDecoderMetadataCallback metadata_callback,
594 FLAC__StreamDecoderErrorCallback error_callback,
598 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
601 static FLAC__StreamDecoderInitStatus init_file_internal_(
602 FLAC__StreamDecoder *decoder,
603 const char *filename,
604 FLAC__StreamDecoderWriteCallback write_callback,
605 FLAC__StreamDecoderMetadataCallback metadata_callback,
606 FLAC__StreamDecoderErrorCallback error_callback,
613 FLAC__ASSERT(0 != decoder);
616 * To make sure that our file does not go unclosed after an error, we
617 * have to do the same entrance checks here that are later performed
618 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
620 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
621 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
623 if(0 == write_callback || 0 == error_callback)
624 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
626 file = filename? fopen(filename, "rb") : stdin;
629 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
631 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
634 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
635 FLAC__StreamDecoder *decoder,
636 const char *filename,
637 FLAC__StreamDecoderWriteCallback write_callback,
638 FLAC__StreamDecoderMetadataCallback metadata_callback,
639 FLAC__StreamDecoderErrorCallback error_callback,
643 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
646 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
647 FLAC__StreamDecoder *decoder,
648 const char *filename,
649 FLAC__StreamDecoderWriteCallback write_callback,
650 FLAC__StreamDecoderMetadataCallback metadata_callback,
651 FLAC__StreamDecoderErrorCallback error_callback,
655 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
658 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
660 FLAC__bool md5_failed = false;
663 FLAC__ASSERT(0 != decoder);
664 FLAC__ASSERT(0 != decoder->private_);
665 FLAC__ASSERT(0 != decoder->protected_);
667 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
670 /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
671 * always call FLAC__MD5Final()
673 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
675 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
676 free(decoder->private_->seek_table.data.seek_table.points);
677 decoder->private_->seek_table.data.seek_table.points = 0;
678 decoder->private_->has_seek_table = false;
680 FLAC__bitbuffer_free(decoder->private_->input);
681 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
683 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
684 * output arrays have a buffer of up to 3 zeroes in front
685 * (at negative indices) for alignment purposes; we use 4
686 * to keep the data well-aligned.
688 if(0 != decoder->private_->output[i]) {
689 free(decoder->private_->output[i]-4);
690 decoder->private_->output[i] = 0;
692 if(0 != decoder->private_->residual_unaligned[i]) {
693 free(decoder->private_->residual_unaligned[i]);
694 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
697 decoder->private_->output_capacity = 0;
698 decoder->private_->output_channels = 0;
701 if(decoder->private_->is_ogg)
702 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
705 if(0 != decoder->private_->file) {
706 if(decoder->private_->file != stdin)
707 fclose(decoder->private_->file);
708 decoder->private_->file = 0;
711 if(decoder->private_->do_md5_checking) {
712 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
715 decoder->private_->is_seeking = false;
717 set_defaults_(decoder);
719 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
724 FLAC_API FLAC__bool FLAC__stream_decoder_set_serial_number(FLAC__StreamDecoder *decoder, long value)
726 FLAC__ASSERT(0 != decoder);
727 FLAC__ASSERT(0 != decoder->private_);
728 FLAC__ASSERT(0 != decoder->protected_);
729 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
732 /* can't check decoder->private_->is_ogg since that's not set until init time */
733 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
741 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
743 FLAC__ASSERT(0 != decoder);
744 FLAC__ASSERT(0 != decoder->protected_);
745 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
747 decoder->protected_->md5_checking = value;
751 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
753 FLAC__ASSERT(0 != decoder);
754 FLAC__ASSERT(0 != decoder->private_);
755 FLAC__ASSERT(0 != decoder->protected_);
756 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
757 /* double protection */
758 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
760 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
762 decoder->private_->metadata_filter[type] = true;
763 if(type == FLAC__METADATA_TYPE_APPLICATION)
764 decoder->private_->metadata_filter_ids_count = 0;
768 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
770 FLAC__ASSERT(0 != decoder);
771 FLAC__ASSERT(0 != decoder->private_);
772 FLAC__ASSERT(0 != decoder->protected_);
773 FLAC__ASSERT(0 != id);
774 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
777 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
780 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
782 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
783 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
784 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
787 decoder->private_->metadata_filter_ids_capacity *= 2;
790 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));
791 decoder->private_->metadata_filter_ids_count++;
796 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
799 FLAC__ASSERT(0 != decoder);
800 FLAC__ASSERT(0 != decoder->private_);
801 FLAC__ASSERT(0 != decoder->protected_);
802 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
804 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
805 decoder->private_->metadata_filter[i] = true;
806 decoder->private_->metadata_filter_ids_count = 0;
810 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
812 FLAC__ASSERT(0 != decoder);
813 FLAC__ASSERT(0 != decoder->private_);
814 FLAC__ASSERT(0 != decoder->protected_);
815 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
816 /* double protection */
817 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
819 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
821 decoder->private_->metadata_filter[type] = false;
822 if(type == FLAC__METADATA_TYPE_APPLICATION)
823 decoder->private_->metadata_filter_ids_count = 0;
827 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
829 FLAC__ASSERT(0 != decoder);
830 FLAC__ASSERT(0 != decoder->private_);
831 FLAC__ASSERT(0 != decoder->protected_);
832 FLAC__ASSERT(0 != id);
833 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
836 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
839 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
841 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
842 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
843 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
846 decoder->private_->metadata_filter_ids_capacity *= 2;
849 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));
850 decoder->private_->metadata_filter_ids_count++;
855 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
857 FLAC__ASSERT(0 != decoder);
858 FLAC__ASSERT(0 != decoder->private_);
859 FLAC__ASSERT(0 != decoder->protected_);
860 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
862 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
863 decoder->private_->metadata_filter_ids_count = 0;
867 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
869 FLAC__ASSERT(0 != decoder);
870 FLAC__ASSERT(0 != decoder->protected_);
871 return decoder->protected_->state;
874 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
876 return FLAC__StreamDecoderStateString[decoder->protected_->state];
879 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
881 FLAC__ASSERT(0 != decoder);
882 FLAC__ASSERT(0 != decoder->protected_);
883 return decoder->protected_->md5_checking;
886 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
888 FLAC__ASSERT(0 != decoder);
889 FLAC__ASSERT(0 != decoder->protected_);
890 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
893 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
895 FLAC__ASSERT(0 != decoder);
896 FLAC__ASSERT(0 != decoder->protected_);
897 return decoder->protected_->channels;
900 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
902 FLAC__ASSERT(0 != decoder);
903 FLAC__ASSERT(0 != decoder->protected_);
904 return decoder->protected_->channel_assignment;
907 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
909 FLAC__ASSERT(0 != decoder);
910 FLAC__ASSERT(0 != decoder->protected_);
911 return decoder->protected_->bits_per_sample;
914 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
916 FLAC__ASSERT(0 != decoder);
917 FLAC__ASSERT(0 != decoder->protected_);
918 return decoder->protected_->sample_rate;
921 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
923 FLAC__ASSERT(0 != decoder);
924 FLAC__ASSERT(0 != decoder->protected_);
925 return decoder->protected_->blocksize;
928 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
930 FLAC__ASSERT(0 != decoder);
931 FLAC__ASSERT(0 != decoder->private_);
932 FLAC__ASSERT(0 != position);
935 if(decoder->private_->is_ogg)
938 if(0 == decoder->private_->tell_callback)
940 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
942 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
943 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
947 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
949 FLAC__ASSERT(0 != decoder);
950 FLAC__ASSERT(0 != decoder->private_);
951 FLAC__ASSERT(0 != decoder->protected_);
953 decoder->private_->samples_decoded = 0;
954 decoder->private_->do_md5_checking = false;
957 if(decoder->private_->is_ogg)
958 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
961 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
962 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
965 decoder->private_->last_frame_number = 0;
966 decoder->private_->last_block_size = 0;
967 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
972 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
974 FLAC__ASSERT(0 != decoder);
975 FLAC__ASSERT(0 != decoder->private_);
976 FLAC__ASSERT(0 != decoder->protected_);
978 if(!FLAC__stream_decoder_flush(decoder)) {
979 /* above call sets the state for us */
984 /*@@@ could go in !internal_reset_hack block below */
985 if(decoder->private_->is_ogg)
986 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
989 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
990 * (internal_reset_hack) don't try to rewind since we are already at
991 * the beginning of the stream and don't want to fail if the input is
994 if(!decoder->private_->internal_reset_hack) {
995 if(decoder->private_->file == stdin)
996 return false; /* can't rewind stdin, reset fails */
997 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
998 return false; /* seekable and seek fails, reset fails */
1001 decoder->private_->internal_reset_hack = false;
1003 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
1005 decoder->private_->has_stream_info = false;
1006 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
1007 free(decoder->private_->seek_table.data.seek_table.points);
1008 decoder->private_->seek_table.data.seek_table.points = 0;
1009 decoder->private_->has_seek_table = false;
1011 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
1013 /* We initialize the FLAC__MD5Context even though we may never use it. This
1014 * is because md5 checking may be turned on to start and then turned off if
1015 * a seek occurs. So we init the context here and finalize it in
1016 * FLAC__seekable_stream_decoder_finish() to make sure things are always
1017 * cleaned up properly.
1019 FLAC__MD5Init(&decoder->private_->md5context);
1021 decoder->private_->first_frame_offset = 0;
1022 decoder->private_->unparseable_frame_count = 0;
1027 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1029 FLAC__bool got_a_frame;
1030 FLAC__ASSERT(0 != decoder);
1031 FLAC__ASSERT(0 != decoder->protected_);
1034 switch(decoder->protected_->state) {
1035 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1036 if(!find_metadata_(decoder))
1037 return false; /* above function sets the status for us */
1039 case FLAC__STREAM_DECODER_READ_METADATA:
1040 if(!read_metadata_(decoder))
1041 return false; /* above function sets the status for us */
1044 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1045 if(!frame_sync_(decoder))
1046 return true; /* above function sets the status for us */
1048 case FLAC__STREAM_DECODER_READ_FRAME:
1049 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1050 return false; /* above function sets the status for us */
1052 return true; /* above function sets the status for us */
1054 case FLAC__STREAM_DECODER_END_OF_STREAM:
1055 case FLAC__STREAM_DECODER_ABORTED:
1064 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1066 FLAC__ASSERT(0 != decoder);
1067 FLAC__ASSERT(0 != decoder->protected_);
1070 switch(decoder->protected_->state) {
1071 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1072 if(!find_metadata_(decoder))
1073 return false; /* above function sets the status for us */
1075 case FLAC__STREAM_DECODER_READ_METADATA:
1076 if(!read_metadata_(decoder))
1077 return false; /* above function sets the status for us */
1079 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1080 case FLAC__STREAM_DECODER_READ_FRAME:
1081 case FLAC__STREAM_DECODER_END_OF_STREAM:
1082 case FLAC__STREAM_DECODER_ABORTED:
1091 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1094 FLAC__ASSERT(0 != decoder);
1095 FLAC__ASSERT(0 != decoder->protected_);
1098 switch(decoder->protected_->state) {
1099 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1100 if(!find_metadata_(decoder))
1101 return false; /* above function sets the status for us */
1103 case FLAC__STREAM_DECODER_READ_METADATA:
1104 if(!read_metadata_(decoder))
1105 return false; /* above function sets the status for us */
1107 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1108 if(!frame_sync_(decoder))
1109 return true; /* above function sets the status for us */
1111 case FLAC__STREAM_DECODER_READ_FRAME:
1112 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1113 return false; /* above function sets the status for us */
1115 case FLAC__STREAM_DECODER_END_OF_STREAM:
1116 case FLAC__STREAM_DECODER_ABORTED:
1125 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1127 FLAC__bool got_a_frame;
1128 FLAC__ASSERT(0 != decoder);
1129 FLAC__ASSERT(0 != decoder->protected_);
1132 switch(decoder->protected_->state) {
1133 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1134 case FLAC__STREAM_DECODER_READ_METADATA:
1135 return false; /* above function sets the status for us */
1136 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1137 if(!frame_sync_(decoder))
1138 return true; /* above function sets the status for us */
1140 case FLAC__STREAM_DECODER_READ_FRAME:
1141 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1142 return false; /* above function sets the status for us */
1144 return true; /* above function sets the status for us */
1146 case FLAC__STREAM_DECODER_END_OF_STREAM:
1147 case FLAC__STREAM_DECODER_ABORTED:
1156 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1158 FLAC__uint64 length;
1160 FLAC__ASSERT(0 != decoder);
1163 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1164 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1165 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1166 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1167 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1171 if(0 == decoder->private_->seek_callback)
1174 FLAC__ASSERT(decoder->private_->seek_callback);
1175 FLAC__ASSERT(decoder->private_->tell_callback);
1176 FLAC__ASSERT(decoder->private_->length_callback);
1177 FLAC__ASSERT(decoder->private_->eof_callback);
1179 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1182 decoder->private_->is_seeking = true;
1184 /* turn off md5 checking if a seek is attempted */
1185 decoder->private_->do_md5_checking = false;
1187 /* 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) */
1188 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1189 decoder->private_->is_seeking = false;
1193 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
1195 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1196 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1198 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1199 /* above call sets the state for us */
1200 decoder->private_->is_seeking = false;
1203 /* check this again in case we didn't know total_samples the first time */
1204 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1205 decoder->private_->is_seeking = false;
1211 const FLAC__bool ok =
1212 #ifdef FLAC__HAS_OGG
1213 decoder->private_->is_ogg?
1214 seek_to_absolute_sample_ogg_(decoder, length, sample) :
1216 seek_to_absolute_sample_(decoder, length, sample)
1218 decoder->private_->is_seeking = false;
1223 /***********************************************************************
1225 * Protected class methods
1227 ***********************************************************************/
1229 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1231 FLAC__ASSERT(0 != decoder);
1232 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
1235 /***********************************************************************
1237 * Private class methods
1239 ***********************************************************************/
1241 void set_defaults_(FLAC__StreamDecoder *decoder)
1243 #ifdef FLAC__HAS_OGG
1244 decoder->private_->is_ogg = false;
1246 decoder->private_->read_callback = 0;
1247 decoder->private_->seek_callback = 0;
1248 decoder->private_->tell_callback = 0;
1249 decoder->private_->length_callback = 0;
1250 decoder->private_->eof_callback = 0;
1251 decoder->private_->write_callback = 0;
1252 decoder->private_->metadata_callback = 0;
1253 decoder->private_->error_callback = 0;
1254 decoder->private_->client_data = 0;
1256 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1257 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1258 decoder->private_->metadata_filter_ids_count = 0;
1260 decoder->protected_->md5_checking = false;
1262 #ifdef FLAC__HAS_OGG
1263 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1268 * This will forcibly set stdin to binary mode (for OSes that require it)
1270 FILE *get_binary_stdin_()
1272 /* if something breaks here it is probably due to the presence or
1273 * absence of an underscore before the identifiers 'setmode',
1274 * 'fileno', and/or 'O_BINARY'; check your system header files.
1276 #if defined _MSC_VER || defined __MINGW32__
1277 _setmode(_fileno(stdin), _O_BINARY);
1278 #elif defined __CYGWIN__ || defined __EMX__
1279 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
1280 setmode(_fileno(stdin), _O_BINARY);
1286 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1291 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
1294 /* simply using realloc() is not practical because the number of channels may change mid-stream */
1296 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1297 if(0 != decoder->private_->output[i]) {
1298 free(decoder->private_->output[i]-4);
1299 decoder->private_->output[i] = 0;
1301 if(0 != decoder->private_->residual_unaligned[i]) {
1302 free(decoder->private_->residual_unaligned[i]);
1303 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1307 for(i = 0; i < channels; i++) {
1309 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
1310 * output arrays have a buffer of up to 3 zeroes in front
1311 * (at negative indices) for alignment purposes; we use 4
1312 * to keep the data well-aligned.
1314 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
1316 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1319 memset(tmp, 0, sizeof(FLAC__int32)*4);
1320 decoder->private_->output[i] = tmp + 4;
1323 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
1325 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
1326 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1331 decoder->private_->output_capacity = size;
1332 decoder->private_->output_channels = channels;
1337 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1341 FLAC__ASSERT(0 != decoder);
1342 FLAC__ASSERT(0 != decoder->private_);
1344 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1345 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
1351 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1355 FLAC__bool first = true;
1357 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1359 for(i = id = 0; i < 4; ) {
1360 if(decoder->private_->cached) {
1361 x = (FLAC__uint32)decoder->private_->lookahead;
1362 decoder->private_->cached = false;
1365 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1366 return false; /* read_callback_ sets the state for us */
1368 if(x == FLAC__STREAM_SYNC_STRING[i]) {
1374 if(x == ID3V2_TAG_[id]) {
1378 if(!skip_id3v2_tag_(decoder))
1379 return false; /* skip_id3v2_tag_ sets the state for us */
1384 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1385 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1386 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1387 return false; /* read_callback_ sets the state for us */
1389 /* 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 */
1390 /* else we have to check if the second byte is the end of a sync code */
1391 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1392 decoder->private_->lookahead = (FLAC__byte)x;
1393 decoder->private_->cached = true;
1395 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1396 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1397 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1403 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1408 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1412 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1415 FLAC__uint32 i, x, type, length;
1417 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1419 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
1420 return false; /* read_callback_ sets the state for us */
1421 is_last = x? true : false;
1423 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
1424 return false; /* read_callback_ sets the state for us */
1426 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
1427 return false; /* read_callback_ sets the state for us */
1429 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1430 if(!read_metadata_streaminfo_(decoder, is_last, length))
1433 decoder->private_->has_stream_info = true;
1434 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))
1435 decoder->private_->do_md5_checking = false;
1436 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1437 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1439 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1440 if(!read_metadata_seektable_(decoder, is_last, length))
1443 decoder->private_->has_seek_table = true;
1444 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1445 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1448 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1449 unsigned real_length = length;
1450 FLAC__StreamMetadata block;
1452 block.is_last = is_last;
1453 block.type = (FLAC__MetadataType)type;
1454 block.length = length;
1456 if(type == FLAC__METADATA_TYPE_APPLICATION) {
1457 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
1458 return false; /* read_callback_ sets the state for us */
1460 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1462 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1467 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
1468 return false; /* read_callback_ sets the state for us */
1472 case FLAC__METADATA_TYPE_PADDING:
1473 /* skip the padding bytes */
1474 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
1475 return false; /* read_callback_ sets the state for us */
1477 case FLAC__METADATA_TYPE_APPLICATION:
1478 /* remember, we read the ID already */
1479 if(real_length > 0) {
1480 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
1481 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1484 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
1485 return false; /* read_callback_ sets the state for us */
1488 block.data.application.data = 0;
1490 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1491 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
1494 case FLAC__METADATA_TYPE_CUESHEET:
1495 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1498 case FLAC__METADATA_TYPE_PICTURE:
1499 if(!read_metadata_picture_(decoder, &block.data.picture))
1502 case FLAC__METADATA_TYPE_STREAMINFO:
1503 case FLAC__METADATA_TYPE_SEEKTABLE:
1507 if(real_length > 0) {
1508 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
1509 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1512 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
1513 return false; /* read_callback_ sets the state for us */
1516 block.data.unknown.data = 0;
1519 if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
1520 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1522 /* now we have to free any malloc'ed data in the block */
1524 case FLAC__METADATA_TYPE_PADDING:
1526 case FLAC__METADATA_TYPE_APPLICATION:
1527 if(0 != block.data.application.data)
1528 free(block.data.application.data);
1530 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1531 if(0 != block.data.vorbis_comment.vendor_string.entry)
1532 free(block.data.vorbis_comment.vendor_string.entry);
1533 if(block.data.vorbis_comment.num_comments > 0)
1534 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1535 if(0 != block.data.vorbis_comment.comments[i].entry)
1536 free(block.data.vorbis_comment.comments[i].entry);
1537 if(0 != block.data.vorbis_comment.comments)
1538 free(block.data.vorbis_comment.comments);
1540 case FLAC__METADATA_TYPE_CUESHEET:
1541 if(block.data.cue_sheet.num_tracks > 0)
1542 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1543 if(0 != block.data.cue_sheet.tracks[i].indices)
1544 free(block.data.cue_sheet.tracks[i].indices);
1545 if(0 != block.data.cue_sheet.tracks)
1546 free(block.data.cue_sheet.tracks);
1548 case FLAC__METADATA_TYPE_PICTURE:
1549 if(0 != block.data.picture.mime_type)
1550 free(block.data.picture.mime_type);
1551 if(0 != block.data.picture.description)
1552 free(block.data.picture.description);
1553 if(0 != block.data.picture.data)
1554 free(block.data.picture.data);
1556 case FLAC__METADATA_TYPE_STREAMINFO:
1557 case FLAC__METADATA_TYPE_SEEKTABLE:
1560 if(0 != block.data.unknown.data)
1561 free(block.data.unknown.data);
1568 /* if this fails, it's OK, it's just a hint for the seek routine */
1569 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1570 decoder->private_->first_frame_offset = 0;
1571 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1577 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1580 unsigned bits, used_bits = 0;
1582 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1584 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1585 decoder->private_->stream_info.is_last = is_last;
1586 decoder->private_->stream_info.length = length;
1588 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1589 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
1590 return false; /* read_callback_ sets the state for us */
1591 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1594 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1595 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
1596 return false; /* read_callback_ sets the state for us */
1597 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1600 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1601 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
1602 return false; /* read_callback_ sets the state for us */
1603 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1606 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1607 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1608 return false; /* read_callback_ sets the state for us */
1609 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1612 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1613 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1614 return false; /* read_callback_ sets the state for us */
1615 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1618 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1619 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1620 return false; /* read_callback_ sets the state for us */
1621 decoder->private_->stream_info.data.stream_info.channels = x+1;
1624 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1625 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1626 return false; /* read_callback_ sets the state for us */
1627 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1630 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1631 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
1632 return false; /* read_callback_ sets the state for us */
1635 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1636 return false; /* read_callback_ sets the state for us */
1639 /* skip the rest of the block */
1640 FLAC__ASSERT(used_bits % 8 == 0);
1641 length -= (used_bits / 8);
1642 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1643 return false; /* read_callback_ sets the state for us */
1648 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1653 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1655 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1656 decoder->private_->seek_table.is_last = is_last;
1657 decoder->private_->seek_table.length = length;
1659 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1661 /* use realloc since we may pass through here several times (e.g. after seeking) */
1662 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
1663 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1666 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1667 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1668 return false; /* read_callback_ sets the state for us */
1669 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1671 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1672 return false; /* read_callback_ sets the state for us */
1673 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1675 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1676 return false; /* read_callback_ sets the state for us */
1677 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1679 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1680 /* if there is a partial point left, skip over it */
1682 /*@@@ do a send_error_to_client_() here? there's an argument for either way */
1683 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1684 return false; /* read_callback_ sets the state for us */
1690 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1694 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1696 /* read vendor string */
1697 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1698 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1699 return false; /* read_callback_ sets the state for us */
1700 if(obj->vendor_string.length > 0) {
1701 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
1702 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1705 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1706 return false; /* read_callback_ sets the state for us */
1707 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
1710 obj->vendor_string.entry = 0;
1712 /* read num comments */
1713 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1714 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1715 return false; /* read_callback_ sets the state for us */
1718 if(obj->num_comments > 0) {
1719 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1720 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1723 for(i = 0; i < obj->num_comments; i++) {
1724 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1725 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1726 return false; /* read_callback_ sets the state for us */
1727 if(obj->comments[i].length > 0) {
1728 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
1729 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1732 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1733 return false; /* read_callback_ sets the state for us */
1734 obj->comments[i].entry[obj->comments[i].length] = '\0';
1737 obj->comments[i].entry = 0;
1747 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1749 FLAC__uint32 i, j, x;
1751 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1753 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1755 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1756 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
1757 return false; /* read_callback_ sets the state for us */
1759 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1760 return false; /* read_callback_ sets the state for us */
1762 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1763 return false; /* read_callback_ sets the state for us */
1764 obj->is_cd = x? true : false;
1766 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1767 return false; /* read_callback_ sets the state for us */
1769 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1770 return false; /* read_callback_ sets the state for us */
1771 obj->num_tracks = x;
1773 if(obj->num_tracks > 0) {
1774 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1775 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1778 for(i = 0; i < obj->num_tracks; i++) {
1779 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1780 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1781 return false; /* read_callback_ sets the state for us */
1783 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1784 return false; /* read_callback_ sets the state for us */
1785 track->number = (FLAC__byte)x;
1787 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1788 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
1789 return false; /* read_callback_ sets the state for us */
1791 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1792 return false; /* read_callback_ sets the state for us */
1795 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1796 return false; /* read_callback_ sets the state for us */
1797 track->pre_emphasis = x;
1799 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1800 return false; /* read_callback_ sets the state for us */
1802 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1803 return false; /* read_callback_ sets the state for us */
1804 track->num_indices = (FLAC__byte)x;
1806 if(track->num_indices > 0) {
1807 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1808 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1811 for(j = 0; j < track->num_indices; j++) {
1812 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1813 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1814 return false; /* read_callback_ sets the state for us */
1816 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1817 return false; /* read_callback_ sets the state for us */
1818 index->number = (FLAC__byte)x;
1820 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1821 return false; /* read_callback_ sets the state for us */
1830 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
1834 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1837 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN, read_callback_, decoder))
1838 return false; /* read_callback_ sets the state for us */
1840 /* read MIME type */
1841 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN, read_callback_, decoder))
1842 return false; /* read_callback_ sets the state for us */
1843 if(0 == (obj->mime_type = (char*)malloc(len+1))) {
1844 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1848 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len, read_callback_, decoder))
1849 return false; /* read_callback_ sets the state for us */
1851 obj->mime_type[len] = '\0';
1853 /* read description */
1854 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN, read_callback_, decoder))
1855 return false; /* read_callback_ sets the state for us */
1856 if(0 == (obj->description = (FLAC__byte*)malloc(len+1))) {
1857 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1861 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len, read_callback_, decoder))
1862 return false; /* read_callback_ sets the state for us */
1864 obj->description[len] = '\0';
1867 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN, read_callback_, decoder))
1868 return false; /* read_callback_ sets the state for us */
1871 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN, read_callback_, decoder))
1872 return false; /* read_callback_ sets the state for us */
1875 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN, read_callback_, decoder))
1876 return false; /* read_callback_ sets the state for us */
1879 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN, read_callback_, decoder))
1880 return false; /* read_callback_ sets the state for us */
1883 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN, read_callback_, decoder))
1884 return false; /* read_callback_ sets the state for us */
1885 if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
1886 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1889 if(obj->data_length > 0) {
1890 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length, read_callback_, decoder))
1891 return false; /* read_callback_ sets the state for us */
1897 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1902 /* skip the version and flags bytes */
1903 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1904 return false; /* read_callback_ sets the state for us */
1905 /* get the size (in bytes) to skip */
1907 for(i = 0; i < 4; i++) {
1908 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1909 return false; /* read_callback_ sets the state for us */
1913 /* skip the rest of the tag */
1914 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1915 return false; /* read_callback_ sets the state for us */
1919 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1922 FLAC__bool first = true;
1924 /* If we know the total number of samples in the stream, stop if we've read that many. */
1925 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1926 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
1927 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1928 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1933 /* make sure we're byte aligned */
1934 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1935 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1936 return false; /* read_callback_ sets the state for us */
1940 if(decoder->private_->cached) {
1941 x = (FLAC__uint32)decoder->private_->lookahead;
1942 decoder->private_->cached = false;
1945 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1946 return false; /* read_callback_ sets the state for us */
1948 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1949 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1950 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1951 return false; /* read_callback_ sets the state for us */
1953 /* 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 */
1954 /* else we have to check if the second byte is the end of a sync code */
1955 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1956 decoder->private_->lookahead = (FLAC__byte)x;
1957 decoder->private_->cached = true;
1959 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1960 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1961 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1966 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1974 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
1978 FLAC__int32 mid, side, left, right;
1979 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1982 *got_a_frame = false;
1986 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1987 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1988 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1990 if(!read_frame_header_(decoder))
1992 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
1994 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1996 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1998 * first figure the correct bits-per-sample of the subframe
2000 unsigned bps = decoder->private_->frame.header.bits_per_sample;
2001 switch(decoder->private_->frame.header.channel_assignment) {
2002 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2003 /* no adjustment needed */
2005 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2006 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2010 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2011 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2015 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2016 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2026 if(!read_subframe_(decoder, channel, bps, do_full_decode))
2028 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2031 if(!read_zero_padding_(decoder))
2035 * Read the frame CRC-16 from the footer and check
2037 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
2038 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
2039 return false; /* read_callback_ sets the state for us */
2040 if(frame_crc == (FLAC__uint16)x) {
2041 if(do_full_decode) {
2042 /* Undo any special channel coding */
2043 switch(decoder->private_->frame.header.channel_assignment) {
2044 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2047 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2048 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2049 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2050 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
2052 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2053 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2054 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2055 decoder->private_->output[0][i] += decoder->private_->output[1][i];
2057 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2058 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2059 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2060 mid = decoder->private_->output[0][i];
2061 side = decoder->private_->output[1][i];
2063 if(side & 1) /* i.e. if 'side' is odd... */
2067 decoder->private_->output[0][i] = left >> 1;
2068 decoder->private_->output[1][i] = right >> 1;
2078 /* Bad frame, emit error and zero the output signal */
2079 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2080 if(do_full_decode) {
2081 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2082 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2087 *got_a_frame = true;
2089 /* put the latest values into the public section of the decoder instance */
2090 decoder->protected_->channels = decoder->private_->frame.header.channels;
2091 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2092 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2093 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2094 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2096 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2097 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2100 if(do_full_decode) {
2101 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
2105 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2109 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2113 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
2114 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2115 unsigned raw_header_len;
2116 FLAC__bool is_unparseable = false;
2117 const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
2118 const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
2120 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
2122 /* init the raw header with the saved bits from synchronization */
2123 raw_header[0] = decoder->private_->header_warmup[0];
2124 raw_header[1] = decoder->private_->header_warmup[1];
2128 * check to make sure that the reserved bits are 0
2130 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
2131 is_unparseable = true;
2135 * Note that along the way as we read the header, we look for a sync
2136 * code inside. If we find one it would indicate that our original
2137 * sync was bad since there cannot be a sync code in a valid header.
2141 * read in the raw header as bytes so we can CRC it, and parse it on the way
2143 for(i = 0; i < 2; i++) {
2144 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
2145 return false; /* read_callback_ sets the state for us */
2146 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2147 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
2148 decoder->private_->lookahead = (FLAC__byte)x;
2149 decoder->private_->cached = true;
2150 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2151 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2154 raw_header[raw_header_len++] = (FLAC__byte)x;
2157 switch(x = raw_header[2] >> 4) {
2159 if(is_known_fixed_blocksize_stream)
2160 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2162 is_unparseable = true;
2165 decoder->private_->frame.header.blocksize = 192;
2171 decoder->private_->frame.header.blocksize = 576 << (x-2);
2185 decoder->private_->frame.header.blocksize = 256 << (x-8);
2192 switch(x = raw_header[2] & 0x0f) {
2194 if(decoder->private_->has_stream_info)
2195 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2197 is_unparseable = true;
2202 is_unparseable = true;
2205 decoder->private_->frame.header.sample_rate = 8000;
2208 decoder->private_->frame.header.sample_rate = 16000;
2211 decoder->private_->frame.header.sample_rate = 22050;
2214 decoder->private_->frame.header.sample_rate = 24000;
2217 decoder->private_->frame.header.sample_rate = 32000;
2220 decoder->private_->frame.header.sample_rate = 44100;
2223 decoder->private_->frame.header.sample_rate = 48000;
2226 decoder->private_->frame.header.sample_rate = 96000;
2231 sample_rate_hint = x;
2234 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2235 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2241 x = (unsigned)(raw_header[3] >> 4);
2243 decoder->private_->frame.header.channels = 2;
2246 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2249 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2252 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2255 is_unparseable = true;
2260 decoder->private_->frame.header.channels = (unsigned)x + 1;
2261 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2264 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
2266 if(decoder->private_->has_stream_info)
2267 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2269 is_unparseable = true;
2272 decoder->private_->frame.header.bits_per_sample = 8;
2275 decoder->private_->frame.header.bits_per_sample = 12;
2278 decoder->private_->frame.header.bits_per_sample = 16;
2281 decoder->private_->frame.header.bits_per_sample = 20;
2284 decoder->private_->frame.header.bits_per_sample = 24;
2288 is_unparseable = true;
2295 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
2296 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2297 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2302 * Now we get to the regrettable consequences of not knowing for sure
2303 * whether we got a frame number or a sample number. There are no
2304 * encoders that do variable-blocksize encoding so unless we know from
2305 * the STREAMINFO that it is variable-blocksize we will assume it is
2306 * fixed-blocksize. The trouble comes when we have no STREAMINFO; again
2307 * we will guess that is fixed-blocksize. Where this can go wrong: 1) a
2308 * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize
2309 * stream that was edited such that one or more frames before or
2310 * including this one do not have the same number of samples as the
2311 * STREAMINFO's min and max blocksize.
2313 if(is_known_variable_blocksize_stream) {
2314 if(blocksize_hint) {
2315 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
2316 return false; /* read_callback_ sets the state for us */
2317 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2318 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2319 decoder->private_->cached = true;
2320 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2321 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2324 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2325 decoder->private_->frame.header.number.sample_number = xx;
2328 is_unparseable = true;
2331 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
2332 return false; /* read_callback_ sets the state for us */
2333 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2334 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2335 decoder->private_->cached = true;
2336 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2337 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2340 decoder->private_->last_frame_number = x;
2341 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2342 if(decoder->private_->has_stream_info) {
2343 FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
2344 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2345 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
2347 else if(blocksize_hint) {
2348 if(decoder->private_->last_block_size)
2349 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x;
2351 is_unparseable = true;
2354 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2355 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
2359 if(blocksize_hint) {
2360 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
2361 return false; /* read_callback_ sets the state for us */
2362 raw_header[raw_header_len++] = (FLAC__byte)x;
2363 if(blocksize_hint == 7) {
2365 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
2366 return false; /* read_callback_ sets the state for us */
2367 raw_header[raw_header_len++] = (FLAC__byte)_x;
2370 decoder->private_->frame.header.blocksize = x+1;
2373 if(sample_rate_hint) {
2374 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
2375 return false; /* read_callback_ sets the state for us */
2376 raw_header[raw_header_len++] = (FLAC__byte)x;
2377 if(sample_rate_hint != 12) {
2379 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
2380 return false; /* read_callback_ sets the state for us */
2381 raw_header[raw_header_len++] = (FLAC__byte)_x;
2384 if(sample_rate_hint == 12)
2385 decoder->private_->frame.header.sample_rate = x*1000;
2386 else if(sample_rate_hint == 13)
2387 decoder->private_->frame.header.sample_rate = x;
2389 decoder->private_->frame.header.sample_rate = x*10;
2392 /* read the CRC-8 byte */
2393 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
2394 return false; /* read_callback_ sets the state for us */
2395 crc8 = (FLAC__byte)x;
2397 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
2398 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2399 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2403 if(is_unparseable) {
2404 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2405 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2412 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2415 FLAC__bool wasted_bits;
2418 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
2419 return false; /* read_callback_ sets the state for us */
2421 wasted_bits = (x & 1);
2426 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
2427 return false; /* read_callback_ sets the state for us */
2428 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2429 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2432 decoder->private_->frame.subframes[channel].wasted_bits = 0;
2435 * Lots of magic numbers here
2438 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2439 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2443 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2447 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2451 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2452 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2456 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
2458 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2462 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2463 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2467 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
2469 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2473 if(wasted_bits && do_full_decode) {
2474 x = decoder->private_->frame.subframes[channel].wasted_bits;
2475 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2476 decoder->private_->output[channel][i] <<= x;
2482 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2484 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
2487 FLAC__int32 *output = decoder->private_->output[channel];
2489 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2491 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
2492 return false; /* read_callback_ sets the state for us */
2494 subframe->value = x;
2496 /* decode the subframe */
2497 if(do_full_decode) {
2498 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2505 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2507 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
2512 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
2514 subframe->residual = decoder->private_->residual[channel];
2515 subframe->order = order;
2517 /* read warm-up samples */
2518 for(u = 0; u < order; u++) {
2519 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
2520 return false; /* read_callback_ sets the state for us */
2521 subframe->warmup[u] = i32;
2524 /* read entropy coding method info */
2525 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
2526 return false; /* read_callback_ sets the state for us */
2527 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2528 switch(subframe->entropy_coding_method.type) {
2529 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2530 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
2531 return false; /* read_callback_ sets the state for us */
2532 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2533 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2536 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2537 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2542 switch(subframe->entropy_coding_method.type) {
2543 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2544 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]))
2551 /* decode the subframe */
2552 if(do_full_decode) {
2553 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2554 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
2560 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2562 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
2567 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
2569 subframe->residual = decoder->private_->residual[channel];
2570 subframe->order = order;
2572 /* read warm-up samples */
2573 for(u = 0; u < order; u++) {
2574 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
2575 return false; /* read_callback_ sets the state for us */
2576 subframe->warmup[u] = i32;
2579 /* read qlp coeff precision */
2580 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
2581 return false; /* read_callback_ sets the state for us */
2582 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
2583 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2584 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2587 subframe->qlp_coeff_precision = u32+1;
2589 /* read qlp shift */
2590 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
2591 return false; /* read_callback_ sets the state for us */
2592 subframe->quantization_level = i32;
2594 /* read quantized lp coefficiencts */
2595 for(u = 0; u < order; u++) {
2596 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
2597 return false; /* read_callback_ sets the state for us */
2598 subframe->qlp_coeff[u] = i32;
2601 /* read entropy coding method info */
2602 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
2603 return false; /* read_callback_ sets the state for us */
2604 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2605 switch(subframe->entropy_coding_method.type) {
2606 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2607 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
2608 return false; /* read_callback_ sets the state for us */
2609 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2610 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2613 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2614 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2619 switch(subframe->entropy_coding_method.type) {
2620 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2621 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]))
2628 /* decode the subframe */
2629 if(do_full_decode) {
2630 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2631 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2632 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
2634 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);
2636 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);
2639 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);
2641 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);
2647 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2649 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2650 FLAC__int32 x, *residual = decoder->private_->residual[channel];
2653 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
2655 subframe->data = residual;
2657 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2658 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
2659 return false; /* read_callback_ sets the state for us */
2663 /* decode the subframe */
2665 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2670 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
2672 FLAC__uint32 rice_parameter;
2674 unsigned partition, sample, u;
2675 const unsigned partitions = 1u << partition_order;
2676 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2679 if(partition_order == 0) {
2680 if(decoder->private_->frame.header.blocksize < predictor_order) {
2681 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2682 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2687 if(partition_samples < predictor_order) {
2688 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2689 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2694 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
2695 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2700 for(partition = 0; partition < partitions; partition++) {
2701 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
2702 return false; /* read_callback_ sets the state for us */
2703 partitioned_rice_contents->parameters[partition] = rice_parameter;
2704 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2705 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2706 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
2707 return false; /* read_callback_ sets the state for us */
2711 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
2712 return false; /* read_callback_ sets the state for us */
2713 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2714 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2715 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
2716 return false; /* read_callback_ sets the state for us */
2717 residual[sample] = i;
2725 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2727 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
2728 FLAC__uint32 zero = 0;
2729 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
2730 return false; /* read_callback_ sets the state for us */
2732 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2733 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2739 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
2741 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2743 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2744 if(!decoder->private_->is_ogg && decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)) {
2746 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2749 else if(*bytes > 0) {
2750 /* While seeking, it is possible for our seek to land in the
2751 * middle of audio data that looks exactly like a frame header
2752 * from a future version of an encoder. When that happens, our
2753 * error callback will get an
2754 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
2755 * unparseable_frame_count. But there is a remote possibility
2756 * that it is properly synced at such a "future-codec frame",
2757 * so to make sure, we wait to see many "unparseable" errors in
2758 * a row before bailing out.
2760 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2761 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2765 const FLAC__StreamDecoderReadStatus status =
2766 #ifdef FLAC__HAS_OGG
2767 decoder->private_->is_ogg?
2768 read_callback_ogg_aspect_(decoder, buffer, bytes) :
2770 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2772 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
2773 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2776 else if(*bytes == 0) {
2777 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2778 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM || (!decoder->private_->is_ogg && decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data))) {
2779 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2790 /* abort to avoid a deadlock */
2791 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2794 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
2795 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2796 * and at the same time hit the end of the stream. There is no way to
2797 * report an Ogg sync loss through the callbacks (see note in
2798 * read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
2799 * So to keep the decoder from stopping at this point we gate the call
2800 * to the eof_callback and let the Ogg decoder aspect set the
2801 * end-of-stream state when it is needed.
2805 #ifdef FLAC__HAS_OGG
2806 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes)
2808 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2809 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
2810 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2811 /* we don't really have a way to handle lost sync via read
2812 * callback so we'll let it pass and let the underlying
2813 * FLAC decoder catch the error
2815 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
2816 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2817 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
2818 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2819 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
2820 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
2821 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
2822 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
2823 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
2824 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2827 /* double protection */
2828 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2832 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
2834 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2836 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2837 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
2838 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
2839 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
2840 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
2841 case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
2842 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2844 /* double protection: */
2846 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2851 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2853 if(decoder->private_->is_seeking) {
2854 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
2855 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
2856 FLAC__uint64 target_sample = decoder->private_->target_sample;
2858 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2860 #ifdef FLAC__HAS_OGG
2861 decoder->private_->got_a_frame = true;
2863 decoder->private_->last_frame = *frame; /* save the frame */
2864 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
2865 unsigned delta = (unsigned)(target_sample - this_frame_sample);
2866 /* kick out of seek mode */
2867 decoder->private_->is_seeking = false;
2868 /* shift out the samples before target_sample */
2871 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
2872 for(channel = 0; channel < frame->header.channels; channel++)
2873 newbuffer[channel] = buffer[channel] + delta;
2874 decoder->private_->last_frame.header.blocksize -= delta;
2875 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2876 /* write the relevant samples */
2877 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2880 /* write the relevant samples */
2881 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2885 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2890 * If we never got STREAMINFO, turn off MD5 checking to save
2891 * cycles since we don't have a sum to compare to anyway
2893 if(!decoder->private_->has_stream_info)
2894 decoder->private_->do_md5_checking = false;
2895 if(decoder->private_->do_md5_checking) {
2896 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2897 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2899 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2903 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2905 if(!decoder->private_->is_seeking)
2906 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2907 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
2908 decoder->private_->unparseable_frame_count++;
2911 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
2913 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound;
2914 FLAC__int64 pos = -1, last_pos = -1;
2915 int i, lower_seek_point = -1, upper_seek_point = -1;
2916 unsigned approx_bytes_per_frame;
2917 FLAC__uint64 last_frame_sample = FLAC__U64L(0xffffffffffffffff);
2918 FLAC__bool needs_seek;
2919 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
2920 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2921 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
2922 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
2923 const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2924 const unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
2925 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
2927 /* we are just guessing here, but we want to guess high, not low */
2928 if(max_framesize > 0)
2929 approx_bytes_per_frame = max_framesize;
2932 * Check if it's a known fixed-blocksize stream. Note that though
2933 * the spec doesn't allow zeroes in the STREAMINFO block, we may
2934 * never get a STREAMINFO block when decoding so the value of
2935 * min_blocksize might be zero.
2937 else if(min_blocksize == max_blocksize && min_blocksize > 0) {
2938 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
2939 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
2942 approx_bytes_per_frame = 4608 * channels * bps/8 + 64;
2945 * First, we set an upper and lower bound on where in the
2946 * stream we will search. For now we assume the worst case
2947 * scenario, which is our best guess at the beginning of
2948 * the first and last frames.
2950 lower_bound = first_frame_offset;
2952 /* calc the upper_bound, beyond which we never want to seek */
2953 if(max_framesize > 0)
2954 upper_bound = stream_length - (max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
2956 upper_bound = stream_length - ((channels * bps * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
2959 * Now we refine the bounds if we have a seektable with
2960 * suitable points. Note that according to the spec they
2961 * must be ordered by ascending sample number.
2964 /* find the closest seek point <= target_sample, if it exists */
2965 for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
2966 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number <= target_sample)
2969 if(i >= 0) { /* i.e. we found a suitable seek point... */
2970 lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
2971 lower_seek_point = i;
2974 /* find the closest seek point > target_sample, if it exists */
2975 for(i = 0; i < (int)seek_table->num_points; i++) {
2976 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number > target_sample)
2979 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
2980 upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
2981 upper_seek_point = i;
2986 * Now guess at where within those bounds our target
2989 if(seek_table && lower_seek_point >= 0) {
2990 /* first see if our sample is within a few frames of the lower seekpoint */
2991 if(seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < seek_table->points[lower_seek_point].sample_number + (seek_table->points[lower_seek_point].frame_samples * 4)) {
2992 pos = (FLAC__int64)lower_bound;
2994 else if(upper_seek_point >= 0) {
2995 const FLAC__uint64 target_offset = target_sample - seek_table->points[lower_seek_point].sample_number;
2996 const FLAC__uint64 range_samples = seek_table->points[upper_seek_point].sample_number - seek_table->points[lower_seek_point].sample_number;
2997 const FLAC__uint64 range_bytes = (upper_bound>lower_bound? upper_bound - lower_bound - 1 : 0);
2998 #ifndef FLAC__INTEGER_ONLY_LIBRARY
2999 #if defined _MSC_VER || defined __MINGW32__
3000 /* with MSVC you have to spoon feed it the casting */
3001 pos = (FLAC__int64)lower_bound + (FLAC__int64)(((FLAC__double)(FLAC__int64)target_offset / (FLAC__double)(FLAC__int64)range_samples) * (FLAC__double)(FLAC__int64)(range_bytes-1)) - approx_bytes_per_frame;
3003 pos = (FLAC__int64)lower_bound + (FLAC__int64)(((FLAC__double)target_offset / (FLAC__double)range_samples) * (FLAC__double)range_bytes) - approx_bytes_per_frame;
3006 /* a little less accurate: */
3007 if (range_bytes <= 0xffffffff)
3008 pos = (FLAC__int64)lower_bound + (FLAC__int64)((target_offset * range_bytes) / range_samples) - approx_bytes_per_frame;
3009 else /* @@@ WATCHOUT, ~2TB limit */
3010 pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_offset>>8) * (range_bytes>>8)) / (range_samples>>16)) - approx_bytes_per_frame;
3016 * If there's no seek table, we need to use the metadata (if we
3017 * have it) and the filelength to estimate the position of the
3018 * frame with the correct sample.
3020 if(pos < 0 && total_samples > 0) {
3022 * For max accuracy we should be using
3023 * (stream_length-first_frame_offset-1) in the divisor, but the
3024 * difference is trivial and (stream_length-first_frame_offset)
3025 * has no chance of underflow.
3027 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3028 #if defined _MSC_VER || defined __MINGW32__
3029 /* with VC++ you have to spoon feed it the casting */
3030 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((FLAC__double)(FLAC__int64)target_sample / (FLAC__double)(FLAC__int64)total_samples) * (FLAC__double)(FLAC__int64)(stream_length-first_frame_offset)) - approx_bytes_per_frame;
3032 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((FLAC__double)target_sample / (FLAC__double)total_samples) * (FLAC__double)(stream_length-first_frame_offset)) - approx_bytes_per_frame;
3035 /* a little less accurate: */
3036 if (stream_length < 0xffffffff)
3037 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((target_sample * (stream_length-first_frame_offset)) / total_samples) - approx_bytes_per_frame;
3038 else /* @@@ WATCHOUT, ~2TB limit */
3039 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((target_sample>>8) * ((stream_length-first_frame_offset)>>8)) / (total_samples>>16)) - approx_bytes_per_frame;
3044 * If there's no seek table and total_samples is unknown, we
3045 * don't even bother trying to figure out a target, we just use
3046 * our current position.
3050 if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) {
3051 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3054 pos = (FLAC__int64)upos;
3060 /* clip the position to the bounds, lower bound takes precedence */
3061 if(pos >= (FLAC__int64)upper_bound) {
3062 pos = (FLAC__int64)upper_bound-1;
3065 if(pos < (FLAC__int64)lower_bound) {
3066 pos = (FLAC__int64)lower_bound;
3070 decoder->private_->target_sample = target_sample;
3073 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3074 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3077 if(!FLAC__stream_decoder_flush(decoder)) {
3078 /* above call sets the state for us */
3082 /* Now we need to get a frame. First we need to reset our
3083 * unparseable_frame_count; if we get too many unparseable
3084 * frames in a row, the read callback will return
3085 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3086 * FLAC__stream_decoder_process_single() to return false.
3088 decoder->private_->unparseable_frame_count = 0;
3089 if(!FLAC__stream_decoder_process_single(decoder)) {
3090 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3093 /* our write callback will change the state when it gets to the target frame */
3094 /* 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 */
3096 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
3097 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3100 if(!decoder->private_->is_seeking) {
3103 else { /* we need to narrow the search */
3104 const FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3105 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3106 if(this_frame_sample == last_frame_sample && pos < last_pos) {
3107 /* our last move backwards wasn't big enough, double it */
3108 pos -= (last_pos - pos);
3112 if(target_sample < this_frame_sample) {
3114 approx_bytes_per_frame = decoder->private_->last_frame.header.blocksize * channels * bps/8 + 64;
3115 pos -= approx_bytes_per_frame;
3118 else { /* target_sample >= this_frame_sample + this frame's blocksize */
3120 if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) {
3121 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3125 pos = (FLAC__int64)upos;
3126 pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
3129 * if we haven't hit the target frame yet and our position hasn't changed,
3130 * it means we're at the end of the stream and the seek target does not exist.
3132 if(last_pos == pos) {
3133 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3138 if(pos < (FLAC__int64)lower_bound)
3139 pos = (FLAC__int64)lower_bound;
3140 last_frame_sample = this_frame_sample;
3147 #ifdef FLAC__HAS_OGG
3148 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3150 FLAC__uint64 left_pos = 0, right_pos = stream_length;
3151 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3152 FLAC__uint64 this_frame_sample = 0; /* only initialized to avoid compiler warning */
3153 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3154 FLAC__bool did_a_seek;
3155 unsigned iteration = 0;
3157 /* In the first iterations, we will calculate the target byte position
3158 * by the distance from the target sample to left_sample and
3159 * right_sample (let's call it "proportional search"). After that, we
3160 * will switch to binary search.
3162 unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
3164 /* We will switch to a linear search once our current sample is less
3165 * than this number of samples ahead of the target sample
3167 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3169 /* If the total number of samples is unknown, use a large value, and
3170 * force binary search immediately.
3172 if(right_sample == 0) {
3173 right_sample = (FLAC__uint64)(-1);
3174 BINARY_SEARCH_AFTER_ITERATION = 0;
3177 decoder->private_->target_sample = target_sample;
3178 for( ; ; iteration++) {
3179 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3180 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3181 pos = (right_pos + left_pos) / 2;
3184 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3185 #if defined _MSC_VER || defined __MINGW32__
3186 /* with MSVC you have to spoon feed it the casting */
3187 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));
3189 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
3192 /* a little less accurate: */
3193 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
3194 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
3195 else /* @@@ WATCHOUT, ~2TB limit */
3196 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
3198 /* @@@ TODO: might want to limit pos to some distance
3199 * before EOF, to make sure we land before the last frame,
3200 * thereby getting a this_frame_sample and so having a better
3201 * estimate. @@@@@@DELETE:this would also mostly (or totally if we could
3202 * be sure to land before the last frame) avoid the
3203 * end-of-stream case we have to check later.
3208 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3209 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3212 if(!FLAC__stream_decoder_flush(decoder)) {
3213 /* above call sets the state for us */
3221 decoder->private_->got_a_frame = false;
3222 if(!FLAC__stream_decoder_process_single(decoder)) {
3223 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3226 if(!decoder->private_->got_a_frame) {
3228 /* this can happen if we seek to a point after the last frame; we drop
3229 * to binary search right away in this case to avoid any wasted
3230 * iterations of proportional search.
3233 BINARY_SEARCH_AFTER_ITERATION = 0;
3236 /* this can probably only happen if total_samples is unknown and the
3237 * target_sample is past the end of the stream
3239 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3243 /* our write callback will change the state when it gets to the target frame */
3244 else if(!decoder->private_->is_seeking/*@@@@@@ && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM*/) {
3248 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3249 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3252 if (this_frame_sample <= target_sample) {
3253 /* The 'equal' case should not happen, since
3254 * FLAC__stream_decoder_process_single()
3255 * should recognize that it has hit the
3256 * target sample and we would exit through
3257 * the 'break' above.
3259 FLAC__ASSERT(this_frame_sample != target_sample);
3261 left_sample = this_frame_sample;
3262 /* sanity check to avoid infinite loop */
3263 if (left_pos == pos) {
3264 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3269 else if(this_frame_sample > target_sample) {
3270 right_sample = this_frame_sample;
3271 /* sanity check to avoid infinite loop */
3272 if (right_pos == pos) {
3273 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3286 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
3291 *bytes = (unsigned)fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3292 if(ferror(decoder->private_->file))
3293 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3294 else if(*bytes == 0)
3295 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3297 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3300 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
3303 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3307 if(decoder->private_->file == stdin)
3308 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3309 else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3310 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3312 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3315 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3320 if(decoder->private_->file == stdin)
3321 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3322 else if((pos = ftello(decoder->private_->file)) < 0)
3323 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3325 *absolute_byte_offset = (FLAC__uint64)pos;
3326 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3330 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3332 struct stat filestats;
3335 if(decoder->private_->file == stdin)
3336 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3337 else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
3338 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3340 *stream_length = (FLAC__uint64)filestats.st_size;
3341 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3345 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3349 return feof(decoder->private_->file)? true : false;