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"
36 #define max(a,b) ((a)>(b)?(a):(b))
38 /***********************************************************************
42 ***********************************************************************/
44 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
46 /***********************************************************************
48 * Private class method prototypes
50 ***********************************************************************/
52 static void set_defaults_(FLAC__StreamDecoder *decoder);
53 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
54 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
55 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
56 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
57 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
58 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
59 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
60 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
61 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
62 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
63 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
64 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
65 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
66 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
67 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
68 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
69 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
70 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);
71 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
72 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
74 /***********************************************************************
78 ***********************************************************************/
80 typedef struct FLAC__StreamDecoderPrivate {
81 FLAC__StreamDecoderReadCallback read_callback;
82 FLAC__StreamDecoderWriteCallback write_callback;
83 FLAC__StreamDecoderMetadataCallback metadata_callback;
84 FLAC__StreamDecoderErrorCallback error_callback;
85 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[]);
86 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[]);
87 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[]);
89 FLAC__BitBuffer *input;
90 FLAC__int32 *output[FLAC__MAX_CHANNELS];
91 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
92 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
93 unsigned output_capacity, output_channels;
94 FLAC__uint32 last_frame_number;
95 FLAC__uint64 samples_decoded;
96 FLAC__bool has_stream_info, has_seek_table;
97 FLAC__StreamMetadata stream_info;
98 FLAC__StreamMetadata seek_table;
99 FLAC__bool metadata_filter[FLAC__METADATA_TYPE_UNDEFINED];
100 FLAC__byte *metadata_filter_ids;
101 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
103 FLAC__bool cached; /* true if there is a byte in lookahead */
104 FLAC__CPUInfo cpuinfo;
105 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
106 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
107 } FLAC__StreamDecoderPrivate;
109 /***********************************************************************
111 * Public static class data
113 ***********************************************************************/
115 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
116 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
117 "FLAC__STREAM_DECODER_READ_METADATA",
118 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
119 "FLAC__STREAM_DECODER_READ_FRAME",
120 "FLAC__STREAM_DECODER_END_OF_STREAM",
121 "FLAC__STREAM_DECODER_ABORTED",
122 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
123 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
124 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
125 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
126 "FLAC__STREAM_DECODER_UNINITIALIZED"
129 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
130 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
131 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
132 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
135 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
136 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
137 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
140 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
141 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
142 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
143 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
146 /***********************************************************************
148 * Class constructor/destructor
150 ***********************************************************************/
151 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
153 FLAC__StreamDecoder *decoder;
156 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
158 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
163 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
164 if(decoder->protected_ == 0) {
169 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
170 if(decoder->private_ == 0) {
171 free(decoder->protected_);
176 decoder->private_->input = FLAC__bitbuffer_new();
177 if(decoder->private_->input == 0) {
178 free(decoder->private_);
179 free(decoder->protected_);
184 decoder->private_->metadata_filter_ids_capacity = 16;
185 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
186 FLAC__bitbuffer_delete(decoder->private_->input);
187 free(decoder->private_);
188 free(decoder->protected_);
193 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
194 decoder->private_->output[i] = 0;
195 decoder->private_->residual[i] = 0;
198 decoder->private_->output_capacity = 0;
199 decoder->private_->output_channels = 0;
200 decoder->private_->has_seek_table = false;
202 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
203 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
205 set_defaults_(decoder);
207 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
212 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
216 FLAC__ASSERT(0 != decoder);
217 FLAC__ASSERT(0 != decoder->protected_);
218 FLAC__ASSERT(0 != decoder->private_);
219 FLAC__ASSERT(0 != decoder->private_->input);
221 FLAC__stream_decoder_finish(decoder);
223 if(0 != decoder->private_->metadata_filter_ids)
224 free(decoder->private_->metadata_filter_ids);
226 FLAC__bitbuffer_delete(decoder->private_->input);
228 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
229 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
231 free(decoder->private_);
232 free(decoder->protected_);
236 /***********************************************************************
238 * Public class methods
240 ***********************************************************************/
242 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
244 FLAC__ASSERT(0 != decoder);
246 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
247 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
249 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
250 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
252 if(!FLAC__bitbuffer_init(decoder->private_->input))
253 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
255 decoder->private_->last_frame_number = 0;
256 decoder->private_->samples_decoded = 0;
257 decoder->private_->has_stream_info = false;
258 decoder->private_->cached = false;
261 * get the CPU info and set the function pointers
263 FLAC__cpu_info(&decoder->private_->cpuinfo);
264 /* first default to the non-asm routines */
265 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
266 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
267 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
268 /* now override with asm where appropriate */
270 if(decoder->private_->cpuinfo.use_asm) {
271 #ifdef FLAC__CPU_IA32
272 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
273 #ifdef FLAC__HAS_NASM
274 if(decoder->private_->cpuinfo.data.ia32.mmx) {
275 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
276 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_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;
287 if(!FLAC__stream_decoder_reset(decoder))
288 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
290 return decoder->protected_->state;
293 FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
296 FLAC__ASSERT(0 != decoder);
297 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
299 if(0 != decoder->private_->seek_table.data.seek_table.points) {
300 free(decoder->private_->seek_table.data.seek_table.points);
301 decoder->private_->seek_table.data.seek_table.points = 0;
302 decoder->private_->has_seek_table = false;
304 FLAC__bitbuffer_free(decoder->private_->input);
305 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
307 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
308 * output arrays have a buffer of up to 3 zeroes in front
309 * (at negative indices) for alignment purposes; we use 4
310 * to keep the data well-aligned.
312 if(0 != decoder->private_->output[i]) {
313 free(decoder->private_->output[i]-4);
314 decoder->private_->output[i] = 0;
316 if(0 != decoder->private_->residual[i]) {
317 free(decoder->private_->residual[i]);
318 decoder->private_->residual[i] = 0;
321 decoder->private_->output_capacity = 0;
322 decoder->private_->output_channels = 0;
324 set_defaults_(decoder);
326 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
329 FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
331 FLAC__ASSERT(0 != decoder);
332 FLAC__ASSERT(0 != decoder->private_);
333 FLAC__ASSERT(0 != decoder->protected_);
334 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
336 decoder->private_->read_callback = value;
340 FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
342 FLAC__ASSERT(0 != decoder);
343 FLAC__ASSERT(0 != decoder->private_);
344 FLAC__ASSERT(0 != decoder->protected_);
345 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
347 decoder->private_->write_callback = value;
351 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
353 FLAC__ASSERT(0 != decoder);
354 FLAC__ASSERT(0 != decoder->private_);
355 FLAC__ASSERT(0 != decoder->protected_);
356 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
358 decoder->private_->metadata_callback = value;
362 FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
364 FLAC__ASSERT(0 != decoder);
365 FLAC__ASSERT(0 != decoder->private_);
366 FLAC__ASSERT(0 != decoder->protected_);
367 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
369 decoder->private_->error_callback = value;
373 FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
375 FLAC__ASSERT(0 != decoder);
376 FLAC__ASSERT(0 != decoder->private_);
377 FLAC__ASSERT(0 != decoder->protected_);
378 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
380 decoder->private_->client_data = value;
384 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
386 FLAC__ASSERT(0 != decoder);
387 FLAC__ASSERT(0 != decoder->private_);
388 FLAC__ASSERT(0 != decoder->protected_);
389 FLAC__ASSERT(type < FLAC__METADATA_TYPE_UNDEFINED);
390 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
392 decoder->private_->metadata_filter[type] = true;
393 if(type == FLAC__METADATA_TYPE_APPLICATION)
394 decoder->private_->metadata_filter_ids_count = 0;
398 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
400 FLAC__ASSERT(0 != decoder);
401 FLAC__ASSERT(0 != decoder->private_);
402 FLAC__ASSERT(0 != decoder->protected_);
403 FLAC__ASSERT(0 != id);
404 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
407 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
410 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
412 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
413 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
414 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
415 decoder->private_->metadata_filter_ids_capacity *= 2;
418 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));
419 decoder->private_->metadata_filter_ids_count++;
424 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
427 FLAC__ASSERT(0 != decoder);
428 FLAC__ASSERT(0 != decoder->private_);
429 FLAC__ASSERT(0 != decoder->protected_);
430 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
432 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
433 decoder->private_->metadata_filter[i] = true;
434 decoder->private_->metadata_filter_ids_count = 0;
438 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
440 FLAC__ASSERT(0 != decoder);
441 FLAC__ASSERT(0 != decoder->private_);
442 FLAC__ASSERT(0 != decoder->protected_);
443 FLAC__ASSERT(type < FLAC__METADATA_TYPE_UNDEFINED);
444 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
446 decoder->private_->metadata_filter[type] = false;
447 if(type == FLAC__METADATA_TYPE_APPLICATION)
448 decoder->private_->metadata_filter_ids_count = 0;
452 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
454 FLAC__ASSERT(0 != decoder);
455 FLAC__ASSERT(0 != decoder->private_);
456 FLAC__ASSERT(0 != decoder->protected_);
457 FLAC__ASSERT(0 != id);
458 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
461 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
464 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
466 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
467 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
468 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
469 decoder->private_->metadata_filter_ids_capacity *= 2;
472 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));
473 decoder->private_->metadata_filter_ids_count++;
478 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
480 FLAC__ASSERT(0 != decoder);
481 FLAC__ASSERT(0 != decoder->private_);
482 FLAC__ASSERT(0 != decoder->protected_);
483 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
485 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
486 decoder->private_->metadata_filter_ids_count = 0;
490 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
492 FLAC__ASSERT(0 != decoder);
493 FLAC__ASSERT(0 != decoder->protected_);
494 return decoder->protected_->state;
497 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
499 FLAC__ASSERT(0 != decoder);
500 FLAC__ASSERT(0 != decoder->protected_);
501 return decoder->protected_->channels;
504 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
506 FLAC__ASSERT(0 != decoder);
507 FLAC__ASSERT(0 != decoder->protected_);
508 return decoder->protected_->channel_assignment;
511 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
513 FLAC__ASSERT(0 != decoder);
514 FLAC__ASSERT(0 != decoder->protected_);
515 return decoder->protected_->bits_per_sample;
518 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
520 FLAC__ASSERT(0 != decoder);
521 FLAC__ASSERT(0 != decoder->protected_);
522 return decoder->protected_->sample_rate;
525 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
527 FLAC__ASSERT(0 != decoder);
528 FLAC__ASSERT(0 != decoder->protected_);
529 return decoder->protected_->blocksize;
532 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
534 FLAC__ASSERT(0 != decoder);
535 FLAC__ASSERT(0 != decoder->private_);
536 FLAC__ASSERT(0 != decoder->protected_);
538 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
539 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
542 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
547 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
549 FLAC__ASSERT(0 != decoder);
550 FLAC__ASSERT(0 != decoder->private_);
551 FLAC__ASSERT(0 != decoder->protected_);
553 if(!FLAC__stream_decoder_flush(decoder)) {
554 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
557 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
559 decoder->private_->samples_decoded = 0;
564 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
566 FLAC__bool got_a_frame;
567 FLAC__ASSERT(0 != decoder);
568 FLAC__ASSERT(0 != decoder->protected_);
571 switch(decoder->protected_->state) {
572 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
573 if(!find_metadata_(decoder))
574 return false; /* above function sets the status for us */
576 case FLAC__STREAM_DECODER_READ_METADATA:
577 if(!read_metadata_(decoder))
578 return false; /* above function sets the status for us */
581 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
582 if(!frame_sync_(decoder))
583 return true; /* above function sets the status for us */
585 case FLAC__STREAM_DECODER_READ_FRAME:
586 if(!read_frame_(decoder, &got_a_frame))
587 return false; /* above function sets the status for us */
589 return true; /* above function sets the status for us */
591 case FLAC__STREAM_DECODER_END_OF_STREAM:
592 case FLAC__STREAM_DECODER_ABORTED:
601 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
603 FLAC__ASSERT(0 != decoder);
604 FLAC__ASSERT(0 != decoder->protected_);
607 switch(decoder->protected_->state) {
608 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
609 if(!find_metadata_(decoder))
610 return false; /* above function sets the status for us */
612 case FLAC__STREAM_DECODER_READ_METADATA:
613 if(!read_metadata_(decoder))
614 return false; /* above function sets the status for us */
616 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
617 case FLAC__STREAM_DECODER_READ_FRAME:
618 case FLAC__STREAM_DECODER_END_OF_STREAM:
619 case FLAC__STREAM_DECODER_ABORTED:
628 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
631 FLAC__ASSERT(0 != decoder);
632 FLAC__ASSERT(0 != decoder->protected_);
635 switch(decoder->protected_->state) {
636 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
637 if(!find_metadata_(decoder))
638 return false; /* above function sets the status for us */
640 case FLAC__STREAM_DECODER_READ_METADATA:
641 if(!read_metadata_(decoder))
642 return false; /* above function sets the status for us */
644 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
645 if(!frame_sync_(decoder))
646 return true; /* above function sets the status for us */
648 case FLAC__STREAM_DECODER_READ_FRAME:
649 if(!read_frame_(decoder, &dummy))
650 return false; /* above function sets the status for us */
652 case FLAC__STREAM_DECODER_END_OF_STREAM:
653 case FLAC__STREAM_DECODER_ABORTED:
662 /***********************************************************************
664 * Protected class methods
666 ***********************************************************************/
668 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
670 FLAC__ASSERT(0 != decoder);
671 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
674 /***********************************************************************
676 * Private class methods
678 ***********************************************************************/
680 void set_defaults_(FLAC__StreamDecoder *decoder)
682 decoder->private_->read_callback = 0;
683 decoder->private_->write_callback = 0;
684 decoder->private_->metadata_callback = 0;
685 decoder->private_->error_callback = 0;
686 decoder->private_->client_data = 0;
688 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
689 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
690 decoder->private_->metadata_filter_ids_count = 0;
693 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
698 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
701 /* simply using realloc() is not practical because the number of channels may change mid-stream */
703 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
704 if(0 != decoder->private_->output[i]) {
705 free(decoder->private_->output[i]-4);
706 decoder->private_->output[i] = 0;
708 if(0 != decoder->private_->residual[i]) {
709 free(decoder->private_->residual[i]);
710 decoder->private_->residual[i] = 0;
714 for(i = 0; i < channels; i++) {
716 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
717 * output arrays have a buffer of up to 3 zeroes in front
718 * (at negative indices) for alignment purposes; we use 4
719 * to keep the data well-aligned.
721 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
723 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
726 memset(tmp, 0, sizeof(FLAC__int32)*4);
727 decoder->private_->output[i] = tmp + 4;
729 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
731 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
734 decoder->private_->residual[i] = tmp;
737 decoder->private_->output_capacity = size;
738 decoder->private_->output_channels = channels;
743 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
747 FLAC__ASSERT(0 != decoder);
748 FLAC__ASSERT(0 != decoder->private_);
750 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
751 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
757 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
761 FLAC__bool first = true;
763 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
765 for(i = id = 0; i < 4; ) {
766 if(decoder->private_->cached) {
767 x = (FLAC__uint32)decoder->private_->lookahead;
768 decoder->private_->cached = false;
771 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
772 return false; /* the read_callback_ sets the state for us */
774 if(x == FLAC__STREAM_SYNC_STRING[i]) {
780 if(x == ID3V2_TAG_[id]) {
784 if(!skip_id3v2_tag_(decoder))
785 return false; /* the read_callback_ sets the state for us */
789 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
790 decoder->private_->header_warmup[0] = (FLAC__byte)x;
791 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
792 return false; /* the read_callback_ sets the state for us */
794 /* 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 */
795 /* else we have to check if the second byte is the end of a sync code */
796 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
797 decoder->private_->lookahead = (FLAC__byte)x;
798 decoder->private_->cached = true;
800 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
801 decoder->private_->header_warmup[1] = (FLAC__byte)x;
802 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
808 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
813 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
817 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
820 FLAC__uint32 i, x, type, length;
822 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
824 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
825 return false; /* the read_callback_ sets the state for us */
826 is_last = x? true : false;
828 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
829 return false; /* the read_callback_ sets the state for us */
831 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
832 return false; /* the read_callback_ sets the state for us */
834 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
835 if(!read_metadata_streaminfo_(decoder, is_last, length))
838 decoder->private_->has_stream_info = true;
839 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
840 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
842 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
843 if(!read_metadata_seektable_(decoder, is_last, length))
846 decoder->private_->has_seek_table = true;
847 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
848 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
851 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
852 unsigned real_length = length;
853 FLAC__StreamMetadata block;
855 block.is_last = is_last;
856 block.type = (FLAC__MetadataType)type;
857 block.length = length;
859 if(type == FLAC__METADATA_TYPE_APPLICATION) {
860 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))
861 return false; /* the read_callback_ sets the state for us */
863 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
865 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
870 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
871 return false; /* the read_callback_ sets the state for us */
875 case FLAC__METADATA_TYPE_PADDING:
876 /* skip the padding bytes */
877 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
878 return false; /* the read_callback_ sets the state for us */
880 case FLAC__METADATA_TYPE_APPLICATION:
881 /* remember, we read the ID already */
882 if(real_length > 0) {
883 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
884 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
887 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
888 return false; /* the read_callback_ sets the state for us */
891 block.data.application.data = 0;
893 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
894 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
897 case FLAC__METADATA_TYPE_CUESHEET:
898 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
901 case FLAC__METADATA_TYPE_STREAMINFO:
902 case FLAC__METADATA_TYPE_SEEKTABLE:
906 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
908 /* now we have to free any malloc'ed data in the block */
910 case FLAC__METADATA_TYPE_PADDING:
912 case FLAC__METADATA_TYPE_APPLICATION:
913 if(0 != block.data.application.data)
914 free(block.data.application.data);
916 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
917 if(0 != block.data.vorbis_comment.vendor_string.entry)
918 free(block.data.vorbis_comment.vendor_string.entry);
919 if(block.data.vorbis_comment.num_comments > 0)
920 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
921 if(0 != block.data.vorbis_comment.comments[i].entry)
922 free(block.data.vorbis_comment.comments[i].entry);
923 if(0 != block.data.vorbis_comment.comments)
924 free(block.data.vorbis_comment.comments);
926 case FLAC__METADATA_TYPE_CUESHEET:
927 if(block.data.cue_sheet.num_tracks > 0)
928 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
929 if(0 != block.data.cue_sheet.tracks[i].indices)
930 free(block.data.cue_sheet.tracks[i].indices);
931 if(0 != block.data.cue_sheet.tracks)
932 free(block.data.cue_sheet.tracks);
934 case FLAC__METADATA_TYPE_STREAMINFO:
935 case FLAC__METADATA_TYPE_SEEKTABLE:
943 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
948 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
951 unsigned bits, used_bits = 0;
953 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
955 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
956 decoder->private_->stream_info.is_last = is_last;
957 decoder->private_->stream_info.length = length;
959 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
960 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
961 return false; /* the read_callback_ sets the state for us */
962 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
965 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
966 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
967 return false; /* the read_callback_ sets the state for us */
968 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
971 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
972 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
973 return false; /* the read_callback_ sets the state for us */
974 decoder->private_->stream_info.data.stream_info.min_framesize = x;
977 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
978 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
979 return false; /* the read_callback_ sets the state for us */
980 decoder->private_->stream_info.data.stream_info.max_framesize = x;
983 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
984 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
985 return false; /* the read_callback_ sets the state for us */
986 decoder->private_->stream_info.data.stream_info.sample_rate = x;
989 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
990 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
991 return false; /* the read_callback_ sets the state for us */
992 decoder->private_->stream_info.data.stream_info.channels = x+1;
995 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
996 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
997 return false; /* the read_callback_ sets the state for us */
998 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1001 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1002 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))
1003 return false; /* the read_callback_ sets the state for us */
1006 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1007 return false; /* the read_callback_ sets the state for us */
1010 /* skip the rest of the block */
1011 FLAC__ASSERT(used_bits % 8 == 0);
1012 length -= (used_bits / 8);
1013 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1014 return false; /* the read_callback_ sets the state for us */
1019 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1024 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1026 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1027 decoder->private_->seek_table.is_last = is_last;
1028 decoder->private_->seek_table.length = length;
1030 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1032 /* use realloc since we may pass through here several times (e.g. after seeking) */
1033 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)))) {
1034 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1037 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1038 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1039 return false; /* the read_callback_ sets the state for us */
1040 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1042 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_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].stream_offset = xx;
1046 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_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].frame_samples = x;
1050 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1051 /* if there is a partial point left, skip over it */
1053 /*@@@ do an error_callback() here? there's an argument for either way */
1054 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1055 return false; /* the read_callback_ sets the state for us */
1061 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1065 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1067 /* read vendor string */
1068 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1069 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1070 return false; /* the read_callback_ sets the state for us */
1071 if(obj->vendor_string.length > 0) {
1072 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length))) {
1073 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1076 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1077 return false; /* the read_callback_ sets the state for us */
1080 obj->vendor_string.entry = 0;
1082 /* read num comments */
1083 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1084 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1085 return false; /* the read_callback_ sets the state for us */
1088 if(obj->num_comments > 0) {
1089 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1090 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1093 for(i = 0; i < obj->num_comments; i++) {
1094 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1095 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1096 return false; /* the read_callback_ sets the state for us */
1097 if(obj->comments[i].length > 0) {
1098 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length))) {
1099 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1102 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1103 return false; /* the read_callback_ sets the state for us */
1106 obj->comments[i].entry = 0;
1116 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1118 FLAC__uint32 i, j, x;
1120 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1122 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1124 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1125 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))
1126 return false; /* the read_callback_ sets the state for us */
1128 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1129 return false; /* the read_callback_ sets the state for us */
1131 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1132 return false; /* the read_callback_ sets the state for us */
1134 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1135 return false; /* the read_callback_ sets the state for us */
1136 obj->num_tracks = x;
1138 if(obj->num_tracks > 0) {
1139 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1140 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1143 for(i = 0; i < obj->num_tracks; i++) {
1144 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1145 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1146 return false; /* the read_callback_ sets the state for us */
1148 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1149 return false; /* the read_callback_ sets the state for us */
1150 track->number = (FLAC__byte)x;
1152 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1153 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))
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_TRACK_TYPE_LEN, read_callback_, decoder))
1157 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_PRE_EMPHASIS_LEN, read_callback_, decoder))
1161 return false; /* the read_callback_ sets the state for us */
1162 track->pre_emphasis = x;
1164 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1165 return false; /* the read_callback_ sets the state for us */
1167 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1168 return false; /* the read_callback_ sets the state for us */
1169 track->num_indices = (FLAC__byte)x;
1171 if(track->num_indices > 0) {
1172 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1173 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1176 for(j = 0; j < track->num_indices; j++) {
1177 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1178 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1179 return false; /* the read_callback_ sets the state for us */
1181 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1182 return false; /* the read_callback_ sets the state for us */
1183 index->number = (FLAC__byte)x;
1185 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1186 return false; /* the read_callback_ sets the state for us */
1195 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1200 /* skip the version and flags bytes */
1201 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1202 return false; /* the read_callback_ sets the state for us */
1203 /* get the size (in bytes) to skip */
1205 for(i = 0; i < 4; i++) {
1206 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1207 return false; /* the read_callback_ sets the state for us */
1211 /* skip the rest of the tag */
1212 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1213 return false; /* the read_callback_ sets the state for us */
1217 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1220 FLAC__bool first = true;
1222 /* If we know the total number of samples in the stream, stop if we've read that many. */
1223 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1224 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1225 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1226 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1231 /* make sure we're byte aligned */
1232 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1233 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1234 return false; /* the read_callback_ sets the state for us */
1238 if(decoder->private_->cached) {
1239 x = (FLAC__uint32)decoder->private_->lookahead;
1240 decoder->private_->cached = false;
1243 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1244 return false; /* the read_callback_ sets the state for us */
1246 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1247 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1248 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1249 return false; /* the read_callback_ sets the state for us */
1251 /* 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 */
1252 /* else we have to check if the second byte is the end of a sync code */
1253 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1254 decoder->private_->lookahead = (FLAC__byte)x;
1255 decoder->private_->cached = true;
1257 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1258 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1259 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1264 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1272 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1276 FLAC__int32 mid, side, left, right;
1277 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1280 *got_a_frame = false;
1284 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1285 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1286 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1288 if(!read_frame_header_(decoder))
1290 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1292 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1294 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1296 * first figure the correct bits-per-sample of the subframe
1298 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1299 switch(decoder->private_->frame.header.channel_assignment) {
1300 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1301 /* no adjustment needed */
1303 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1304 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1308 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1309 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1313 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1314 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1324 if(!read_subframe_(decoder, channel, bps))
1326 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1327 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1331 if(!read_zero_padding_(decoder))
1335 * Read the frame CRC-16 from the footer and check
1337 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1338 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1339 return false; /* the read_callback_ sets the state for us */
1340 if(frame_crc == (FLAC__uint16)x) {
1341 /* Undo any special channel coding */
1342 switch(decoder->private_->frame.header.channel_assignment) {
1343 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1346 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1347 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1348 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1349 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1351 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1352 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1353 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1354 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1356 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1357 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1358 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1359 mid = decoder->private_->output[0][i];
1360 side = decoder->private_->output[1][i];
1362 if(side & 1) /* i.e. if 'side' is odd... */
1366 decoder->private_->output[0][i] = left >> 1;
1367 decoder->private_->output[1][i] = right >> 1;
1376 /* Bad frame, emit error and zero the output signal */
1377 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1378 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1379 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1383 *got_a_frame = true;
1385 /* put the latest values into the public section of the decoder instance */
1386 decoder->protected_->channels = decoder->private_->frame.header.channels;
1387 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1388 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1389 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1390 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1392 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1393 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1396 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)
1399 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1403 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1407 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1408 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1409 unsigned raw_header_len;
1410 FLAC__bool is_unparseable = false;
1411 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);
1412 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);
1414 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1416 /* init the raw header with the saved bits from synchronization */
1417 raw_header[0] = decoder->private_->header_warmup[0];
1418 raw_header[1] = decoder->private_->header_warmup[1];
1422 * check to make sure that the reserved bits are 0
1424 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1425 is_unparseable = true;
1429 * Note that along the way as we read the header, we look for a sync
1430 * code inside. If we find one it would indicate that our original
1431 * sync was bad since there cannot be a sync code in a valid header.
1435 * read in the raw header as bytes so we can CRC it, and parse it on the way
1437 for(i = 0; i < 2; i++) {
1438 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1439 return false; /* the read_callback_ sets the state for us */
1440 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1441 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1442 decoder->private_->lookahead = (FLAC__byte)x;
1443 decoder->private_->cached = true;
1444 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1445 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1448 raw_header[raw_header_len++] = (FLAC__byte)x;
1451 switch(x = raw_header[2] >> 4) {
1453 if(is_known_fixed_blocksize_stream)
1454 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1456 is_unparseable = true;
1459 decoder->private_->frame.header.blocksize = 192;
1465 decoder->private_->frame.header.blocksize = 576 << (x-2);
1479 decoder->private_->frame.header.blocksize = 256 << (x-8);
1486 switch(x = raw_header[2] & 0x0f) {
1488 if(decoder->private_->has_stream_info)
1489 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1491 is_unparseable = true;
1496 is_unparseable = true;
1499 decoder->private_->frame.header.sample_rate = 8000;
1502 decoder->private_->frame.header.sample_rate = 16000;
1505 decoder->private_->frame.header.sample_rate = 22050;
1508 decoder->private_->frame.header.sample_rate = 24000;
1511 decoder->private_->frame.header.sample_rate = 32000;
1514 decoder->private_->frame.header.sample_rate = 44100;
1517 decoder->private_->frame.header.sample_rate = 48000;
1520 decoder->private_->frame.header.sample_rate = 96000;
1525 sample_rate_hint = x;
1528 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1529 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1535 x = (unsigned)(raw_header[3] >> 4);
1537 decoder->private_->frame.header.channels = 2;
1540 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1543 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1546 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1549 is_unparseable = true;
1554 decoder->private_->frame.header.channels = (unsigned)x + 1;
1555 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1558 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1560 if(decoder->private_->has_stream_info)
1561 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1563 is_unparseable = true;
1566 decoder->private_->frame.header.bits_per_sample = 8;
1569 decoder->private_->frame.header.bits_per_sample = 12;
1572 decoder->private_->frame.header.bits_per_sample = 16;
1575 decoder->private_->frame.header.bits_per_sample = 20;
1578 decoder->private_->frame.header.bits_per_sample = 24;
1582 is_unparseable = true;
1589 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1590 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1591 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1595 if(blocksize_hint && is_known_variable_blocksize_stream) {
1596 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1597 return false; /* the read_callback_ sets the state for us */
1598 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1599 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1600 decoder->private_->cached = true;
1601 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1602 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1605 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1606 decoder->private_->frame.header.number.sample_number = xx;
1609 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1610 return false; /* the read_callback_ sets the state for us */
1611 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1612 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1613 decoder->private_->cached = true;
1614 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1615 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1618 decoder->private_->last_frame_number = x;
1619 if(decoder->private_->has_stream_info) {
1620 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1621 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1624 is_unparseable = true;
1628 if(blocksize_hint) {
1629 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1630 return false; /* the read_callback_ sets the state for us */
1631 raw_header[raw_header_len++] = (FLAC__byte)x;
1632 if(blocksize_hint == 7) {
1634 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1635 return false; /* the read_callback_ sets the state for us */
1636 raw_header[raw_header_len++] = (FLAC__byte)_x;
1639 decoder->private_->frame.header.blocksize = x+1;
1642 if(sample_rate_hint) {
1643 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1644 return false; /* the read_callback_ sets the state for us */
1645 raw_header[raw_header_len++] = (FLAC__byte)x;
1646 if(sample_rate_hint != 12) {
1648 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1649 return false; /* the read_callback_ sets the state for us */
1650 raw_header[raw_header_len++] = (FLAC__byte)_x;
1653 if(sample_rate_hint == 12)
1654 decoder->private_->frame.header.sample_rate = x*1000;
1655 else if(sample_rate_hint == 13)
1656 decoder->private_->frame.header.sample_rate = x;
1658 decoder->private_->frame.header.sample_rate = x*10;
1661 /* read the CRC-8 byte */
1662 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1663 return false; /* the read_callback_ sets the state for us */
1664 crc8 = (FLAC__byte)x;
1666 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1667 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1668 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1672 if(is_unparseable) {
1673 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1680 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1683 FLAC__bool wasted_bits;
1685 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1686 return false; /* the read_callback_ sets the state for us */
1688 wasted_bits = (x & 1);
1693 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1694 return false; /* the read_callback_ sets the state for us */
1695 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1696 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1699 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1702 * Lots of magic numbers here
1705 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1706 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1710 if(!read_subframe_constant_(decoder, channel, bps))
1714 if(!read_subframe_verbatim_(decoder, channel, bps))
1718 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1722 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1726 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1730 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1736 x = decoder->private_->frame.subframes[channel].wasted_bits;
1737 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1738 decoder->private_->output[channel][i] <<= x;
1744 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1746 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1749 FLAC__int32 *output = decoder->private_->output[channel];
1751 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1753 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1754 return false; /* the read_callback_ sets the state for us */
1756 subframe->value = x;
1758 /* decode the subframe */
1759 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1765 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1767 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1772 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1774 subframe->residual = decoder->private_->residual[channel];
1775 subframe->order = order;
1777 /* read warm-up samples */
1778 for(u = 0; u < order; u++) {
1779 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1780 return false; /* the read_callback_ sets the state for us */
1781 subframe->warmup[u] = i32;
1784 /* read entropy coding method info */
1785 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1786 return false; /* the read_callback_ sets the state for us */
1787 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1788 switch(subframe->entropy_coding_method.type) {
1789 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1790 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1791 return false; /* the read_callback_ sets the state for us */
1792 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1793 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1796 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1801 switch(subframe->entropy_coding_method.type) {
1802 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1803 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]))
1810 /* decode the subframe */
1811 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1812 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1817 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1819 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1824 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1826 subframe->residual = decoder->private_->residual[channel];
1827 subframe->order = order;
1829 /* read warm-up samples */
1830 for(u = 0; u < order; u++) {
1831 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1832 return false; /* the read_callback_ sets the state for us */
1833 subframe->warmup[u] = i32;
1836 /* read qlp coeff precision */
1837 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1838 return false; /* the read_callback_ sets the state for us */
1839 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1840 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1841 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1844 subframe->qlp_coeff_precision = u32+1;
1846 /* read qlp shift */
1847 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1848 return false; /* the read_callback_ sets the state for us */
1849 subframe->quantization_level = i32;
1851 /* read quantized lp coefficiencts */
1852 for(u = 0; u < order; u++) {
1853 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1854 return false; /* the read_callback_ sets the state for us */
1855 subframe->qlp_coeff[u] = i32;
1858 /* read entropy coding method info */
1859 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1860 return false; /* the read_callback_ sets the state for us */
1861 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1862 switch(subframe->entropy_coding_method.type) {
1863 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1864 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1865 return false; /* the read_callback_ sets the state for us */
1866 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1867 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1870 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1875 switch(subframe->entropy_coding_method.type) {
1876 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1877 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]))
1884 /* decode the subframe */
1885 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1886 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1887 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1888 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);
1890 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);
1892 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);
1897 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1899 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1900 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1903 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1905 subframe->data = residual;
1907 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1908 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1909 return false; /* the read_callback_ sets the state for us */
1913 /* decode the subframe */
1914 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1919 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1921 FLAC__uint32 rice_parameter;
1923 unsigned partition, sample, u;
1924 const unsigned partitions = 1u << partition_order;
1925 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1927 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1928 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1933 for(partition = 0; partition < partitions; partition++) {
1934 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1935 return false; /* the read_callback_ sets the state for us */
1936 partitioned_rice_contents->parameters[partition] = rice_parameter;
1937 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1938 #ifdef FLAC__SYMMETRIC_RICE
1939 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1940 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1941 return false; /* the read_callback_ sets the state for us */
1942 residual[sample] = i;
1945 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1946 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1947 return false; /* the read_callback_ sets the state for us */
1952 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1953 return false; /* the read_callback_ sets the state for us */
1954 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1955 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1956 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1957 return false; /* the read_callback_ sets the state for us */
1958 residual[sample] = i;
1966 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1968 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1969 FLAC__uint32 zero = 0;
1970 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1971 return false; /* the read_callback_ sets the state for us */
1973 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1974 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1980 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1982 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1983 FLAC__StreamDecoderReadStatus status;
1985 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1986 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1987 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1988 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1989 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1990 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;