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 void stream_decoder_set_defaults_(FLAC__StreamDecoder *decoder);
46 static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
47 static FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
48 static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
49 static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
50 static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
51 static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
52 static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
53 static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
54 static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
55 static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
56 static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
57 static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
58 static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
59 static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
60 static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
61 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
63 /***********************************************************************
67 ***********************************************************************/
69 typedef struct FLAC__StreamDecoderPrivate {
70 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
71 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
72 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
73 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
74 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[]);
75 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[]);
77 FLAC__BitBuffer *input;
78 FLAC__int32 *output[FLAC__MAX_CHANNELS];
79 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
80 unsigned output_capacity, output_channels;
81 FLAC__uint32 last_frame_number;
82 FLAC__uint64 samples_decoded;
83 FLAC__bool has_stream_info, has_seek_table;
84 FLAC__StreamMetadata stream_info;
85 FLAC__StreamMetadata seek_table;
86 FLAC__bool metadata_filter[FLAC__METADATA_TYPE_VORBIS_COMMENT+1];
87 FLAC__byte *metadata_filter_ids;
88 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
90 FLAC__bool cached; /* true if there is a byte in lookahead */
91 FLAC__CPUInfo cpuinfo;
92 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
93 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
94 } FLAC__StreamDecoderPrivate;
96 /***********************************************************************
98 * Public static class data
100 ***********************************************************************/
102 const char * const FLAC__StreamDecoderStateString[] = {
103 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
104 "FLAC__STREAM_DECODER_READ_METADATA",
105 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
106 "FLAC__STREAM_DECODER_READ_FRAME",
107 "FLAC__STREAM_DECODER_END_OF_STREAM",
108 "FLAC__STREAM_DECODER_ABORTED",
109 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
110 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
111 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
112 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
113 "FLAC__STREAM_DECODER_UNINITIALIZED"
116 const char * const FLAC__StreamDecoderReadStatusString[] = {
117 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
118 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
119 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
122 const char * const FLAC__StreamDecoderWriteStatusString[] = {
123 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
124 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
127 const char * const FLAC__StreamDecoderErrorStatusString[] = {
128 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
129 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
130 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
133 /***********************************************************************
135 * Class constructor/destructor
137 ***********************************************************************/
138 FLAC__StreamDecoder *FLAC__stream_decoder_new()
140 FLAC__StreamDecoder *decoder;
143 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
145 decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
149 decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
150 if(decoder->protected_ == 0) {
154 decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
155 if(decoder->private_ == 0) {
156 free(decoder->protected_);
160 decoder->private_->input = FLAC__bitbuffer_new();
161 if(decoder->private_->input == 0) {
162 free(decoder->private_);
163 free(decoder->protected_);
168 decoder->private_->metadata_filter_ids_capacity = 16;
169 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
170 FLAC__bitbuffer_delete(decoder->private_->input);
171 free(decoder->private_);
172 free(decoder->protected_);
177 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
178 decoder->private_->output[i] = 0;
179 decoder->private_->residual[i] = 0;
182 decoder->private_->output_capacity = 0;
183 decoder->private_->output_channels = 0;
184 decoder->private_->has_seek_table = false;
186 stream_decoder_set_defaults_(decoder);
188 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
193 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
195 FLAC__ASSERT(decoder != 0);
196 FLAC__ASSERT(decoder->protected_ != 0);
197 FLAC__ASSERT(decoder->private_ != 0);
198 FLAC__ASSERT(decoder->private_->input != 0);
200 FLAC__stream_decoder_finish(decoder);
202 if(decoder->private_->metadata_filter_ids != 0)
203 free(decoder->private_->metadata_filter_ids);
205 FLAC__bitbuffer_delete(decoder->private_->input);
206 free(decoder->private_);
207 free(decoder->protected_);
211 /***********************************************************************
213 * Public class methods
215 ***********************************************************************/
217 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
219 FLAC__ASSERT(decoder != 0);
221 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
222 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
224 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
225 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
227 if(!FLAC__bitbuffer_init(decoder->private_->input))
228 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
230 decoder->private_->last_frame_number = 0;
231 decoder->private_->samples_decoded = 0;
232 decoder->private_->has_stream_info = false;
233 decoder->private_->cached = false;
236 * get the CPU info and set the function pointers
238 FLAC__cpu_info(&decoder->private_->cpuinfo);
239 /* first default to the non-asm routines */
240 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
241 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
242 /* now override with asm where appropriate */
244 if(decoder->private_->cpuinfo.use_asm) {
245 #ifdef FLAC__CPU_IA32
246 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
247 #ifdef FLAC__HAS_NASM
248 if(decoder->private_->cpuinfo.data.ia32.mmx) {
249 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
250 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
253 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
254 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
261 return decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
264 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
267 FLAC__ASSERT(decoder != 0);
268 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
270 if(decoder->private_->has_seek_table) {
271 FLAC__ASSERT(decoder->private_->seek_table.data.seek_table.points != 0);
272 free(decoder->private_->seek_table.data.seek_table.points);
273 decoder->private_->seek_table.data.seek_table.points = 0;
274 decoder->private_->has_seek_table = false;
276 FLAC__bitbuffer_free(decoder->private_->input);
277 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
278 /* 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. */
279 if(decoder->private_->output[i] != 0) {
280 free(decoder->private_->output[i]-4);
281 decoder->private_->output[i] = 0;
283 if(decoder->private_->residual[i] != 0) {
284 free(decoder->private_->residual[i]);
285 decoder->private_->residual[i] = 0;
288 decoder->private_->output_capacity = 0;
289 decoder->private_->output_channels = 0;
291 stream_decoder_set_defaults_(decoder);
293 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
296 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))
298 FLAC__ASSERT(decoder != 0);
299 FLAC__ASSERT(decoder->private_ != 0);
300 FLAC__ASSERT(decoder->protected_ != 0);
301 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
303 decoder->private_->read_callback = value;
307 FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data))
309 FLAC__ASSERT(decoder != 0);
310 FLAC__ASSERT(decoder->private_ != 0);
311 FLAC__ASSERT(decoder->protected_ != 0);
312 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
314 decoder->private_->write_callback = value;
318 FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data))
320 FLAC__ASSERT(decoder != 0);
321 FLAC__ASSERT(decoder->private_ != 0);
322 FLAC__ASSERT(decoder->protected_ != 0);
323 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
325 decoder->private_->metadata_callback = value;
329 FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
331 FLAC__ASSERT(decoder != 0);
332 FLAC__ASSERT(decoder->private_ != 0);
333 FLAC__ASSERT(decoder->protected_ != 0);
334 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
336 decoder->private_->error_callback = value;
340 FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
342 FLAC__ASSERT(decoder != 0);
343 FLAC__ASSERT(decoder->private_ != 0);
344 FLAC__ASSERT(decoder->protected_ != 0);
345 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
347 decoder->private_->client_data = value;
351 FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
353 FLAC__ASSERT(decoder != 0);
354 FLAC__ASSERT(decoder->private_ != 0);
355 FLAC__ASSERT(decoder->protected_ != 0);
356 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
357 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
359 decoder->private_->metadata_filter[type] = true;
360 if(type == FLAC__METADATA_TYPE_APPLICATION)
361 decoder->private_->metadata_filter_ids_count = 0;
365 FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
367 FLAC__ASSERT(decoder != 0);
368 FLAC__ASSERT(decoder->private_ != 0);
369 FLAC__ASSERT(decoder->protected_ != 0);
370 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
373 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
376 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
378 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
379 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
380 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
381 decoder->private_->metadata_filter_ids_capacity *= 2;
384 memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
385 decoder->private_->metadata_filter_ids_count++;
390 FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
393 FLAC__ASSERT(decoder != 0);
394 FLAC__ASSERT(decoder->private_ != 0);
395 FLAC__ASSERT(decoder->protected_ != 0);
396 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
398 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
399 decoder->private_->metadata_filter[i] = true;
400 decoder->private_->metadata_filter_ids_count = 0;
404 FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
406 FLAC__ASSERT(decoder != 0);
407 FLAC__ASSERT(decoder->private_ != 0);
408 FLAC__ASSERT(decoder->protected_ != 0);
409 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
410 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
412 decoder->private_->metadata_filter[type] = false;
413 if(type == FLAC__METADATA_TYPE_APPLICATION)
414 decoder->private_->metadata_filter_ids_count = 0;
418 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
420 FLAC__ASSERT(decoder != 0);
421 FLAC__ASSERT(decoder->private_ != 0);
422 FLAC__ASSERT(decoder->protected_ != 0);
423 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
426 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
429 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
431 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
432 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
433 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
434 decoder->private_->metadata_filter_ids_capacity *= 2;
437 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));
438 decoder->private_->metadata_filter_ids_count++;
443 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
445 FLAC__ASSERT(decoder != 0);
446 FLAC__ASSERT(decoder->private_ != 0);
447 FLAC__ASSERT(decoder->protected_ != 0);
448 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
450 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
451 decoder->private_->metadata_filter_ids_count = 0;
455 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
457 FLAC__ASSERT(decoder != 0);
458 FLAC__ASSERT(decoder->protected_ != 0);
459 return decoder->protected_->state;
462 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
464 FLAC__ASSERT(decoder != 0);
465 FLAC__ASSERT(decoder->protected_ != 0);
466 return decoder->protected_->channels;
469 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
471 FLAC__ASSERT(decoder != 0);
472 FLAC__ASSERT(decoder->protected_ != 0);
473 return decoder->protected_->channel_assignment;
476 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
478 FLAC__ASSERT(decoder != 0);
479 FLAC__ASSERT(decoder->protected_ != 0);
480 return decoder->protected_->bits_per_sample;
483 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
485 FLAC__ASSERT(decoder != 0);
486 FLAC__ASSERT(decoder->protected_ != 0);
487 return decoder->protected_->sample_rate;
490 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
492 FLAC__ASSERT(decoder != 0);
493 FLAC__ASSERT(decoder->protected_ != 0);
494 return decoder->protected_->blocksize;
497 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
499 FLAC__ASSERT(decoder != 0);
500 FLAC__ASSERT(decoder->private_ != 0);
501 FLAC__ASSERT(decoder->protected_ != 0);
503 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
504 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
507 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
512 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
514 FLAC__ASSERT(decoder != 0);
515 FLAC__ASSERT(decoder->private_ != 0);
516 FLAC__ASSERT(decoder->protected_ != 0);
518 if(!FLAC__stream_decoder_flush(decoder)) {
519 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
522 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
524 decoder->private_->samples_decoded = 0;
529 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
532 FLAC__ASSERT(decoder != 0);
534 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
537 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
539 if(!FLAC__stream_decoder_reset(decoder)) {
540 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
545 switch(decoder->protected_->state) {
546 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
547 if(!stream_decoder_find_metadata_(decoder))
548 return false; /* above function sets the status for us */
550 case FLAC__STREAM_DECODER_READ_METADATA:
551 if(!stream_decoder_read_metadata_(decoder))
552 return false; /* above function sets the status for us */
554 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
555 if(!stream_decoder_frame_sync_(decoder))
556 return true; /* above function sets the status for us */
558 case FLAC__STREAM_DECODER_READ_FRAME:
559 if(!stream_decoder_read_frame_(decoder, &dummy))
560 return false; /* above function sets the status for us */
562 case FLAC__STREAM_DECODER_END_OF_STREAM:
563 case FLAC__STREAM_DECODER_ABORTED:
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:
596 case FLAC__STREAM_DECODER_READ_FRAME:
597 case FLAC__STREAM_DECODER_END_OF_STREAM:
598 case FLAC__STREAM_DECODER_ABORTED:
606 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
608 FLAC__bool got_a_frame;
609 FLAC__ASSERT(decoder != 0);
611 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
614 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
617 switch(decoder->protected_->state) {
618 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
619 if(!stream_decoder_frame_sync_(decoder))
620 return true; /* above function sets the status for us */
622 case FLAC__STREAM_DECODER_READ_FRAME:
623 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
624 return false; /* above function sets the status for us */
626 return true; /* above function sets the status for us */
628 case FLAC__STREAM_DECODER_END_OF_STREAM:
629 case FLAC__STREAM_DECODER_ABORTED:
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:
658 case FLAC__STREAM_DECODER_ABORTED:
666 /***********************************************************************
668 * Protected class methods
670 ***********************************************************************/
672 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
674 FLAC__ASSERT(decoder != 0);
675 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
678 /***********************************************************************
680 * Private class methods
682 ***********************************************************************/
684 void stream_decoder_set_defaults_(FLAC__StreamDecoder *decoder)
686 decoder->private_->read_callback = 0;
687 decoder->private_->write_callback = 0;
688 decoder->private_->metadata_callback = 0;
689 decoder->private_->error_callback = 0;
690 decoder->private_->client_data = 0;
692 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
693 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
694 decoder->private_->metadata_filter_ids_count = 0;
697 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
702 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
705 /* @@@ should change to use realloc() */
707 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
708 if(decoder->private_->output[i] != 0) {
709 free(decoder->private_->output[i]);
710 decoder->private_->output[i] = 0;
712 if(decoder->private_->residual[i] != 0) {
713 free(decoder->private_->residual[i]);
714 decoder->private_->residual[i] = 0;
718 for(i = 0; i < channels; i++) {
719 /* 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. */
720 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
722 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
725 memset(tmp, 0, sizeof(FLAC__int32)*4);
726 decoder->private_->output[i] = tmp + 4;
728 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
730 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
733 decoder->private_->residual[i] = tmp;
736 decoder->private_->output_capacity = size;
737 decoder->private_->output_channels = channels;
742 FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
746 FLAC__ASSERT(0 != decoder);
747 FLAC__ASSERT(0 != decoder->private_);
749 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
750 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
756 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
760 FLAC__bool first = true;
762 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
764 for(i = id = 0; i < 4; ) {
765 if(decoder->private_->cached) {
766 x = (FLAC__uint32)decoder->private_->lookahead;
767 decoder->private_->cached = false;
770 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
771 return false; /* the read_callback_ sets the state for us */
773 if(x == FLAC__STREAM_SYNC_STRING[i]) {
779 if(x == ID3V2_TAG_[id]) {
783 if(!stream_decoder_skip_id3v2_tag_(decoder))
784 return false; /* the read_callback_ sets the state for us */
788 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
789 decoder->private_->header_warmup[0] = (FLAC__byte)x;
790 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
791 return false; /* the read_callback_ sets the state for us */
793 /* 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 */
794 /* else we have to check if the second byte is the end of a sync code */
795 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
796 decoder->private_->lookahead = (FLAC__byte)x;
797 decoder->private_->cached = true;
799 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
800 decoder->private_->header_warmup[1] = (FLAC__byte)x;
801 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
807 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
812 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
816 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
818 FLAC__uint32 i, x, last_block, type, length;
821 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
823 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
824 return false; /* the read_callback_ sets the state for us */
825 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
826 return false; /* the read_callback_ sets the state for us */
827 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
828 return false; /* the read_callback_ sets the state for us */
829 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
830 unsigned used_bits = 0;
831 decoder->private_->stream_info.type = type;
832 decoder->private_->stream_info.is_last = last_block;
833 decoder->private_->stream_info.length = length;
835 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
836 return false; /* the read_callback_ sets the state for us */
837 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
838 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
840 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
841 return false; /* the read_callback_ sets the state for us */
842 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
843 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
845 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
846 return false; /* the read_callback_ sets the state for us */
847 decoder->private_->stream_info.data.stream_info.min_framesize = x;
848 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
850 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
851 return false; /* the read_callback_ sets the state for us */
852 decoder->private_->stream_info.data.stream_info.max_framesize = x;
853 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
855 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
856 return false; /* the read_callback_ sets the state for us */
857 decoder->private_->stream_info.data.stream_info.sample_rate = x;
858 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
860 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
861 return false; /* the read_callback_ sets the state for us */
862 decoder->private_->stream_info.data.stream_info.channels = x+1;
863 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
865 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
866 return false; /* the read_callback_ sets the state for us */
867 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
868 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
870 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))
871 return false; /* the read_callback_ sets the state for us */
872 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
874 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
875 return false; /* the read_callback_ sets the state for us */
878 /* skip the rest of the block */
879 FLAC__ASSERT(used_bits % 8 == 0);
880 length -= (used_bits / 8);
881 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
882 return false; /* the read_callback_ sets the state for us */
884 decoder->private_->has_stream_info = true;
885 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
886 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
888 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
889 decoder->private_->seek_table.type = type;
890 decoder->private_->seek_table.is_last = last_block;
891 decoder->private_->seek_table.length = length;
893 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
895 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)))) {
896 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
899 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
900 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
901 return false; /* the read_callback_ sets the state for us */
902 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
904 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
905 return false; /* the read_callback_ sets the state for us */
906 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
908 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
909 return false; /* the read_callback_ sets the state for us */
910 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
912 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
913 /* if there is a partial point left, skip over it */
915 /*@@@ do an error_callback() here? there's an argument for either way */
916 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
917 return false; /* the read_callback_ sets the state for us */
920 decoder->private_->has_seek_table = true;
921 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
922 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
925 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
926 unsigned real_length = length;
927 FLAC__StreamMetadata block;
929 block.is_last = last_block;
931 block.length = length;
933 if(type == FLAC__METADATA_TYPE_APPLICATION) {
934 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
935 return false; /* the read_callback_ sets the state for us */
937 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
939 if(decoder->private_->metadata_filter_ids_count > 0 && stream_decoder_has_id_filtered_(decoder, block.data.application.id))
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 */
949 case FLAC__METADATA_TYPE_PADDING:
950 /* skip the padding bytes */
951 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
952 return false; /* the read_callback_ sets the state for us */
954 case FLAC__METADATA_TYPE_APPLICATION:
955 /* remember, we read the ID already */
956 if(real_length > 0) {
957 if(0 == (block.data.application.data = malloc(real_length))) {
958 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
961 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
962 return false; /* the read_callback_ sets the state for us */
965 block.data.application.data = 0;
967 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
968 /* read vendor string */
969 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
970 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
971 return false; /* the read_callback_ sets the state for us */
972 if(block.data.vorbis_comment.vendor_string.length > 0) {
973 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
974 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
977 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))
978 return false; /* the read_callback_ sets the state for us */
981 block.data.vorbis_comment.vendor_string.entry = 0;
983 /* read num comments */
984 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
985 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.num_comments, read_callback_, decoder))
986 return false; /* the read_callback_ sets the state for us */
989 if(block.data.vorbis_comment.num_comments > 0) {
990 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
991 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
994 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
995 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
996 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
997 return false; /* the read_callback_ sets the state for us */
998 if(block.data.vorbis_comment.comments[i].length > 0) {
999 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
1000 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1003 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))
1004 return false; /* the read_callback_ sets the state for us */
1007 block.data.vorbis_comment.comments[i].entry = 0;
1011 block.data.vorbis_comment.comments = 0;
1014 case FLAC__METADATA_TYPE_STREAMINFO:
1015 case FLAC__METADATA_TYPE_SEEKTABLE:
1019 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1021 /* now we have to free any malloc'ed data in the block */
1023 case FLAC__METADATA_TYPE_PADDING:
1025 case FLAC__METADATA_TYPE_APPLICATION:
1026 if(0 != block.data.application.data)
1027 free(block.data.application.data);
1029 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1030 if(0 != block.data.vorbis_comment.vendor_string.entry)
1031 free(block.data.vorbis_comment.vendor_string.entry);
1032 if(block.data.vorbis_comment.num_comments > 0)
1033 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1034 if(0 != block.data.vorbis_comment.comments[i].entry)
1035 free(block.data.vorbis_comment.comments[i].entry);
1036 if(0 != block.data.vorbis_comment.comments)
1037 free(block.data.vorbis_comment.comments);
1039 case FLAC__METADATA_TYPE_STREAMINFO:
1040 case FLAC__METADATA_TYPE_SEEKTABLE:
1048 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1053 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1058 /* skip the version and flags bytes */
1059 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1060 return false; /* the read_callback_ sets the state for us */
1061 /* get the size (in bytes) to skip */
1063 for(i = 0; i < 4; i++) {
1064 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1065 return false; /* the read_callback_ sets the state for us */
1069 /* skip the rest of the tag */
1070 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1071 return false; /* the read_callback_ sets the state for us */
1075 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
1078 FLAC__bool first = true;
1080 /* If we know the total number of samples in the stream, stop if we've read that many. */
1081 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1082 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1083 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1084 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1089 /* make sure we're byte aligned */
1090 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1091 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1092 return false; /* the read_callback_ sets the state for us */
1096 if(decoder->private_->cached) {
1097 x = (FLAC__uint32)decoder->private_->lookahead;
1098 decoder->private_->cached = false;
1101 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1102 return false; /* the read_callback_ sets the state for us */
1104 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1105 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1106 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1107 return false; /* the read_callback_ sets the state for us */
1109 /* 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 */
1110 /* else we have to check if the second byte is the end of a sync code */
1111 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1112 decoder->private_->lookahead = (FLAC__byte)x;
1113 decoder->private_->cached = true;
1115 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1116 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1117 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1122 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1130 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1134 FLAC__int32 mid, side, left, right;
1135 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1138 *got_a_frame = false;
1142 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1143 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1144 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1146 if(!stream_decoder_read_frame_header_(decoder))
1148 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1150 if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1152 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1154 * first figure the correct bits-per-sample of the subframe
1156 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1157 switch(decoder->private_->frame.header.channel_assignment) {
1158 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1159 /* no adjustment needed */
1161 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1162 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1166 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1167 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1171 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1172 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1182 if(!stream_decoder_read_subframe_(decoder, channel, bps))
1184 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1185 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1189 if(!stream_decoder_read_zero_padding_(decoder))
1193 * Read the frame CRC-16 from the footer and check
1195 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1196 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1197 return false; /* the read_callback_ sets the state for us */
1198 if(frame_crc == (FLAC__uint16)x) {
1199 /* Undo any special channel coding */
1200 switch(decoder->private_->frame.header.channel_assignment) {
1201 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1204 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1205 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1206 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1207 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1209 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1210 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1211 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1212 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1214 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1215 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1216 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1217 mid = decoder->private_->output[0][i];
1218 side = decoder->private_->output[1][i];
1220 if(side & 1) /* i.e. if 'side' is odd... */
1224 decoder->private_->output[0][i] = left >> 1;
1225 decoder->private_->output[1][i] = right >> 1;
1234 /* Bad frame, emit error and zero the output signal */
1235 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1236 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1237 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1241 *got_a_frame = true;
1243 /* put the latest values into the public section of the decoder instance */
1244 decoder->protected_->channels = decoder->private_->frame.header.channels;
1245 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1246 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1247 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1248 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1250 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1251 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1254 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
1257 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1261 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
1265 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1266 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1267 unsigned raw_header_len;
1268 FLAC__bool is_unparseable = false;
1269 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);
1270 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);
1272 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1274 /* init the raw header with the saved bits from synchronization */
1275 raw_header[0] = decoder->private_->header_warmup[0];
1276 raw_header[1] = decoder->private_->header_warmup[1];
1280 * check to make sure that the reserved bits are 0
1282 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1283 is_unparseable = true;
1287 * Note that along the way as we read the header, we look for a sync
1288 * code inside. If we find one it would indicate that our original
1289 * sync was bad since there cannot be a sync code in a valid header.
1293 * read in the raw header as bytes so we can CRC it, and parse it on the way
1295 for(i = 0; i < 2; i++) {
1296 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1297 return false; /* the read_callback_ sets the state for us */
1298 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1299 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1300 decoder->private_->lookahead = (FLAC__byte)x;
1301 decoder->private_->cached = true;
1302 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1303 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1306 raw_header[raw_header_len++] = (FLAC__byte)x;
1309 switch(x = raw_header[2] >> 4) {
1311 if(is_known_fixed_blocksize_stream)
1312 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1314 is_unparseable = true;
1317 decoder->private_->frame.header.blocksize = 192;
1323 decoder->private_->frame.header.blocksize = 576 << (x-2);
1337 decoder->private_->frame.header.blocksize = 256 << (x-8);
1344 switch(x = raw_header[2] & 0x0f) {
1346 if(decoder->private_->has_stream_info)
1347 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1349 is_unparseable = true;
1354 is_unparseable = true;
1357 decoder->private_->frame.header.sample_rate = 8000;
1360 decoder->private_->frame.header.sample_rate = 16000;
1363 decoder->private_->frame.header.sample_rate = 22050;
1366 decoder->private_->frame.header.sample_rate = 24000;
1369 decoder->private_->frame.header.sample_rate = 32000;
1372 decoder->private_->frame.header.sample_rate = 44100;
1375 decoder->private_->frame.header.sample_rate = 48000;
1378 decoder->private_->frame.header.sample_rate = 96000;
1383 sample_rate_hint = x;
1386 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1387 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1393 x = (unsigned)(raw_header[3] >> 4);
1395 decoder->private_->frame.header.channels = 2;
1398 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1401 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1404 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1407 is_unparseable = true;
1412 decoder->private_->frame.header.channels = (unsigned)x + 1;
1413 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1416 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1418 if(decoder->private_->has_stream_info)
1419 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1421 is_unparseable = true;
1424 decoder->private_->frame.header.bits_per_sample = 8;
1427 decoder->private_->frame.header.bits_per_sample = 12;
1430 decoder->private_->frame.header.bits_per_sample = 16;
1433 decoder->private_->frame.header.bits_per_sample = 20;
1436 decoder->private_->frame.header.bits_per_sample = 24;
1440 is_unparseable = true;
1447 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1448 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1449 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1453 if(blocksize_hint && is_known_variable_blocksize_stream) {
1454 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1455 return false; /* the read_callback_ sets the state for us */
1456 if(xx == 0xffffffffffffffff) { /* 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_STATUS_BAD_HEADER, decoder->private_->client_data);
1460 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1463 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1464 decoder->private_->frame.header.number.sample_number = xx;
1467 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1468 return false; /* the read_callback_ sets the state for us */
1469 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1470 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1471 decoder->private_->cached = true;
1472 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1473 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1476 decoder->private_->last_frame_number = x;
1477 if(decoder->private_->has_stream_info) {
1478 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1479 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1482 is_unparseable = true;
1486 if(blocksize_hint) {
1487 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1488 return false; /* the read_callback_ sets the state for us */
1489 raw_header[raw_header_len++] = (FLAC__byte)x;
1490 if(blocksize_hint == 7) {
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;
1497 decoder->private_->frame.header.blocksize = x+1;
1500 if(sample_rate_hint) {
1501 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1502 return false; /* the read_callback_ sets the state for us */
1503 raw_header[raw_header_len++] = (FLAC__byte)x;
1504 if(sample_rate_hint != 12) {
1506 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1507 return false; /* the read_callback_ sets the state for us */
1508 raw_header[raw_header_len++] = (FLAC__byte)_x;
1511 if(sample_rate_hint == 12)
1512 decoder->private_->frame.header.sample_rate = x*1000;
1513 else if(sample_rate_hint == 13)
1514 decoder->private_->frame.header.sample_rate = x;
1516 decoder->private_->frame.header.sample_rate = x*10;
1519 /* read the CRC-8 byte */
1520 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1521 return false; /* the read_callback_ sets the state for us */
1522 crc8 = (FLAC__byte)x;
1524 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1525 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1526 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1530 if(is_unparseable) {
1531 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1538 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1541 FLAC__bool wasted_bits;
1543 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1544 return false; /* the read_callback_ sets the state for us */
1546 wasted_bits = (x & 1);
1551 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1552 return false; /* the read_callback_ sets the state for us */
1553 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1554 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1557 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1560 * Lots of magic numbers here
1563 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1564 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1568 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1572 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1576 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1580 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1584 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1588 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1594 x = decoder->private_->frame.subframes[channel].wasted_bits;
1595 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1596 decoder->private_->output[channel][i] <<= x;
1602 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1604 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1607 FLAC__int32 *output = decoder->private_->output[channel];
1609 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1611 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1612 return false; /* the read_callback_ sets the state for us */
1614 subframe->value = x;
1616 /* decode the subframe */
1617 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1623 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1625 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1630 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1632 subframe->residual = decoder->private_->residual[channel];
1633 subframe->order = order;
1635 /* read warm-up samples */
1636 for(u = 0; u < order; u++) {
1637 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1638 return false; /* the read_callback_ sets the state for us */
1639 subframe->warmup[u] = i32;
1642 /* read entropy coding method info */
1643 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1644 return false; /* the read_callback_ sets the state for us */
1645 subframe->entropy_coding_method.type = u32;
1646 switch(subframe->entropy_coding_method.type) {
1647 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1648 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1649 return false; /* the read_callback_ sets the state for us */
1650 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1653 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1658 switch(subframe->entropy_coding_method.type) {
1659 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1660 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1667 /* decode the subframe */
1668 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1669 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1674 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1676 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1681 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1683 subframe->residual = decoder->private_->residual[channel];
1684 subframe->order = order;
1686 /* read warm-up samples */
1687 for(u = 0; u < order; u++) {
1688 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1689 return false; /* the read_callback_ sets the state for us */
1690 subframe->warmup[u] = i32;
1693 /* read qlp coeff precision */
1694 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1695 return false; /* the read_callback_ sets the state for us */
1696 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1697 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1698 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1701 subframe->qlp_coeff_precision = u32+1;
1703 /* read qlp shift */
1704 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1705 return false; /* the read_callback_ sets the state for us */
1706 subframe->quantization_level = i32;
1708 /* read quantized lp coefficiencts */
1709 for(u = 0; u < order; u++) {
1710 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1711 return false; /* the read_callback_ sets the state for us */
1712 subframe->qlp_coeff[u] = i32;
1715 /* read entropy coding method info */
1716 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1717 return false; /* the read_callback_ sets the state for us */
1718 subframe->entropy_coding_method.type = u32;
1719 switch(subframe->entropy_coding_method.type) {
1720 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1721 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1722 return false; /* the read_callback_ sets the state for us */
1723 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1726 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1731 switch(subframe->entropy_coding_method.type) {
1732 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1733 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1740 /* decode the subframe */
1741 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1742 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1743 decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
1745 decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
1750 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1752 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1753 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1756 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1758 subframe->data = residual;
1760 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1761 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1762 return false; /* the read_callback_ sets the state for us */
1766 /* decode the subframe */
1767 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1772 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1774 FLAC__uint32 rice_parameter;
1776 unsigned partition, sample, u;
1777 const unsigned partition_order = partitioned_rice->order;
1778 const unsigned partitions = 1u << partition_order;
1779 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1782 for(partition = 0; partition < partitions; partition++) {
1783 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1784 return false; /* the read_callback_ sets the state for us */
1785 partitioned_rice->parameters[partition] = rice_parameter;
1786 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1787 #ifdef FLAC__SYMMETRIC_RICE
1788 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1789 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1790 return false; /* the read_callback_ sets the state for us */
1791 residual[sample] = i;
1794 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1795 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1796 return false; /* the read_callback_ sets the state for us */
1801 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1802 return false; /* the read_callback_ sets the state for us */
1803 partitioned_rice->raw_bits[partition] = rice_parameter;
1804 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1805 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1806 return false; /* the read_callback_ sets the state for us */
1807 residual[sample] = i;
1815 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1817 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1818 FLAC__uint32 zero = 0;
1819 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1820 return false; /* the read_callback_ sets the state for us */
1822 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1823 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1829 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1831 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1832 FLAC__StreamDecoderReadStatus status;
1834 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1835 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1836 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1837 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1838 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1839 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;