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/lpc.h"
31 /***********************************************************************
35 ***********************************************************************/
37 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
39 /***********************************************************************
41 * Private class method prototypes
43 ***********************************************************************/
45 static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
46 static FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
47 static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
48 static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
49 static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
50 static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
51 static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
52 static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
53 static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
54 static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
55 static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
56 static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
57 static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
58 static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
59 static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
60 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
62 /***********************************************************************
66 ***********************************************************************/
68 typedef struct FLAC__StreamDecoderPrivate {
69 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
70 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
71 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
72 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
73 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[]);
74 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[]);
76 FLAC__BitBuffer *input;
77 FLAC__int32 *output[FLAC__MAX_CHANNELS];
78 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
79 unsigned output_capacity, output_channels;
80 FLAC__uint32 last_frame_number;
81 FLAC__uint64 samples_decoded;
82 FLAC__bool has_stream_info, has_seek_table;
83 FLAC__StreamMetaData stream_info;
84 FLAC__StreamMetaData seek_table;
85 FLAC__bool metadata_filter[FLAC__METADATA_TYPE_VORBIS_COMMENT+1];
86 FLAC__byte *metadata_filter_ids;
87 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
89 FLAC__bool cached; /* true if there is a byte in lookahead */
90 FLAC__CPUInfo cpuinfo;
91 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
92 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
93 } FLAC__StreamDecoderPrivate;
95 /***********************************************************************
97 * Public static class data
99 ***********************************************************************/
101 const char *FLAC__StreamDecoderStateString[] = {
102 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
103 "FLAC__STREAM_DECODER_READ_METADATA",
104 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
105 "FLAC__STREAM_DECODER_READ_FRAME",
106 "FLAC__STREAM_DECODER_END_OF_STREAM",
107 "FLAC__STREAM_DECODER_ABORTED",
108 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
109 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
110 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
111 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
112 "FLAC__STREAM_DECODER_UNINITIALIZED"
115 const char *FLAC__StreamDecoderReadStatusString[] = {
116 "FLAC__STREAM_DECODER_READ_CONTINUE",
117 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
118 "FLAC__STREAM_DECODER_READ_ABORT"
121 const char *FLAC__StreamDecoderWriteStatusString[] = {
122 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
123 "FLAC__STREAM_DECODER_WRITE_ABORT"
126 const char *FLAC__StreamDecoderErrorStatusString[] = {
127 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
128 "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
129 "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
132 /***********************************************************************
134 * Class constructor/destructor
136 ***********************************************************************/
137 FLAC__StreamDecoder *FLAC__stream_decoder_new()
139 FLAC__StreamDecoder *decoder;
141 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
143 decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
147 decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
148 if(decoder->protected_ == 0) {
152 decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
153 if(decoder->private_ == 0) {
154 free(decoder->protected_);
158 decoder->private_->input = FLAC__bitbuffer_new();
159 if(decoder->private_->input == 0) {
160 free(decoder->private_);
161 free(decoder->protected_);
166 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
168 decoder->private_->read_callback = 0;
169 decoder->private_->write_callback = 0;
170 decoder->private_->metadata_callback = 0;
171 decoder->private_->error_callback = 0;
172 decoder->private_->client_data = 0;
177 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
179 FLAC__ASSERT(decoder != 0);
180 FLAC__ASSERT(decoder->protected_ != 0);
181 FLAC__ASSERT(decoder->private_ != 0);
182 FLAC__ASSERT(decoder->private_->input != 0);
184 FLAC__bitbuffer_delete(decoder->private_->input);
185 free(decoder->private_);
186 free(decoder->protected_);
190 /***********************************************************************
192 * Public class methods
194 ***********************************************************************/
196 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
200 FLAC__ASSERT(decoder != 0);
202 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
203 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
205 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
207 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
208 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
210 if(!FLAC__bitbuffer_init(decoder->private_->input))
211 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
213 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
214 decoder->private_->output[i] = 0;
215 decoder->private_->residual[i] = 0;
218 decoder->private_->output_capacity = 0;
219 decoder->private_->output_channels = 0;
220 decoder->private_->last_frame_number = 0;
221 decoder->private_->samples_decoded = 0;
222 decoder->private_->has_stream_info = false;
223 decoder->private_->has_seek_table = false;
225 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
226 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
227 decoder->private_->metadata_filter_ids_capacity = 16;
228 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
229 decoder->private_->metadata_filter_ids_capacity = 0;
230 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
232 decoder->private_->metadata_filter_ids_count = 0;
234 decoder->private_->cached = false;
237 * get the CPU info and set the function pointers
239 FLAC__cpu_info(&decoder->private_->cpuinfo);
240 /* first default to the non-asm routines */
241 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
242 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
243 /* now override with asm where appropriate */
245 if(decoder->private_->cpuinfo.use_asm) {
246 #ifdef FLAC__CPU_IA32
247 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
248 #ifdef FLAC__HAS_NASM
249 if(decoder->private_->cpuinfo.data.ia32.mmx) {
250 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
251 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
254 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
255 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
262 return decoder->protected_->state;
265 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
268 FLAC__ASSERT(decoder != 0);
269 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
271 if(decoder->private_->has_seek_table) {
272 free(decoder->private_->seek_table.data.seek_table.points);
273 decoder->private_->seek_table.data.seek_table.points = 0;
275 FLAC__bitbuffer_free(decoder->private_->input);
276 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
277 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
278 if(decoder->private_->output[i] != 0) {
279 free(decoder->private_->output[i]-4);
280 decoder->private_->output[i] = 0;
282 if(decoder->private_->residual[i] != 0) {
283 free(decoder->private_->residual[i]);
284 decoder->private_->residual[i] = 0;
287 if(decoder->private_->metadata_filter_ids != 0) {
288 free(decoder->private_->metadata_filter_ids);
289 decoder->private_->metadata_filter_ids = 0;
291 decoder->private_->output_capacity = 0;
292 decoder->private_->output_channels = 0;
293 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
296 FLAC__bool FLAC__stream_decoder_set_read_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
298 FLAC__ASSERT(decoder != 0);
299 FLAC__ASSERT(decoder->private_ != 0);
300 FLAC__ASSERT(decoder->protected_ != 0);
301 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
303 decoder->private_->read_callback = value;
307 FLAC__bool FLAC__stream_decoder_set_write_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
309 FLAC__ASSERT(decoder != 0);
310 FLAC__ASSERT(decoder->private_ != 0);
311 FLAC__ASSERT(decoder->protected_ != 0);
312 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
314 decoder->private_->write_callback = value;
318 FLAC__bool FLAC__stream_decoder_set_metadata_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
320 FLAC__ASSERT(decoder != 0);
321 FLAC__ASSERT(decoder->private_ != 0);
322 FLAC__ASSERT(decoder->protected_ != 0);
323 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
325 decoder->private_->metadata_callback = value;
329 FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
331 FLAC__ASSERT(decoder != 0);
332 FLAC__ASSERT(decoder->private_ != 0);
333 FLAC__ASSERT(decoder->protected_ != 0);
334 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
336 decoder->private_->error_callback = value;
340 FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
342 FLAC__ASSERT(decoder != 0);
343 FLAC__ASSERT(decoder->private_ != 0);
344 FLAC__ASSERT(decoder->protected_ != 0);
345 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
347 decoder->private_->client_data = value;
351 FLAC__bool FLAC__stream_decoder_set_metadata_respond(const FLAC__StreamDecoder *decoder, FLAC__MetaDataType type)
353 FLAC__ASSERT(decoder != 0);
354 FLAC__ASSERT(decoder->private_ != 0);
355 FLAC__ASSERT(decoder->protected_ != 0);
356 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
357 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
359 decoder->private_->metadata_filter[type] = true;
360 if(type == FLAC__METADATA_TYPE_APPLICATION)
361 decoder->private_->metadata_filter_ids_count = 0;
365 FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(const FLAC__StreamDecoder *decoder, FLAC__byte id[4])
367 FLAC__ASSERT(decoder != 0);
368 FLAC__ASSERT(decoder->private_ != 0);
369 FLAC__ASSERT(decoder->protected_ != 0);
370 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
373 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
376 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
378 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
379 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
380 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
381 decoder->private_->metadata_filter_ids_capacity *= 2;
384 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));
385 decoder->private_->metadata_filter_ids_count++;
390 FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(const FLAC__StreamDecoder *decoder)
392 FLAC__ASSERT(decoder != 0);
393 FLAC__ASSERT(decoder->private_ != 0);
394 FLAC__ASSERT(decoder->protected_ != 0);
395 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
397 /* OK, technically 0x01010101 != true, but it still means true */
398 memset(decoder->private_->metadata_filter, 1, sizeof(decoder->private_->metadata_filter));
399 decoder->private_->metadata_filter_ids_count = 0;
403 FLAC__bool FLAC__stream_decoder_set_metadata_ignore(const FLAC__StreamDecoder *decoder, FLAC__MetaDataType type)
405 FLAC__ASSERT(decoder != 0);
406 FLAC__ASSERT(decoder->private_ != 0);
407 FLAC__ASSERT(decoder->protected_ != 0);
408 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
409 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
411 decoder->private_->metadata_filter[type] = false;
412 if(type == FLAC__METADATA_TYPE_APPLICATION)
413 decoder->private_->metadata_filter_ids_count = 0;
417 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(const FLAC__StreamDecoder *decoder, FLAC__byte id[4])
419 FLAC__ASSERT(decoder != 0);
420 FLAC__ASSERT(decoder->private_ != 0);
421 FLAC__ASSERT(decoder->protected_ != 0);
422 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
425 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
428 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
430 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
431 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
432 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
433 decoder->private_->metadata_filter_ids_capacity *= 2;
436 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));
437 decoder->private_->metadata_filter_ids_count++;
442 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(const FLAC__StreamDecoder *decoder)
444 FLAC__ASSERT(decoder != 0);
445 FLAC__ASSERT(decoder->private_ != 0);
446 FLAC__ASSERT(decoder->protected_ != 0);
447 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
449 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
450 decoder->private_->metadata_filter_ids_count = 0;
454 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
456 FLAC__ASSERT(decoder != 0);
457 FLAC__ASSERT(decoder->protected_ != 0);
458 return decoder->protected_->state;
461 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
463 FLAC__ASSERT(decoder != 0);
464 FLAC__ASSERT(decoder->protected_ != 0);
465 return decoder->protected_->channels;
468 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
470 FLAC__ASSERT(decoder != 0);
471 FLAC__ASSERT(decoder->protected_ != 0);
472 return decoder->protected_->channel_assignment;
475 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
477 FLAC__ASSERT(decoder != 0);
478 FLAC__ASSERT(decoder->protected_ != 0);
479 return decoder->protected_->bits_per_sample;
482 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
484 FLAC__ASSERT(decoder != 0);
485 FLAC__ASSERT(decoder->protected_ != 0);
486 return decoder->protected_->sample_rate;
489 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
491 FLAC__ASSERT(decoder != 0);
492 FLAC__ASSERT(decoder->protected_ != 0);
493 return decoder->protected_->blocksize;
496 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
498 FLAC__ASSERT(decoder != 0);
499 FLAC__ASSERT(decoder->private_ != 0);
500 FLAC__ASSERT(decoder->protected_ != 0);
502 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
503 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
506 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
511 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
513 FLAC__ASSERT(decoder != 0);
514 FLAC__ASSERT(decoder->private_ != 0);
515 FLAC__ASSERT(decoder->protected_ != 0);
517 if(!FLAC__stream_decoder_flush(decoder)) {
518 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
521 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
523 decoder->private_->samples_decoded = 0;
528 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
531 FLAC__ASSERT(decoder != 0);
533 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
536 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
538 if(!FLAC__stream_decoder_reset(decoder)) {
539 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
544 switch(decoder->protected_->state) {
545 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
546 if(!stream_decoder_find_metadata_(decoder))
547 return false; /* above function sets the status for us */
549 case FLAC__STREAM_DECODER_READ_METADATA:
550 if(!stream_decoder_read_metadata_(decoder))
551 return false; /* above function sets the status for us */
553 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
554 if(!stream_decoder_frame_sync_(decoder))
555 return true; /* above function sets the status for us */
557 case FLAC__STREAM_DECODER_READ_FRAME:
558 if(!stream_decoder_read_frame_(decoder, &dummy))
559 return false; /* above function sets the status for us */
561 case FLAC__STREAM_DECODER_END_OF_STREAM:
569 FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
571 FLAC__ASSERT(decoder != 0);
573 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
576 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
578 if(!FLAC__stream_decoder_reset(decoder)) {
579 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
584 switch(decoder->protected_->state) {
585 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
586 if(!stream_decoder_find_metadata_(decoder))
587 return false; /* above function sets the status for us */
589 case FLAC__STREAM_DECODER_READ_METADATA:
590 if(!stream_decoder_read_metadata_(decoder))
591 return false; /* above function sets the status for us */
593 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
595 case FLAC__STREAM_DECODER_READ_FRAME:
597 case FLAC__STREAM_DECODER_END_OF_STREAM:
605 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
607 FLAC__bool got_a_frame;
608 FLAC__ASSERT(decoder != 0);
610 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
613 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
616 switch(decoder->protected_->state) {
617 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
618 if(!stream_decoder_frame_sync_(decoder))
619 return true; /* above function sets the status for us */
621 case FLAC__STREAM_DECODER_READ_FRAME:
622 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
623 return false; /* above function sets the status for us */
625 return true; /* above function sets the status for us */
627 case FLAC__STREAM_DECODER_END_OF_STREAM:
635 FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
638 FLAC__ASSERT(decoder != 0);
640 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
643 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
646 switch(decoder->protected_->state) {
647 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
648 if(!stream_decoder_frame_sync_(decoder))
649 return true; /* above function sets the status for us */
651 case FLAC__STREAM_DECODER_READ_FRAME:
652 if(!stream_decoder_read_frame_(decoder, &dummy))
653 return false; /* above function sets the status for us */
655 case FLAC__STREAM_DECODER_END_OF_STREAM:
663 /***********************************************************************
665 * Protected class methods
667 ***********************************************************************/
669 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
671 FLAC__ASSERT(decoder != 0);
672 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
675 /***********************************************************************
677 * Private class methods
679 ***********************************************************************/
681 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
686 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
689 /* @@@ should change to use realloc() */
691 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
692 if(decoder->private_->output[i] != 0) {
693 free(decoder->private_->output[i]);
694 decoder->private_->output[i] = 0;
696 if(decoder->private_->residual[i] != 0) {
697 free(decoder->private_->residual[i]);
698 decoder->private_->residual[i] = 0;
702 for(i = 0; i < channels; i++) {
703 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
704 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
706 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
709 memset(tmp, 0, sizeof(FLAC__int32)*4);
710 decoder->private_->output[i] = tmp + 4;
712 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
714 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
717 decoder->private_->residual[i] = tmp;
720 decoder->private_->output_capacity = size;
721 decoder->private_->output_channels = channels;
726 FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
730 FLAC__ASSERT(0 != decoder);
731 FLAC__ASSERT(0 != decoder->private_);
733 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
734 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
740 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
744 FLAC__bool first = true;
746 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
748 for(i = id = 0; i < 4; ) {
749 if(decoder->private_->cached) {
750 x = (FLAC__uint32)decoder->private_->lookahead;
751 decoder->private_->cached = false;
754 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
755 return false; /* the read_callback_ sets the state for us */
757 if(x == FLAC__STREAM_SYNC_STRING[i]) {
763 if(x == ID3V2_TAG_[id]) {
767 if(!stream_decoder_skip_id3v2_tag_(decoder))
768 return false; /* the read_callback_ sets the state for us */
772 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
773 decoder->private_->header_warmup[0] = (FLAC__byte)x;
774 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
775 return false; /* the read_callback_ sets the state for us */
777 /* 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 */
778 /* else we have to check if the second byte is the end of a sync code */
779 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
780 decoder->private_->lookahead = (FLAC__byte)x;
781 decoder->private_->cached = true;
783 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
784 decoder->private_->header_warmup[1] = (FLAC__byte)x;
785 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
791 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
796 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
800 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
802 FLAC__uint32 i, x, last_block, type, length;
805 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
807 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
808 return false; /* the read_callback_ sets the state for us */
809 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
810 return false; /* the read_callback_ sets the state for us */
811 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
812 return false; /* the read_callback_ sets the state for us */
813 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
814 unsigned used_bits = 0;
815 decoder->private_->stream_info.type = type;
816 decoder->private_->stream_info.is_last = last_block;
817 decoder->private_->stream_info.length = length;
819 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
820 return false; /* the read_callback_ sets the state for us */
821 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
822 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
824 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
825 return false; /* the read_callback_ sets the state for us */
826 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
827 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
829 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
830 return false; /* the read_callback_ sets the state for us */
831 decoder->private_->stream_info.data.stream_info.min_framesize = x;
832 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
834 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
835 return false; /* the read_callback_ sets the state for us */
836 decoder->private_->stream_info.data.stream_info.max_framesize = x;
837 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
839 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
840 return false; /* the read_callback_ sets the state for us */
841 decoder->private_->stream_info.data.stream_info.sample_rate = x;
842 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
844 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
845 return false; /* the read_callback_ sets the state for us */
846 decoder->private_->stream_info.data.stream_info.channels = x+1;
847 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
849 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
850 return false; /* the read_callback_ sets the state for us */
851 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
852 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
854 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))
855 return false; /* the read_callback_ sets the state for us */
856 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
858 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
859 return false; /* the read_callback_ sets the state for us */
862 /* skip the rest of the block */
863 FLAC__ASSERT(used_bits % 8 == 0);
864 length -= (used_bits / 8);
865 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
866 return false; /* the read_callback_ sets the state for us */
868 decoder->private_->has_stream_info = true;
869 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
870 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
872 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
873 unsigned real_points;
875 decoder->private_->seek_table.type = type;
876 decoder->private_->seek_table.is_last = last_block;
877 decoder->private_->seek_table.length = length;
879 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
881 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)))) {
882 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
885 for(i = real_points = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
886 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
887 return false; /* the read_callback_ sets the state for us */
888 decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
890 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
891 return false; /* the read_callback_ sets the state for us */
892 decoder->private_->seek_table.data.seek_table.points[real_points].stream_offset = xx;
894 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
895 return false; /* the read_callback_ sets the state for us */
896 decoder->private_->seek_table.data.seek_table.points[real_points].frame_samples = x;
898 if(decoder->private_->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
901 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
902 decoder->private_->seek_table.data.seek_table.num_points = real_points;
904 /* if there is a partial point left, skip over it */
906 /*@@@ do an error_callback() here? there's an argument for either way */
907 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
908 return false; /* the read_callback_ sets the state for us */
911 decoder->private_->has_seek_table = true;
912 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
913 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
916 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
917 unsigned real_length = length;
918 FLAC__StreamMetaData block;
920 block.is_last = last_block;
922 block.length = length;
924 if(type == FLAC__METADATA_TYPE_APPLICATION && decoder->private_->metadata_filter_ids_count > 0) {
925 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
926 return false; /* the read_callback_ sets the state for us */
928 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
930 if(stream_decoder_has_id_filtered_(decoder, block.data.application.id))
935 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
936 return false; /* the read_callback_ sets the state for us */
940 case FLAC__METADATA_TYPE_PADDING:
941 /* skip the padding bytes */
942 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
943 return false; /* the read_callback_ sets the state for us */
945 case FLAC__METADATA_TYPE_APPLICATION:
946 /* remember, we read the ID already */
947 if(0 == (block.data.application.data = malloc(real_length))) {
948 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
951 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
952 return false; /* the read_callback_ sets the state for us */
954 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
955 /* read vendor string */
956 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, read_callback_, decoder))
957 return false; /* the read_callback_ sets the state for us */
958 if(block.data.vorbis_comment.vendor_string.length > 0) {
959 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
960 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
963 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))
964 return false; /* the read_callback_ sets the state for us */
967 block.data.vorbis_comment.vendor_string.entry = 0;
969 /* read num comments */
970 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &block.data.vorbis_comment.num_comments, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN, read_callback_, decoder))
971 return false; /* the read_callback_ sets the state for us */
974 if(block.data.vorbis_comment.num_comments > 0) {
975 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)))) {
976 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
979 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
980 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, read_callback_, decoder))
981 return false; /* the read_callback_ sets the state for us */
982 if(block.data.vorbis_comment.comments[i].length > 0) {
983 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
984 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
987 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))
988 return false; /* the read_callback_ sets the state for us */
991 block.data.vorbis_comment.comments[i].entry = 0;
995 block.data.vorbis_comment.comments = 0;
998 case FLAC__METADATA_TYPE_STREAMINFO:
999 case FLAC__METADATA_TYPE_SEEKTABLE:
1003 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1005 /* now we have to free any malloc'ed data in the block */
1007 case FLAC__METADATA_TYPE_PADDING:
1009 case FLAC__METADATA_TYPE_APPLICATION:
1010 if(0 != block.data.application.data)
1011 free(block.data.application.data);
1013 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1014 if(0 != block.data.vorbis_comment.vendor_string.entry)
1015 free(block.data.vorbis_comment.vendor_string.entry);
1016 if(block.data.vorbis_comment.num_comments > 0)
1017 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1018 if(0 != block.data.vorbis_comment.comments[i].entry)
1019 free(block.data.vorbis_comment.comments[i].entry);
1020 if(0 != block.data.vorbis_comment.comments)
1021 free(block.data.vorbis_comment.comments);
1023 case FLAC__METADATA_TYPE_STREAMINFO:
1024 case FLAC__METADATA_TYPE_SEEKTABLE:
1032 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1037 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1042 /* skip the version and flags bytes */
1043 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1044 return false; /* the read_callback_ sets the state for us */
1045 /* get the size (in bytes) to skip */
1047 for(i = 0; i < 4; i++) {
1048 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1049 return false; /* the read_callback_ sets the state for us */
1053 /* skip the rest of the tag */
1054 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1055 return false; /* the read_callback_ sets the state for us */
1059 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
1062 FLAC__bool first = true;
1064 /* If we know the total number of samples in the stream, stop if we've read that many. */
1065 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1066 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1067 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1068 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1073 /* make sure we're byte aligned */
1074 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1075 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1076 return false; /* the read_callback_ sets the state for us */
1080 if(decoder->private_->cached) {
1081 x = (FLAC__uint32)decoder->private_->lookahead;
1082 decoder->private_->cached = false;
1085 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1086 return false; /* the read_callback_ sets the state for us */
1088 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1089 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1090 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1091 return false; /* the read_callback_ sets the state for us */
1093 /* 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 */
1094 /* else we have to check if the second byte is the end of a sync code */
1095 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1096 decoder->private_->lookahead = (FLAC__byte)x;
1097 decoder->private_->cached = true;
1099 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1100 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1101 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1106 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1114 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1118 FLAC__int32 mid, side, left, right;
1119 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1122 *got_a_frame = false;
1126 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1127 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1128 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1130 if(!stream_decoder_read_frame_header_(decoder))
1132 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1134 if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1136 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1138 * first figure the correct bits-per-sample of the subframe
1140 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1141 switch(decoder->private_->frame.header.channel_assignment) {
1142 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1143 /* no adjustment needed */
1145 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1146 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1150 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1151 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1155 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1156 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1166 if(!stream_decoder_read_subframe_(decoder, channel, bps))
1168 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1169 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1173 if(!stream_decoder_read_zero_padding_(decoder))
1177 * Read the frame CRC-16 from the footer and check
1179 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1180 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1181 return false; /* the read_callback_ sets the state for us */
1182 if(frame_crc == (FLAC__uint16)x) {
1183 /* Undo any special channel coding */
1184 switch(decoder->private_->frame.header.channel_assignment) {
1185 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1188 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1189 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1190 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1191 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1193 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1194 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1195 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1196 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1198 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1199 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1200 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1201 mid = decoder->private_->output[0][i];
1202 side = decoder->private_->output[1][i];
1204 if(side & 1) /* i.e. if 'side' is odd... */
1208 decoder->private_->output[0][i] = left >> 1;
1209 decoder->private_->output[1][i] = right >> 1;
1218 /* Bad frame, emit error and zero the output signal */
1219 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1220 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1221 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1225 *got_a_frame = true;
1227 /* put the latest values into the public section of the decoder instance */
1228 decoder->protected_->channels = decoder->private_->frame.header.channels;
1229 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1230 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1231 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1232 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1234 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1235 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1238 /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
1239 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
1242 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1246 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
1250 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1251 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1252 unsigned raw_header_len;
1253 FLAC__bool is_unparseable = false;
1254 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);
1255 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);
1257 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1259 /* init the raw header with the saved bits from synchronization */
1260 raw_header[0] = decoder->private_->header_warmup[0];
1261 raw_header[1] = decoder->private_->header_warmup[1];
1265 * check to make sure that the reserved bits are 0
1267 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1268 is_unparseable = true;
1272 * Note that along the way as we read the header, we look for a sync
1273 * code inside. If we find one it would indicate that our original
1274 * sync was bad since there cannot be a sync code in a valid header.
1278 * read in the raw header as bytes so we can CRC it, and parse it on the way
1280 for(i = 0; i < 2; i++) {
1281 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1282 return false; /* the read_callback_ sets the state for us */
1283 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1284 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1285 decoder->private_->lookahead = (FLAC__byte)x;
1286 decoder->private_->cached = true;
1287 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1288 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1291 raw_header[raw_header_len++] = (FLAC__byte)x;
1294 switch(x = raw_header[2] >> 4) {
1296 if(is_known_fixed_blocksize_stream)
1297 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1299 is_unparseable = true;
1302 decoder->private_->frame.header.blocksize = 192;
1308 decoder->private_->frame.header.blocksize = 576 << (x-2);
1322 decoder->private_->frame.header.blocksize = 256 << (x-8);
1329 switch(x = raw_header[2] & 0x0f) {
1331 if(decoder->private_->has_stream_info)
1332 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1334 is_unparseable = true;
1339 is_unparseable = true;
1342 decoder->private_->frame.header.sample_rate = 8000;
1345 decoder->private_->frame.header.sample_rate = 16000;
1348 decoder->private_->frame.header.sample_rate = 22050;
1351 decoder->private_->frame.header.sample_rate = 24000;
1354 decoder->private_->frame.header.sample_rate = 32000;
1357 decoder->private_->frame.header.sample_rate = 44100;
1360 decoder->private_->frame.header.sample_rate = 48000;
1363 decoder->private_->frame.header.sample_rate = 96000;
1368 sample_rate_hint = x;
1371 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1372 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1378 x = (unsigned)(raw_header[3] >> 4);
1380 decoder->private_->frame.header.channels = 2;
1383 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1386 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1389 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1392 is_unparseable = true;
1397 decoder->private_->frame.header.channels = (unsigned)x + 1;
1398 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1401 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1403 if(decoder->private_->has_stream_info)
1404 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1406 is_unparseable = true;
1409 decoder->private_->frame.header.bits_per_sample = 8;
1412 decoder->private_->frame.header.bits_per_sample = 12;
1415 decoder->private_->frame.header.bits_per_sample = 16;
1418 decoder->private_->frame.header.bits_per_sample = 20;
1421 decoder->private_->frame.header.bits_per_sample = 24;
1425 is_unparseable = true;
1432 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1433 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1434 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1438 if(blocksize_hint && is_known_variable_blocksize_stream) {
1439 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1440 return false; /* the read_callback_ sets the state for us */
1441 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1442 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1443 decoder->private_->cached = true;
1444 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1445 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1448 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1449 decoder->private_->frame.header.number.sample_number = xx;
1452 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1453 return false; /* the read_callback_ sets the state for us */
1454 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1455 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1456 decoder->private_->cached = true;
1457 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1458 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1461 decoder->private_->last_frame_number = x;
1462 if(decoder->private_->has_stream_info) {
1463 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1464 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1467 is_unparseable = true;
1471 if(blocksize_hint) {
1472 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1473 return false; /* the read_callback_ sets the state for us */
1474 raw_header[raw_header_len++] = (FLAC__byte)x;
1475 if(blocksize_hint == 7) {
1477 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1478 return false; /* the read_callback_ sets the state for us */
1479 raw_header[raw_header_len++] = (FLAC__byte)_x;
1482 decoder->private_->frame.header.blocksize = x+1;
1485 if(sample_rate_hint) {
1486 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1487 return false; /* the read_callback_ sets the state for us */
1488 raw_header[raw_header_len++] = (FLAC__byte)x;
1489 if(sample_rate_hint != 12) {
1491 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1492 return false; /* the read_callback_ sets the state for us */
1493 raw_header[raw_header_len++] = (FLAC__byte)_x;
1496 if(sample_rate_hint == 12)
1497 decoder->private_->frame.header.sample_rate = x*1000;
1498 else if(sample_rate_hint == 13)
1499 decoder->private_->frame.header.sample_rate = x;
1501 decoder->private_->frame.header.sample_rate = x*10;
1504 /* read the CRC-8 byte */
1505 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1506 return false; /* the read_callback_ sets the state for us */
1507 crc8 = (FLAC__byte)x;
1509 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1510 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1511 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1515 if(is_unparseable) {
1516 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1523 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1526 FLAC__bool wasted_bits;
1528 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1529 return false; /* the read_callback_ sets the state for us */
1531 wasted_bits = (x & 1);
1536 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1537 return false; /* the read_callback_ sets the state for us */
1538 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1539 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1542 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1545 * Lots of magic numbers here
1548 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1549 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1553 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1557 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1561 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1565 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1569 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1573 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1579 x = decoder->private_->frame.subframes[channel].wasted_bits;
1580 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1581 decoder->private_->output[channel][i] <<= x;
1587 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1589 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1592 FLAC__int32 *output = decoder->private_->output[channel];
1594 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1596 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1597 return false; /* the read_callback_ sets the state for us */
1599 subframe->value = x;
1601 /* decode the subframe */
1602 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1608 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1610 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1615 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1617 subframe->residual = decoder->private_->residual[channel];
1618 subframe->order = order;
1620 /* read warm-up samples */
1621 for(u = 0; u < order; u++) {
1622 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1623 return false; /* the read_callback_ sets the state for us */
1624 subframe->warmup[u] = i32;
1627 /* read entropy coding method info */
1628 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1629 return false; /* the read_callback_ sets the state for us */
1630 subframe->entropy_coding_method.type = u32;
1631 switch(subframe->entropy_coding_method.type) {
1632 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1633 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1634 return false; /* the read_callback_ sets the state for us */
1635 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1638 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1643 switch(subframe->entropy_coding_method.type) {
1644 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1645 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1652 /* decode the subframe */
1653 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1654 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1659 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1661 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1666 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1668 subframe->residual = decoder->private_->residual[channel];
1669 subframe->order = order;
1671 /* read warm-up samples */
1672 for(u = 0; u < order; u++) {
1673 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1674 return false; /* the read_callback_ sets the state for us */
1675 subframe->warmup[u] = i32;
1678 /* read qlp coeff precision */
1679 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1680 return false; /* the read_callback_ sets the state for us */
1681 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1682 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1683 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1686 subframe->qlp_coeff_precision = u32+1;
1688 /* read qlp shift */
1689 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1690 return false; /* the read_callback_ sets the state for us */
1691 subframe->quantization_level = i32;
1693 /* read quantized lp coefficiencts */
1694 for(u = 0; u < order; u++) {
1695 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1696 return false; /* the read_callback_ sets the state for us */
1697 subframe->qlp_coeff[u] = i32;
1700 /* read entropy coding method info */
1701 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1702 return false; /* the read_callback_ sets the state for us */
1703 subframe->entropy_coding_method.type = u32;
1704 switch(subframe->entropy_coding_method.type) {
1705 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1706 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1707 return false; /* the read_callback_ sets the state for us */
1708 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1711 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1716 switch(subframe->entropy_coding_method.type) {
1717 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1718 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1725 /* decode the subframe */
1726 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1727 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1728 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);
1730 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);
1735 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1737 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1738 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1741 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1743 subframe->data = residual;
1745 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1746 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1747 return false; /* the read_callback_ sets the state for us */
1751 /* decode the subframe */
1752 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1757 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1759 FLAC__uint32 rice_parameter;
1761 unsigned partition, sample, u;
1762 const unsigned partition_order = partitioned_rice->order;
1763 const unsigned partitions = 1u << partition_order;
1764 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1767 for(partition = 0; partition < partitions; partition++) {
1768 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1769 return false; /* the read_callback_ sets the state for us */
1770 partitioned_rice->parameters[partition] = rice_parameter;
1771 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1772 #ifdef FLAC__SYMMETRIC_RICE
1773 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1774 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1775 return false; /* the read_callback_ sets the state for us */
1776 residual[sample] = i;
1779 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1780 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1781 return false; /* the read_callback_ sets the state for us */
1786 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1787 return false; /* the read_callback_ sets the state for us */
1788 partitioned_rice->raw_bits[partition] = rice_parameter;
1789 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1790 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1791 return false; /* the read_callback_ sets the state for us */
1792 residual[sample] = i;
1800 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1802 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1803 FLAC__uint32 zero = 0;
1804 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1805 return false; /* the read_callback_ sets the state for us */
1807 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1808 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1814 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1816 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1817 FLAC__StreamDecoderReadStatus status;
1819 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1820 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1821 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1822 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1823 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1824 return status == FLAC__STREAM_DECODER_READ_CONTINUE;