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/cpu.h"
27 #include "private/crc.h"
28 #include "private/fixed.h"
29 #include "private/format.h"
30 #include "private/lpc.h"
35 #define max(a,b) ((a)>(b)?(a):(b))
37 /***********************************************************************
41 ***********************************************************************/
43 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
45 /***********************************************************************
47 * Private class method prototypes
49 ***********************************************************************/
51 static void set_defaults_(FLAC__StreamDecoder *decoder);
52 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
53 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
54 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
55 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
56 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
57 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
58 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
59 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
60 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
61 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
62 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
63 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
64 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
65 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);
66 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
67 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
69 /***********************************************************************
73 ***********************************************************************/
75 typedef struct FLAC__StreamDecoderPrivate {
76 FLAC__StreamDecoderReadCallback read_callback;
77 FLAC__StreamDecoderWriteCallback write_callback;
78 FLAC__StreamDecoderMetadataCallback metadata_callback;
79 FLAC__StreamDecoderErrorCallback error_callback;
80 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[]);
81 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[]);
82 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[]);
84 FLAC__BitBuffer *input;
85 FLAC__int32 *output[FLAC__MAX_CHANNELS];
86 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
87 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
88 unsigned output_capacity, output_channels;
89 FLAC__uint32 last_frame_number;
90 FLAC__uint64 samples_decoded;
91 FLAC__bool has_stream_info, has_seek_table;
92 FLAC__StreamMetadata stream_info;
93 FLAC__StreamMetadata seek_table;
94 FLAC__bool metadata_filter[FLAC__METADATA_TYPE_VORBIS_COMMENT+1];
95 FLAC__byte *metadata_filter_ids;
96 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
98 FLAC__bool cached; /* true if there is a byte in lookahead */
99 FLAC__CPUInfo cpuinfo;
100 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
101 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
102 } FLAC__StreamDecoderPrivate;
104 /***********************************************************************
106 * Public static class data
108 ***********************************************************************/
110 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
111 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
112 "FLAC__STREAM_DECODER_READ_METADATA",
113 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
114 "FLAC__STREAM_DECODER_READ_FRAME",
115 "FLAC__STREAM_DECODER_END_OF_STREAM",
116 "FLAC__STREAM_DECODER_ABORTED",
117 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
118 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
119 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
120 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
121 "FLAC__STREAM_DECODER_UNINITIALIZED"
124 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
125 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
126 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
127 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
130 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
131 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
132 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
135 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
136 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
137 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
138 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
141 /***********************************************************************
143 * Class constructor/destructor
145 ***********************************************************************/
146 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
148 FLAC__StreamDecoder *decoder;
151 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
153 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
158 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
159 if(decoder->protected_ == 0) {
164 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
165 if(decoder->private_ == 0) {
166 free(decoder->protected_);
171 decoder->private_->input = FLAC__bitbuffer_new();
172 if(decoder->private_->input == 0) {
173 free(decoder->private_);
174 free(decoder->protected_);
179 decoder->private_->metadata_filter_ids_capacity = 16;
180 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
181 FLAC__bitbuffer_delete(decoder->private_->input);
182 free(decoder->private_);
183 free(decoder->protected_);
188 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
189 decoder->private_->output[i] = 0;
190 decoder->private_->residual[i] = 0;
193 decoder->private_->output_capacity = 0;
194 decoder->private_->output_channels = 0;
195 decoder->private_->has_seek_table = false;
197 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
198 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
200 set_defaults_(decoder);
202 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
207 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
211 FLAC__ASSERT(0 != decoder);
212 FLAC__ASSERT(0 != decoder->protected_);
213 FLAC__ASSERT(0 != decoder->private_);
214 FLAC__ASSERT(0 != decoder->private_->input);
216 FLAC__stream_decoder_finish(decoder);
218 if(0 != decoder->private_->metadata_filter_ids)
219 free(decoder->private_->metadata_filter_ids);
221 FLAC__bitbuffer_delete(decoder->private_->input);
223 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
224 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
226 free(decoder->private_);
227 free(decoder->protected_);
231 /***********************************************************************
233 * Public class methods
235 ***********************************************************************/
237 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
239 FLAC__ASSERT(0 != decoder);
241 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
242 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
244 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
245 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
247 if(!FLAC__bitbuffer_init(decoder->private_->input))
248 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
250 decoder->private_->last_frame_number = 0;
251 decoder->private_->samples_decoded = 0;
252 decoder->private_->has_stream_info = false;
253 decoder->private_->cached = false;
256 * get the CPU info and set the function pointers
258 FLAC__cpu_info(&decoder->private_->cpuinfo);
259 /* first default to the non-asm routines */
260 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
261 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
262 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
263 /* now override with asm where appropriate */
265 if(decoder->private_->cpuinfo.use_asm) {
266 #ifdef FLAC__CPU_IA32
267 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
268 #ifdef FLAC__HAS_NASM
269 if(decoder->private_->cpuinfo.data.ia32.mmx) {
270 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
271 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
274 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
275 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
282 if(!FLAC__stream_decoder_reset(decoder))
283 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
285 return decoder->protected_->state;
288 FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
291 FLAC__ASSERT(0 != decoder);
292 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
294 if(decoder->private_->has_seek_table) {
295 FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
296 free(decoder->private_->seek_table.data.seek_table.points);
297 decoder->private_->seek_table.data.seek_table.points = 0;
298 decoder->private_->has_seek_table = false;
300 FLAC__bitbuffer_free(decoder->private_->input);
301 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
303 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
304 * output arrays have a buffer of up to 3 zeroes in front
305 * (at negative indices) for alignment purposes; we use 4
306 * to keep the data well-aligned.
308 if(0 != decoder->private_->output[i]) {
309 free(decoder->private_->output[i]-4);
310 decoder->private_->output[i] = 0;
312 if(0 != decoder->private_->residual[i]) {
313 free(decoder->private_->residual[i]);
314 decoder->private_->residual[i] = 0;
317 decoder->private_->output_capacity = 0;
318 decoder->private_->output_channels = 0;
320 set_defaults_(decoder);
322 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
325 FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
327 FLAC__ASSERT(0 != decoder);
328 FLAC__ASSERT(0 != decoder->private_);
329 FLAC__ASSERT(0 != decoder->protected_);
330 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
332 decoder->private_->read_callback = value;
336 FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
338 FLAC__ASSERT(0 != decoder);
339 FLAC__ASSERT(0 != decoder->private_);
340 FLAC__ASSERT(0 != decoder->protected_);
341 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
343 decoder->private_->write_callback = value;
347 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
349 FLAC__ASSERT(0 != decoder);
350 FLAC__ASSERT(0 != decoder->private_);
351 FLAC__ASSERT(0 != decoder->protected_);
352 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
354 decoder->private_->metadata_callback = value;
358 FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
360 FLAC__ASSERT(0 != decoder);
361 FLAC__ASSERT(0 != decoder->private_);
362 FLAC__ASSERT(0 != decoder->protected_);
363 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
365 decoder->private_->error_callback = value;
369 FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
371 FLAC__ASSERT(0 != decoder);
372 FLAC__ASSERT(0 != decoder->private_);
373 FLAC__ASSERT(0 != decoder->protected_);
374 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
376 decoder->private_->client_data = value;
380 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
382 FLAC__ASSERT(0 != decoder);
383 FLAC__ASSERT(0 != decoder->private_);
384 FLAC__ASSERT(0 != decoder->protected_);
385 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
386 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
388 decoder->private_->metadata_filter[type] = true;
389 if(type == FLAC__METADATA_TYPE_APPLICATION)
390 decoder->private_->metadata_filter_ids_count = 0;
394 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
396 FLAC__ASSERT(0 != decoder);
397 FLAC__ASSERT(0 != decoder->private_);
398 FLAC__ASSERT(0 != decoder->protected_);
399 FLAC__ASSERT(0 != id);
400 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
403 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
406 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
408 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
409 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
410 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
411 decoder->private_->metadata_filter_ids_capacity *= 2;
414 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));
415 decoder->private_->metadata_filter_ids_count++;
420 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
423 FLAC__ASSERT(0 != decoder);
424 FLAC__ASSERT(0 != decoder->private_);
425 FLAC__ASSERT(0 != decoder->protected_);
426 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
428 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
429 decoder->private_->metadata_filter[i] = true;
430 decoder->private_->metadata_filter_ids_count = 0;
434 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
436 FLAC__ASSERT(0 != decoder);
437 FLAC__ASSERT(0 != decoder->private_);
438 FLAC__ASSERT(0 != decoder->protected_);
439 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
440 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
442 decoder->private_->metadata_filter[type] = false;
443 if(type == FLAC__METADATA_TYPE_APPLICATION)
444 decoder->private_->metadata_filter_ids_count = 0;
448 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
450 FLAC__ASSERT(0 != decoder);
451 FLAC__ASSERT(0 != decoder->private_);
452 FLAC__ASSERT(0 != decoder->protected_);
453 FLAC__ASSERT(0 != id);
454 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
457 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
460 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
462 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
463 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
464 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
465 decoder->private_->metadata_filter_ids_capacity *= 2;
468 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));
469 decoder->private_->metadata_filter_ids_count++;
474 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
476 FLAC__ASSERT(0 != decoder);
477 FLAC__ASSERT(0 != decoder->private_);
478 FLAC__ASSERT(0 != decoder->protected_);
479 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
481 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
482 decoder->private_->metadata_filter_ids_count = 0;
486 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
488 FLAC__ASSERT(0 != decoder);
489 FLAC__ASSERT(0 != decoder->protected_);
490 return decoder->protected_->state;
493 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
495 FLAC__ASSERT(0 != decoder);
496 FLAC__ASSERT(0 != decoder->protected_);
497 return decoder->protected_->channels;
500 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
502 FLAC__ASSERT(0 != decoder);
503 FLAC__ASSERT(0 != decoder->protected_);
504 return decoder->protected_->channel_assignment;
507 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
509 FLAC__ASSERT(0 != decoder);
510 FLAC__ASSERT(0 != decoder->protected_);
511 return decoder->protected_->bits_per_sample;
514 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
516 FLAC__ASSERT(0 != decoder);
517 FLAC__ASSERT(0 != decoder->protected_);
518 return decoder->protected_->sample_rate;
521 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
523 FLAC__ASSERT(0 != decoder);
524 FLAC__ASSERT(0 != decoder->protected_);
525 return decoder->protected_->blocksize;
528 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
530 FLAC__ASSERT(0 != decoder);
531 FLAC__ASSERT(0 != decoder->private_);
532 FLAC__ASSERT(0 != decoder->protected_);
534 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
535 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
538 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
543 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
545 FLAC__ASSERT(0 != decoder);
546 FLAC__ASSERT(0 != decoder->private_);
547 FLAC__ASSERT(0 != decoder->protected_);
549 if(!FLAC__stream_decoder_flush(decoder)) {
550 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
553 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
555 decoder->private_->samples_decoded = 0;
560 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
562 FLAC__bool got_a_frame;
563 FLAC__ASSERT(0 != decoder);
564 FLAC__ASSERT(0 != decoder->protected_);
567 switch(decoder->protected_->state) {
568 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
569 if(!find_metadata_(decoder))
570 return false; /* above function sets the status for us */
572 case FLAC__STREAM_DECODER_READ_METADATA:
573 if(!read_metadata_(decoder))
574 return false; /* above function sets the status for us */
577 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
578 if(!frame_sync_(decoder))
579 return true; /* above function sets the status for us */
581 case FLAC__STREAM_DECODER_READ_FRAME:
582 if(!read_frame_(decoder, &got_a_frame))
583 return false; /* above function sets the status for us */
585 return true; /* above function sets the status for us */
587 case FLAC__STREAM_DECODER_END_OF_STREAM:
588 case FLAC__STREAM_DECODER_ABORTED:
597 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
599 FLAC__ASSERT(0 != decoder);
600 FLAC__ASSERT(0 != decoder->protected_);
603 switch(decoder->protected_->state) {
604 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
605 if(!find_metadata_(decoder))
606 return false; /* above function sets the status for us */
608 case FLAC__STREAM_DECODER_READ_METADATA:
609 if(!read_metadata_(decoder))
610 return false; /* above function sets the status for us */
612 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
613 case FLAC__STREAM_DECODER_READ_FRAME:
614 case FLAC__STREAM_DECODER_END_OF_STREAM:
615 case FLAC__STREAM_DECODER_ABORTED:
624 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
627 FLAC__ASSERT(0 != decoder);
628 FLAC__ASSERT(0 != decoder->protected_);
631 switch(decoder->protected_->state) {
632 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
633 if(!find_metadata_(decoder))
634 return false; /* above function sets the status for us */
636 case FLAC__STREAM_DECODER_READ_METADATA:
637 if(!read_metadata_(decoder))
638 return false; /* above function sets the status for us */
640 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
641 if(!frame_sync_(decoder))
642 return true; /* above function sets the status for us */
644 case FLAC__STREAM_DECODER_READ_FRAME:
645 if(!read_frame_(decoder, &dummy))
646 return false; /* above function sets the status for us */
648 case FLAC__STREAM_DECODER_END_OF_STREAM:
649 case FLAC__STREAM_DECODER_ABORTED:
658 /***********************************************************************
660 * Protected class methods
662 ***********************************************************************/
664 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
666 FLAC__ASSERT(0 != decoder);
667 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
670 /***********************************************************************
672 * Private class methods
674 ***********************************************************************/
676 void set_defaults_(FLAC__StreamDecoder *decoder)
678 decoder->private_->read_callback = 0;
679 decoder->private_->write_callback = 0;
680 decoder->private_->metadata_callback = 0;
681 decoder->private_->error_callback = 0;
682 decoder->private_->client_data = 0;
684 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
685 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
686 decoder->private_->metadata_filter_ids_count = 0;
689 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
694 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
697 /* simply using realloc() is not practical because the number of channels may change mid-stream */
699 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
700 if(0 != decoder->private_->output[i]) {
701 free(decoder->private_->output[i]-4);
702 decoder->private_->output[i] = 0;
704 if(0 != decoder->private_->residual[i]) {
705 free(decoder->private_->residual[i]);
706 decoder->private_->residual[i] = 0;
710 for(i = 0; i < channels; i++) {
712 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
713 * output arrays have a buffer of up to 3 zeroes in front
714 * (at negative indices) for alignment purposes; we use 4
715 * to keep the data well-aligned.
717 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
719 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
722 memset(tmp, 0, sizeof(FLAC__int32)*4);
723 decoder->private_->output[i] = tmp + 4;
725 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
727 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
730 decoder->private_->residual[i] = tmp;
733 decoder->private_->output_capacity = size;
734 decoder->private_->output_channels = channels;
739 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
743 FLAC__ASSERT(0 != decoder);
744 FLAC__ASSERT(0 != decoder->private_);
746 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
747 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
753 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
757 FLAC__bool first = true;
759 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
761 for(i = id = 0; i < 4; ) {
762 if(decoder->private_->cached) {
763 x = (FLAC__uint32)decoder->private_->lookahead;
764 decoder->private_->cached = false;
767 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
768 return false; /* the read_callback_ sets the state for us */
770 if(x == FLAC__STREAM_SYNC_STRING[i]) {
776 if(x == ID3V2_TAG_[id]) {
780 if(!skip_id3v2_tag_(decoder))
781 return false; /* the read_callback_ sets the state for us */
785 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
786 decoder->private_->header_warmup[0] = (FLAC__byte)x;
787 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
788 return false; /* the read_callback_ sets the state for us */
790 /* 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 */
791 /* else we have to check if the second byte is the end of a sync code */
792 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
793 decoder->private_->lookahead = (FLAC__byte)x;
794 decoder->private_->cached = true;
796 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
797 decoder->private_->header_warmup[1] = (FLAC__byte)x;
798 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
804 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
809 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
813 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
815 FLAC__uint32 i, x, last_block, type, length;
818 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
820 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
821 return false; /* the read_callback_ sets the state for us */
822 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
823 return false; /* the read_callback_ sets the state for us */
824 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
825 return false; /* the read_callback_ sets the state for us */
826 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
827 unsigned used_bits = 0;
828 decoder->private_->stream_info.type = type;
829 decoder->private_->stream_info.is_last = last_block;
830 decoder->private_->stream_info.length = length;
832 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
833 return false; /* the read_callback_ sets the state for us */
834 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
835 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
837 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
838 return false; /* the read_callback_ sets the state for us */
839 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
840 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
842 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
843 return false; /* the read_callback_ sets the state for us */
844 decoder->private_->stream_info.data.stream_info.min_framesize = x;
845 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
847 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
848 return false; /* the read_callback_ sets the state for us */
849 decoder->private_->stream_info.data.stream_info.max_framesize = x;
850 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
852 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
853 return false; /* the read_callback_ sets the state for us */
854 decoder->private_->stream_info.data.stream_info.sample_rate = x;
855 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
857 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
858 return false; /* the read_callback_ sets the state for us */
859 decoder->private_->stream_info.data.stream_info.channels = x+1;
860 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
862 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
863 return false; /* the read_callback_ sets the state for us */
864 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
865 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
867 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))
868 return false; /* the read_callback_ sets the state for us */
869 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
871 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
872 return false; /* the read_callback_ sets the state for us */
875 /* skip the rest of the block */
876 FLAC__ASSERT(used_bits % 8 == 0);
877 length -= (used_bits / 8);
878 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
879 return false; /* the read_callback_ sets the state for us */
881 decoder->private_->has_stream_info = true;
882 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
883 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
885 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
886 decoder->private_->seek_table.type = type;
887 decoder->private_->seek_table.is_last = last_block;
888 decoder->private_->seek_table.length = length;
890 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
892 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
893 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
896 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
897 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
898 return false; /* the read_callback_ sets the state for us */
899 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
901 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
902 return false; /* the read_callback_ sets the state for us */
903 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
905 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
906 return false; /* the read_callback_ sets the state for us */
907 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
909 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
910 /* if there is a partial point left, skip over it */
912 /*@@@ do an error_callback() here? there's an argument for either way */
913 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
914 return false; /* the read_callback_ sets the state for us */
917 decoder->private_->has_seek_table = true;
918 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
919 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
922 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
923 unsigned real_length = length;
924 FLAC__StreamMetadata block;
926 block.is_last = last_block;
928 block.length = length;
930 if(type == FLAC__METADATA_TYPE_APPLICATION) {
931 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
932 return false; /* the read_callback_ sets the state for us */
934 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
936 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
941 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
942 return false; /* the read_callback_ sets the state for us */
946 case FLAC__METADATA_TYPE_PADDING:
947 /* skip the padding bytes */
948 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
949 return false; /* the read_callback_ sets the state for us */
951 case FLAC__METADATA_TYPE_APPLICATION:
952 /* remember, we read the ID already */
953 if(real_length > 0) {
954 if(0 == (block.data.application.data = malloc(real_length))) {
955 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
958 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
959 return false; /* the read_callback_ sets the state for us */
962 block.data.application.data = 0;
964 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
965 /* read vendor string */
966 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
967 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
968 return false; /* the read_callback_ sets the state for us */
969 if(block.data.vorbis_comment.vendor_string.length > 0) {
970 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
971 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
974 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.vendor_string.entry, block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
975 return false; /* the read_callback_ sets the state for us */
978 block.data.vorbis_comment.vendor_string.entry = 0;
980 /* read num comments */
981 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
982 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.num_comments, read_callback_, decoder))
983 return false; /* the read_callback_ sets the state for us */
986 if(block.data.vorbis_comment.num_comments > 0) {
987 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
988 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
991 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
992 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
993 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
994 return false; /* the read_callback_ sets the state for us */
995 if(block.data.vorbis_comment.comments[i].length > 0) {
996 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
997 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1000 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.comments[i].entry, block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
1001 return false; /* the read_callback_ sets the state for us */
1004 block.data.vorbis_comment.comments[i].entry = 0;
1008 block.data.vorbis_comment.comments = 0;
1011 case FLAC__METADATA_TYPE_STREAMINFO:
1012 case FLAC__METADATA_TYPE_SEEKTABLE:
1016 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1018 /* now we have to free any malloc'ed data in the block */
1020 case FLAC__METADATA_TYPE_PADDING:
1022 case FLAC__METADATA_TYPE_APPLICATION:
1023 if(0 != block.data.application.data)
1024 free(block.data.application.data);
1026 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1027 if(0 != block.data.vorbis_comment.vendor_string.entry)
1028 free(block.data.vorbis_comment.vendor_string.entry);
1029 if(block.data.vorbis_comment.num_comments > 0)
1030 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1031 if(0 != block.data.vorbis_comment.comments[i].entry)
1032 free(block.data.vorbis_comment.comments[i].entry);
1033 if(0 != block.data.vorbis_comment.comments)
1034 free(block.data.vorbis_comment.comments);
1036 case FLAC__METADATA_TYPE_STREAMINFO:
1037 case FLAC__METADATA_TYPE_SEEKTABLE:
1045 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1050 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1055 /* skip the version and flags bytes */
1056 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1057 return false; /* the read_callback_ sets the state for us */
1058 /* get the size (in bytes) to skip */
1060 for(i = 0; i < 4; i++) {
1061 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1062 return false; /* the read_callback_ sets the state for us */
1066 /* skip the rest of the tag */
1067 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1068 return false; /* the read_callback_ sets the state for us */
1072 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1075 FLAC__bool first = true;
1077 /* If we know the total number of samples in the stream, stop if we've read that many. */
1078 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1079 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1080 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1081 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1086 /* make sure we're byte aligned */
1087 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1088 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1089 return false; /* the read_callback_ sets the state for us */
1093 if(decoder->private_->cached) {
1094 x = (FLAC__uint32)decoder->private_->lookahead;
1095 decoder->private_->cached = false;
1098 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1099 return false; /* the read_callback_ sets the state for us */
1101 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1102 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1103 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1104 return false; /* the read_callback_ sets the state for us */
1106 /* 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 */
1107 /* else we have to check if the second byte is the end of a sync code */
1108 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1109 decoder->private_->lookahead = (FLAC__byte)x;
1110 decoder->private_->cached = true;
1112 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1113 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1114 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1119 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1127 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1131 FLAC__int32 mid, side, left, right;
1132 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1135 *got_a_frame = false;
1139 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1140 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1141 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1143 if(!read_frame_header_(decoder))
1145 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1147 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1149 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1151 * first figure the correct bits-per-sample of the subframe
1153 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1154 switch(decoder->private_->frame.header.channel_assignment) {
1155 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1156 /* no adjustment needed */
1158 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1159 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1163 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1164 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1168 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1169 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1179 if(!read_subframe_(decoder, channel, bps))
1181 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1182 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1186 if(!read_zero_padding_(decoder))
1190 * Read the frame CRC-16 from the footer and check
1192 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1193 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1194 return false; /* the read_callback_ sets the state for us */
1195 if(frame_crc == (FLAC__uint16)x) {
1196 /* Undo any special channel coding */
1197 switch(decoder->private_->frame.header.channel_assignment) {
1198 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1201 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1202 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1203 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1204 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1206 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1207 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1208 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1209 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1211 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1212 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1213 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1214 mid = decoder->private_->output[0][i];
1215 side = decoder->private_->output[1][i];
1217 if(side & 1) /* i.e. if 'side' is odd... */
1221 decoder->private_->output[0][i] = left >> 1;
1222 decoder->private_->output[1][i] = right >> 1;
1231 /* Bad frame, emit error and zero the output signal */
1232 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1233 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1234 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1238 *got_a_frame = true;
1240 /* put the latest values into the public section of the decoder instance */
1241 decoder->protected_->channels = decoder->private_->frame.header.channels;
1242 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1243 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1244 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1245 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1247 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1248 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1251 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)
1254 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1258 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1262 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1263 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1264 unsigned raw_header_len;
1265 FLAC__bool is_unparseable = false;
1266 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);
1267 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);
1269 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1271 /* init the raw header with the saved bits from synchronization */
1272 raw_header[0] = decoder->private_->header_warmup[0];
1273 raw_header[1] = decoder->private_->header_warmup[1];
1277 * check to make sure that the reserved bits are 0
1279 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1280 is_unparseable = true;
1284 * Note that along the way as we read the header, we look for a sync
1285 * code inside. If we find one it would indicate that our original
1286 * sync was bad since there cannot be a sync code in a valid header.
1290 * read in the raw header as bytes so we can CRC it, and parse it on the way
1292 for(i = 0; i < 2; i++) {
1293 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1294 return false; /* the read_callback_ sets the state for us */
1295 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1296 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1297 decoder->private_->lookahead = (FLAC__byte)x;
1298 decoder->private_->cached = true;
1299 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1300 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1303 raw_header[raw_header_len++] = (FLAC__byte)x;
1306 switch(x = raw_header[2] >> 4) {
1308 if(is_known_fixed_blocksize_stream)
1309 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1311 is_unparseable = true;
1314 decoder->private_->frame.header.blocksize = 192;
1320 decoder->private_->frame.header.blocksize = 576 << (x-2);
1334 decoder->private_->frame.header.blocksize = 256 << (x-8);
1341 switch(x = raw_header[2] & 0x0f) {
1343 if(decoder->private_->has_stream_info)
1344 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1346 is_unparseable = true;
1351 is_unparseable = true;
1354 decoder->private_->frame.header.sample_rate = 8000;
1357 decoder->private_->frame.header.sample_rate = 16000;
1360 decoder->private_->frame.header.sample_rate = 22050;
1363 decoder->private_->frame.header.sample_rate = 24000;
1366 decoder->private_->frame.header.sample_rate = 32000;
1369 decoder->private_->frame.header.sample_rate = 44100;
1372 decoder->private_->frame.header.sample_rate = 48000;
1375 decoder->private_->frame.header.sample_rate = 96000;
1380 sample_rate_hint = x;
1383 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1384 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1390 x = (unsigned)(raw_header[3] >> 4);
1392 decoder->private_->frame.header.channels = 2;
1395 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1398 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1401 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1404 is_unparseable = true;
1409 decoder->private_->frame.header.channels = (unsigned)x + 1;
1410 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1413 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1415 if(decoder->private_->has_stream_info)
1416 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1418 is_unparseable = true;
1421 decoder->private_->frame.header.bits_per_sample = 8;
1424 decoder->private_->frame.header.bits_per_sample = 12;
1427 decoder->private_->frame.header.bits_per_sample = 16;
1430 decoder->private_->frame.header.bits_per_sample = 20;
1433 decoder->private_->frame.header.bits_per_sample = 24;
1437 is_unparseable = true;
1444 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1445 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1446 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1450 if(blocksize_hint && is_known_variable_blocksize_stream) {
1451 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1452 return false; /* the read_callback_ sets the state for us */
1453 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1454 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1455 decoder->private_->cached = true;
1456 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1457 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1460 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1461 decoder->private_->frame.header.number.sample_number = xx;
1464 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1465 return false; /* the read_callback_ sets the state for us */
1466 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1467 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1468 decoder->private_->cached = true;
1469 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1470 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1473 decoder->private_->last_frame_number = x;
1474 if(decoder->private_->has_stream_info) {
1475 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1476 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1479 is_unparseable = true;
1483 if(blocksize_hint) {
1484 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1485 return false; /* the read_callback_ sets the state for us */
1486 raw_header[raw_header_len++] = (FLAC__byte)x;
1487 if(blocksize_hint == 7) {
1489 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1490 return false; /* the read_callback_ sets the state for us */
1491 raw_header[raw_header_len++] = (FLAC__byte)_x;
1494 decoder->private_->frame.header.blocksize = x+1;
1497 if(sample_rate_hint) {
1498 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1499 return false; /* the read_callback_ sets the state for us */
1500 raw_header[raw_header_len++] = (FLAC__byte)x;
1501 if(sample_rate_hint != 12) {
1503 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1504 return false; /* the read_callback_ sets the state for us */
1505 raw_header[raw_header_len++] = (FLAC__byte)_x;
1508 if(sample_rate_hint == 12)
1509 decoder->private_->frame.header.sample_rate = x*1000;
1510 else if(sample_rate_hint == 13)
1511 decoder->private_->frame.header.sample_rate = x;
1513 decoder->private_->frame.header.sample_rate = x*10;
1516 /* read the CRC-8 byte */
1517 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1518 return false; /* the read_callback_ sets the state for us */
1519 crc8 = (FLAC__byte)x;
1521 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1522 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1523 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1527 if(is_unparseable) {
1528 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1535 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1538 FLAC__bool wasted_bits;
1540 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1541 return false; /* the read_callback_ sets the state for us */
1543 wasted_bits = (x & 1);
1548 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1549 return false; /* the read_callback_ sets the state for us */
1550 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1551 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1554 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1557 * Lots of magic numbers here
1560 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1561 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1565 if(!read_subframe_constant_(decoder, channel, bps))
1569 if(!read_subframe_verbatim_(decoder, channel, bps))
1573 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1577 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1581 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1585 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1591 x = decoder->private_->frame.subframes[channel].wasted_bits;
1592 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1593 decoder->private_->output[channel][i] <<= x;
1599 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1601 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1604 FLAC__int32 *output = decoder->private_->output[channel];
1606 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1608 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1609 return false; /* the read_callback_ sets the state for us */
1611 subframe->value = x;
1613 /* decode the subframe */
1614 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1620 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1622 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1627 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1629 subframe->residual = decoder->private_->residual[channel];
1630 subframe->order = order;
1632 /* read warm-up samples */
1633 for(u = 0; u < order; u++) {
1634 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1635 return false; /* the read_callback_ sets the state for us */
1636 subframe->warmup[u] = i32;
1639 /* read entropy coding method info */
1640 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1641 return false; /* the read_callback_ sets the state for us */
1642 subframe->entropy_coding_method.type = u32;
1643 switch(subframe->entropy_coding_method.type) {
1644 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1645 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1646 return false; /* the read_callback_ sets the state for us */
1647 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1648 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1651 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1656 switch(subframe->entropy_coding_method.type) {
1657 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1658 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]))
1665 /* decode the subframe */
1666 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1667 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1672 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1674 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1679 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1681 subframe->residual = decoder->private_->residual[channel];
1682 subframe->order = order;
1684 /* read warm-up samples */
1685 for(u = 0; u < order; u++) {
1686 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1687 return false; /* the read_callback_ sets the state for us */
1688 subframe->warmup[u] = i32;
1691 /* read qlp coeff precision */
1692 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1693 return false; /* the read_callback_ sets the state for us */
1694 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1695 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1696 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1699 subframe->qlp_coeff_precision = u32+1;
1701 /* read qlp shift */
1702 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1703 return false; /* the read_callback_ sets the state for us */
1704 subframe->quantization_level = i32;
1706 /* read quantized lp coefficiencts */
1707 for(u = 0; u < order; u++) {
1708 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1709 return false; /* the read_callback_ sets the state for us */
1710 subframe->qlp_coeff[u] = i32;
1713 /* read entropy coding method info */
1714 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1715 return false; /* the read_callback_ sets the state for us */
1716 subframe->entropy_coding_method.type = u32;
1717 switch(subframe->entropy_coding_method.type) {
1718 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1719 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1720 return false; /* the read_callback_ sets the state for us */
1721 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1722 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1725 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1730 switch(subframe->entropy_coding_method.type) {
1731 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1732 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]))
1739 /* decode the subframe */
1740 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1741 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1742 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1743 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);
1745 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);
1747 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);
1752 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1754 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1755 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1758 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1760 subframe->data = residual;
1762 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1763 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1764 return false; /* the read_callback_ sets the state for us */
1768 /* decode the subframe */
1769 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1774 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1776 FLAC__uint32 rice_parameter;
1778 unsigned partition, sample, u;
1779 const unsigned partitions = 1u << partition_order;
1780 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1782 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1783 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1788 for(partition = 0; partition < partitions; partition++) {
1789 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1790 return false; /* the read_callback_ sets the state for us */
1791 partitioned_rice_contents->parameters[partition] = rice_parameter;
1792 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1793 #ifdef FLAC__SYMMETRIC_RICE
1794 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1795 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1796 return false; /* the read_callback_ sets the state for us */
1797 residual[sample] = i;
1800 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1801 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1802 return false; /* the read_callback_ sets the state for us */
1807 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1808 return false; /* the read_callback_ sets the state for us */
1809 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1810 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1811 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1812 return false; /* the read_callback_ sets the state for us */
1813 residual[sample] = i;
1821 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1823 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1824 FLAC__uint32 zero = 0;
1825 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1826 return false; /* the read_callback_ sets the state for us */
1828 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1829 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1835 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1837 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1838 FLAC__StreamDecoderReadStatus status;
1840 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1841 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1842 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1843 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1844 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1845 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;