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;
174 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
175 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
176 decoder->private_->metadata_filter_ids_capacity = 16;
177 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
178 FLAC__bitbuffer_delete(decoder->private_->input);
179 free(decoder->private_);
180 free(decoder->protected_);
184 decoder->private_->metadata_filter_ids_count = 0;
189 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
191 FLAC__ASSERT(decoder != 0);
192 FLAC__ASSERT(decoder->protected_ != 0);
193 FLAC__ASSERT(decoder->private_ != 0);
194 FLAC__ASSERT(decoder->private_->input != 0);
196 FLAC__bitbuffer_delete(decoder->private_->input);
197 free(decoder->private_);
198 free(decoder->protected_);
202 /***********************************************************************
204 * Public class methods
206 ***********************************************************************/
208 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
212 FLAC__ASSERT(decoder != 0);
214 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
215 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
217 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
219 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
220 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
222 if(!FLAC__bitbuffer_init(decoder->private_->input))
223 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
225 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
226 decoder->private_->output[i] = 0;
227 decoder->private_->residual[i] = 0;
230 decoder->private_->output_capacity = 0;
231 decoder->private_->output_channels = 0;
232 decoder->private_->last_frame_number = 0;
233 decoder->private_->samples_decoded = 0;
234 decoder->private_->has_stream_info = false;
235 decoder->private_->has_seek_table = false;
236 decoder->private_->cached = false;
239 * get the CPU info and set the function pointers
241 FLAC__cpu_info(&decoder->private_->cpuinfo);
242 /* first default to the non-asm routines */
243 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
244 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
245 /* now override with asm where appropriate */
247 if(decoder->private_->cpuinfo.use_asm) {
248 #ifdef FLAC__CPU_IA32
249 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
250 #ifdef FLAC__HAS_NASM
251 if(decoder->private_->cpuinfo.data.ia32.mmx) {
252 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
253 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
256 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
257 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
264 return decoder->protected_->state;
267 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
270 FLAC__ASSERT(decoder != 0);
271 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
273 if(decoder->private_->has_seek_table) {
274 free(decoder->private_->seek_table.data.seek_table.points);
275 decoder->private_->seek_table.data.seek_table.points = 0;
277 FLAC__bitbuffer_free(decoder->private_->input);
278 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
279 /* 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. */
280 if(decoder->private_->output[i] != 0) {
281 free(decoder->private_->output[i]-4);
282 decoder->private_->output[i] = 0;
284 if(decoder->private_->residual[i] != 0) {
285 free(decoder->private_->residual[i]);
286 decoder->private_->residual[i] = 0;
289 if(decoder->private_->metadata_filter_ids != 0) {
290 free(decoder->private_->metadata_filter_ids);
291 decoder->private_->metadata_filter_ids = 0;
293 decoder->private_->output_capacity = 0;
294 decoder->private_->output_channels = 0;
295 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
298 FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
300 FLAC__ASSERT(decoder != 0);
301 FLAC__ASSERT(decoder->private_ != 0);
302 FLAC__ASSERT(decoder->protected_ != 0);
303 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
305 decoder->private_->read_callback = value;
309 FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
311 FLAC__ASSERT(decoder != 0);
312 FLAC__ASSERT(decoder->private_ != 0);
313 FLAC__ASSERT(decoder->protected_ != 0);
314 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
316 decoder->private_->write_callback = value;
320 FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
322 FLAC__ASSERT(decoder != 0);
323 FLAC__ASSERT(decoder->private_ != 0);
324 FLAC__ASSERT(decoder->protected_ != 0);
325 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
327 decoder->private_->metadata_callback = value;
331 FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
333 FLAC__ASSERT(decoder != 0);
334 FLAC__ASSERT(decoder->private_ != 0);
335 FLAC__ASSERT(decoder->protected_ != 0);
336 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
338 decoder->private_->error_callback = value;
342 FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
344 FLAC__ASSERT(decoder != 0);
345 FLAC__ASSERT(decoder->private_ != 0);
346 FLAC__ASSERT(decoder->protected_ != 0);
347 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
349 decoder->private_->client_data = value;
353 FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetaDataType type)
355 FLAC__ASSERT(decoder != 0);
356 FLAC__ASSERT(decoder->private_ != 0);
357 FLAC__ASSERT(decoder->protected_ != 0);
358 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
359 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
361 decoder->private_->metadata_filter[type] = true;
362 if(type == FLAC__METADATA_TYPE_APPLICATION)
363 decoder->private_->metadata_filter_ids_count = 0;
367 FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
369 FLAC__ASSERT(decoder != 0);
370 FLAC__ASSERT(decoder->private_ != 0);
371 FLAC__ASSERT(decoder->protected_ != 0);
372 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
375 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
378 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
380 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
381 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
382 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
383 decoder->private_->metadata_filter_ids_capacity *= 2;
386 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));
387 decoder->private_->metadata_filter_ids_count++;
392 FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
394 FLAC__ASSERT(decoder != 0);
395 FLAC__ASSERT(decoder->private_ != 0);
396 FLAC__ASSERT(decoder->protected_ != 0);
397 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
399 /* OK, technically 0x01010101 != true, but it still means true */
400 memset(decoder->private_->metadata_filter, 1, sizeof(decoder->private_->metadata_filter));
401 decoder->private_->metadata_filter_ids_count = 0;
405 FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetaDataType type)
407 FLAC__ASSERT(decoder != 0);
408 FLAC__ASSERT(decoder->private_ != 0);
409 FLAC__ASSERT(decoder->protected_ != 0);
410 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
411 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
413 decoder->private_->metadata_filter[type] = false;
414 if(type == FLAC__METADATA_TYPE_APPLICATION)
415 decoder->private_->metadata_filter_ids_count = 0;
419 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
421 FLAC__ASSERT(decoder != 0);
422 FLAC__ASSERT(decoder->private_ != 0);
423 FLAC__ASSERT(decoder->protected_ != 0);
424 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
427 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
430 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
432 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
433 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
434 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
435 decoder->private_->metadata_filter_ids_capacity *= 2;
438 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));
439 decoder->private_->metadata_filter_ids_count++;
444 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
446 FLAC__ASSERT(decoder != 0);
447 FLAC__ASSERT(decoder->private_ != 0);
448 FLAC__ASSERT(decoder->protected_ != 0);
449 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
451 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
452 decoder->private_->metadata_filter_ids_count = 0;
456 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
458 FLAC__ASSERT(decoder != 0);
459 FLAC__ASSERT(decoder->protected_ != 0);
460 return decoder->protected_->state;
463 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
465 FLAC__ASSERT(decoder != 0);
466 FLAC__ASSERT(decoder->protected_ != 0);
467 return decoder->protected_->channels;
470 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
472 FLAC__ASSERT(decoder != 0);
473 FLAC__ASSERT(decoder->protected_ != 0);
474 return decoder->protected_->channel_assignment;
477 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
479 FLAC__ASSERT(decoder != 0);
480 FLAC__ASSERT(decoder->protected_ != 0);
481 return decoder->protected_->bits_per_sample;
484 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
486 FLAC__ASSERT(decoder != 0);
487 FLAC__ASSERT(decoder->protected_ != 0);
488 return decoder->protected_->sample_rate;
491 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
493 FLAC__ASSERT(decoder != 0);
494 FLAC__ASSERT(decoder->protected_ != 0);
495 return decoder->protected_->blocksize;
498 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
500 FLAC__ASSERT(decoder != 0);
501 FLAC__ASSERT(decoder->private_ != 0);
502 FLAC__ASSERT(decoder->protected_ != 0);
504 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
505 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
508 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
513 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
515 FLAC__ASSERT(decoder != 0);
516 FLAC__ASSERT(decoder->private_ != 0);
517 FLAC__ASSERT(decoder->protected_ != 0);
519 if(!FLAC__stream_decoder_flush(decoder)) {
520 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
523 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
525 decoder->private_->samples_decoded = 0;
530 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
533 FLAC__ASSERT(decoder != 0);
535 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
538 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
540 if(!FLAC__stream_decoder_reset(decoder)) {
541 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
546 switch(decoder->protected_->state) {
547 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
548 if(!stream_decoder_find_metadata_(decoder))
549 return false; /* above function sets the status for us */
551 case FLAC__STREAM_DECODER_READ_METADATA:
552 if(!stream_decoder_read_metadata_(decoder))
553 return false; /* above function sets the status for us */
555 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
556 if(!stream_decoder_frame_sync_(decoder))
557 return true; /* above function sets the status for us */
559 case FLAC__STREAM_DECODER_READ_FRAME:
560 if(!stream_decoder_read_frame_(decoder, &dummy))
561 return false; /* above function sets the status for us */
563 case FLAC__STREAM_DECODER_END_OF_STREAM:
571 FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
573 FLAC__ASSERT(decoder != 0);
575 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
578 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
580 if(!FLAC__stream_decoder_reset(decoder)) {
581 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
586 switch(decoder->protected_->state) {
587 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
588 if(!stream_decoder_find_metadata_(decoder))
589 return false; /* above function sets the status for us */
591 case FLAC__STREAM_DECODER_READ_METADATA:
592 if(!stream_decoder_read_metadata_(decoder))
593 return false; /* above function sets the status for us */
595 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
597 case FLAC__STREAM_DECODER_READ_FRAME:
599 case FLAC__STREAM_DECODER_END_OF_STREAM:
607 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
609 FLAC__bool got_a_frame;
610 FLAC__ASSERT(decoder != 0);
612 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
615 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
618 switch(decoder->protected_->state) {
619 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
620 if(!stream_decoder_frame_sync_(decoder))
621 return true; /* above function sets the status for us */
623 case FLAC__STREAM_DECODER_READ_FRAME:
624 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
625 return false; /* above function sets the status for us */
627 return true; /* above function sets the status for us */
629 case FLAC__STREAM_DECODER_END_OF_STREAM:
637 FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
640 FLAC__ASSERT(decoder != 0);
642 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
645 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
648 switch(decoder->protected_->state) {
649 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
650 if(!stream_decoder_frame_sync_(decoder))
651 return true; /* above function sets the status for us */
653 case FLAC__STREAM_DECODER_READ_FRAME:
654 if(!stream_decoder_read_frame_(decoder, &dummy))
655 return false; /* above function sets the status for us */
657 case FLAC__STREAM_DECODER_END_OF_STREAM:
665 /***********************************************************************
667 * Protected class methods
669 ***********************************************************************/
671 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
673 FLAC__ASSERT(decoder != 0);
674 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
677 /***********************************************************************
679 * Private class methods
681 ***********************************************************************/
683 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
688 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
691 /* @@@ should change to use realloc() */
693 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
694 if(decoder->private_->output[i] != 0) {
695 free(decoder->private_->output[i]);
696 decoder->private_->output[i] = 0;
698 if(decoder->private_->residual[i] != 0) {
699 free(decoder->private_->residual[i]);
700 decoder->private_->residual[i] = 0;
704 for(i = 0; i < channels; i++) {
705 /* 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. */
706 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
708 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
711 memset(tmp, 0, sizeof(FLAC__int32)*4);
712 decoder->private_->output[i] = tmp + 4;
714 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
716 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
719 decoder->private_->residual[i] = tmp;
722 decoder->private_->output_capacity = size;
723 decoder->private_->output_channels = channels;
728 FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
732 FLAC__ASSERT(0 != decoder);
733 FLAC__ASSERT(0 != decoder->private_);
735 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
736 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
742 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
746 FLAC__bool first = true;
748 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
750 for(i = id = 0; i < 4; ) {
751 if(decoder->private_->cached) {
752 x = (FLAC__uint32)decoder->private_->lookahead;
753 decoder->private_->cached = false;
756 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
757 return false; /* the read_callback_ sets the state for us */
759 if(x == FLAC__STREAM_SYNC_STRING[i]) {
765 if(x == ID3V2_TAG_[id]) {
769 if(!stream_decoder_skip_id3v2_tag_(decoder))
770 return false; /* the read_callback_ sets the state for us */
774 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
775 decoder->private_->header_warmup[0] = (FLAC__byte)x;
776 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
777 return false; /* the read_callback_ sets the state for us */
779 /* 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 */
780 /* else we have to check if the second byte is the end of a sync code */
781 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
782 decoder->private_->lookahead = (FLAC__byte)x;
783 decoder->private_->cached = true;
785 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
786 decoder->private_->header_warmup[1] = (FLAC__byte)x;
787 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
793 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
798 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
802 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
804 FLAC__uint32 i, x, last_block, type, length;
807 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
809 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_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, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
812 return false; /* the read_callback_ sets the state for us */
813 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
814 return false; /* the read_callback_ sets the state for us */
815 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
816 unsigned used_bits = 0;
817 decoder->private_->stream_info.type = type;
818 decoder->private_->stream_info.is_last = last_block;
819 decoder->private_->stream_info.length = length;
821 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
822 return false; /* the read_callback_ sets the state for us */
823 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
824 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
826 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
827 return false; /* the read_callback_ sets the state for us */
828 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
829 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
831 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
832 return false; /* the read_callback_ sets the state for us */
833 decoder->private_->stream_info.data.stream_info.min_framesize = x;
834 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
836 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
837 return false; /* the read_callback_ sets the state for us */
838 decoder->private_->stream_info.data.stream_info.max_framesize = x;
839 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
841 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
842 return false; /* the read_callback_ sets the state for us */
843 decoder->private_->stream_info.data.stream_info.sample_rate = x;
844 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
846 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
847 return false; /* the read_callback_ sets the state for us */
848 decoder->private_->stream_info.data.stream_info.channels = x+1;
849 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
851 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
852 return false; /* the read_callback_ sets the state for us */
853 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
854 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
856 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))
857 return false; /* the read_callback_ sets the state for us */
858 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
860 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
861 return false; /* the read_callback_ sets the state for us */
864 /* skip the rest of the block */
865 FLAC__ASSERT(used_bits % 8 == 0);
866 length -= (used_bits / 8);
867 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
868 return false; /* the read_callback_ sets the state for us */
870 decoder->private_->has_stream_info = true;
871 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
872 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
874 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
875 unsigned real_points;
877 decoder->private_->seek_table.type = type;
878 decoder->private_->seek_table.is_last = last_block;
879 decoder->private_->seek_table.length = length;
881 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
883 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)))) {
884 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
887 for(i = real_points = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
888 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
889 return false; /* the read_callback_ sets the state for us */
890 decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
892 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
893 return false; /* the read_callback_ sets the state for us */
894 decoder->private_->seek_table.data.seek_table.points[real_points].stream_offset = xx;
896 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
897 return false; /* the read_callback_ sets the state for us */
898 decoder->private_->seek_table.data.seek_table.points[real_points].frame_samples = x;
900 if(decoder->private_->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
903 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
904 decoder->private_->seek_table.data.seek_table.num_points = real_points;
906 /* if there is a partial point left, skip over it */
908 /*@@@ do an error_callback() here? there's an argument for either way */
909 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
910 return false; /* the read_callback_ sets the state for us */
913 decoder->private_->has_seek_table = true;
914 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
915 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
918 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
919 unsigned real_length = length;
920 FLAC__StreamMetaData block;
922 block.is_last = last_block;
924 block.length = length;
926 if(type == FLAC__METADATA_TYPE_APPLICATION) {
927 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
928 return false; /* the read_callback_ sets the state for us */
930 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
932 if(decoder->private_->metadata_filter_ids_count > 0 && stream_decoder_has_id_filtered_(decoder, block.data.application.id))
937 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
938 return false; /* the read_callback_ sets the state for us */
942 case FLAC__METADATA_TYPE_PADDING:
943 /* skip the padding bytes */
944 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
945 return false; /* the read_callback_ sets the state for us */
947 case FLAC__METADATA_TYPE_APPLICATION:
948 /* remember, we read the ID already */
949 if(real_length > 0) {
950 if(0 == (block.data.application.data = malloc(real_length))) {
951 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
954 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
955 return false; /* the read_callback_ sets the state for us */
958 block.data.application.data = 0;
960 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
961 /* read vendor string */
962 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))
963 return false; /* the read_callback_ sets the state for us */
964 if(block.data.vorbis_comment.vendor_string.length > 0) {
965 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
966 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
969 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))
970 return false; /* the read_callback_ sets the state for us */
973 block.data.vorbis_comment.vendor_string.entry = 0;
975 /* read num comments */
976 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))
977 return false; /* the read_callback_ sets the state for us */
980 if(block.data.vorbis_comment.num_comments > 0) {
981 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)))) {
982 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
985 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
986 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))
987 return false; /* the read_callback_ sets the state for us */
988 if(block.data.vorbis_comment.comments[i].length > 0) {
989 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
990 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
993 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))
994 return false; /* the read_callback_ sets the state for us */
997 block.data.vorbis_comment.comments[i].entry = 0;
1001 block.data.vorbis_comment.comments = 0;
1004 case FLAC__METADATA_TYPE_STREAMINFO:
1005 case FLAC__METADATA_TYPE_SEEKTABLE:
1009 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1011 /* now we have to free any malloc'ed data in the block */
1013 case FLAC__METADATA_TYPE_PADDING:
1015 case FLAC__METADATA_TYPE_APPLICATION:
1016 if(0 != block.data.application.data)
1017 free(block.data.application.data);
1019 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1020 if(0 != block.data.vorbis_comment.vendor_string.entry)
1021 free(block.data.vorbis_comment.vendor_string.entry);
1022 if(block.data.vorbis_comment.num_comments > 0)
1023 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1024 if(0 != block.data.vorbis_comment.comments[i].entry)
1025 free(block.data.vorbis_comment.comments[i].entry);
1026 if(0 != block.data.vorbis_comment.comments)
1027 free(block.data.vorbis_comment.comments);
1029 case FLAC__METADATA_TYPE_STREAMINFO:
1030 case FLAC__METADATA_TYPE_SEEKTABLE:
1038 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1043 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1048 /* skip the version and flags bytes */
1049 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1050 return false; /* the read_callback_ sets the state for us */
1051 /* get the size (in bytes) to skip */
1053 for(i = 0; i < 4; i++) {
1054 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1055 return false; /* the read_callback_ sets the state for us */
1059 /* skip the rest of the tag */
1060 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1061 return false; /* the read_callback_ sets the state for us */
1065 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
1068 FLAC__bool first = true;
1070 /* If we know the total number of samples in the stream, stop if we've read that many. */
1071 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1072 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1073 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1074 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1079 /* make sure we're byte aligned */
1080 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1081 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1082 return false; /* the read_callback_ sets the state for us */
1086 if(decoder->private_->cached) {
1087 x = (FLAC__uint32)decoder->private_->lookahead;
1088 decoder->private_->cached = false;
1091 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1092 return false; /* the read_callback_ sets the state for us */
1094 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1095 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1096 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1097 return false; /* the read_callback_ sets the state for us */
1099 /* 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 */
1100 /* else we have to check if the second byte is the end of a sync code */
1101 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1102 decoder->private_->lookahead = (FLAC__byte)x;
1103 decoder->private_->cached = true;
1105 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1106 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1107 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1112 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1120 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1124 FLAC__int32 mid, side, left, right;
1125 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1128 *got_a_frame = false;
1132 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1133 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1134 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1136 if(!stream_decoder_read_frame_header_(decoder))
1138 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1140 if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1142 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1144 * first figure the correct bits-per-sample of the subframe
1146 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1147 switch(decoder->private_->frame.header.channel_assignment) {
1148 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1149 /* no adjustment needed */
1151 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1152 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1156 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1157 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1161 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1162 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1172 if(!stream_decoder_read_subframe_(decoder, channel, bps))
1174 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1175 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1179 if(!stream_decoder_read_zero_padding_(decoder))
1183 * Read the frame CRC-16 from the footer and check
1185 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1186 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1187 return false; /* the read_callback_ sets the state for us */
1188 if(frame_crc == (FLAC__uint16)x) {
1189 /* Undo any special channel coding */
1190 switch(decoder->private_->frame.header.channel_assignment) {
1191 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1194 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1195 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1196 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1197 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1199 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1200 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1201 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1202 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1204 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1205 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1206 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1207 mid = decoder->private_->output[0][i];
1208 side = decoder->private_->output[1][i];
1210 if(side & 1) /* i.e. if 'side' is odd... */
1214 decoder->private_->output[0][i] = left >> 1;
1215 decoder->private_->output[1][i] = right >> 1;
1224 /* Bad frame, emit error and zero the output signal */
1225 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1226 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1227 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1231 *got_a_frame = true;
1233 /* put the latest values into the public section of the decoder instance */
1234 decoder->protected_->channels = decoder->private_->frame.header.channels;
1235 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1236 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1237 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1238 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1240 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1241 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1244 /* 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: */
1245 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
1248 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1252 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
1256 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1257 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1258 unsigned raw_header_len;
1259 FLAC__bool is_unparseable = false;
1260 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);
1261 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);
1263 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1265 /* init the raw header with the saved bits from synchronization */
1266 raw_header[0] = decoder->private_->header_warmup[0];
1267 raw_header[1] = decoder->private_->header_warmup[1];
1271 * check to make sure that the reserved bits are 0
1273 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1274 is_unparseable = true;
1278 * Note that along the way as we read the header, we look for a sync
1279 * code inside. If we find one it would indicate that our original
1280 * sync was bad since there cannot be a sync code in a valid header.
1284 * read in the raw header as bytes so we can CRC it, and parse it on the way
1286 for(i = 0; i < 2; i++) {
1287 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1288 return false; /* the read_callback_ sets the state for us */
1289 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1290 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1291 decoder->private_->lookahead = (FLAC__byte)x;
1292 decoder->private_->cached = true;
1293 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1294 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1297 raw_header[raw_header_len++] = (FLAC__byte)x;
1300 switch(x = raw_header[2] >> 4) {
1302 if(is_known_fixed_blocksize_stream)
1303 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1305 is_unparseable = true;
1308 decoder->private_->frame.header.blocksize = 192;
1314 decoder->private_->frame.header.blocksize = 576 << (x-2);
1328 decoder->private_->frame.header.blocksize = 256 << (x-8);
1335 switch(x = raw_header[2] & 0x0f) {
1337 if(decoder->private_->has_stream_info)
1338 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1340 is_unparseable = true;
1345 is_unparseable = true;
1348 decoder->private_->frame.header.sample_rate = 8000;
1351 decoder->private_->frame.header.sample_rate = 16000;
1354 decoder->private_->frame.header.sample_rate = 22050;
1357 decoder->private_->frame.header.sample_rate = 24000;
1360 decoder->private_->frame.header.sample_rate = 32000;
1363 decoder->private_->frame.header.sample_rate = 44100;
1366 decoder->private_->frame.header.sample_rate = 48000;
1369 decoder->private_->frame.header.sample_rate = 96000;
1374 sample_rate_hint = x;
1377 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1378 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1384 x = (unsigned)(raw_header[3] >> 4);
1386 decoder->private_->frame.header.channels = 2;
1389 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1392 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1395 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1398 is_unparseable = true;
1403 decoder->private_->frame.header.channels = (unsigned)x + 1;
1404 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1407 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1409 if(decoder->private_->has_stream_info)
1410 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1412 is_unparseable = true;
1415 decoder->private_->frame.header.bits_per_sample = 8;
1418 decoder->private_->frame.header.bits_per_sample = 12;
1421 decoder->private_->frame.header.bits_per_sample = 16;
1424 decoder->private_->frame.header.bits_per_sample = 20;
1427 decoder->private_->frame.header.bits_per_sample = 24;
1431 is_unparseable = true;
1438 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1439 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1440 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1444 if(blocksize_hint && is_known_variable_blocksize_stream) {
1445 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1446 return false; /* the read_callback_ sets the state for us */
1447 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1448 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1449 decoder->private_->cached = true;
1450 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1451 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1454 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1455 decoder->private_->frame.header.number.sample_number = xx;
1458 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1459 return false; /* the read_callback_ sets the state for us */
1460 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1461 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1462 decoder->private_->cached = true;
1463 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1464 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1467 decoder->private_->last_frame_number = x;
1468 if(decoder->private_->has_stream_info) {
1469 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1470 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1473 is_unparseable = true;
1477 if(blocksize_hint) {
1478 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1479 return false; /* the read_callback_ sets the state for us */
1480 raw_header[raw_header_len++] = (FLAC__byte)x;
1481 if(blocksize_hint == 7) {
1483 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1484 return false; /* the read_callback_ sets the state for us */
1485 raw_header[raw_header_len++] = (FLAC__byte)_x;
1488 decoder->private_->frame.header.blocksize = x+1;
1491 if(sample_rate_hint) {
1492 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1493 return false; /* the read_callback_ sets the state for us */
1494 raw_header[raw_header_len++] = (FLAC__byte)x;
1495 if(sample_rate_hint != 12) {
1497 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1498 return false; /* the read_callback_ sets the state for us */
1499 raw_header[raw_header_len++] = (FLAC__byte)_x;
1502 if(sample_rate_hint == 12)
1503 decoder->private_->frame.header.sample_rate = x*1000;
1504 else if(sample_rate_hint == 13)
1505 decoder->private_->frame.header.sample_rate = x;
1507 decoder->private_->frame.header.sample_rate = x*10;
1510 /* read the CRC-8 byte */
1511 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1512 return false; /* the read_callback_ sets the state for us */
1513 crc8 = (FLAC__byte)x;
1515 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1516 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1517 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1521 if(is_unparseable) {
1522 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1529 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1532 FLAC__bool wasted_bits;
1534 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1535 return false; /* the read_callback_ sets the state for us */
1537 wasted_bits = (x & 1);
1542 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1543 return false; /* the read_callback_ sets the state for us */
1544 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1545 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1548 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1551 * Lots of magic numbers here
1554 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1555 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1559 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1563 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1567 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1571 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1575 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1579 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1585 x = decoder->private_->frame.subframes[channel].wasted_bits;
1586 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1587 decoder->private_->output[channel][i] <<= x;
1593 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1595 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1598 FLAC__int32 *output = decoder->private_->output[channel];
1600 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1602 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1603 return false; /* the read_callback_ sets the state for us */
1605 subframe->value = x;
1607 /* decode the subframe */
1608 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1614 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1616 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1621 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1623 subframe->residual = decoder->private_->residual[channel];
1624 subframe->order = order;
1626 /* read warm-up samples */
1627 for(u = 0; u < order; u++) {
1628 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1629 return false; /* the read_callback_ sets the state for us */
1630 subframe->warmup[u] = i32;
1633 /* read entropy coding method info */
1634 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1635 return false; /* the read_callback_ sets the state for us */
1636 subframe->entropy_coding_method.type = u32;
1637 switch(subframe->entropy_coding_method.type) {
1638 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1639 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1640 return false; /* the read_callback_ sets the state for us */
1641 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1644 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1649 switch(subframe->entropy_coding_method.type) {
1650 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1651 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1658 /* decode the subframe */
1659 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1660 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1665 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1667 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1672 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1674 subframe->residual = decoder->private_->residual[channel];
1675 subframe->order = order;
1677 /* read warm-up samples */
1678 for(u = 0; u < order; u++) {
1679 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1680 return false; /* the read_callback_ sets the state for us */
1681 subframe->warmup[u] = i32;
1684 /* read qlp coeff precision */
1685 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1686 return false; /* the read_callback_ sets the state for us */
1687 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1688 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1689 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1692 subframe->qlp_coeff_precision = u32+1;
1694 /* read qlp shift */
1695 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1696 return false; /* the read_callback_ sets the state for us */
1697 subframe->quantization_level = i32;
1699 /* read quantized lp coefficiencts */
1700 for(u = 0; u < order; u++) {
1701 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1702 return false; /* the read_callback_ sets the state for us */
1703 subframe->qlp_coeff[u] = i32;
1706 /* read entropy coding method info */
1707 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1708 return false; /* the read_callback_ sets the state for us */
1709 subframe->entropy_coding_method.type = u32;
1710 switch(subframe->entropy_coding_method.type) {
1711 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1712 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1713 return false; /* the read_callback_ sets the state for us */
1714 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1717 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1722 switch(subframe->entropy_coding_method.type) {
1723 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1724 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1731 /* decode the subframe */
1732 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1733 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1734 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);
1736 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);
1741 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1743 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1744 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1747 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1749 subframe->data = residual;
1751 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1752 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1753 return false; /* the read_callback_ sets the state for us */
1757 /* decode the subframe */
1758 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1763 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1765 FLAC__uint32 rice_parameter;
1767 unsigned partition, sample, u;
1768 const unsigned partition_order = partitioned_rice->order;
1769 const unsigned partitions = 1u << partition_order;
1770 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1773 for(partition = 0; partition < partitions; partition++) {
1774 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1775 return false; /* the read_callback_ sets the state for us */
1776 partitioned_rice->parameters[partition] = rice_parameter;
1777 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1778 #ifdef FLAC__SYMMETRIC_RICE
1779 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1780 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1781 return false; /* the read_callback_ sets the state for us */
1782 residual[sample] = i;
1785 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1786 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1787 return false; /* the read_callback_ sets the state for us */
1792 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1793 return false; /* the read_callback_ sets the state for us */
1794 partitioned_rice->raw_bits[partition] = rice_parameter;
1795 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1796 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1797 return false; /* the read_callback_ sets the state for us */
1798 residual[sample] = i;
1806 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1808 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1809 FLAC__uint32 zero = 0;
1810 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1811 return false; /* the read_callback_ sets the state for us */
1813 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1814 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1820 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1822 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1823 FLAC__StreamDecoderReadStatus status;
1825 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1826 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1827 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1828 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1829 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1830 return status == FLAC__STREAM_DECODER_READ_CONTINUE;