1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 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[FLAC__METADATA_TYPE_UNDEFINED];
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 < FLAC__METADATA_TYPE_UNDEFINED);
394 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
396 decoder->private_->metadata_filter[type] = true;
397 if(type == FLAC__METADATA_TYPE_APPLICATION)
398 decoder->private_->metadata_filter_ids_count = 0;
402 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
404 FLAC__ASSERT(0 != decoder);
405 FLAC__ASSERT(0 != decoder->private_);
406 FLAC__ASSERT(0 != decoder->protected_);
407 FLAC__ASSERT(0 != id);
408 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
411 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
414 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
416 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
417 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
418 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
419 decoder->private_->metadata_filter_ids_capacity *= 2;
422 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));
423 decoder->private_->metadata_filter_ids_count++;
428 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
431 FLAC__ASSERT(0 != decoder);
432 FLAC__ASSERT(0 != decoder->private_);
433 FLAC__ASSERT(0 != decoder->protected_);
434 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
436 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
437 decoder->private_->metadata_filter[i] = true;
438 decoder->private_->metadata_filter_ids_count = 0;
442 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
444 FLAC__ASSERT(0 != decoder);
445 FLAC__ASSERT(0 != decoder->private_);
446 FLAC__ASSERT(0 != decoder->protected_);
447 FLAC__ASSERT(type < FLAC__METADATA_TYPE_UNDEFINED);
448 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
450 decoder->private_->metadata_filter[type] = false;
451 if(type == FLAC__METADATA_TYPE_APPLICATION)
452 decoder->private_->metadata_filter_ids_count = 0;
456 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
458 FLAC__ASSERT(0 != decoder);
459 FLAC__ASSERT(0 != decoder->private_);
460 FLAC__ASSERT(0 != decoder->protected_);
461 FLAC__ASSERT(0 != id);
462 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
465 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
468 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
470 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
471 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
472 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
473 decoder->private_->metadata_filter_ids_capacity *= 2;
476 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));
477 decoder->private_->metadata_filter_ids_count++;
482 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
484 FLAC__ASSERT(0 != decoder);
485 FLAC__ASSERT(0 != decoder->private_);
486 FLAC__ASSERT(0 != decoder->protected_);
487 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
489 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
490 decoder->private_->metadata_filter_ids_count = 0;
494 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
496 FLAC__ASSERT(0 != decoder);
497 FLAC__ASSERT(0 != decoder->protected_);
498 return decoder->protected_->state;
501 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
503 FLAC__ASSERT(0 != decoder);
504 FLAC__ASSERT(0 != decoder->protected_);
505 return decoder->protected_->channels;
508 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
510 FLAC__ASSERT(0 != decoder);
511 FLAC__ASSERT(0 != decoder->protected_);
512 return decoder->protected_->channel_assignment;
515 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
517 FLAC__ASSERT(0 != decoder);
518 FLAC__ASSERT(0 != decoder->protected_);
519 return decoder->protected_->bits_per_sample;
522 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
524 FLAC__ASSERT(0 != decoder);
525 FLAC__ASSERT(0 != decoder->protected_);
526 return decoder->protected_->sample_rate;
529 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
531 FLAC__ASSERT(0 != decoder);
532 FLAC__ASSERT(0 != decoder->protected_);
533 return decoder->protected_->blocksize;
536 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
538 FLAC__ASSERT(0 != decoder);
539 FLAC__ASSERT(0 != decoder->private_);
540 FLAC__ASSERT(0 != decoder->protected_);
542 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
543 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
546 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
551 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
553 FLAC__ASSERT(0 != decoder);
554 FLAC__ASSERT(0 != decoder->private_);
555 FLAC__ASSERT(0 != decoder->protected_);
557 if(!FLAC__stream_decoder_flush(decoder)) {
558 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
561 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
563 decoder->private_->samples_decoded = 0;
568 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
570 FLAC__bool got_a_frame;
571 FLAC__ASSERT(0 != decoder);
572 FLAC__ASSERT(0 != decoder->protected_);
575 switch(decoder->protected_->state) {
576 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
577 if(!find_metadata_(decoder))
578 return false; /* above function sets the status for us */
580 case FLAC__STREAM_DECODER_READ_METADATA:
581 if(!read_metadata_(decoder))
582 return false; /* above function sets the status for us */
585 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
586 if(!frame_sync_(decoder))
587 return true; /* above function sets the status for us */
589 case FLAC__STREAM_DECODER_READ_FRAME:
590 if(!read_frame_(decoder, &got_a_frame))
591 return false; /* above function sets the status for us */
593 return true; /* above function sets the status for us */
595 case FLAC__STREAM_DECODER_END_OF_STREAM:
596 case FLAC__STREAM_DECODER_ABORTED:
605 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
607 FLAC__ASSERT(0 != decoder);
608 FLAC__ASSERT(0 != decoder->protected_);
611 switch(decoder->protected_->state) {
612 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
613 if(!find_metadata_(decoder))
614 return false; /* above function sets the status for us */
616 case FLAC__STREAM_DECODER_READ_METADATA:
617 if(!read_metadata_(decoder))
618 return false; /* above function sets the status for us */
620 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
621 case FLAC__STREAM_DECODER_READ_FRAME:
622 case FLAC__STREAM_DECODER_END_OF_STREAM:
623 case FLAC__STREAM_DECODER_ABORTED:
632 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
635 FLAC__ASSERT(0 != decoder);
636 FLAC__ASSERT(0 != decoder->protected_);
639 switch(decoder->protected_->state) {
640 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
641 if(!find_metadata_(decoder))
642 return false; /* above function sets the status for us */
644 case FLAC__STREAM_DECODER_READ_METADATA:
645 if(!read_metadata_(decoder))
646 return false; /* above function sets the status for us */
648 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
649 if(!frame_sync_(decoder))
650 return true; /* above function sets the status for us */
652 case FLAC__STREAM_DECODER_READ_FRAME:
653 if(!read_frame_(decoder, &dummy))
654 return false; /* above function sets the status for us */
656 case FLAC__STREAM_DECODER_END_OF_STREAM:
657 case FLAC__STREAM_DECODER_ABORTED:
666 /***********************************************************************
668 * Protected class methods
670 ***********************************************************************/
672 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
674 FLAC__ASSERT(0 != decoder);
675 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
678 /***********************************************************************
680 * Private class methods
682 ***********************************************************************/
684 void set_defaults_(FLAC__StreamDecoder *decoder)
686 decoder->private_->read_callback = 0;
687 decoder->private_->write_callback = 0;
688 decoder->private_->metadata_callback = 0;
689 decoder->private_->error_callback = 0;
690 decoder->private_->client_data = 0;
692 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
693 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
694 decoder->private_->metadata_filter_ids_count = 0;
697 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
702 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
705 /* simply using realloc() is not practical because the number of channels may change mid-stream */
707 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
708 if(0 != decoder->private_->output[i]) {
709 free(decoder->private_->output[i]-4);
710 decoder->private_->output[i] = 0;
712 if(0 != decoder->private_->residual[i]) {
713 free(decoder->private_->residual[i]);
714 decoder->private_->residual[i] = 0;
718 for(i = 0; i < channels; i++) {
720 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
721 * output arrays have a buffer of up to 3 zeroes in front
722 * (at negative indices) for alignment purposes; we use 4
723 * to keep the data well-aligned.
725 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
727 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
730 memset(tmp, 0, sizeof(FLAC__int32)*4);
731 decoder->private_->output[i] = tmp + 4;
733 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
735 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
738 decoder->private_->residual[i] = tmp;
741 decoder->private_->output_capacity = size;
742 decoder->private_->output_channels = channels;
747 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
751 FLAC__ASSERT(0 != decoder);
752 FLAC__ASSERT(0 != decoder->private_);
754 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
755 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
761 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
765 FLAC__bool first = true;
767 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
769 for(i = id = 0; i < 4; ) {
770 if(decoder->private_->cached) {
771 x = (FLAC__uint32)decoder->private_->lookahead;
772 decoder->private_->cached = false;
775 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
776 return false; /* the read_callback_ sets the state for us */
778 if(x == FLAC__STREAM_SYNC_STRING[i]) {
784 if(x == ID3V2_TAG_[id]) {
788 if(!skip_id3v2_tag_(decoder))
789 return false; /* the read_callback_ sets the state for us */
793 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
794 decoder->private_->header_warmup[0] = (FLAC__byte)x;
795 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
796 return false; /* the read_callback_ sets the state for us */
798 /* 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 */
799 /* else we have to check if the second byte is the end of a sync code */
800 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
801 decoder->private_->lookahead = (FLAC__byte)x;
802 decoder->private_->cached = true;
804 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
805 decoder->private_->header_warmup[1] = (FLAC__byte)x;
806 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
812 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
817 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
821 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
824 FLAC__uint32 i, x, type, length;
826 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
828 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
829 return false; /* the read_callback_ sets the state for us */
830 is_last = x? true : false;
832 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
833 return false; /* the read_callback_ sets the state for us */
835 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
836 return false; /* the read_callback_ sets the state for us */
838 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
839 if(!read_metadata_streaminfo_(decoder, is_last, length))
842 decoder->private_->has_stream_info = true;
843 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
844 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
846 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
847 if(!read_metadata_seektable_(decoder, is_last, length))
850 decoder->private_->has_seek_table = true;
851 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
852 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
855 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
856 unsigned real_length = length;
857 FLAC__StreamMetadata block;
859 block.is_last = is_last;
860 block.type = (FLAC__MetadataType)type;
861 block.length = length;
863 if(type == FLAC__METADATA_TYPE_APPLICATION) {
864 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))
865 return false; /* the read_callback_ sets the state for us */
867 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
869 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
874 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
875 return false; /* the read_callback_ sets the state for us */
879 case FLAC__METADATA_TYPE_PADDING:
880 /* skip the padding bytes */
881 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
882 return false; /* the read_callback_ sets the state for us */
884 case FLAC__METADATA_TYPE_APPLICATION:
885 /* remember, we read the ID already */
886 if(real_length > 0) {
887 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
888 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
891 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
892 return false; /* the read_callback_ sets the state for us */
895 block.data.application.data = 0;
897 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
898 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
901 case FLAC__METADATA_TYPE_CUESHEET:
902 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
905 case FLAC__METADATA_TYPE_STREAMINFO:
906 case FLAC__METADATA_TYPE_SEEKTABLE:
910 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
912 /* now we have to free any malloc'ed data in the block */
914 case FLAC__METADATA_TYPE_PADDING:
916 case FLAC__METADATA_TYPE_APPLICATION:
917 if(0 != block.data.application.data)
918 free(block.data.application.data);
920 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
921 if(0 != block.data.vorbis_comment.vendor_string.entry)
922 free(block.data.vorbis_comment.vendor_string.entry);
923 if(block.data.vorbis_comment.num_comments > 0)
924 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
925 if(0 != block.data.vorbis_comment.comments[i].entry)
926 free(block.data.vorbis_comment.comments[i].entry);
927 if(0 != block.data.vorbis_comment.comments)
928 free(block.data.vorbis_comment.comments);
930 case FLAC__METADATA_TYPE_CUESHEET:
931 if(block.data.cue_sheet.num_tracks > 0)
932 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
933 if(0 != block.data.cue_sheet.tracks[i].indices)
934 free(block.data.cue_sheet.tracks[i].indices);
935 if(0 != block.data.cue_sheet.tracks)
936 free(block.data.cue_sheet.tracks);
938 case FLAC__METADATA_TYPE_STREAMINFO:
939 case FLAC__METADATA_TYPE_SEEKTABLE:
947 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
952 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
955 unsigned bits, used_bits = 0;
957 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
959 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
960 decoder->private_->stream_info.is_last = is_last;
961 decoder->private_->stream_info.length = length;
963 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
964 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
965 return false; /* the read_callback_ sets the state for us */
966 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
969 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
970 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
971 return false; /* the read_callback_ sets the state for us */
972 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
975 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
976 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
977 return false; /* the read_callback_ sets the state for us */
978 decoder->private_->stream_info.data.stream_info.min_framesize = x;
981 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
982 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
983 return false; /* the read_callback_ sets the state for us */
984 decoder->private_->stream_info.data.stream_info.max_framesize = x;
987 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
988 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
989 return false; /* the read_callback_ sets the state for us */
990 decoder->private_->stream_info.data.stream_info.sample_rate = x;
993 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
994 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
995 return false; /* the read_callback_ sets the state for us */
996 decoder->private_->stream_info.data.stream_info.channels = x+1;
999 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1000 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1001 return false; /* the read_callback_ sets the state for us */
1002 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1005 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1006 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))
1007 return false; /* the read_callback_ sets the state for us */
1010 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1011 return false; /* the read_callback_ sets the state for us */
1014 /* skip the rest of the block */
1015 FLAC__ASSERT(used_bits % 8 == 0);
1016 length -= (used_bits / 8);
1017 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1018 return false; /* the read_callback_ sets the state for us */
1023 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1028 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1030 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1031 decoder->private_->seek_table.is_last = is_last;
1032 decoder->private_->seek_table.length = length;
1034 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1036 /* use realloc since we may pass through here several times (e.g. after seeking) */
1037 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)))) {
1038 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1041 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1042 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1043 return false; /* the read_callback_ sets the state for us */
1044 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1046 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1047 return false; /* the read_callback_ sets the state for us */
1048 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1050 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1051 return false; /* the read_callback_ sets the state for us */
1052 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1054 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1055 /* if there is a partial point left, skip over it */
1057 /*@@@ do an error_callback() here? there's an argument for either way */
1058 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1059 return false; /* the read_callback_ sets the state for us */
1065 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1069 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1071 /* read vendor string */
1072 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1073 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1074 return false; /* the read_callback_ sets the state for us */
1075 if(obj->vendor_string.length > 0) {
1076 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length))) {
1077 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1080 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1081 return false; /* the read_callback_ sets the state for us */
1084 obj->vendor_string.entry = 0;
1086 /* read num comments */
1087 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1088 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1089 return false; /* the read_callback_ sets the state for us */
1092 if(obj->num_comments > 0) {
1093 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1094 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1097 for(i = 0; i < obj->num_comments; i++) {
1098 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1099 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1100 return false; /* the read_callback_ sets the state for us */
1101 if(obj->comments[i].length > 0) {
1102 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length))) {
1103 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1106 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1107 return false; /* the read_callback_ sets the state for us */
1110 obj->comments[i].entry = 0;
1120 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1122 FLAC__uint32 i, j, x;
1124 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1126 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1128 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1129 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))
1130 return false; /* the read_callback_ sets the state for us */
1132 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1133 return false; /* the read_callback_ sets the state for us */
1135 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1136 return false; /* the read_callback_ sets the state for us */
1138 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1139 return false; /* the read_callback_ sets the state for us */
1140 obj->num_tracks = x;
1142 if(obj->num_tracks > 0) {
1143 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1144 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1147 for(i = 0; i < obj->num_tracks; i++) {
1148 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1149 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1150 return false; /* the read_callback_ sets the state for us */
1152 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1153 return false; /* the read_callback_ sets the state for us */
1154 track->number = (FLAC__byte)x;
1156 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1157 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))
1158 return false; /* the read_callback_ sets the state for us */
1160 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1161 return false; /* the read_callback_ sets the state for us */
1164 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1165 return false; /* the read_callback_ sets the state for us */
1166 track->pre_emphasis = x;
1168 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1169 return false; /* the read_callback_ sets the state for us */
1171 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1172 return false; /* the read_callback_ sets the state for us */
1173 track->num_indices = (FLAC__byte)x;
1175 if(track->num_indices > 0) {
1176 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1177 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1180 for(j = 0; j < track->num_indices; j++) {
1181 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1182 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, 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_INDEX_NUMBER_LEN, read_callback_, decoder))
1186 return false; /* the read_callback_ sets the state for us */
1187 index->number = (FLAC__byte)x;
1189 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1190 return false; /* the read_callback_ sets the state for us */
1199 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1204 /* skip the version and flags bytes */
1205 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1206 return false; /* the read_callback_ sets the state for us */
1207 /* get the size (in bytes) to skip */
1209 for(i = 0; i < 4; i++) {
1210 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1211 return false; /* the read_callback_ sets the state for us */
1215 /* skip the rest of the tag */
1216 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1217 return false; /* the read_callback_ sets the state for us */
1221 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1224 FLAC__bool first = true;
1226 /* If we know the total number of samples in the stream, stop if we've read that many. */
1227 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1228 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1229 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1230 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1235 /* make sure we're byte aligned */
1236 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1237 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1238 return false; /* the read_callback_ sets the state for us */
1242 if(decoder->private_->cached) {
1243 x = (FLAC__uint32)decoder->private_->lookahead;
1244 decoder->private_->cached = false;
1247 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1248 return false; /* the read_callback_ sets the state for us */
1250 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1251 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1252 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1253 return false; /* the read_callback_ sets the state for us */
1255 /* 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 */
1256 /* else we have to check if the second byte is the end of a sync code */
1257 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1258 decoder->private_->lookahead = (FLAC__byte)x;
1259 decoder->private_->cached = true;
1261 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1262 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1263 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1268 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1276 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1280 FLAC__int32 mid, side, left, right;
1281 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1284 *got_a_frame = false;
1288 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1289 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1290 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1292 if(!read_frame_header_(decoder))
1294 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1296 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1298 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1300 * first figure the correct bits-per-sample of the subframe
1302 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1303 switch(decoder->private_->frame.header.channel_assignment) {
1304 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1305 /* no adjustment needed */
1307 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1308 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1312 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1313 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1317 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1318 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1328 if(!read_subframe_(decoder, channel, bps))
1330 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1331 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1335 if(!read_zero_padding_(decoder))
1339 * Read the frame CRC-16 from the footer and check
1341 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1342 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1343 return false; /* the read_callback_ sets the state for us */
1344 if(frame_crc == (FLAC__uint16)x) {
1345 /* Undo any special channel coding */
1346 switch(decoder->private_->frame.header.channel_assignment) {
1347 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1350 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1351 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1352 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1353 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1355 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1356 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1357 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1358 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1360 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1361 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1362 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1363 mid = decoder->private_->output[0][i];
1364 side = decoder->private_->output[1][i];
1366 if(side & 1) /* i.e. if 'side' is odd... */
1370 decoder->private_->output[0][i] = left >> 1;
1371 decoder->private_->output[1][i] = right >> 1;
1380 /* Bad frame, emit error and zero the output signal */
1381 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1382 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1383 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1387 *got_a_frame = true;
1389 /* put the latest values into the public section of the decoder instance */
1390 decoder->protected_->channels = decoder->private_->frame.header.channels;
1391 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1392 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1393 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1394 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1396 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1397 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1400 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)
1403 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1407 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1411 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1412 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1413 unsigned raw_header_len;
1414 FLAC__bool is_unparseable = false;
1415 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);
1416 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);
1418 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1420 /* init the raw header with the saved bits from synchronization */
1421 raw_header[0] = decoder->private_->header_warmup[0];
1422 raw_header[1] = decoder->private_->header_warmup[1];
1426 * check to make sure that the reserved bits are 0
1428 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1429 is_unparseable = true;
1433 * Note that along the way as we read the header, we look for a sync
1434 * code inside. If we find one it would indicate that our original
1435 * sync was bad since there cannot be a sync code in a valid header.
1439 * read in the raw header as bytes so we can CRC it, and parse it on the way
1441 for(i = 0; i < 2; i++) {
1442 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1443 return false; /* the read_callback_ sets the state for us */
1444 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1445 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1446 decoder->private_->lookahead = (FLAC__byte)x;
1447 decoder->private_->cached = true;
1448 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1449 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1452 raw_header[raw_header_len++] = (FLAC__byte)x;
1455 switch(x = raw_header[2] >> 4) {
1457 if(is_known_fixed_blocksize_stream)
1458 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1460 is_unparseable = true;
1463 decoder->private_->frame.header.blocksize = 192;
1469 decoder->private_->frame.header.blocksize = 576 << (x-2);
1483 decoder->private_->frame.header.blocksize = 256 << (x-8);
1490 switch(x = raw_header[2] & 0x0f) {
1492 if(decoder->private_->has_stream_info)
1493 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1495 is_unparseable = true;
1500 is_unparseable = true;
1503 decoder->private_->frame.header.sample_rate = 8000;
1506 decoder->private_->frame.header.sample_rate = 16000;
1509 decoder->private_->frame.header.sample_rate = 22050;
1512 decoder->private_->frame.header.sample_rate = 24000;
1515 decoder->private_->frame.header.sample_rate = 32000;
1518 decoder->private_->frame.header.sample_rate = 44100;
1521 decoder->private_->frame.header.sample_rate = 48000;
1524 decoder->private_->frame.header.sample_rate = 96000;
1529 sample_rate_hint = x;
1532 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1533 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1539 x = (unsigned)(raw_header[3] >> 4);
1541 decoder->private_->frame.header.channels = 2;
1544 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1547 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1550 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1553 is_unparseable = true;
1558 decoder->private_->frame.header.channels = (unsigned)x + 1;
1559 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1562 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1564 if(decoder->private_->has_stream_info)
1565 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1567 is_unparseable = true;
1570 decoder->private_->frame.header.bits_per_sample = 8;
1573 decoder->private_->frame.header.bits_per_sample = 12;
1576 decoder->private_->frame.header.bits_per_sample = 16;
1579 decoder->private_->frame.header.bits_per_sample = 20;
1582 decoder->private_->frame.header.bits_per_sample = 24;
1586 is_unparseable = true;
1593 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1594 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1595 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1599 if(blocksize_hint && is_known_variable_blocksize_stream) {
1600 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1601 return false; /* the read_callback_ sets the state for us */
1602 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1603 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1604 decoder->private_->cached = true;
1605 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1606 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1609 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1610 decoder->private_->frame.header.number.sample_number = xx;
1613 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1614 return false; /* the read_callback_ sets the state for us */
1615 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1616 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1617 decoder->private_->cached = true;
1618 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1619 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1622 decoder->private_->last_frame_number = x;
1623 if(decoder->private_->has_stream_info) {
1624 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1625 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1628 is_unparseable = true;
1632 if(blocksize_hint) {
1633 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1634 return false; /* the read_callback_ sets the state for us */
1635 raw_header[raw_header_len++] = (FLAC__byte)x;
1636 if(blocksize_hint == 7) {
1638 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1639 return false; /* the read_callback_ sets the state for us */
1640 raw_header[raw_header_len++] = (FLAC__byte)_x;
1643 decoder->private_->frame.header.blocksize = x+1;
1646 if(sample_rate_hint) {
1647 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1648 return false; /* the read_callback_ sets the state for us */
1649 raw_header[raw_header_len++] = (FLAC__byte)x;
1650 if(sample_rate_hint != 12) {
1652 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1653 return false; /* the read_callback_ sets the state for us */
1654 raw_header[raw_header_len++] = (FLAC__byte)_x;
1657 if(sample_rate_hint == 12)
1658 decoder->private_->frame.header.sample_rate = x*1000;
1659 else if(sample_rate_hint == 13)
1660 decoder->private_->frame.header.sample_rate = x;
1662 decoder->private_->frame.header.sample_rate = x*10;
1665 /* read the CRC-8 byte */
1666 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1667 return false; /* the read_callback_ sets the state for us */
1668 crc8 = (FLAC__byte)x;
1670 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1671 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1672 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1676 if(is_unparseable) {
1677 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1684 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1687 FLAC__bool wasted_bits;
1689 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1690 return false; /* the read_callback_ sets the state for us */
1692 wasted_bits = (x & 1);
1697 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1698 return false; /* the read_callback_ sets the state for us */
1699 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1700 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1703 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1706 * Lots of magic numbers here
1709 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1710 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1714 if(!read_subframe_constant_(decoder, channel, bps))
1718 if(!read_subframe_verbatim_(decoder, channel, bps))
1722 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1726 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1730 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1734 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1740 x = decoder->private_->frame.subframes[channel].wasted_bits;
1741 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1742 decoder->private_->output[channel][i] <<= x;
1748 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1750 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1753 FLAC__int32 *output = decoder->private_->output[channel];
1755 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1757 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1758 return false; /* the read_callback_ sets the state for us */
1760 subframe->value = x;
1762 /* decode the subframe */
1763 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1769 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1771 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1776 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1778 subframe->residual = decoder->private_->residual[channel];
1779 subframe->order = order;
1781 /* read warm-up samples */
1782 for(u = 0; u < order; u++) {
1783 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1784 return false; /* the read_callback_ sets the state for us */
1785 subframe->warmup[u] = i32;
1788 /* read entropy coding method info */
1789 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1790 return false; /* the read_callback_ sets the state for us */
1791 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1792 switch(subframe->entropy_coding_method.type) {
1793 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1794 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1795 return false; /* the read_callback_ sets the state for us */
1796 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1797 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1800 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1805 switch(subframe->entropy_coding_method.type) {
1806 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1807 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]))
1814 /* decode the subframe */
1815 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1816 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1821 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1823 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1828 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1830 subframe->residual = decoder->private_->residual[channel];
1831 subframe->order = order;
1833 /* read warm-up samples */
1834 for(u = 0; u < order; u++) {
1835 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1836 return false; /* the read_callback_ sets the state for us */
1837 subframe->warmup[u] = i32;
1840 /* read qlp coeff precision */
1841 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1842 return false; /* the read_callback_ sets the state for us */
1843 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1844 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1845 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1848 subframe->qlp_coeff_precision = u32+1;
1850 /* read qlp shift */
1851 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1852 return false; /* the read_callback_ sets the state for us */
1853 subframe->quantization_level = i32;
1855 /* read quantized lp coefficiencts */
1856 for(u = 0; u < order; u++) {
1857 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1858 return false; /* the read_callback_ sets the state for us */
1859 subframe->qlp_coeff[u] = i32;
1862 /* read entropy coding method info */
1863 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1864 return false; /* the read_callback_ sets the state for us */
1865 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1866 switch(subframe->entropy_coding_method.type) {
1867 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1868 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1869 return false; /* the read_callback_ sets the state for us */
1870 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1871 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1874 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1879 switch(subframe->entropy_coding_method.type) {
1880 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1881 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]))
1888 /* decode the subframe */
1889 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1890 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1891 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1892 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);
1894 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);
1896 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);
1901 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1903 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1904 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1907 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1909 subframe->data = residual;
1911 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1912 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1913 return false; /* the read_callback_ sets the state for us */
1917 /* decode the subframe */
1918 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1923 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1925 FLAC__uint32 rice_parameter;
1927 unsigned partition, sample, u;
1928 const unsigned partitions = 1u << partition_order;
1929 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1931 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1932 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1937 for(partition = 0; partition < partitions; partition++) {
1938 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1939 return false; /* the read_callback_ sets the state for us */
1940 partitioned_rice_contents->parameters[partition] = rice_parameter;
1941 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1942 #ifdef FLAC__SYMMETRIC_RICE
1943 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1944 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1945 return false; /* the read_callback_ sets the state for us */
1946 residual[sample] = i;
1949 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1950 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1951 return false; /* the read_callback_ sets the state for us */
1956 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1957 return false; /* the read_callback_ sets the state for us */
1958 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1959 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1960 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1961 return false; /* the read_callback_ sets the state for us */
1962 residual[sample] = i;
1970 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1972 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1973 FLAC__uint32 zero = 0;
1974 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1975 return false; /* the read_callback_ sets the state for us */
1977 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1978 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1984 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1986 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1987 FLAC__StreamDecoderReadStatus status;
1989 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1990 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1991 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1992 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1993 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1994 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;