1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memset/memcpy() */
23 #include "FLAC/assert.h"
24 #include "protected/stream_decoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/bitmath.h"
27 #include "private/cpu.h"
28 #include "private/crc.h"
29 #include "private/fixed.h"
30 #include "private/format.h"
31 #include "private/lpc.h"
40 #define max(a,b) ((a)>(b)?(a):(b))
42 /***********************************************************************
46 ***********************************************************************/
48 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
50 /***********************************************************************
52 * Private class method prototypes
54 ***********************************************************************/
56 static void set_defaults_(FLAC__StreamDecoder *decoder);
57 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
58 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
59 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
60 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
61 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
62 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
63 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
64 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
65 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
66 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
67 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
68 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
69 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
70 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
71 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
72 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
73 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
74 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);
75 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
76 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
78 /***********************************************************************
82 ***********************************************************************/
84 typedef struct FLAC__StreamDecoderPrivate {
85 FLAC__StreamDecoderReadCallback read_callback;
86 FLAC__StreamDecoderWriteCallback write_callback;
87 FLAC__StreamDecoderMetadataCallback metadata_callback;
88 FLAC__StreamDecoderErrorCallback error_callback;
89 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[]);
90 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[]);
91 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[]);
93 FLAC__BitBuffer *input;
94 FLAC__int32 *output[FLAC__MAX_CHANNELS];
95 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
96 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
97 unsigned output_capacity, output_channels;
98 FLAC__uint32 last_frame_number;
99 FLAC__uint64 samples_decoded;
100 FLAC__bool has_stream_info, has_seek_table;
101 FLAC__StreamMetadata stream_info;
102 FLAC__StreamMetadata seek_table;
103 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
104 FLAC__byte *metadata_filter_ids;
105 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
107 FLAC__bool cached; /* true if there is a byte in lookahead */
108 FLAC__CPUInfo cpuinfo;
109 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
110 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
111 } FLAC__StreamDecoderPrivate;
113 /***********************************************************************
115 * Public static class data
117 ***********************************************************************/
119 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
120 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
121 "FLAC__STREAM_DECODER_READ_METADATA",
122 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
123 "FLAC__STREAM_DECODER_READ_FRAME",
124 "FLAC__STREAM_DECODER_END_OF_STREAM",
125 "FLAC__STREAM_DECODER_ABORTED",
126 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
127 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
128 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
129 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
130 "FLAC__STREAM_DECODER_UNINITIALIZED"
133 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
134 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
135 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
136 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
139 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
140 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
141 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
144 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
145 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
146 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
147 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
150 /***********************************************************************
152 * Class constructor/destructor
154 ***********************************************************************/
155 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
157 FLAC__StreamDecoder *decoder;
160 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
162 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
167 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
168 if(decoder->protected_ == 0) {
173 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
174 if(decoder->private_ == 0) {
175 free(decoder->protected_);
180 decoder->private_->input = FLAC__bitbuffer_new();
181 if(decoder->private_->input == 0) {
182 free(decoder->private_);
183 free(decoder->protected_);
188 decoder->private_->metadata_filter_ids_capacity = 16;
189 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
190 FLAC__bitbuffer_delete(decoder->private_->input);
191 free(decoder->private_);
192 free(decoder->protected_);
197 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
198 decoder->private_->output[i] = 0;
199 decoder->private_->residual[i] = 0;
202 decoder->private_->output_capacity = 0;
203 decoder->private_->output_channels = 0;
204 decoder->private_->has_seek_table = false;
206 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
207 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
209 set_defaults_(decoder);
211 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
216 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
220 FLAC__ASSERT(0 != decoder);
221 FLAC__ASSERT(0 != decoder->protected_);
222 FLAC__ASSERT(0 != decoder->private_);
223 FLAC__ASSERT(0 != decoder->private_->input);
225 FLAC__stream_decoder_finish(decoder);
227 if(0 != decoder->private_->metadata_filter_ids)
228 free(decoder->private_->metadata_filter_ids);
230 FLAC__bitbuffer_delete(decoder->private_->input);
232 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
233 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
235 free(decoder->private_);
236 free(decoder->protected_);
240 /***********************************************************************
242 * Public class methods
244 ***********************************************************************/
246 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
248 FLAC__ASSERT(0 != decoder);
250 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
251 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
253 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
254 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
256 if(!FLAC__bitbuffer_init(decoder->private_->input))
257 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
259 decoder->private_->last_frame_number = 0;
260 decoder->private_->samples_decoded = 0;
261 decoder->private_->has_stream_info = false;
262 decoder->private_->cached = false;
265 * get the CPU info and set the function pointers
267 FLAC__cpu_info(&decoder->private_->cpuinfo);
268 /* first default to the non-asm routines */
269 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
270 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
271 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
272 /* now override with asm where appropriate */
274 if(decoder->private_->cpuinfo.use_asm) {
275 #ifdef FLAC__CPU_IA32
276 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
277 #ifdef FLAC__HAS_NASM
278 if(decoder->private_->cpuinfo.data.ia32.mmx) {
279 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
280 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
283 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
284 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
291 if(!FLAC__stream_decoder_reset(decoder))
292 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
294 return decoder->protected_->state;
297 FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
300 FLAC__ASSERT(0 != decoder);
301 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
303 if(0 != decoder->private_->seek_table.data.seek_table.points) {
304 free(decoder->private_->seek_table.data.seek_table.points);
305 decoder->private_->seek_table.data.seek_table.points = 0;
306 decoder->private_->has_seek_table = false;
308 FLAC__bitbuffer_free(decoder->private_->input);
309 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
311 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
312 * output arrays have a buffer of up to 3 zeroes in front
313 * (at negative indices) for alignment purposes; we use 4
314 * to keep the data well-aligned.
316 if(0 != decoder->private_->output[i]) {
317 free(decoder->private_->output[i]-4);
318 decoder->private_->output[i] = 0;
320 if(0 != decoder->private_->residual[i]) {
321 free(decoder->private_->residual[i]);
322 decoder->private_->residual[i] = 0;
325 decoder->private_->output_capacity = 0;
326 decoder->private_->output_channels = 0;
328 set_defaults_(decoder);
330 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
333 FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
335 FLAC__ASSERT(0 != decoder);
336 FLAC__ASSERT(0 != decoder->private_);
337 FLAC__ASSERT(0 != decoder->protected_);
338 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
340 decoder->private_->read_callback = value;
344 FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
346 FLAC__ASSERT(0 != decoder);
347 FLAC__ASSERT(0 != decoder->private_);
348 FLAC__ASSERT(0 != decoder->protected_);
349 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
351 decoder->private_->write_callback = value;
355 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
357 FLAC__ASSERT(0 != decoder);
358 FLAC__ASSERT(0 != decoder->private_);
359 FLAC__ASSERT(0 != decoder->protected_);
360 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
362 decoder->private_->metadata_callback = value;
366 FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
368 FLAC__ASSERT(0 != decoder);
369 FLAC__ASSERT(0 != decoder->private_);
370 FLAC__ASSERT(0 != decoder->protected_);
371 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
373 decoder->private_->error_callback = value;
377 FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
379 FLAC__ASSERT(0 != decoder);
380 FLAC__ASSERT(0 != decoder->private_);
381 FLAC__ASSERT(0 != decoder->protected_);
382 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
384 decoder->private_->client_data = value;
388 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
390 FLAC__ASSERT(0 != decoder);
391 FLAC__ASSERT(0 != decoder->private_);
392 FLAC__ASSERT(0 != decoder->protected_);
393 FLAC__ASSERT(type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
394 /* double protection */
395 if(type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
397 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
399 decoder->private_->metadata_filter[type] = true;
400 if(type == FLAC__METADATA_TYPE_APPLICATION)
401 decoder->private_->metadata_filter_ids_count = 0;
405 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
407 FLAC__ASSERT(0 != decoder);
408 FLAC__ASSERT(0 != decoder->private_);
409 FLAC__ASSERT(0 != decoder->protected_);
410 FLAC__ASSERT(0 != id);
411 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
414 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
417 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
419 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
420 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
421 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
422 decoder->private_->metadata_filter_ids_capacity *= 2;
425 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));
426 decoder->private_->metadata_filter_ids_count++;
431 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
434 FLAC__ASSERT(0 != decoder);
435 FLAC__ASSERT(0 != decoder->private_);
436 FLAC__ASSERT(0 != decoder->protected_);
437 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
439 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
440 decoder->private_->metadata_filter[i] = true;
441 decoder->private_->metadata_filter_ids_count = 0;
445 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
447 FLAC__ASSERT(0 != decoder);
448 FLAC__ASSERT(0 != decoder->private_);
449 FLAC__ASSERT(0 != decoder->protected_);
450 FLAC__ASSERT(type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
451 /* double protection */
452 if(type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
454 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
456 decoder->private_->metadata_filter[type] = false;
457 if(type == FLAC__METADATA_TYPE_APPLICATION)
458 decoder->private_->metadata_filter_ids_count = 0;
462 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
464 FLAC__ASSERT(0 != decoder);
465 FLAC__ASSERT(0 != decoder->private_);
466 FLAC__ASSERT(0 != decoder->protected_);
467 FLAC__ASSERT(0 != id);
468 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
471 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
474 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
476 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
477 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
478 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
479 decoder->private_->metadata_filter_ids_capacity *= 2;
482 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));
483 decoder->private_->metadata_filter_ids_count++;
488 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
490 FLAC__ASSERT(0 != decoder);
491 FLAC__ASSERT(0 != decoder->private_);
492 FLAC__ASSERT(0 != decoder->protected_);
493 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
495 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
496 decoder->private_->metadata_filter_ids_count = 0;
500 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
502 FLAC__ASSERT(0 != decoder);
503 FLAC__ASSERT(0 != decoder->protected_);
504 return decoder->protected_->state;
507 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
509 FLAC__ASSERT(0 != decoder);
510 FLAC__ASSERT(0 != decoder->protected_);
511 return decoder->protected_->channels;
514 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
516 FLAC__ASSERT(0 != decoder);
517 FLAC__ASSERT(0 != decoder->protected_);
518 return decoder->protected_->channel_assignment;
521 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
523 FLAC__ASSERT(0 != decoder);
524 FLAC__ASSERT(0 != decoder->protected_);
525 return decoder->protected_->bits_per_sample;
528 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
530 FLAC__ASSERT(0 != decoder);
531 FLAC__ASSERT(0 != decoder->protected_);
532 return decoder->protected_->sample_rate;
535 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
537 FLAC__ASSERT(0 != decoder);
538 FLAC__ASSERT(0 != decoder->protected_);
539 return decoder->protected_->blocksize;
542 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
544 FLAC__ASSERT(0 != decoder);
545 FLAC__ASSERT(0 != decoder->private_);
546 FLAC__ASSERT(0 != decoder->protected_);
548 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
549 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
552 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
557 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
559 FLAC__ASSERT(0 != decoder);
560 FLAC__ASSERT(0 != decoder->private_);
561 FLAC__ASSERT(0 != decoder->protected_);
563 if(!FLAC__stream_decoder_flush(decoder)) {
564 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
567 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
569 decoder->private_->samples_decoded = 0;
574 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
576 FLAC__bool got_a_frame;
577 FLAC__ASSERT(0 != decoder);
578 FLAC__ASSERT(0 != decoder->protected_);
581 switch(decoder->protected_->state) {
582 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
583 if(!find_metadata_(decoder))
584 return false; /* above function sets the status for us */
586 case FLAC__STREAM_DECODER_READ_METADATA:
587 if(!read_metadata_(decoder))
588 return false; /* above function sets the status for us */
591 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
592 if(!frame_sync_(decoder))
593 return true; /* above function sets the status for us */
595 case FLAC__STREAM_DECODER_READ_FRAME:
596 if(!read_frame_(decoder, &got_a_frame))
597 return false; /* above function sets the status for us */
599 return true; /* above function sets the status for us */
601 case FLAC__STREAM_DECODER_END_OF_STREAM:
602 case FLAC__STREAM_DECODER_ABORTED:
611 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
613 FLAC__ASSERT(0 != decoder);
614 FLAC__ASSERT(0 != decoder->protected_);
617 switch(decoder->protected_->state) {
618 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
619 if(!find_metadata_(decoder))
620 return false; /* above function sets the status for us */
622 case FLAC__STREAM_DECODER_READ_METADATA:
623 if(!read_metadata_(decoder))
624 return false; /* above function sets the status for us */
626 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
627 case FLAC__STREAM_DECODER_READ_FRAME:
628 case FLAC__STREAM_DECODER_END_OF_STREAM:
629 case FLAC__STREAM_DECODER_ABORTED:
638 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
641 FLAC__ASSERT(0 != decoder);
642 FLAC__ASSERT(0 != decoder->protected_);
645 switch(decoder->protected_->state) {
646 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
647 if(!find_metadata_(decoder))
648 return false; /* above function sets the status for us */
650 case FLAC__STREAM_DECODER_READ_METADATA:
651 if(!read_metadata_(decoder))
652 return false; /* above function sets the status for us */
654 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
655 if(!frame_sync_(decoder))
656 return true; /* above function sets the status for us */
658 case FLAC__STREAM_DECODER_READ_FRAME:
659 if(!read_frame_(decoder, &dummy))
660 return false; /* above function sets the status for us */
662 case FLAC__STREAM_DECODER_END_OF_STREAM:
663 case FLAC__STREAM_DECODER_ABORTED:
672 /***********************************************************************
674 * Protected class methods
676 ***********************************************************************/
678 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
680 FLAC__ASSERT(0 != decoder);
681 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
684 /***********************************************************************
686 * Private class methods
688 ***********************************************************************/
690 void set_defaults_(FLAC__StreamDecoder *decoder)
692 decoder->private_->read_callback = 0;
693 decoder->private_->write_callback = 0;
694 decoder->private_->metadata_callback = 0;
695 decoder->private_->error_callback = 0;
696 decoder->private_->client_data = 0;
698 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
699 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
700 decoder->private_->metadata_filter_ids_count = 0;
703 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
708 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
711 /* simply using realloc() is not practical because the number of channels may change mid-stream */
713 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
714 if(0 != decoder->private_->output[i]) {
715 free(decoder->private_->output[i]-4);
716 decoder->private_->output[i] = 0;
718 if(0 != decoder->private_->residual[i]) {
719 free(decoder->private_->residual[i]);
720 decoder->private_->residual[i] = 0;
724 for(i = 0; i < channels; i++) {
726 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
727 * output arrays have a buffer of up to 3 zeroes in front
728 * (at negative indices) for alignment purposes; we use 4
729 * to keep the data well-aligned.
731 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
733 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
736 memset(tmp, 0, sizeof(FLAC__int32)*4);
737 decoder->private_->output[i] = tmp + 4;
739 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
741 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
744 decoder->private_->residual[i] = tmp;
747 decoder->private_->output_capacity = size;
748 decoder->private_->output_channels = channels;
753 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
757 FLAC__ASSERT(0 != decoder);
758 FLAC__ASSERT(0 != decoder->private_);
760 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
761 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
767 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
771 FLAC__bool first = true;
773 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
775 for(i = id = 0; i < 4; ) {
776 if(decoder->private_->cached) {
777 x = (FLAC__uint32)decoder->private_->lookahead;
778 decoder->private_->cached = false;
781 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
782 return false; /* the read_callback_ sets the state for us */
784 if(x == FLAC__STREAM_SYNC_STRING[i]) {
790 if(x == ID3V2_TAG_[id]) {
794 if(!skip_id3v2_tag_(decoder))
795 return false; /* the read_callback_ sets the state for us */
799 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
800 decoder->private_->header_warmup[0] = (FLAC__byte)x;
801 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
802 return false; /* the read_callback_ sets the state for us */
804 /* 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 */
805 /* else we have to check if the second byte is the end of a sync code */
806 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
807 decoder->private_->lookahead = (FLAC__byte)x;
808 decoder->private_->cached = true;
810 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
811 decoder->private_->header_warmup[1] = (FLAC__byte)x;
812 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
818 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
823 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
827 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
830 FLAC__uint32 i, x, type, length;
832 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
834 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
835 return false; /* the read_callback_ sets the state for us */
836 is_last = x? true : false;
838 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
839 return false; /* the read_callback_ sets the state for us */
841 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
842 return false; /* the read_callback_ sets the state for us */
844 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
845 if(!read_metadata_streaminfo_(decoder, is_last, length))
848 decoder->private_->has_stream_info = true;
849 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
850 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
852 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
853 if(!read_metadata_seektable_(decoder, is_last, length))
856 decoder->private_->has_seek_table = true;
857 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
858 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
861 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
862 unsigned real_length = length;
863 FLAC__StreamMetadata block;
865 block.is_last = is_last;
866 block.type = (FLAC__MetadataType)type;
867 block.length = length;
869 if(type == FLAC__METADATA_TYPE_APPLICATION) {
870 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))
871 return false; /* the read_callback_ sets the state for us */
873 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
875 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
880 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
881 return false; /* the read_callback_ sets the state for us */
885 case FLAC__METADATA_TYPE_PADDING:
886 /* skip the padding bytes */
887 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
888 return false; /* the read_callback_ sets the state for us */
890 case FLAC__METADATA_TYPE_APPLICATION:
891 /* remember, we read the ID already */
892 if(real_length > 0) {
893 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
894 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
897 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
898 return false; /* the read_callback_ sets the state for us */
901 block.data.application.data = 0;
903 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
904 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
907 case FLAC__METADATA_TYPE_CUESHEET:
908 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
911 case FLAC__METADATA_TYPE_STREAMINFO:
912 case FLAC__METADATA_TYPE_SEEKTABLE:
916 if(real_length > 0) {
917 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
918 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
921 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
922 return false; /* the read_callback_ sets the state for us */
925 block.data.unknown.data = 0;
928 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
930 /* now we have to free any malloc'ed data in the block */
932 case FLAC__METADATA_TYPE_PADDING:
934 case FLAC__METADATA_TYPE_APPLICATION:
935 if(0 != block.data.application.data)
936 free(block.data.application.data);
938 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
939 if(0 != block.data.vorbis_comment.vendor_string.entry)
940 free(block.data.vorbis_comment.vendor_string.entry);
941 if(block.data.vorbis_comment.num_comments > 0)
942 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
943 if(0 != block.data.vorbis_comment.comments[i].entry)
944 free(block.data.vorbis_comment.comments[i].entry);
945 if(0 != block.data.vorbis_comment.comments)
946 free(block.data.vorbis_comment.comments);
948 case FLAC__METADATA_TYPE_CUESHEET:
949 if(block.data.cue_sheet.num_tracks > 0)
950 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
951 if(0 != block.data.cue_sheet.tracks[i].indices)
952 free(block.data.cue_sheet.tracks[i].indices);
953 if(0 != block.data.cue_sheet.tracks)
954 free(block.data.cue_sheet.tracks);
956 case FLAC__METADATA_TYPE_STREAMINFO:
957 case FLAC__METADATA_TYPE_SEEKTABLE:
960 if(0 != block.data.unknown.data)
961 free(block.data.unknown.data);
968 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
973 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
976 unsigned bits, used_bits = 0;
978 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
980 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
981 decoder->private_->stream_info.is_last = is_last;
982 decoder->private_->stream_info.length = length;
984 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
985 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
986 return false; /* the read_callback_ sets the state for us */
987 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
990 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
991 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
992 return false; /* the read_callback_ sets the state for us */
993 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
996 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
997 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
998 return false; /* the read_callback_ sets the state for us */
999 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1002 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1003 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1004 return false; /* the read_callback_ sets the state for us */
1005 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1008 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1009 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1010 return false; /* the read_callback_ sets the state for us */
1011 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1014 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1015 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1016 return false; /* the read_callback_ sets the state for us */
1017 decoder->private_->stream_info.data.stream_info.channels = x+1;
1020 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1021 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1022 return false; /* the read_callback_ sets the state for us */
1023 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1026 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1027 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))
1028 return false; /* the read_callback_ sets the state for us */
1031 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1032 return false; /* the read_callback_ sets the state for us */
1035 /* skip the rest of the block */
1036 FLAC__ASSERT(used_bits % 8 == 0);
1037 length -= (used_bits / 8);
1038 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1039 return false; /* the read_callback_ sets the state for us */
1044 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1049 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1051 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1052 decoder->private_->seek_table.is_last = is_last;
1053 decoder->private_->seek_table.length = length;
1055 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1057 /* use realloc since we may pass through here several times (e.g. after seeking) */
1058 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)))) {
1059 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1062 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1063 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1064 return false; /* the read_callback_ sets the state for us */
1065 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1067 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1068 return false; /* the read_callback_ sets the state for us */
1069 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1071 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1072 return false; /* the read_callback_ sets the state for us */
1073 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1075 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1076 /* if there is a partial point left, skip over it */
1078 /*@@@ do an error_callback() here? there's an argument for either way */
1079 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1080 return false; /* the read_callback_ sets the state for us */
1086 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1090 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1092 /* read vendor string */
1093 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1094 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1095 return false; /* the read_callback_ sets the state for us */
1096 if(obj->vendor_string.length > 0) {
1097 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length))) {
1098 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1101 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1102 return false; /* the read_callback_ sets the state for us */
1105 obj->vendor_string.entry = 0;
1107 /* read num comments */
1108 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1109 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1110 return false; /* the read_callback_ sets the state for us */
1113 if(obj->num_comments > 0) {
1114 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1115 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1118 for(i = 0; i < obj->num_comments; i++) {
1119 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1120 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1121 return false; /* the read_callback_ sets the state for us */
1122 if(obj->comments[i].length > 0) {
1123 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length))) {
1124 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1127 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1128 return false; /* the read_callback_ sets the state for us */
1131 obj->comments[i].entry = 0;
1141 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1143 FLAC__uint32 i, j, x;
1145 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1147 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1149 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1150 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
1151 return false; /* the read_callback_ sets the state for us */
1153 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1154 return false; /* the read_callback_ sets the state for us */
1156 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1157 return false; /* the read_callback_ sets the state for us */
1158 obj->is_cd = x? true : false;
1160 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1161 return false; /* the read_callback_ sets the state for us */
1163 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1164 return false; /* the read_callback_ sets the state for us */
1165 obj->num_tracks = x;
1167 if(obj->num_tracks > 0) {
1168 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1169 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1172 for(i = 0; i < obj->num_tracks; i++) {
1173 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1174 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1175 return false; /* the read_callback_ sets the state for us */
1177 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1178 return false; /* the read_callback_ sets the state for us */
1179 track->number = (FLAC__byte)x;
1181 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1182 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
1183 return false; /* the read_callback_ sets the state for us */
1185 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1186 return false; /* the read_callback_ sets the state for us */
1189 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1190 return false; /* the read_callback_ sets the state for us */
1191 track->pre_emphasis = x;
1193 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1194 return false; /* the read_callback_ sets the state for us */
1196 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1197 return false; /* the read_callback_ sets the state for us */
1198 track->num_indices = (FLAC__byte)x;
1200 if(track->num_indices > 0) {
1201 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1202 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1205 for(j = 0; j < track->num_indices; j++) {
1206 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1207 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1208 return false; /* the read_callback_ sets the state for us */
1210 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1211 return false; /* the read_callback_ sets the state for us */
1212 index->number = (FLAC__byte)x;
1214 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1215 return false; /* the read_callback_ sets the state for us */
1224 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1229 /* skip the version and flags bytes */
1230 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1231 return false; /* the read_callback_ sets the state for us */
1232 /* get the size (in bytes) to skip */
1234 for(i = 0; i < 4; i++) {
1235 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1236 return false; /* the read_callback_ sets the state for us */
1240 /* skip the rest of the tag */
1241 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1242 return false; /* the read_callback_ sets the state for us */
1246 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1249 FLAC__bool first = true;
1251 /* If we know the total number of samples in the stream, stop if we've read that many. */
1252 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1253 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1254 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1255 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1260 /* make sure we're byte aligned */
1261 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1262 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1263 return false; /* the read_callback_ sets the state for us */
1267 if(decoder->private_->cached) {
1268 x = (FLAC__uint32)decoder->private_->lookahead;
1269 decoder->private_->cached = false;
1272 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1273 return false; /* the read_callback_ sets the state for us */
1275 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1276 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1277 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1278 return false; /* the read_callback_ sets the state for us */
1280 /* 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 */
1281 /* else we have to check if the second byte is the end of a sync code */
1282 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1283 decoder->private_->lookahead = (FLAC__byte)x;
1284 decoder->private_->cached = true;
1286 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1287 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1288 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1293 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1301 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1305 FLAC__int32 mid, side, left, right;
1306 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1309 *got_a_frame = false;
1313 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1314 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1315 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1317 if(!read_frame_header_(decoder))
1319 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1321 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1323 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1325 * first figure the correct bits-per-sample of the subframe
1327 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1328 switch(decoder->private_->frame.header.channel_assignment) {
1329 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1330 /* no adjustment needed */
1332 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1333 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1337 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1338 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1342 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1343 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1353 if(!read_subframe_(decoder, channel, bps))
1355 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1356 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1360 if(!read_zero_padding_(decoder))
1364 * Read the frame CRC-16 from the footer and check
1366 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1367 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1368 return false; /* the read_callback_ sets the state for us */
1369 if(frame_crc == (FLAC__uint16)x) {
1370 /* Undo any special channel coding */
1371 switch(decoder->private_->frame.header.channel_assignment) {
1372 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1375 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1376 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1377 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1378 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1380 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1381 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1382 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1383 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1385 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1386 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1387 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1388 mid = decoder->private_->output[0][i];
1389 side = decoder->private_->output[1][i];
1391 if(side & 1) /* i.e. if 'side' is odd... */
1395 decoder->private_->output[0][i] = left >> 1;
1396 decoder->private_->output[1][i] = right >> 1;
1405 /* Bad frame, emit error and zero the output signal */
1406 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1407 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1408 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1412 *got_a_frame = true;
1414 /* put the latest values into the public section of the decoder instance */
1415 decoder->protected_->channels = decoder->private_->frame.header.channels;
1416 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1417 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1418 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1419 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1421 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1422 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1425 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)
1428 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1432 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1436 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1437 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1438 unsigned raw_header_len;
1439 FLAC__bool is_unparseable = false;
1440 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);
1441 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);
1443 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1445 /* init the raw header with the saved bits from synchronization */
1446 raw_header[0] = decoder->private_->header_warmup[0];
1447 raw_header[1] = decoder->private_->header_warmup[1];
1451 * check to make sure that the reserved bits are 0
1453 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1454 is_unparseable = true;
1458 * Note that along the way as we read the header, we look for a sync
1459 * code inside. If we find one it would indicate that our original
1460 * sync was bad since there cannot be a sync code in a valid header.
1464 * read in the raw header as bytes so we can CRC it, and parse it on the way
1466 for(i = 0; i < 2; i++) {
1467 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1468 return false; /* the read_callback_ sets the state for us */
1469 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1470 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1471 decoder->private_->lookahead = (FLAC__byte)x;
1472 decoder->private_->cached = true;
1473 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1474 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1477 raw_header[raw_header_len++] = (FLAC__byte)x;
1480 switch(x = raw_header[2] >> 4) {
1482 if(is_known_fixed_blocksize_stream)
1483 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1485 is_unparseable = true;
1488 decoder->private_->frame.header.blocksize = 192;
1494 decoder->private_->frame.header.blocksize = 576 << (x-2);
1508 decoder->private_->frame.header.blocksize = 256 << (x-8);
1515 switch(x = raw_header[2] & 0x0f) {
1517 if(decoder->private_->has_stream_info)
1518 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1520 is_unparseable = true;
1525 is_unparseable = true;
1528 decoder->private_->frame.header.sample_rate = 8000;
1531 decoder->private_->frame.header.sample_rate = 16000;
1534 decoder->private_->frame.header.sample_rate = 22050;
1537 decoder->private_->frame.header.sample_rate = 24000;
1540 decoder->private_->frame.header.sample_rate = 32000;
1543 decoder->private_->frame.header.sample_rate = 44100;
1546 decoder->private_->frame.header.sample_rate = 48000;
1549 decoder->private_->frame.header.sample_rate = 96000;
1554 sample_rate_hint = x;
1557 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1558 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1564 x = (unsigned)(raw_header[3] >> 4);
1566 decoder->private_->frame.header.channels = 2;
1569 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1572 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1575 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1578 is_unparseable = true;
1583 decoder->private_->frame.header.channels = (unsigned)x + 1;
1584 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1587 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1589 if(decoder->private_->has_stream_info)
1590 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1592 is_unparseable = true;
1595 decoder->private_->frame.header.bits_per_sample = 8;
1598 decoder->private_->frame.header.bits_per_sample = 12;
1601 decoder->private_->frame.header.bits_per_sample = 16;
1604 decoder->private_->frame.header.bits_per_sample = 20;
1607 decoder->private_->frame.header.bits_per_sample = 24;
1611 is_unparseable = true;
1618 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1619 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1620 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1624 if(blocksize_hint && is_known_variable_blocksize_stream) {
1625 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1626 return false; /* the read_callback_ sets the state for us */
1627 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1628 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1629 decoder->private_->cached = true;
1630 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1631 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1634 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1635 decoder->private_->frame.header.number.sample_number = xx;
1638 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1639 return false; /* the read_callback_ sets the state for us */
1640 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1641 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1642 decoder->private_->cached = true;
1643 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1644 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1647 decoder->private_->last_frame_number = x;
1648 if(decoder->private_->has_stream_info) {
1649 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1650 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1653 is_unparseable = true;
1657 if(blocksize_hint) {
1658 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1659 return false; /* the read_callback_ sets the state for us */
1660 raw_header[raw_header_len++] = (FLAC__byte)x;
1661 if(blocksize_hint == 7) {
1663 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1664 return false; /* the read_callback_ sets the state for us */
1665 raw_header[raw_header_len++] = (FLAC__byte)_x;
1668 decoder->private_->frame.header.blocksize = x+1;
1671 if(sample_rate_hint) {
1672 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1673 return false; /* the read_callback_ sets the state for us */
1674 raw_header[raw_header_len++] = (FLAC__byte)x;
1675 if(sample_rate_hint != 12) {
1677 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1678 return false; /* the read_callback_ sets the state for us */
1679 raw_header[raw_header_len++] = (FLAC__byte)_x;
1682 if(sample_rate_hint == 12)
1683 decoder->private_->frame.header.sample_rate = x*1000;
1684 else if(sample_rate_hint == 13)
1685 decoder->private_->frame.header.sample_rate = x;
1687 decoder->private_->frame.header.sample_rate = x*10;
1690 /* read the CRC-8 byte */
1691 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1692 return false; /* the read_callback_ sets the state for us */
1693 crc8 = (FLAC__byte)x;
1695 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1696 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1697 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1701 if(is_unparseable) {
1702 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1709 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1712 FLAC__bool wasted_bits;
1714 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1715 return false; /* the read_callback_ sets the state for us */
1717 wasted_bits = (x & 1);
1722 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1723 return false; /* the read_callback_ sets the state for us */
1724 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1725 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1728 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1731 * Lots of magic numbers here
1734 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1735 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1739 if(!read_subframe_constant_(decoder, channel, bps))
1743 if(!read_subframe_verbatim_(decoder, channel, bps))
1747 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1751 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1755 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1759 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1765 x = decoder->private_->frame.subframes[channel].wasted_bits;
1766 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1767 decoder->private_->output[channel][i] <<= x;
1773 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1775 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1778 FLAC__int32 *output = decoder->private_->output[channel];
1780 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1782 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1783 return false; /* the read_callback_ sets the state for us */
1785 subframe->value = x;
1787 /* decode the subframe */
1788 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1794 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1796 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1801 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1803 subframe->residual = decoder->private_->residual[channel];
1804 subframe->order = order;
1806 /* read warm-up samples */
1807 for(u = 0; u < order; u++) {
1808 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1809 return false; /* the read_callback_ sets the state for us */
1810 subframe->warmup[u] = i32;
1813 /* read entropy coding method info */
1814 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1815 return false; /* the read_callback_ sets the state for us */
1816 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1817 switch(subframe->entropy_coding_method.type) {
1818 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1819 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1820 return false; /* the read_callback_ sets the state for us */
1821 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1822 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1825 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1830 switch(subframe->entropy_coding_method.type) {
1831 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1832 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]))
1839 /* decode the subframe */
1840 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1841 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1846 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1848 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1853 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1855 subframe->residual = decoder->private_->residual[channel];
1856 subframe->order = order;
1858 /* read warm-up samples */
1859 for(u = 0; u < order; u++) {
1860 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1861 return false; /* the read_callback_ sets the state for us */
1862 subframe->warmup[u] = i32;
1865 /* read qlp coeff precision */
1866 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1867 return false; /* the read_callback_ sets the state for us */
1868 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1869 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1870 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1873 subframe->qlp_coeff_precision = u32+1;
1875 /* read qlp shift */
1876 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1877 return false; /* the read_callback_ sets the state for us */
1878 subframe->quantization_level = i32;
1880 /* read quantized lp coefficiencts */
1881 for(u = 0; u < order; u++) {
1882 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1883 return false; /* the read_callback_ sets the state for us */
1884 subframe->qlp_coeff[u] = i32;
1887 /* read entropy coding method info */
1888 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1889 return false; /* the read_callback_ sets the state for us */
1890 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1891 switch(subframe->entropy_coding_method.type) {
1892 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1893 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1894 return false; /* the read_callback_ sets the state for us */
1895 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1896 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1899 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1904 switch(subframe->entropy_coding_method.type) {
1905 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1906 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]))
1913 /* decode the subframe */
1914 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1915 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1916 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1917 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);
1919 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);
1921 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);
1926 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1928 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1929 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1932 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1934 subframe->data = residual;
1936 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1937 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1938 return false; /* the read_callback_ sets the state for us */
1942 /* decode the subframe */
1943 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1948 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1950 FLAC__uint32 rice_parameter;
1952 unsigned partition, sample, u;
1953 const unsigned partitions = 1u << partition_order;
1954 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1956 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1957 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1962 for(partition = 0; partition < partitions; partition++) {
1963 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1964 return false; /* the read_callback_ sets the state for us */
1965 partitioned_rice_contents->parameters[partition] = rice_parameter;
1966 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1967 #ifdef FLAC__SYMMETRIC_RICE
1968 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1969 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1970 return false; /* the read_callback_ sets the state for us */
1971 residual[sample] = i;
1974 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1975 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1976 return false; /* the read_callback_ sets the state for us */
1981 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1982 return false; /* the read_callback_ sets the state for us */
1983 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1984 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1985 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1986 return false; /* the read_callback_ sets the state for us */
1987 residual[sample] = i;
1995 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1997 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1998 FLAC__uint32 zero = 0;
1999 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
2000 return false; /* the read_callback_ sets the state for us */
2002 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
2003 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2009 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
2011 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2012 FLAC__StreamDecoderReadStatus status;
2014 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
2015 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
2016 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2017 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
2018 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2019 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;