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 set_defaults_(FLAC__StreamDecoder *decoder);
46 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
47 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
48 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
49 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
50 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
51 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
52 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
53 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
54 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
55 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
56 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
57 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
58 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
59 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
60 static FLAC__bool 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__StreamDecoderReadCallback read_callback;
71 FLAC__StreamDecoderWriteCallback write_callback;
72 FLAC__StreamDecoderMetadataCallback metadata_callback;
73 FLAC__StreamDecoderErrorCallback error_callback;
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 set_defaults_(decoder);
188 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
193 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
195 FLAC__ASSERT(0 != decoder);
196 FLAC__ASSERT(0 != decoder->protected_);
197 FLAC__ASSERT(0 != decoder->private_);
198 FLAC__ASSERT(0 != decoder->private_->input);
200 FLAC__stream_decoder_finish(decoder);
202 if(0 != decoder->private_->metadata_filter_ids)
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(0 != decoder);
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 if(!FLAC__stream_decoder_reset(decoder))
262 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
264 return decoder->protected_->state;
267 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
270 FLAC__ASSERT(0 != decoder);
271 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
273 if(decoder->private_->has_seek_table) {
274 FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
275 free(decoder->private_->seek_table.data.seek_table.points);
276 decoder->private_->seek_table.data.seek_table.points = 0;
277 decoder->private_->has_seek_table = false;
279 FLAC__bitbuffer_free(decoder->private_->input);
280 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
281 /* 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. */
282 if(0 != decoder->private_->output[i]) {
283 free(decoder->private_->output[i]-4);
284 decoder->private_->output[i] = 0;
286 if(0 != decoder->private_->residual[i]) {
287 free(decoder->private_->residual[i]);
288 decoder->private_->residual[i] = 0;
291 decoder->private_->output_capacity = 0;
292 decoder->private_->output_channels = 0;
294 set_defaults_(decoder);
296 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
299 FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
301 FLAC__ASSERT(0 != decoder);
302 FLAC__ASSERT(0 != decoder->private_);
303 FLAC__ASSERT(0 != decoder->protected_);
304 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
306 decoder->private_->read_callback = value;
310 FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
312 FLAC__ASSERT(0 != decoder);
313 FLAC__ASSERT(0 != decoder->private_);
314 FLAC__ASSERT(0 != decoder->protected_);
315 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
317 decoder->private_->write_callback = value;
321 FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
323 FLAC__ASSERT(0 != decoder);
324 FLAC__ASSERT(0 != decoder->private_);
325 FLAC__ASSERT(0 != decoder->protected_);
326 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
328 decoder->private_->metadata_callback = value;
332 FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
334 FLAC__ASSERT(0 != decoder);
335 FLAC__ASSERT(0 != decoder->private_);
336 FLAC__ASSERT(0 != decoder->protected_);
337 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
339 decoder->private_->error_callback = value;
343 FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
345 FLAC__ASSERT(0 != decoder);
346 FLAC__ASSERT(0 != decoder->private_);
347 FLAC__ASSERT(0 != decoder->protected_);
348 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
350 decoder->private_->client_data = value;
354 FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
356 FLAC__ASSERT(0 != decoder);
357 FLAC__ASSERT(0 != decoder->private_);
358 FLAC__ASSERT(0 != decoder->protected_);
359 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
360 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
362 decoder->private_->metadata_filter[type] = true;
363 if(type == FLAC__METADATA_TYPE_APPLICATION)
364 decoder->private_->metadata_filter_ids_count = 0;
368 FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
370 FLAC__ASSERT(0 != decoder);
371 FLAC__ASSERT(0 != decoder->private_);
372 FLAC__ASSERT(0 != decoder->protected_);
373 FLAC__ASSERT(0 != id);
374 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
377 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
380 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
382 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
383 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
384 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
385 decoder->private_->metadata_filter_ids_capacity *= 2;
388 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));
389 decoder->private_->metadata_filter_ids_count++;
394 FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
397 FLAC__ASSERT(0 != decoder);
398 FLAC__ASSERT(0 != decoder->private_);
399 FLAC__ASSERT(0 != decoder->protected_);
400 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
402 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
403 decoder->private_->metadata_filter[i] = true;
404 decoder->private_->metadata_filter_ids_count = 0;
408 FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
410 FLAC__ASSERT(0 != decoder);
411 FLAC__ASSERT(0 != decoder->private_);
412 FLAC__ASSERT(0 != decoder->protected_);
413 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
414 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
416 decoder->private_->metadata_filter[type] = false;
417 if(type == FLAC__METADATA_TYPE_APPLICATION)
418 decoder->private_->metadata_filter_ids_count = 0;
422 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
424 FLAC__ASSERT(0 != decoder);
425 FLAC__ASSERT(0 != decoder->private_);
426 FLAC__ASSERT(0 != decoder->protected_);
427 FLAC__ASSERT(0 != id);
428 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
431 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
434 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
436 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
437 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
438 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
439 decoder->private_->metadata_filter_ids_capacity *= 2;
442 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));
443 decoder->private_->metadata_filter_ids_count++;
448 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
450 FLAC__ASSERT(0 != decoder);
451 FLAC__ASSERT(0 != decoder->private_);
452 FLAC__ASSERT(0 != decoder->protected_);
453 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
455 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
456 decoder->private_->metadata_filter_ids_count = 0;
460 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
462 FLAC__ASSERT(0 != decoder);
463 FLAC__ASSERT(0 != decoder->protected_);
464 return decoder->protected_->state;
467 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
469 FLAC__ASSERT(0 != decoder);
470 FLAC__ASSERT(0 != decoder->protected_);
471 return decoder->protected_->channels;
474 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
476 FLAC__ASSERT(0 != decoder);
477 FLAC__ASSERT(0 != decoder->protected_);
478 return decoder->protected_->channel_assignment;
481 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
483 FLAC__ASSERT(0 != decoder);
484 FLAC__ASSERT(0 != decoder->protected_);
485 return decoder->protected_->bits_per_sample;
488 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
490 FLAC__ASSERT(0 != decoder);
491 FLAC__ASSERT(0 != decoder->protected_);
492 return decoder->protected_->sample_rate;
495 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
497 FLAC__ASSERT(0 != decoder);
498 FLAC__ASSERT(0 != decoder->protected_);
499 return decoder->protected_->blocksize;
502 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
504 FLAC__ASSERT(0 != decoder);
505 FLAC__ASSERT(0 != decoder->private_);
506 FLAC__ASSERT(0 != decoder->protected_);
508 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
509 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
512 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
517 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
519 FLAC__ASSERT(0 != decoder);
520 FLAC__ASSERT(0 != decoder->private_);
521 FLAC__ASSERT(0 != decoder->protected_);
523 if(!FLAC__stream_decoder_flush(decoder)) {
524 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
527 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
529 decoder->private_->samples_decoded = 0;
534 FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
536 FLAC__bool got_a_frame;
537 FLAC__ASSERT(0 != decoder);
538 FLAC__ASSERT(0 != decoder->protected_);
541 switch(decoder->protected_->state) {
542 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
543 if(!find_metadata_(decoder))
544 return false; /* above function sets the status for us */
546 case FLAC__STREAM_DECODER_READ_METADATA:
547 if(!read_metadata_(decoder))
548 return false; /* above function sets the status for us */
551 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
552 if(!frame_sync_(decoder))
553 return true; /* above function sets the status for us */
555 case FLAC__STREAM_DECODER_READ_FRAME:
556 if(!read_frame_(decoder, &got_a_frame))
557 return false; /* above function sets the status for us */
559 return true; /* above function sets the status for us */
561 case FLAC__STREAM_DECODER_END_OF_STREAM:
562 case FLAC__STREAM_DECODER_ABORTED:
571 FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
573 FLAC__ASSERT(0 != decoder);
574 FLAC__ASSERT(0 != decoder->protected_);
577 switch(decoder->protected_->state) {
578 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
579 if(!find_metadata_(decoder))
580 return false; /* above function sets the status for us */
582 case FLAC__STREAM_DECODER_READ_METADATA:
583 if(!read_metadata_(decoder))
584 return false; /* above function sets the status for us */
586 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
587 case FLAC__STREAM_DECODER_READ_FRAME:
588 case FLAC__STREAM_DECODER_END_OF_STREAM:
589 case FLAC__STREAM_DECODER_ABORTED:
598 FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
601 FLAC__ASSERT(0 != decoder);
602 FLAC__ASSERT(0 != decoder->protected_);
605 switch(decoder->protected_->state) {
606 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
607 if(!find_metadata_(decoder))
608 return false; /* above function sets the status for us */
610 case FLAC__STREAM_DECODER_READ_METADATA:
611 if(!read_metadata_(decoder))
612 return false; /* above function sets the status for us */
614 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
615 if(!frame_sync_(decoder))
616 return true; /* above function sets the status for us */
618 case FLAC__STREAM_DECODER_READ_FRAME:
619 if(!read_frame_(decoder, &dummy))
620 return false; /* above function sets the status for us */
622 case FLAC__STREAM_DECODER_END_OF_STREAM:
623 case FLAC__STREAM_DECODER_ABORTED:
632 /***********************************************************************
634 * Protected class methods
636 ***********************************************************************/
638 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
640 FLAC__ASSERT(0 != decoder);
641 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
644 /***********************************************************************
646 * Private class methods
648 ***********************************************************************/
650 void set_defaults_(FLAC__StreamDecoder *decoder)
652 decoder->private_->read_callback = 0;
653 decoder->private_->write_callback = 0;
654 decoder->private_->metadata_callback = 0;
655 decoder->private_->error_callback = 0;
656 decoder->private_->client_data = 0;
658 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
659 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
660 decoder->private_->metadata_filter_ids_count = 0;
663 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
668 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
671 /* @@@ should change to use realloc() */
673 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
674 if(0 != decoder->private_->output[i]) {
675 free(decoder->private_->output[i]);
676 decoder->private_->output[i] = 0;
678 if(0 != decoder->private_->residual[i]) {
679 free(decoder->private_->residual[i]);
680 decoder->private_->residual[i] = 0;
684 for(i = 0; i < channels; i++) {
685 /* 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. */
686 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
688 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
691 memset(tmp, 0, sizeof(FLAC__int32)*4);
692 decoder->private_->output[i] = tmp + 4;
694 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
696 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
699 decoder->private_->residual[i] = tmp;
702 decoder->private_->output_capacity = size;
703 decoder->private_->output_channels = channels;
708 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
712 FLAC__ASSERT(0 != decoder);
713 FLAC__ASSERT(0 != decoder->private_);
715 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
716 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
722 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
726 FLAC__bool first = true;
728 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
730 for(i = id = 0; i < 4; ) {
731 if(decoder->private_->cached) {
732 x = (FLAC__uint32)decoder->private_->lookahead;
733 decoder->private_->cached = false;
736 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
737 return false; /* the read_callback_ sets the state for us */
739 if(x == FLAC__STREAM_SYNC_STRING[i]) {
745 if(x == ID3V2_TAG_[id]) {
749 if(!skip_id3v2_tag_(decoder))
750 return false; /* the read_callback_ sets the state for us */
754 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
755 decoder->private_->header_warmup[0] = (FLAC__byte)x;
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 /* 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 */
760 /* else we have to check if the second byte is the end of a sync code */
761 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
762 decoder->private_->lookahead = (FLAC__byte)x;
763 decoder->private_->cached = true;
765 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
766 decoder->private_->header_warmup[1] = (FLAC__byte)x;
767 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
773 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
778 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
782 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
784 FLAC__uint32 i, x, last_block, type, length;
787 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
789 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
790 return false; /* the read_callback_ sets the state for us */
791 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
792 return false; /* the read_callback_ sets the state for us */
793 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
794 return false; /* the read_callback_ sets the state for us */
795 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
796 unsigned used_bits = 0;
797 decoder->private_->stream_info.type = type;
798 decoder->private_->stream_info.is_last = last_block;
799 decoder->private_->stream_info.length = length;
801 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
802 return false; /* the read_callback_ sets the state for us */
803 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
804 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
806 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
807 return false; /* the read_callback_ sets the state for us */
808 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
809 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
811 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
812 return false; /* the read_callback_ sets the state for us */
813 decoder->private_->stream_info.data.stream_info.min_framesize = x;
814 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
816 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
817 return false; /* the read_callback_ sets the state for us */
818 decoder->private_->stream_info.data.stream_info.max_framesize = x;
819 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
821 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
822 return false; /* the read_callback_ sets the state for us */
823 decoder->private_->stream_info.data.stream_info.sample_rate = x;
824 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
826 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
827 return false; /* the read_callback_ sets the state for us */
828 decoder->private_->stream_info.data.stream_info.channels = x+1;
829 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
831 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
832 return false; /* the read_callback_ sets the state for us */
833 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
834 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
836 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))
837 return false; /* the read_callback_ sets the state for us */
838 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
840 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
841 return false; /* the read_callback_ sets the state for us */
844 /* skip the rest of the block */
845 FLAC__ASSERT(used_bits % 8 == 0);
846 length -= (used_bits / 8);
847 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
848 return false; /* the read_callback_ sets the state for us */
850 decoder->private_->has_stream_info = true;
851 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
852 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
854 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
855 decoder->private_->seek_table.type = type;
856 decoder->private_->seek_table.is_last = last_block;
857 decoder->private_->seek_table.length = length;
859 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
861 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)))) {
862 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
865 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
866 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
867 return false; /* the read_callback_ sets the state for us */
868 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
870 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
871 return false; /* the read_callback_ sets the state for us */
872 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
874 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
875 return false; /* the read_callback_ sets the state for us */
876 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
878 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
879 /* if there is a partial point left, skip over it */
881 /*@@@ do an error_callback() here? there's an argument for either way */
882 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
883 return false; /* the read_callback_ sets the state for us */
886 decoder->private_->has_seek_table = true;
887 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
888 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
891 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
892 unsigned real_length = length;
893 FLAC__StreamMetadata block;
895 block.is_last = last_block;
897 block.length = length;
899 if(type == FLAC__METADATA_TYPE_APPLICATION) {
900 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
901 return false; /* the read_callback_ sets the state for us */
903 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
905 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
910 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
911 return false; /* the read_callback_ sets the state for us */
915 case FLAC__METADATA_TYPE_PADDING:
916 /* skip the padding bytes */
917 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
918 return false; /* the read_callback_ sets the state for us */
920 case FLAC__METADATA_TYPE_APPLICATION:
921 /* remember, we read the ID already */
922 if(real_length > 0) {
923 if(0 == (block.data.application.data = malloc(real_length))) {
924 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
927 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
928 return false; /* the read_callback_ sets the state for us */
931 block.data.application.data = 0;
933 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
934 /* read vendor string */
935 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
936 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
937 return false; /* the read_callback_ sets the state for us */
938 if(block.data.vorbis_comment.vendor_string.length > 0) {
939 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
940 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
943 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))
944 return false; /* the read_callback_ sets the state for us */
947 block.data.vorbis_comment.vendor_string.entry = 0;
949 /* read num comments */
950 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
951 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.num_comments, read_callback_, decoder))
952 return false; /* the read_callback_ sets the state for us */
955 if(block.data.vorbis_comment.num_comments > 0) {
956 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
957 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
960 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
961 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
962 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
963 return false; /* the read_callback_ sets the state for us */
964 if(block.data.vorbis_comment.comments[i].length > 0) {
965 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
966 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
969 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.comments[i].entry, block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
970 return false; /* the read_callback_ sets the state for us */
973 block.data.vorbis_comment.comments[i].entry = 0;
977 block.data.vorbis_comment.comments = 0;
980 case FLAC__METADATA_TYPE_STREAMINFO:
981 case FLAC__METADATA_TYPE_SEEKTABLE:
985 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
987 /* now we have to free any malloc'ed data in the block */
989 case FLAC__METADATA_TYPE_PADDING:
991 case FLAC__METADATA_TYPE_APPLICATION:
992 if(0 != block.data.application.data)
993 free(block.data.application.data);
995 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
996 if(0 != block.data.vorbis_comment.vendor_string.entry)
997 free(block.data.vorbis_comment.vendor_string.entry);
998 if(block.data.vorbis_comment.num_comments > 0)
999 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1000 if(0 != block.data.vorbis_comment.comments[i].entry)
1001 free(block.data.vorbis_comment.comments[i].entry);
1002 if(0 != block.data.vorbis_comment.comments)
1003 free(block.data.vorbis_comment.comments);
1005 case FLAC__METADATA_TYPE_STREAMINFO:
1006 case FLAC__METADATA_TYPE_SEEKTABLE:
1014 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1019 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1024 /* skip the version and flags bytes */
1025 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1026 return false; /* the read_callback_ sets the state for us */
1027 /* get the size (in bytes) to skip */
1029 for(i = 0; i < 4; i++) {
1030 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1031 return false; /* the read_callback_ sets the state for us */
1035 /* skip the rest of the tag */
1036 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1037 return false; /* the read_callback_ sets the state for us */
1041 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1044 FLAC__bool first = true;
1046 /* If we know the total number of samples in the stream, stop if we've read that many. */
1047 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1048 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1049 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1050 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1055 /* make sure we're byte aligned */
1056 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1057 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1058 return false; /* the read_callback_ sets the state for us */
1062 if(decoder->private_->cached) {
1063 x = (FLAC__uint32)decoder->private_->lookahead;
1064 decoder->private_->cached = false;
1067 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1068 return false; /* the read_callback_ sets the state for us */
1070 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1071 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1072 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1073 return false; /* the read_callback_ sets the state for us */
1075 /* 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 */
1076 /* else we have to check if the second byte is the end of a sync code */
1077 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1078 decoder->private_->lookahead = (FLAC__byte)x;
1079 decoder->private_->cached = true;
1081 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1082 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1083 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1088 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1096 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1100 FLAC__int32 mid, side, left, right;
1101 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1104 *got_a_frame = false;
1108 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1109 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1110 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1112 if(!read_frame_header_(decoder))
1114 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1116 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1118 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1120 * first figure the correct bits-per-sample of the subframe
1122 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1123 switch(decoder->private_->frame.header.channel_assignment) {
1124 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1125 /* no adjustment needed */
1127 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1128 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1132 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1133 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1137 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1138 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1148 if(!read_subframe_(decoder, channel, bps))
1150 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1151 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1155 if(!read_zero_padding_(decoder))
1159 * Read the frame CRC-16 from the footer and check
1161 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1162 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1163 return false; /* the read_callback_ sets the state for us */
1164 if(frame_crc == (FLAC__uint16)x) {
1165 /* Undo any special channel coding */
1166 switch(decoder->private_->frame.header.channel_assignment) {
1167 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1170 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1171 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1172 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1173 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1175 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1176 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1177 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1178 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1180 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1181 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1182 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1183 mid = decoder->private_->output[0][i];
1184 side = decoder->private_->output[1][i];
1186 if(side & 1) /* i.e. if 'side' is odd... */
1190 decoder->private_->output[0][i] = left >> 1;
1191 decoder->private_->output[1][i] = right >> 1;
1200 /* Bad frame, emit error and zero the output signal */
1201 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1202 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1203 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1207 *got_a_frame = true;
1209 /* put the latest values into the public section of the decoder instance */
1210 decoder->protected_->channels = decoder->private_->frame.header.channels;
1211 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1212 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1213 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1214 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1216 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1217 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1220 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)
1223 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1227 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1231 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1232 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1233 unsigned raw_header_len;
1234 FLAC__bool is_unparseable = false;
1235 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);
1236 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);
1238 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1240 /* init the raw header with the saved bits from synchronization */
1241 raw_header[0] = decoder->private_->header_warmup[0];
1242 raw_header[1] = decoder->private_->header_warmup[1];
1246 * check to make sure that the reserved bits are 0
1248 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1249 is_unparseable = true;
1253 * Note that along the way as we read the header, we look for a sync
1254 * code inside. If we find one it would indicate that our original
1255 * sync was bad since there cannot be a sync code in a valid header.
1259 * read in the raw header as bytes so we can CRC it, and parse it on the way
1261 for(i = 0; i < 2; i++) {
1262 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1263 return false; /* the read_callback_ sets the state for us */
1264 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1265 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1266 decoder->private_->lookahead = (FLAC__byte)x;
1267 decoder->private_->cached = true;
1268 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1269 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1272 raw_header[raw_header_len++] = (FLAC__byte)x;
1275 switch(x = raw_header[2] >> 4) {
1277 if(is_known_fixed_blocksize_stream)
1278 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1280 is_unparseable = true;
1283 decoder->private_->frame.header.blocksize = 192;
1289 decoder->private_->frame.header.blocksize = 576 << (x-2);
1303 decoder->private_->frame.header.blocksize = 256 << (x-8);
1310 switch(x = raw_header[2] & 0x0f) {
1312 if(decoder->private_->has_stream_info)
1313 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1315 is_unparseable = true;
1320 is_unparseable = true;
1323 decoder->private_->frame.header.sample_rate = 8000;
1326 decoder->private_->frame.header.sample_rate = 16000;
1329 decoder->private_->frame.header.sample_rate = 22050;
1332 decoder->private_->frame.header.sample_rate = 24000;
1335 decoder->private_->frame.header.sample_rate = 32000;
1338 decoder->private_->frame.header.sample_rate = 44100;
1341 decoder->private_->frame.header.sample_rate = 48000;
1344 decoder->private_->frame.header.sample_rate = 96000;
1349 sample_rate_hint = x;
1352 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1353 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1359 x = (unsigned)(raw_header[3] >> 4);
1361 decoder->private_->frame.header.channels = 2;
1364 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1367 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1370 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1373 is_unparseable = true;
1378 decoder->private_->frame.header.channels = (unsigned)x + 1;
1379 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1382 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1384 if(decoder->private_->has_stream_info)
1385 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1387 is_unparseable = true;
1390 decoder->private_->frame.header.bits_per_sample = 8;
1393 decoder->private_->frame.header.bits_per_sample = 12;
1396 decoder->private_->frame.header.bits_per_sample = 16;
1399 decoder->private_->frame.header.bits_per_sample = 20;
1402 decoder->private_->frame.header.bits_per_sample = 24;
1406 is_unparseable = true;
1413 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1414 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1415 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1419 if(blocksize_hint && is_known_variable_blocksize_stream) {
1420 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1421 return false; /* the read_callback_ sets the state for us */
1422 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1423 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1424 decoder->private_->cached = true;
1425 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1426 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1429 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1430 decoder->private_->frame.header.number.sample_number = xx;
1433 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1434 return false; /* the read_callback_ sets the state for us */
1435 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1436 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1437 decoder->private_->cached = true;
1438 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1439 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1442 decoder->private_->last_frame_number = x;
1443 if(decoder->private_->has_stream_info) {
1444 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1445 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1448 is_unparseable = true;
1452 if(blocksize_hint) {
1453 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1454 return false; /* the read_callback_ sets the state for us */
1455 raw_header[raw_header_len++] = (FLAC__byte)x;
1456 if(blocksize_hint == 7) {
1458 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1459 return false; /* the read_callback_ sets the state for us */
1460 raw_header[raw_header_len++] = (FLAC__byte)_x;
1463 decoder->private_->frame.header.blocksize = x+1;
1466 if(sample_rate_hint) {
1467 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1468 return false; /* the read_callback_ sets the state for us */
1469 raw_header[raw_header_len++] = (FLAC__byte)x;
1470 if(sample_rate_hint != 12) {
1472 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1473 return false; /* the read_callback_ sets the state for us */
1474 raw_header[raw_header_len++] = (FLAC__byte)_x;
1477 if(sample_rate_hint == 12)
1478 decoder->private_->frame.header.sample_rate = x*1000;
1479 else if(sample_rate_hint == 13)
1480 decoder->private_->frame.header.sample_rate = x;
1482 decoder->private_->frame.header.sample_rate = x*10;
1485 /* read the CRC-8 byte */
1486 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1487 return false; /* the read_callback_ sets the state for us */
1488 crc8 = (FLAC__byte)x;
1490 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1491 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1492 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1496 if(is_unparseable) {
1497 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1504 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1507 FLAC__bool wasted_bits;
1509 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1510 return false; /* the read_callback_ sets the state for us */
1512 wasted_bits = (x & 1);
1517 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1518 return false; /* the read_callback_ sets the state for us */
1519 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1520 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1523 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1526 * Lots of magic numbers here
1529 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1530 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1534 if(!read_subframe_constant_(decoder, channel, bps))
1538 if(!read_subframe_verbatim_(decoder, channel, bps))
1542 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1546 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1550 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1554 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1560 x = decoder->private_->frame.subframes[channel].wasted_bits;
1561 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1562 decoder->private_->output[channel][i] <<= x;
1568 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1570 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1573 FLAC__int32 *output = decoder->private_->output[channel];
1575 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1577 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1578 return false; /* the read_callback_ sets the state for us */
1580 subframe->value = x;
1582 /* decode the subframe */
1583 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1589 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1591 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1596 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1598 subframe->residual = decoder->private_->residual[channel];
1599 subframe->order = order;
1601 /* read warm-up samples */
1602 for(u = 0; u < order; u++) {
1603 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1604 return false; /* the read_callback_ sets the state for us */
1605 subframe->warmup[u] = i32;
1608 /* read entropy coding method info */
1609 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1610 return false; /* the read_callback_ sets the state for us */
1611 subframe->entropy_coding_method.type = u32;
1612 switch(subframe->entropy_coding_method.type) {
1613 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1614 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1615 return false; /* the read_callback_ sets the state for us */
1616 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1619 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1624 switch(subframe->entropy_coding_method.type) {
1625 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1626 if(!read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1633 /* decode the subframe */
1634 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1635 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1640 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1642 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1647 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1649 subframe->residual = decoder->private_->residual[channel];
1650 subframe->order = order;
1652 /* read warm-up samples */
1653 for(u = 0; u < order; u++) {
1654 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1655 return false; /* the read_callback_ sets the state for us */
1656 subframe->warmup[u] = i32;
1659 /* read qlp coeff precision */
1660 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1661 return false; /* the read_callback_ sets the state for us */
1662 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1663 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1664 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1667 subframe->qlp_coeff_precision = u32+1;
1669 /* read qlp shift */
1670 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1671 return false; /* the read_callback_ sets the state for us */
1672 subframe->quantization_level = i32;
1674 /* read quantized lp coefficiencts */
1675 for(u = 0; u < order; u++) {
1676 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1677 return false; /* the read_callback_ sets the state for us */
1678 subframe->qlp_coeff[u] = i32;
1681 /* read entropy coding method info */
1682 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1683 return false; /* the read_callback_ sets the state for us */
1684 subframe->entropy_coding_method.type = u32;
1685 switch(subframe->entropy_coding_method.type) {
1686 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1687 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1688 return false; /* the read_callback_ sets the state for us */
1689 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1692 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1697 switch(subframe->entropy_coding_method.type) {
1698 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1699 if(!read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1706 /* decode the subframe */
1707 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1708 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1709 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);
1711 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);
1716 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1718 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1719 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1722 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1724 subframe->data = residual;
1726 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1727 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1728 return false; /* the read_callback_ sets the state for us */
1732 /* decode the subframe */
1733 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1738 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1740 FLAC__uint32 rice_parameter;
1742 unsigned partition, sample, u;
1743 const unsigned partition_order = partitioned_rice->order;
1744 const unsigned partitions = 1u << partition_order;
1745 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1748 for(partition = 0; partition < partitions; partition++) {
1749 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1750 return false; /* the read_callback_ sets the state for us */
1751 partitioned_rice->parameters[partition] = rice_parameter;
1752 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1753 #ifdef FLAC__SYMMETRIC_RICE
1754 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1755 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1756 return false; /* the read_callback_ sets the state for us */
1757 residual[sample] = i;
1760 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1761 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1762 return false; /* the read_callback_ sets the state for us */
1767 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1768 return false; /* the read_callback_ sets the state for us */
1769 partitioned_rice->raw_bits[partition] = rice_parameter;
1770 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1771 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1772 return false; /* the read_callback_ sets the state for us */
1773 residual[sample] = i;
1781 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1783 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1784 FLAC__uint32 zero = 0;
1785 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1786 return false; /* the read_callback_ sets the state for us */
1788 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1789 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1795 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1797 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1798 FLAC__StreamDecoderReadStatus status;
1800 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1801 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1802 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1803 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1804 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1805 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;