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(0 == (block.data.application.data = malloc(real_length))) {
950 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
953 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
954 return false; /* the read_callback_ sets the state for us */
956 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
957 /* read vendor string */
958 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))
959 return false; /* the read_callback_ sets the state for us */
960 if(block.data.vorbis_comment.vendor_string.length > 0) {
961 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
962 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
965 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))
966 return false; /* the read_callback_ sets the state for us */
969 block.data.vorbis_comment.vendor_string.entry = 0;
971 /* read num comments */
972 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))
973 return false; /* the read_callback_ sets the state for us */
976 if(block.data.vorbis_comment.num_comments > 0) {
977 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)))) {
978 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
981 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
982 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))
983 return false; /* the read_callback_ sets the state for us */
984 if(block.data.vorbis_comment.comments[i].length > 0) {
985 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
986 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
989 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))
990 return false; /* the read_callback_ sets the state for us */
993 block.data.vorbis_comment.comments[i].entry = 0;
997 block.data.vorbis_comment.comments = 0;
1000 case FLAC__METADATA_TYPE_STREAMINFO:
1001 case FLAC__METADATA_TYPE_SEEKTABLE:
1005 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1007 /* now we have to free any malloc'ed data in the block */
1009 case FLAC__METADATA_TYPE_PADDING:
1011 case FLAC__METADATA_TYPE_APPLICATION:
1012 if(0 != block.data.application.data)
1013 free(block.data.application.data);
1015 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1016 if(0 != block.data.vorbis_comment.vendor_string.entry)
1017 free(block.data.vorbis_comment.vendor_string.entry);
1018 if(block.data.vorbis_comment.num_comments > 0)
1019 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1020 if(0 != block.data.vorbis_comment.comments[i].entry)
1021 free(block.data.vorbis_comment.comments[i].entry);
1022 if(0 != block.data.vorbis_comment.comments)
1023 free(block.data.vorbis_comment.comments);
1025 case FLAC__METADATA_TYPE_STREAMINFO:
1026 case FLAC__METADATA_TYPE_SEEKTABLE:
1034 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1039 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1044 /* skip the version and flags bytes */
1045 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1046 return false; /* the read_callback_ sets the state for us */
1047 /* get the size (in bytes) to skip */
1049 for(i = 0; i < 4; i++) {
1050 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1051 return false; /* the read_callback_ sets the state for us */
1055 /* skip the rest of the tag */
1056 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1057 return false; /* the read_callback_ sets the state for us */
1061 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
1064 FLAC__bool first = true;
1066 /* If we know the total number of samples in the stream, stop if we've read that many. */
1067 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1068 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1069 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1070 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1075 /* make sure we're byte aligned */
1076 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1077 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1078 return false; /* the read_callback_ sets the state for us */
1082 if(decoder->private_->cached) {
1083 x = (FLAC__uint32)decoder->private_->lookahead;
1084 decoder->private_->cached = false;
1087 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1088 return false; /* the read_callback_ sets the state for us */
1090 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1091 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1092 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1093 return false; /* the read_callback_ sets the state for us */
1095 /* 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 */
1096 /* else we have to check if the second byte is the end of a sync code */
1097 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1098 decoder->private_->lookahead = (FLAC__byte)x;
1099 decoder->private_->cached = true;
1101 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1102 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1103 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1108 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1116 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1120 FLAC__int32 mid, side, left, right;
1121 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1124 *got_a_frame = false;
1128 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1129 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1130 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1132 if(!stream_decoder_read_frame_header_(decoder))
1134 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1136 if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1138 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1140 * first figure the correct bits-per-sample of the subframe
1142 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1143 switch(decoder->private_->frame.header.channel_assignment) {
1144 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1145 /* no adjustment needed */
1147 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1148 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1152 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1153 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1157 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1158 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1168 if(!stream_decoder_read_subframe_(decoder, channel, bps))
1170 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1171 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1175 if(!stream_decoder_read_zero_padding_(decoder))
1179 * Read the frame CRC-16 from the footer and check
1181 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1182 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1183 return false; /* the read_callback_ sets the state for us */
1184 if(frame_crc == (FLAC__uint16)x) {
1185 /* Undo any special channel coding */
1186 switch(decoder->private_->frame.header.channel_assignment) {
1187 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1190 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1191 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1192 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1193 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1195 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1196 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1197 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1198 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1200 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1201 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1202 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1203 mid = decoder->private_->output[0][i];
1204 side = decoder->private_->output[1][i];
1206 if(side & 1) /* i.e. if 'side' is odd... */
1210 decoder->private_->output[0][i] = left >> 1;
1211 decoder->private_->output[1][i] = right >> 1;
1220 /* Bad frame, emit error and zero the output signal */
1221 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1222 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1223 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1227 *got_a_frame = true;
1229 /* put the latest values into the public section of the decoder instance */
1230 decoder->protected_->channels = decoder->private_->frame.header.channels;
1231 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1232 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1233 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1234 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1236 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1237 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1240 /* 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: */
1241 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
1244 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1248 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
1252 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1253 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1254 unsigned raw_header_len;
1255 FLAC__bool is_unparseable = false;
1256 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);
1257 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);
1259 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1261 /* init the raw header with the saved bits from synchronization */
1262 raw_header[0] = decoder->private_->header_warmup[0];
1263 raw_header[1] = decoder->private_->header_warmup[1];
1267 * check to make sure that the reserved bits are 0
1269 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1270 is_unparseable = true;
1274 * Note that along the way as we read the header, we look for a sync
1275 * code inside. If we find one it would indicate that our original
1276 * sync was bad since there cannot be a sync code in a valid header.
1280 * read in the raw header as bytes so we can CRC it, and parse it on the way
1282 for(i = 0; i < 2; i++) {
1283 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1284 return false; /* the read_callback_ sets the state for us */
1285 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1286 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1287 decoder->private_->lookahead = (FLAC__byte)x;
1288 decoder->private_->cached = true;
1289 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1290 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1293 raw_header[raw_header_len++] = (FLAC__byte)x;
1296 switch(x = raw_header[2] >> 4) {
1298 if(is_known_fixed_blocksize_stream)
1299 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1301 is_unparseable = true;
1304 decoder->private_->frame.header.blocksize = 192;
1310 decoder->private_->frame.header.blocksize = 576 << (x-2);
1324 decoder->private_->frame.header.blocksize = 256 << (x-8);
1331 switch(x = raw_header[2] & 0x0f) {
1333 if(decoder->private_->has_stream_info)
1334 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1336 is_unparseable = true;
1341 is_unparseable = true;
1344 decoder->private_->frame.header.sample_rate = 8000;
1347 decoder->private_->frame.header.sample_rate = 16000;
1350 decoder->private_->frame.header.sample_rate = 22050;
1353 decoder->private_->frame.header.sample_rate = 24000;
1356 decoder->private_->frame.header.sample_rate = 32000;
1359 decoder->private_->frame.header.sample_rate = 44100;
1362 decoder->private_->frame.header.sample_rate = 48000;
1365 decoder->private_->frame.header.sample_rate = 96000;
1370 sample_rate_hint = x;
1373 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1374 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1380 x = (unsigned)(raw_header[3] >> 4);
1382 decoder->private_->frame.header.channels = 2;
1385 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1388 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1391 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1394 is_unparseable = true;
1399 decoder->private_->frame.header.channels = (unsigned)x + 1;
1400 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1403 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1405 if(decoder->private_->has_stream_info)
1406 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1408 is_unparseable = true;
1411 decoder->private_->frame.header.bits_per_sample = 8;
1414 decoder->private_->frame.header.bits_per_sample = 12;
1417 decoder->private_->frame.header.bits_per_sample = 16;
1420 decoder->private_->frame.header.bits_per_sample = 20;
1423 decoder->private_->frame.header.bits_per_sample = 24;
1427 is_unparseable = true;
1434 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1435 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1436 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1440 if(blocksize_hint && is_known_variable_blocksize_stream) {
1441 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1442 return false; /* the read_callback_ sets the state for us */
1443 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1444 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1445 decoder->private_->cached = true;
1446 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1447 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1450 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1451 decoder->private_->frame.header.number.sample_number = xx;
1454 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1455 return false; /* the read_callback_ sets the state for us */
1456 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1457 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1458 decoder->private_->cached = true;
1459 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1460 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1463 decoder->private_->last_frame_number = x;
1464 if(decoder->private_->has_stream_info) {
1465 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1466 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1469 is_unparseable = true;
1473 if(blocksize_hint) {
1474 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1475 return false; /* the read_callback_ sets the state for us */
1476 raw_header[raw_header_len++] = (FLAC__byte)x;
1477 if(blocksize_hint == 7) {
1479 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1480 return false; /* the read_callback_ sets the state for us */
1481 raw_header[raw_header_len++] = (FLAC__byte)_x;
1484 decoder->private_->frame.header.blocksize = x+1;
1487 if(sample_rate_hint) {
1488 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1489 return false; /* the read_callback_ sets the state for us */
1490 raw_header[raw_header_len++] = (FLAC__byte)x;
1491 if(sample_rate_hint != 12) {
1493 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1494 return false; /* the read_callback_ sets the state for us */
1495 raw_header[raw_header_len++] = (FLAC__byte)_x;
1498 if(sample_rate_hint == 12)
1499 decoder->private_->frame.header.sample_rate = x*1000;
1500 else if(sample_rate_hint == 13)
1501 decoder->private_->frame.header.sample_rate = x;
1503 decoder->private_->frame.header.sample_rate = x*10;
1506 /* read the CRC-8 byte */
1507 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1508 return false; /* the read_callback_ sets the state for us */
1509 crc8 = (FLAC__byte)x;
1511 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1512 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1513 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1517 if(is_unparseable) {
1518 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1525 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1528 FLAC__bool wasted_bits;
1530 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1531 return false; /* the read_callback_ sets the state for us */
1533 wasted_bits = (x & 1);
1538 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1539 return false; /* the read_callback_ sets the state for us */
1540 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1541 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1544 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1547 * Lots of magic numbers here
1550 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1551 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1555 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1559 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1563 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1567 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1571 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1575 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1581 x = decoder->private_->frame.subframes[channel].wasted_bits;
1582 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1583 decoder->private_->output[channel][i] <<= x;
1589 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1591 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1594 FLAC__int32 *output = decoder->private_->output[channel];
1596 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1598 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1599 return false; /* the read_callback_ sets the state for us */
1601 subframe->value = x;
1603 /* decode the subframe */
1604 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1610 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1612 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1617 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1619 subframe->residual = decoder->private_->residual[channel];
1620 subframe->order = order;
1622 /* read warm-up samples */
1623 for(u = 0; u < order; u++) {
1624 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1625 return false; /* the read_callback_ sets the state for us */
1626 subframe->warmup[u] = i32;
1629 /* read entropy coding method info */
1630 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1631 return false; /* the read_callback_ sets the state for us */
1632 subframe->entropy_coding_method.type = u32;
1633 switch(subframe->entropy_coding_method.type) {
1634 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1635 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1636 return false; /* the read_callback_ sets the state for us */
1637 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1640 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1645 switch(subframe->entropy_coding_method.type) {
1646 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1647 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1654 /* decode the subframe */
1655 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1656 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1661 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1663 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1668 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1670 subframe->residual = decoder->private_->residual[channel];
1671 subframe->order = order;
1673 /* read warm-up samples */
1674 for(u = 0; u < order; u++) {
1675 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1676 return false; /* the read_callback_ sets the state for us */
1677 subframe->warmup[u] = i32;
1680 /* read qlp coeff precision */
1681 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1682 return false; /* the read_callback_ sets the state for us */
1683 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1684 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1685 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1688 subframe->qlp_coeff_precision = u32+1;
1690 /* read qlp shift */
1691 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1692 return false; /* the read_callback_ sets the state for us */
1693 subframe->quantization_level = i32;
1695 /* read quantized lp coefficiencts */
1696 for(u = 0; u < order; u++) {
1697 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1698 return false; /* the read_callback_ sets the state for us */
1699 subframe->qlp_coeff[u] = i32;
1702 /* read entropy coding method info */
1703 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1704 return false; /* the read_callback_ sets the state for us */
1705 subframe->entropy_coding_method.type = u32;
1706 switch(subframe->entropy_coding_method.type) {
1707 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1708 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1709 return false; /* the read_callback_ sets the state for us */
1710 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1713 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1718 switch(subframe->entropy_coding_method.type) {
1719 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1720 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1727 /* decode the subframe */
1728 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1729 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1730 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);
1732 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);
1737 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1739 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1740 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1743 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1745 subframe->data = residual;
1747 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1748 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1749 return false; /* the read_callback_ sets the state for us */
1753 /* decode the subframe */
1754 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1759 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1761 FLAC__uint32 rice_parameter;
1763 unsigned partition, sample, u;
1764 const unsigned partition_order = partitioned_rice->order;
1765 const unsigned partitions = 1u << partition_order;
1766 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1769 for(partition = 0; partition < partitions; partition++) {
1770 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1771 return false; /* the read_callback_ sets the state for us */
1772 partitioned_rice->parameters[partition] = rice_parameter;
1773 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1774 #ifdef FLAC__SYMMETRIC_RICE
1775 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1776 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1777 return false; /* the read_callback_ sets the state for us */
1778 residual[sample] = i;
1781 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1782 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1783 return false; /* the read_callback_ sets the state for us */
1788 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1789 return false; /* the read_callback_ sets the state for us */
1790 partitioned_rice->raw_bits[partition] = rice_parameter;
1791 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1792 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1793 return false; /* the read_callback_ sets the state for us */
1794 residual[sample] = i;
1802 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1804 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1805 FLAC__uint32 zero = 0;
1806 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1807 return false; /* the read_callback_ sets the state for us */
1809 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1810 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1816 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1818 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1819 FLAC__StreamDecoderReadStatus status;
1821 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1822 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1823 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1824 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1825 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1826 return status == FLAC__STREAM_DECODER_READ_CONTINUE;