1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004 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.
33 #include <stdlib.h> /* for malloc() */
34 #include <string.h> /* for memset/memcpy() */
35 #include "FLAC/assert.h"
36 #include "protected/stream_decoder.h"
37 #include "private/bitbuffer.h"
38 #include "private/bitmath.h"
39 #include "private/cpu.h"
40 #include "private/crc.h"
41 #include "private/fixed.h"
42 #include "private/format.h"
43 #include "private/lpc.h"
52 #define max(a,b) ((a)>(b)?(a):(b))
54 /***********************************************************************
58 ***********************************************************************/
60 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
62 /***********************************************************************
64 * Private class method prototypes
66 ***********************************************************************/
68 static void set_defaults_(FLAC__StreamDecoder *decoder);
69 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
70 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
71 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
72 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
73 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
74 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
75 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
76 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
77 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
78 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
79 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
80 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
81 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
82 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
83 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
84 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
85 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
86 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);
87 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
88 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
90 /***********************************************************************
94 ***********************************************************************/
96 typedef struct FLAC__StreamDecoderPrivate {
97 FLAC__StreamDecoderReadCallback read_callback;
98 FLAC__StreamDecoderWriteCallback write_callback;
99 FLAC__StreamDecoderMetadataCallback metadata_callback;
100 FLAC__StreamDecoderErrorCallback error_callback;
101 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[]);
102 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[]);
103 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[]);
105 FLAC__BitBuffer *input;
106 FLAC__int32 *output[FLAC__MAX_CHANNELS];
107 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
108 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
109 unsigned output_capacity, output_channels;
110 FLAC__uint32 last_frame_number;
111 FLAC__uint64 samples_decoded;
112 FLAC__bool has_stream_info, has_seek_table;
113 FLAC__StreamMetadata stream_info;
114 FLAC__StreamMetadata seek_table;
115 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
116 FLAC__byte *metadata_filter_ids;
117 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
119 FLAC__bool cached; /* true if there is a byte in lookahead */
120 FLAC__CPUInfo cpuinfo;
121 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
122 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
123 } FLAC__StreamDecoderPrivate;
125 /***********************************************************************
127 * Public static class data
129 ***********************************************************************/
131 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
132 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
133 "FLAC__STREAM_DECODER_READ_METADATA",
134 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
135 "FLAC__STREAM_DECODER_READ_FRAME",
136 "FLAC__STREAM_DECODER_END_OF_STREAM",
137 "FLAC__STREAM_DECODER_ABORTED",
138 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
139 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
140 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
141 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
142 "FLAC__STREAM_DECODER_UNINITIALIZED"
145 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
146 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
147 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
148 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
151 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
152 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
153 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
156 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
157 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
158 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
159 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
162 /***********************************************************************
164 * Class constructor/destructor
166 ***********************************************************************/
167 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
169 FLAC__StreamDecoder *decoder;
172 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
174 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
179 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
180 if(decoder->protected_ == 0) {
185 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
186 if(decoder->private_ == 0) {
187 free(decoder->protected_);
192 decoder->private_->input = FLAC__bitbuffer_new();
193 if(decoder->private_->input == 0) {
194 free(decoder->private_);
195 free(decoder->protected_);
200 decoder->private_->metadata_filter_ids_capacity = 16;
201 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
202 FLAC__bitbuffer_delete(decoder->private_->input);
203 free(decoder->private_);
204 free(decoder->protected_);
209 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
210 decoder->private_->output[i] = 0;
211 decoder->private_->residual[i] = 0;
214 decoder->private_->output_capacity = 0;
215 decoder->private_->output_channels = 0;
216 decoder->private_->has_seek_table = false;
218 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
219 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
221 set_defaults_(decoder);
223 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
228 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
232 FLAC__ASSERT(0 != decoder);
233 FLAC__ASSERT(0 != decoder->protected_);
234 FLAC__ASSERT(0 != decoder->private_);
235 FLAC__ASSERT(0 != decoder->private_->input);
237 FLAC__stream_decoder_finish(decoder);
239 if(0 != decoder->private_->metadata_filter_ids)
240 free(decoder->private_->metadata_filter_ids);
242 FLAC__bitbuffer_delete(decoder->private_->input);
244 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
245 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
247 free(decoder->private_);
248 free(decoder->protected_);
252 /***********************************************************************
254 * Public class methods
256 ***********************************************************************/
258 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
260 FLAC__ASSERT(0 != decoder);
262 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
263 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
265 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
266 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
268 if(!FLAC__bitbuffer_init(decoder->private_->input))
269 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
271 decoder->private_->last_frame_number = 0;
272 decoder->private_->samples_decoded = 0;
273 decoder->private_->has_stream_info = false;
274 decoder->private_->cached = false;
277 * get the CPU info and set the function pointers
279 FLAC__cpu_info(&decoder->private_->cpuinfo);
280 /* first default to the non-asm routines */
281 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
282 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
283 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
284 /* now override with asm where appropriate */
286 if(decoder->private_->cpuinfo.use_asm) {
287 #ifdef FLAC__CPU_IA32
288 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
289 #ifdef FLAC__HAS_NASM
290 if(decoder->private_->cpuinfo.data.ia32.mmx) {
291 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
292 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
295 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
296 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
303 if(!FLAC__stream_decoder_reset(decoder))
304 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
306 return decoder->protected_->state;
309 FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
312 FLAC__ASSERT(0 != decoder);
313 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
315 if(0 != decoder->private_->seek_table.data.seek_table.points) {
316 free(decoder->private_->seek_table.data.seek_table.points);
317 decoder->private_->seek_table.data.seek_table.points = 0;
318 decoder->private_->has_seek_table = false;
320 FLAC__bitbuffer_free(decoder->private_->input);
321 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
323 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
324 * output arrays have a buffer of up to 3 zeroes in front
325 * (at negative indices) for alignment purposes; we use 4
326 * to keep the data well-aligned.
328 if(0 != decoder->private_->output[i]) {
329 free(decoder->private_->output[i]-4);
330 decoder->private_->output[i] = 0;
332 if(0 != decoder->private_->residual[i]) {
333 free(decoder->private_->residual[i]);
334 decoder->private_->residual[i] = 0;
337 decoder->private_->output_capacity = 0;
338 decoder->private_->output_channels = 0;
340 set_defaults_(decoder);
342 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
345 FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
347 FLAC__ASSERT(0 != decoder);
348 FLAC__ASSERT(0 != decoder->private_);
349 FLAC__ASSERT(0 != decoder->protected_);
350 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
352 decoder->private_->read_callback = value;
356 FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
358 FLAC__ASSERT(0 != decoder);
359 FLAC__ASSERT(0 != decoder->private_);
360 FLAC__ASSERT(0 != decoder->protected_);
361 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
363 decoder->private_->write_callback = value;
367 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
369 FLAC__ASSERT(0 != decoder);
370 FLAC__ASSERT(0 != decoder->private_);
371 FLAC__ASSERT(0 != decoder->protected_);
372 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
374 decoder->private_->metadata_callback = value;
378 FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
380 FLAC__ASSERT(0 != decoder);
381 FLAC__ASSERT(0 != decoder->private_);
382 FLAC__ASSERT(0 != decoder->protected_);
383 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
385 decoder->private_->error_callback = value;
389 FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
391 FLAC__ASSERT(0 != decoder);
392 FLAC__ASSERT(0 != decoder->private_);
393 FLAC__ASSERT(0 != decoder->protected_);
394 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
396 decoder->private_->client_data = value;
400 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
402 FLAC__ASSERT(0 != decoder);
403 FLAC__ASSERT(0 != decoder->private_);
404 FLAC__ASSERT(0 != decoder->protected_);
405 FLAC__ASSERT((unsigned)type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
406 /* double protection */
407 if((unsigned)type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
409 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
411 decoder->private_->metadata_filter[type] = true;
412 if(type == FLAC__METADATA_TYPE_APPLICATION)
413 decoder->private_->metadata_filter_ids_count = 0;
417 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
419 FLAC__ASSERT(0 != decoder);
420 FLAC__ASSERT(0 != decoder->private_);
421 FLAC__ASSERT(0 != decoder->protected_);
422 FLAC__ASSERT(0 != id);
423 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
426 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
429 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
431 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
432 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
433 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
434 decoder->private_->metadata_filter_ids_capacity *= 2;
437 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));
438 decoder->private_->metadata_filter_ids_count++;
443 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
446 FLAC__ASSERT(0 != decoder);
447 FLAC__ASSERT(0 != decoder->private_);
448 FLAC__ASSERT(0 != decoder->protected_);
449 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
451 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
452 decoder->private_->metadata_filter[i] = true;
453 decoder->private_->metadata_filter_ids_count = 0;
457 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
459 FLAC__ASSERT(0 != decoder);
460 FLAC__ASSERT(0 != decoder->private_);
461 FLAC__ASSERT(0 != decoder->protected_);
462 FLAC__ASSERT((unsigned)type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
463 /* double protection */
464 if((unsigned)type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
466 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
468 decoder->private_->metadata_filter[type] = false;
469 if(type == FLAC__METADATA_TYPE_APPLICATION)
470 decoder->private_->metadata_filter_ids_count = 0;
474 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
476 FLAC__ASSERT(0 != decoder);
477 FLAC__ASSERT(0 != decoder->private_);
478 FLAC__ASSERT(0 != decoder->protected_);
479 FLAC__ASSERT(0 != id);
480 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
483 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
486 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
488 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
489 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
490 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
491 decoder->private_->metadata_filter_ids_capacity *= 2;
494 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));
495 decoder->private_->metadata_filter_ids_count++;
500 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
502 FLAC__ASSERT(0 != decoder);
503 FLAC__ASSERT(0 != decoder->private_);
504 FLAC__ASSERT(0 != decoder->protected_);
505 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
507 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
508 decoder->private_->metadata_filter_ids_count = 0;
512 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
514 FLAC__ASSERT(0 != decoder);
515 FLAC__ASSERT(0 != decoder->protected_);
516 return decoder->protected_->state;
519 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
521 return FLAC__StreamDecoderStateString[decoder->protected_->state];
524 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
526 FLAC__ASSERT(0 != decoder);
527 FLAC__ASSERT(0 != decoder->protected_);
528 return decoder->protected_->channels;
531 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
533 FLAC__ASSERT(0 != decoder);
534 FLAC__ASSERT(0 != decoder->protected_);
535 return decoder->protected_->channel_assignment;
538 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
540 FLAC__ASSERT(0 != decoder);
541 FLAC__ASSERT(0 != decoder->protected_);
542 return decoder->protected_->bits_per_sample;
545 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
547 FLAC__ASSERT(0 != decoder);
548 FLAC__ASSERT(0 != decoder->protected_);
549 return decoder->protected_->sample_rate;
552 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
554 FLAC__ASSERT(0 != decoder);
555 FLAC__ASSERT(0 != decoder->protected_);
556 return decoder->protected_->blocksize;
559 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
561 FLAC__ASSERT(0 != decoder);
562 FLAC__ASSERT(0 != decoder->private_);
563 FLAC__ASSERT(0 != decoder->protected_);
565 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
566 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
569 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
574 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
576 FLAC__ASSERT(0 != decoder);
577 FLAC__ASSERT(0 != decoder->private_);
578 FLAC__ASSERT(0 != decoder->protected_);
580 if(!FLAC__stream_decoder_flush(decoder)) {
581 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
584 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
586 decoder->private_->samples_decoded = 0;
591 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
593 FLAC__bool got_a_frame;
594 FLAC__ASSERT(0 != decoder);
595 FLAC__ASSERT(0 != decoder->protected_);
598 switch(decoder->protected_->state) {
599 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
600 if(!find_metadata_(decoder))
601 return false; /* above function sets the status for us */
603 case FLAC__STREAM_DECODER_READ_METADATA:
604 if(!read_metadata_(decoder))
605 return false; /* above function sets the status for us */
608 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
609 if(!frame_sync_(decoder))
610 return true; /* above function sets the status for us */
612 case FLAC__STREAM_DECODER_READ_FRAME:
613 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
614 return false; /* above function sets the status for us */
616 return true; /* above function sets the status for us */
618 case FLAC__STREAM_DECODER_END_OF_STREAM:
619 case FLAC__STREAM_DECODER_ABORTED:
628 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
630 FLAC__ASSERT(0 != decoder);
631 FLAC__ASSERT(0 != decoder->protected_);
634 switch(decoder->protected_->state) {
635 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
636 if(!find_metadata_(decoder))
637 return false; /* above function sets the status for us */
639 case FLAC__STREAM_DECODER_READ_METADATA:
640 if(!read_metadata_(decoder))
641 return false; /* above function sets the status for us */
643 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
644 case FLAC__STREAM_DECODER_READ_FRAME:
645 case FLAC__STREAM_DECODER_END_OF_STREAM:
646 case FLAC__STREAM_DECODER_ABORTED:
655 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
658 FLAC__ASSERT(0 != decoder);
659 FLAC__ASSERT(0 != decoder->protected_);
662 switch(decoder->protected_->state) {
663 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
664 if(!find_metadata_(decoder))
665 return false; /* above function sets the status for us */
667 case FLAC__STREAM_DECODER_READ_METADATA:
668 if(!read_metadata_(decoder))
669 return false; /* above function sets the status for us */
671 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
672 if(!frame_sync_(decoder))
673 return true; /* above function sets the status for us */
675 case FLAC__STREAM_DECODER_READ_FRAME:
676 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
677 return false; /* above function sets the status for us */
679 case FLAC__STREAM_DECODER_END_OF_STREAM:
680 case FLAC__STREAM_DECODER_ABORTED:
689 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
691 FLAC__bool got_a_frame;
692 FLAC__ASSERT(0 != decoder);
693 FLAC__ASSERT(0 != decoder->protected_);
696 switch(decoder->protected_->state) {
697 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
698 case FLAC__STREAM_DECODER_READ_METADATA:
699 return false; /* above function sets the status for us */
700 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
701 if(!frame_sync_(decoder))
702 return true; /* above function sets the status for us */
704 case FLAC__STREAM_DECODER_READ_FRAME:
705 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
706 return false; /* above function sets the status for us */
708 return true; /* above function sets the status for us */
710 case FLAC__STREAM_DECODER_END_OF_STREAM:
711 case FLAC__STREAM_DECODER_ABORTED:
720 /***********************************************************************
722 * Protected class methods
724 ***********************************************************************/
726 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
728 FLAC__ASSERT(0 != decoder);
729 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
732 /***********************************************************************
734 * Private class methods
736 ***********************************************************************/
738 void set_defaults_(FLAC__StreamDecoder *decoder)
740 decoder->private_->read_callback = 0;
741 decoder->private_->write_callback = 0;
742 decoder->private_->metadata_callback = 0;
743 decoder->private_->error_callback = 0;
744 decoder->private_->client_data = 0;
746 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
747 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
748 decoder->private_->metadata_filter_ids_count = 0;
751 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
756 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
759 /* simply using realloc() is not practical because the number of channels may change mid-stream */
761 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
762 if(0 != decoder->private_->output[i]) {
763 free(decoder->private_->output[i]-4);
764 decoder->private_->output[i] = 0;
766 if(0 != decoder->private_->residual[i]) {
767 free(decoder->private_->residual[i]);
768 decoder->private_->residual[i] = 0;
772 for(i = 0; i < channels; i++) {
774 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
775 * output arrays have a buffer of up to 3 zeroes in front
776 * (at negative indices) for alignment purposes; we use 4
777 * to keep the data well-aligned.
779 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
781 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
784 memset(tmp, 0, sizeof(FLAC__int32)*4);
785 decoder->private_->output[i] = tmp + 4;
787 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
789 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
792 decoder->private_->residual[i] = tmp;
795 decoder->private_->output_capacity = size;
796 decoder->private_->output_channels = channels;
801 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
805 FLAC__ASSERT(0 != decoder);
806 FLAC__ASSERT(0 != decoder->private_);
808 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
809 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
815 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
819 FLAC__bool first = true;
821 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
823 for(i = id = 0; i < 4; ) {
824 if(decoder->private_->cached) {
825 x = (FLAC__uint32)decoder->private_->lookahead;
826 decoder->private_->cached = false;
829 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
830 return false; /* the read_callback_ sets the state for us */
832 if(x == FLAC__STREAM_SYNC_STRING[i]) {
838 if(x == ID3V2_TAG_[id]) {
842 if(!skip_id3v2_tag_(decoder))
843 return false; /* the read_callback_ sets the state for us */
847 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
848 decoder->private_->header_warmup[0] = (FLAC__byte)x;
849 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
850 return false; /* the read_callback_ sets the state for us */
852 /* 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 */
853 /* else we have to check if the second byte is the end of a sync code */
854 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
855 decoder->private_->lookahead = (FLAC__byte)x;
856 decoder->private_->cached = true;
858 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
859 decoder->private_->header_warmup[1] = (FLAC__byte)x;
860 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
866 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
871 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
875 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
878 FLAC__uint32 i, x, type, length;
880 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
882 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
883 return false; /* the read_callback_ sets the state for us */
884 is_last = x? true : false;
886 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
887 return false; /* the read_callback_ sets the state for us */
889 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
890 return false; /* the read_callback_ sets the state for us */
892 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
893 if(!read_metadata_streaminfo_(decoder, is_last, length))
896 decoder->private_->has_stream_info = true;
897 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
898 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
900 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
901 if(!read_metadata_seektable_(decoder, is_last, length))
904 decoder->private_->has_seek_table = true;
905 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
906 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
909 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
910 unsigned real_length = length;
911 FLAC__StreamMetadata block;
913 block.is_last = is_last;
914 block.type = (FLAC__MetadataType)type;
915 block.length = length;
917 if(type == FLAC__METADATA_TYPE_APPLICATION) {
918 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))
919 return false; /* the read_callback_ sets the state for us */
921 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
923 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
928 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
929 return false; /* the read_callback_ sets the state for us */
933 case FLAC__METADATA_TYPE_PADDING:
934 /* skip the padding bytes */
935 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
936 return false; /* the read_callback_ sets the state for us */
938 case FLAC__METADATA_TYPE_APPLICATION:
939 /* remember, we read the ID already */
940 if(real_length > 0) {
941 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
942 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
945 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
946 return false; /* the read_callback_ sets the state for us */
949 block.data.application.data = 0;
951 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
952 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
955 case FLAC__METADATA_TYPE_CUESHEET:
956 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
959 case FLAC__METADATA_TYPE_STREAMINFO:
960 case FLAC__METADATA_TYPE_SEEKTABLE:
964 if(real_length > 0) {
965 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
966 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
969 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
970 return false; /* the read_callback_ sets the state for us */
973 block.data.unknown.data = 0;
976 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
978 /* now we have to free any malloc'ed data in the block */
980 case FLAC__METADATA_TYPE_PADDING:
982 case FLAC__METADATA_TYPE_APPLICATION:
983 if(0 != block.data.application.data)
984 free(block.data.application.data);
986 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
987 if(0 != block.data.vorbis_comment.vendor_string.entry)
988 free(block.data.vorbis_comment.vendor_string.entry);
989 if(block.data.vorbis_comment.num_comments > 0)
990 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
991 if(0 != block.data.vorbis_comment.comments[i].entry)
992 free(block.data.vorbis_comment.comments[i].entry);
993 if(0 != block.data.vorbis_comment.comments)
994 free(block.data.vorbis_comment.comments);
996 case FLAC__METADATA_TYPE_CUESHEET:
997 if(block.data.cue_sheet.num_tracks > 0)
998 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
999 if(0 != block.data.cue_sheet.tracks[i].indices)
1000 free(block.data.cue_sheet.tracks[i].indices);
1001 if(0 != block.data.cue_sheet.tracks)
1002 free(block.data.cue_sheet.tracks);
1004 case FLAC__METADATA_TYPE_STREAMINFO:
1005 case FLAC__METADATA_TYPE_SEEKTABLE:
1008 if(0 != block.data.unknown.data)
1009 free(block.data.unknown.data);
1016 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1021 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1024 unsigned bits, used_bits = 0;
1026 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1028 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1029 decoder->private_->stream_info.is_last = is_last;
1030 decoder->private_->stream_info.length = length;
1032 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1033 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
1034 return false; /* the read_callback_ sets the state for us */
1035 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1038 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1039 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
1040 return false; /* the read_callback_ sets the state for us */
1041 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1044 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1045 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
1046 return false; /* the read_callback_ sets the state for us */
1047 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1050 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1051 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1052 return false; /* the read_callback_ sets the state for us */
1053 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1056 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1057 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1058 return false; /* the read_callback_ sets the state for us */
1059 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1062 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1063 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1064 return false; /* the read_callback_ sets the state for us */
1065 decoder->private_->stream_info.data.stream_info.channels = x+1;
1068 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1069 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1070 return false; /* the read_callback_ sets the state for us */
1071 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1074 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1075 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))
1076 return false; /* the read_callback_ sets the state for us */
1079 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1080 return false; /* the read_callback_ sets the state for us */
1083 /* skip the rest of the block */
1084 FLAC__ASSERT(used_bits % 8 == 0);
1085 length -= (used_bits / 8);
1086 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1087 return false; /* the read_callback_ sets the state for us */
1092 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1097 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1099 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1100 decoder->private_->seek_table.is_last = is_last;
1101 decoder->private_->seek_table.length = length;
1103 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1105 /* use realloc since we may pass through here several times (e.g. after seeking) */
1106 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)))) {
1107 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1110 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1111 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1112 return false; /* the read_callback_ sets the state for us */
1113 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1115 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1116 return false; /* the read_callback_ sets the state for us */
1117 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1119 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1120 return false; /* the read_callback_ sets the state for us */
1121 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1123 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1124 /* if there is a partial point left, skip over it */
1126 /*@@@ do an error_callback() here? there's an argument for either way */
1127 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1128 return false; /* the read_callback_ sets the state for us */
1134 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1138 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1140 /* read vendor string */
1141 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1142 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1143 return false; /* the read_callback_ sets the state for us */
1144 if(obj->vendor_string.length > 0) {
1145 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length))) {
1146 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1149 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1150 return false; /* the read_callback_ sets the state for us */
1153 obj->vendor_string.entry = 0;
1155 /* read num comments */
1156 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1157 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1158 return false; /* the read_callback_ sets the state for us */
1161 if(obj->num_comments > 0) {
1162 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1163 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1166 for(i = 0; i < obj->num_comments; i++) {
1167 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1168 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1169 return false; /* the read_callback_ sets the state for us */
1170 if(obj->comments[i].length > 0) {
1171 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length))) {
1172 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1175 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1176 return false; /* the read_callback_ sets the state for us */
1179 obj->comments[i].entry = 0;
1189 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1191 FLAC__uint32 i, j, x;
1193 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1195 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1197 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1198 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))
1199 return false; /* the read_callback_ sets the state for us */
1201 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1202 return false; /* the read_callback_ sets the state for us */
1204 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1205 return false; /* the read_callback_ sets the state for us */
1206 obj->is_cd = x? true : false;
1208 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1209 return false; /* the read_callback_ sets the state for us */
1211 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1212 return false; /* the read_callback_ sets the state for us */
1213 obj->num_tracks = x;
1215 if(obj->num_tracks > 0) {
1216 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1217 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1220 for(i = 0; i < obj->num_tracks; i++) {
1221 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1222 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1223 return false; /* the read_callback_ sets the state for us */
1225 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1226 return false; /* the read_callback_ sets the state for us */
1227 track->number = (FLAC__byte)x;
1229 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1230 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))
1231 return false; /* the read_callback_ sets the state for us */
1233 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1234 return false; /* the read_callback_ sets the state for us */
1237 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1238 return false; /* the read_callback_ sets the state for us */
1239 track->pre_emphasis = x;
1241 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1242 return false; /* the read_callback_ sets the state for us */
1244 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1245 return false; /* the read_callback_ sets the state for us */
1246 track->num_indices = (FLAC__byte)x;
1248 if(track->num_indices > 0) {
1249 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1250 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1253 for(j = 0; j < track->num_indices; j++) {
1254 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1255 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1256 return false; /* the read_callback_ sets the state for us */
1258 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1259 return false; /* the read_callback_ sets the state for us */
1260 index->number = (FLAC__byte)x;
1262 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1263 return false; /* the read_callback_ sets the state for us */
1272 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1277 /* skip the version and flags bytes */
1278 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1279 return false; /* the read_callback_ sets the state for us */
1280 /* get the size (in bytes) to skip */
1282 for(i = 0; i < 4; i++) {
1283 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1284 return false; /* the read_callback_ sets the state for us */
1288 /* skip the rest of the tag */
1289 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1290 return false; /* the read_callback_ sets the state for us */
1294 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1297 FLAC__bool first = true;
1299 /* If we know the total number of samples in the stream, stop if we've read that many. */
1300 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1301 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1302 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1303 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1308 /* make sure we're byte aligned */
1309 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1310 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1311 return false; /* the read_callback_ sets the state for us */
1315 if(decoder->private_->cached) {
1316 x = (FLAC__uint32)decoder->private_->lookahead;
1317 decoder->private_->cached = false;
1320 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1321 return false; /* the read_callback_ sets the state for us */
1323 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1324 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1325 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1326 return false; /* the read_callback_ sets the state for us */
1328 /* 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 */
1329 /* else we have to check if the second byte is the end of a sync code */
1330 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1331 decoder->private_->lookahead = (FLAC__byte)x;
1332 decoder->private_->cached = true;
1334 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1335 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1336 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1341 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1349 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
1353 FLAC__int32 mid, side, left, right;
1354 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1357 *got_a_frame = false;
1361 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1362 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1363 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1365 if(!read_frame_header_(decoder))
1367 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1369 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1371 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1373 * first figure the correct bits-per-sample of the subframe
1375 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1376 switch(decoder->private_->frame.header.channel_assignment) {
1377 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1378 /* no adjustment needed */
1380 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1381 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1385 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1386 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1390 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1391 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1401 if(!read_subframe_(decoder, channel, bps, do_full_decode))
1403 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1404 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1408 if(!read_zero_padding_(decoder))
1412 * Read the frame CRC-16 from the footer and check
1414 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1415 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1416 return false; /* the read_callback_ sets the state for us */
1417 if(frame_crc == (FLAC__uint16)x) {
1418 if(do_full_decode) {
1419 /* Undo any special channel coding */
1420 switch(decoder->private_->frame.header.channel_assignment) {
1421 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1424 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1425 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1426 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1427 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1429 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1430 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1431 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1432 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1434 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1435 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1436 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1437 mid = decoder->private_->output[0][i];
1438 side = decoder->private_->output[1][i];
1440 if(side & 1) /* i.e. if 'side' is odd... */
1444 decoder->private_->output[0][i] = left >> 1;
1445 decoder->private_->output[1][i] = right >> 1;
1455 /* Bad frame, emit error and zero the output signal */
1456 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1457 if(do_full_decode) {
1458 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1459 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1464 *got_a_frame = true;
1466 /* put the latest values into the public section of the decoder instance */
1467 decoder->protected_->channels = decoder->private_->frame.header.channels;
1468 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1469 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1470 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1471 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1473 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1474 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1477 if(do_full_decode) {
1478 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
1482 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1486 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1490 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1491 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1492 unsigned raw_header_len;
1493 FLAC__bool is_unparseable = false;
1494 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);
1495 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);
1497 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1499 /* init the raw header with the saved bits from synchronization */
1500 raw_header[0] = decoder->private_->header_warmup[0];
1501 raw_header[1] = decoder->private_->header_warmup[1];
1505 * check to make sure that the reserved bits are 0
1507 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1508 is_unparseable = true;
1512 * Note that along the way as we read the header, we look for a sync
1513 * code inside. If we find one it would indicate that our original
1514 * sync was bad since there cannot be a sync code in a valid header.
1518 * read in the raw header as bytes so we can CRC it, and parse it on the way
1520 for(i = 0; i < 2; i++) {
1521 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1522 return false; /* the read_callback_ sets the state for us */
1523 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1524 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1525 decoder->private_->lookahead = (FLAC__byte)x;
1526 decoder->private_->cached = true;
1527 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1528 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1531 raw_header[raw_header_len++] = (FLAC__byte)x;
1534 switch(x = raw_header[2] >> 4) {
1536 if(is_known_fixed_blocksize_stream)
1537 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1539 is_unparseable = true;
1542 decoder->private_->frame.header.blocksize = 192;
1548 decoder->private_->frame.header.blocksize = 576 << (x-2);
1562 decoder->private_->frame.header.blocksize = 256 << (x-8);
1569 switch(x = raw_header[2] & 0x0f) {
1571 if(decoder->private_->has_stream_info)
1572 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1574 is_unparseable = true;
1579 is_unparseable = true;
1582 decoder->private_->frame.header.sample_rate = 8000;
1585 decoder->private_->frame.header.sample_rate = 16000;
1588 decoder->private_->frame.header.sample_rate = 22050;
1591 decoder->private_->frame.header.sample_rate = 24000;
1594 decoder->private_->frame.header.sample_rate = 32000;
1597 decoder->private_->frame.header.sample_rate = 44100;
1600 decoder->private_->frame.header.sample_rate = 48000;
1603 decoder->private_->frame.header.sample_rate = 96000;
1608 sample_rate_hint = x;
1611 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1612 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1618 x = (unsigned)(raw_header[3] >> 4);
1620 decoder->private_->frame.header.channels = 2;
1623 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1626 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1629 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1632 is_unparseable = true;
1637 decoder->private_->frame.header.channels = (unsigned)x + 1;
1638 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1641 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1643 if(decoder->private_->has_stream_info)
1644 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1646 is_unparseable = true;
1649 decoder->private_->frame.header.bits_per_sample = 8;
1652 decoder->private_->frame.header.bits_per_sample = 12;
1655 decoder->private_->frame.header.bits_per_sample = 16;
1658 decoder->private_->frame.header.bits_per_sample = 20;
1661 decoder->private_->frame.header.bits_per_sample = 24;
1665 is_unparseable = true;
1672 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1673 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1674 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1678 if(blocksize_hint && is_known_variable_blocksize_stream) {
1679 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1680 return false; /* the read_callback_ sets the state for us */
1681 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1682 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1683 decoder->private_->cached = true;
1684 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1685 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1688 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1689 decoder->private_->frame.header.number.sample_number = xx;
1692 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1693 return false; /* the read_callback_ sets the state for us */
1694 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1695 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1696 decoder->private_->cached = true;
1697 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1698 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1701 decoder->private_->last_frame_number = x;
1702 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1703 if(blocksize_hint) {
1704 if(decoder->private_->has_stream_info)
1705 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1707 is_unparseable = true;
1710 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->frame.header.blocksize * (FLAC__int64)x;
1713 if(blocksize_hint) {
1714 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1715 return false; /* the read_callback_ sets the state for us */
1716 raw_header[raw_header_len++] = (FLAC__byte)x;
1717 if(blocksize_hint == 7) {
1719 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1720 return false; /* the read_callback_ sets the state for us */
1721 raw_header[raw_header_len++] = (FLAC__byte)_x;
1724 decoder->private_->frame.header.blocksize = x+1;
1727 if(sample_rate_hint) {
1728 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1729 return false; /* the read_callback_ sets the state for us */
1730 raw_header[raw_header_len++] = (FLAC__byte)x;
1731 if(sample_rate_hint != 12) {
1733 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1734 return false; /* the read_callback_ sets the state for us */
1735 raw_header[raw_header_len++] = (FLAC__byte)_x;
1738 if(sample_rate_hint == 12)
1739 decoder->private_->frame.header.sample_rate = x*1000;
1740 else if(sample_rate_hint == 13)
1741 decoder->private_->frame.header.sample_rate = x;
1743 decoder->private_->frame.header.sample_rate = x*10;
1746 /* read the CRC-8 byte */
1747 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1748 return false; /* the read_callback_ sets the state for us */
1749 crc8 = (FLAC__byte)x;
1751 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1752 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1753 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1757 if(is_unparseable) {
1758 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1765 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
1768 FLAC__bool wasted_bits;
1770 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1771 return false; /* the read_callback_ sets the state for us */
1773 wasted_bits = (x & 1);
1778 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1779 return false; /* the read_callback_ sets the state for us */
1780 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1781 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1784 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1787 * Lots of magic numbers here
1790 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1791 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1795 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
1799 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
1803 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1807 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
1811 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1815 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
1819 if(wasted_bits && do_full_decode) {
1821 x = decoder->private_->frame.subframes[channel].wasted_bits;
1822 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1823 decoder->private_->output[channel][i] <<= x;
1829 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
1831 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1834 FLAC__int32 *output = decoder->private_->output[channel];
1836 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1838 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1839 return false; /* the read_callback_ sets the state for us */
1841 subframe->value = x;
1843 /* decode the subframe */
1844 if(do_full_decode) {
1845 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1852 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
1854 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1859 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1861 subframe->residual = decoder->private_->residual[channel];
1862 subframe->order = order;
1864 /* read warm-up samples */
1865 for(u = 0; u < order; u++) {
1866 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1867 return false; /* the read_callback_ sets the state for us */
1868 subframe->warmup[u] = i32;
1871 /* read entropy coding method info */
1872 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1873 return false; /* the read_callback_ sets the state for us */
1874 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1875 switch(subframe->entropy_coding_method.type) {
1876 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1877 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1878 return false; /* the read_callback_ sets the state for us */
1879 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1880 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1883 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1888 switch(subframe->entropy_coding_method.type) {
1889 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1890 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]))
1897 /* decode the subframe */
1898 if(do_full_decode) {
1899 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1900 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1906 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
1908 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1913 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1915 subframe->residual = decoder->private_->residual[channel];
1916 subframe->order = order;
1918 /* read warm-up samples */
1919 for(u = 0; u < order; u++) {
1920 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1921 return false; /* the read_callback_ sets the state for us */
1922 subframe->warmup[u] = i32;
1925 /* read qlp coeff precision */
1926 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1927 return false; /* the read_callback_ sets the state for us */
1928 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1929 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1930 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1933 subframe->qlp_coeff_precision = u32+1;
1935 /* read qlp shift */
1936 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1937 return false; /* the read_callback_ sets the state for us */
1938 subframe->quantization_level = i32;
1940 /* read quantized lp coefficiencts */
1941 for(u = 0; u < order; u++) {
1942 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1943 return false; /* the read_callback_ sets the state for us */
1944 subframe->qlp_coeff[u] = i32;
1947 /* read entropy coding method info */
1948 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1949 return false; /* the read_callback_ sets the state for us */
1950 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1951 switch(subframe->entropy_coding_method.type) {
1952 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1953 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1954 return false; /* the read_callback_ sets the state for us */
1955 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1956 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1959 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1964 switch(subframe->entropy_coding_method.type) {
1965 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1966 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]))
1973 /* decode the subframe */
1974 if(do_full_decode) {
1975 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1976 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1977 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1978 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);
1980 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);
1982 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);
1988 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
1990 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1991 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1994 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1996 subframe->data = residual;
1998 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1999 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
2000 return false; /* the read_callback_ sets the state for us */
2004 /* decode the subframe */
2006 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2011 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
2013 FLAC__uint32 rice_parameter;
2015 unsigned partition, sample, u;
2016 const unsigned partitions = 1u << partition_order;
2017 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2019 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
2020 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2025 for(partition = 0; partition < partitions; partition++) {
2026 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
2027 return false; /* the read_callback_ sets the state for us */
2028 partitioned_rice_contents->parameters[partition] = rice_parameter;
2029 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2030 #ifdef FLAC__SYMMETRIC_RICE
2031 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2032 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
2033 return false; /* the read_callback_ sets the state for us */
2034 residual[sample] = i;
2037 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2038 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
2039 return false; /* the read_callback_ sets the state for us */
2044 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
2045 return false; /* the read_callback_ sets the state for us */
2046 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2047 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2048 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
2049 return false; /* the read_callback_ sets the state for us */
2050 residual[sample] = i;
2058 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2060 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
2061 FLAC__uint32 zero = 0;
2062 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
2063 return false; /* the read_callback_ sets the state for us */
2065 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
2066 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2072 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
2074 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2075 FLAC__StreamDecoderReadStatus status;
2077 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
2078 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
2079 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2080 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
2081 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2082 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;