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 memset(decoder, 0, sizeof(FLAC__StreamDecoder));
151 decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
152 if(decoder->protected_ == 0) {
156 memset(decoder->protected_, 0, sizeof(FLAC__StreamDecoderProtected));
158 decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
159 if(decoder->private_ == 0) {
160 free(decoder->protected_);
164 memset(decoder->private_, 0, sizeof(FLAC__StreamDecoderPrivate));
166 decoder->private_->input = FLAC__bitbuffer_new();
167 if(decoder->private_->input == 0) {
168 free(decoder->private_);
169 free(decoder->protected_);
174 decoder->private_->metadata_filter_ids_capacity = 16;
175 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
176 FLAC__bitbuffer_delete(decoder->private_->input);
177 free(decoder->private_);
178 free(decoder->protected_);
183 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
184 decoder->private_->output[i] = 0;
185 decoder->private_->residual[i] = 0;
188 decoder->private_->output_capacity = 0;
189 decoder->private_->output_channels = 0;
190 decoder->private_->has_seek_table = false;
192 set_defaults_(decoder);
194 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
199 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
201 FLAC__ASSERT(0 != decoder);
202 FLAC__ASSERT(0 != decoder->protected_);
203 FLAC__ASSERT(0 != decoder->private_);
204 FLAC__ASSERT(0 != decoder->private_->input);
206 FLAC__stream_decoder_finish(decoder);
208 if(0 != decoder->private_->metadata_filter_ids)
209 free(decoder->private_->metadata_filter_ids);
211 FLAC__bitbuffer_delete(decoder->private_->input);
212 free(decoder->private_);
213 free(decoder->protected_);
217 /***********************************************************************
219 * Public class methods
221 ***********************************************************************/
223 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
225 FLAC__ASSERT(0 != decoder);
227 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
228 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
230 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
231 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
233 if(!FLAC__bitbuffer_init(decoder->private_->input))
234 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
236 decoder->private_->last_frame_number = 0;
237 decoder->private_->samples_decoded = 0;
238 decoder->private_->has_stream_info = false;
239 decoder->private_->cached = false;
242 * get the CPU info and set the function pointers
244 FLAC__cpu_info(&decoder->private_->cpuinfo);
245 /* first default to the non-asm routines */
246 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
247 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
248 /* now override with asm where appropriate */
250 if(decoder->private_->cpuinfo.use_asm) {
251 #ifdef FLAC__CPU_IA32
252 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
253 #ifdef FLAC__HAS_NASM
254 if(decoder->private_->cpuinfo.data.ia32.mmx) {
255 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
256 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
259 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
260 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
267 if(!FLAC__stream_decoder_reset(decoder))
268 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
270 return decoder->protected_->state;
273 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
276 FLAC__ASSERT(0 != decoder);
277 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
279 if(decoder->private_->has_seek_table) {
280 FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
281 free(decoder->private_->seek_table.data.seek_table.points);
282 decoder->private_->seek_table.data.seek_table.points = 0;
283 decoder->private_->has_seek_table = false;
285 FLAC__bitbuffer_free(decoder->private_->input);
286 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
287 /* 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. */
288 if(0 != decoder->private_->output[i]) {
289 free(decoder->private_->output[i]-4);
290 decoder->private_->output[i] = 0;
292 if(0 != decoder->private_->residual[i]) {
293 free(decoder->private_->residual[i]);
294 decoder->private_->residual[i] = 0;
297 decoder->private_->output_capacity = 0;
298 decoder->private_->output_channels = 0;
300 set_defaults_(decoder);
302 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
305 FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
307 FLAC__ASSERT(0 != decoder);
308 FLAC__ASSERT(0 != decoder->private_);
309 FLAC__ASSERT(0 != decoder->protected_);
310 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
312 decoder->private_->read_callback = value;
316 FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
318 FLAC__ASSERT(0 != decoder);
319 FLAC__ASSERT(0 != decoder->private_);
320 FLAC__ASSERT(0 != decoder->protected_);
321 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
323 decoder->private_->write_callback = value;
327 FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
329 FLAC__ASSERT(0 != decoder);
330 FLAC__ASSERT(0 != decoder->private_);
331 FLAC__ASSERT(0 != decoder->protected_);
332 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
334 decoder->private_->metadata_callback = value;
338 FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
340 FLAC__ASSERT(0 != decoder);
341 FLAC__ASSERT(0 != decoder->private_);
342 FLAC__ASSERT(0 != decoder->protected_);
343 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
345 decoder->private_->error_callback = value;
349 FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
351 FLAC__ASSERT(0 != decoder);
352 FLAC__ASSERT(0 != decoder->private_);
353 FLAC__ASSERT(0 != decoder->protected_);
354 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
356 decoder->private_->client_data = value;
360 FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
362 FLAC__ASSERT(0 != decoder);
363 FLAC__ASSERT(0 != decoder->private_);
364 FLAC__ASSERT(0 != decoder->protected_);
365 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
366 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
368 decoder->private_->metadata_filter[type] = true;
369 if(type == FLAC__METADATA_TYPE_APPLICATION)
370 decoder->private_->metadata_filter_ids_count = 0;
374 FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
376 FLAC__ASSERT(0 != decoder);
377 FLAC__ASSERT(0 != decoder->private_);
378 FLAC__ASSERT(0 != decoder->protected_);
379 FLAC__ASSERT(0 != id);
380 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
383 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
386 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
388 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
389 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
390 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
391 decoder->private_->metadata_filter_ids_capacity *= 2;
394 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));
395 decoder->private_->metadata_filter_ids_count++;
400 FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
403 FLAC__ASSERT(0 != decoder);
404 FLAC__ASSERT(0 != decoder->private_);
405 FLAC__ASSERT(0 != decoder->protected_);
406 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
408 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
409 decoder->private_->metadata_filter[i] = true;
410 decoder->private_->metadata_filter_ids_count = 0;
414 FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
416 FLAC__ASSERT(0 != decoder);
417 FLAC__ASSERT(0 != decoder->private_);
418 FLAC__ASSERT(0 != decoder->protected_);
419 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
420 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
422 decoder->private_->metadata_filter[type] = false;
423 if(type == FLAC__METADATA_TYPE_APPLICATION)
424 decoder->private_->metadata_filter_ids_count = 0;
428 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
430 FLAC__ASSERT(0 != decoder);
431 FLAC__ASSERT(0 != decoder->private_);
432 FLAC__ASSERT(0 != decoder->protected_);
433 FLAC__ASSERT(0 != id);
434 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
437 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
440 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
442 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
443 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
444 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
445 decoder->private_->metadata_filter_ids_capacity *= 2;
448 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));
449 decoder->private_->metadata_filter_ids_count++;
454 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
456 FLAC__ASSERT(0 != decoder);
457 FLAC__ASSERT(0 != decoder->private_);
458 FLAC__ASSERT(0 != decoder->protected_);
459 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
461 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
462 decoder->private_->metadata_filter_ids_count = 0;
466 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
468 FLAC__ASSERT(0 != decoder);
469 FLAC__ASSERT(0 != decoder->protected_);
470 return decoder->protected_->state;
473 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
475 FLAC__ASSERT(0 != decoder);
476 FLAC__ASSERT(0 != decoder->protected_);
477 return decoder->protected_->channels;
480 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
482 FLAC__ASSERT(0 != decoder);
483 FLAC__ASSERT(0 != decoder->protected_);
484 return decoder->protected_->channel_assignment;
487 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
489 FLAC__ASSERT(0 != decoder);
490 FLAC__ASSERT(0 != decoder->protected_);
491 return decoder->protected_->bits_per_sample;
494 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
496 FLAC__ASSERT(0 != decoder);
497 FLAC__ASSERT(0 != decoder->protected_);
498 return decoder->protected_->sample_rate;
501 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
503 FLAC__ASSERT(0 != decoder);
504 FLAC__ASSERT(0 != decoder->protected_);
505 return decoder->protected_->blocksize;
508 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
510 FLAC__ASSERT(0 != decoder);
511 FLAC__ASSERT(0 != decoder->private_);
512 FLAC__ASSERT(0 != decoder->protected_);
514 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
515 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
518 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
523 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
525 FLAC__ASSERT(0 != decoder);
526 FLAC__ASSERT(0 != decoder->private_);
527 FLAC__ASSERT(0 != decoder->protected_);
529 if(!FLAC__stream_decoder_flush(decoder)) {
530 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
533 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
535 decoder->private_->samples_decoded = 0;
540 FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
542 FLAC__bool got_a_frame;
543 FLAC__ASSERT(0 != decoder);
544 FLAC__ASSERT(0 != decoder->protected_);
547 switch(decoder->protected_->state) {
548 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
549 if(!find_metadata_(decoder))
550 return false; /* above function sets the status for us */
552 case FLAC__STREAM_DECODER_READ_METADATA:
553 if(!read_metadata_(decoder))
554 return false; /* above function sets the status for us */
557 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
558 if(!frame_sync_(decoder))
559 return true; /* above function sets the status for us */
561 case FLAC__STREAM_DECODER_READ_FRAME:
562 if(!read_frame_(decoder, &got_a_frame))
563 return false; /* above function sets the status for us */
565 return true; /* above function sets the status for us */
567 case FLAC__STREAM_DECODER_END_OF_STREAM:
568 case FLAC__STREAM_DECODER_ABORTED:
577 FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
579 FLAC__ASSERT(0 != decoder);
580 FLAC__ASSERT(0 != decoder->protected_);
583 switch(decoder->protected_->state) {
584 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
585 if(!find_metadata_(decoder))
586 return false; /* above function sets the status for us */
588 case FLAC__STREAM_DECODER_READ_METADATA:
589 if(!read_metadata_(decoder))
590 return false; /* above function sets the status for us */
592 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
593 case FLAC__STREAM_DECODER_READ_FRAME:
594 case FLAC__STREAM_DECODER_END_OF_STREAM:
595 case FLAC__STREAM_DECODER_ABORTED:
604 FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
607 FLAC__ASSERT(0 != decoder);
608 FLAC__ASSERT(0 != decoder->protected_);
611 switch(decoder->protected_->state) {
612 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
613 if(!find_metadata_(decoder))
614 return false; /* above function sets the status for us */
616 case FLAC__STREAM_DECODER_READ_METADATA:
617 if(!read_metadata_(decoder))
618 return false; /* above function sets the status for us */
620 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
621 if(!frame_sync_(decoder))
622 return true; /* above function sets the status for us */
624 case FLAC__STREAM_DECODER_READ_FRAME:
625 if(!read_frame_(decoder, &dummy))
626 return false; /* above function sets the status for us */
628 case FLAC__STREAM_DECODER_END_OF_STREAM:
629 case FLAC__STREAM_DECODER_ABORTED:
638 /***********************************************************************
640 * Protected class methods
642 ***********************************************************************/
644 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
646 FLAC__ASSERT(0 != decoder);
647 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
650 /***********************************************************************
652 * Private class methods
654 ***********************************************************************/
656 void set_defaults_(FLAC__StreamDecoder *decoder)
658 decoder->private_->read_callback = 0;
659 decoder->private_->write_callback = 0;
660 decoder->private_->metadata_callback = 0;
661 decoder->private_->error_callback = 0;
662 decoder->private_->client_data = 0;
664 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
665 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
666 decoder->private_->metadata_filter_ids_count = 0;
669 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
674 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
677 /* @@@ should change to use realloc() */
679 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
680 if(0 != decoder->private_->output[i]) {
681 free(decoder->private_->output[i]);
682 decoder->private_->output[i] = 0;
684 if(0 != decoder->private_->residual[i]) {
685 free(decoder->private_->residual[i]);
686 decoder->private_->residual[i] = 0;
690 for(i = 0; i < channels; i++) {
691 /* 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. */
692 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
694 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
697 memset(tmp, 0, sizeof(FLAC__int32)*4);
698 decoder->private_->output[i] = tmp + 4;
700 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
702 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
705 decoder->private_->residual[i] = tmp;
708 decoder->private_->output_capacity = size;
709 decoder->private_->output_channels = channels;
714 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
718 FLAC__ASSERT(0 != decoder);
719 FLAC__ASSERT(0 != decoder->private_);
721 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
722 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
728 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
732 FLAC__bool first = true;
734 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
736 for(i = id = 0; i < 4; ) {
737 if(decoder->private_->cached) {
738 x = (FLAC__uint32)decoder->private_->lookahead;
739 decoder->private_->cached = false;
742 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
743 return false; /* the read_callback_ sets the state for us */
745 if(x == FLAC__STREAM_SYNC_STRING[i]) {
751 if(x == ID3V2_TAG_[id]) {
755 if(!skip_id3v2_tag_(decoder))
756 return false; /* the read_callback_ sets the state for us */
760 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
761 decoder->private_->header_warmup[0] = (FLAC__byte)x;
762 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
763 return false; /* the read_callback_ sets the state for us */
765 /* 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 */
766 /* else we have to check if the second byte is the end of a sync code */
767 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
768 decoder->private_->lookahead = (FLAC__byte)x;
769 decoder->private_->cached = true;
771 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
772 decoder->private_->header_warmup[1] = (FLAC__byte)x;
773 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
779 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
784 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
788 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
790 FLAC__uint32 i, x, last_block, type, length;
793 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
795 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
796 return false; /* the read_callback_ sets the state for us */
797 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
798 return false; /* the read_callback_ sets the state for us */
799 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
800 return false; /* the read_callback_ sets the state for us */
801 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
802 unsigned used_bits = 0;
803 decoder->private_->stream_info.type = type;
804 decoder->private_->stream_info.is_last = last_block;
805 decoder->private_->stream_info.length = length;
807 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
808 return false; /* the read_callback_ sets the state for us */
809 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
810 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
812 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
813 return false; /* the read_callback_ sets the state for us */
814 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
815 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
817 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
818 return false; /* the read_callback_ sets the state for us */
819 decoder->private_->stream_info.data.stream_info.min_framesize = x;
820 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
822 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
823 return false; /* the read_callback_ sets the state for us */
824 decoder->private_->stream_info.data.stream_info.max_framesize = x;
825 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
827 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
828 return false; /* the read_callback_ sets the state for us */
829 decoder->private_->stream_info.data.stream_info.sample_rate = x;
830 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
832 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
833 return false; /* the read_callback_ sets the state for us */
834 decoder->private_->stream_info.data.stream_info.channels = x+1;
835 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
837 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
838 return false; /* the read_callback_ sets the state for us */
839 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
840 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
842 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))
843 return false; /* the read_callback_ sets the state for us */
844 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
846 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
847 return false; /* the read_callback_ sets the state for us */
850 /* skip the rest of the block */
851 FLAC__ASSERT(used_bits % 8 == 0);
852 length -= (used_bits / 8);
853 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
854 return false; /* the read_callback_ sets the state for us */
856 decoder->private_->has_stream_info = true;
857 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
858 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
860 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
861 decoder->private_->seek_table.type = type;
862 decoder->private_->seek_table.is_last = last_block;
863 decoder->private_->seek_table.length = length;
865 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
867 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)))) {
868 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
871 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
872 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
873 return false; /* the read_callback_ sets the state for us */
874 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
876 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
877 return false; /* the read_callback_ sets the state for us */
878 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
880 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
881 return false; /* the read_callback_ sets the state for us */
882 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
884 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
885 /* if there is a partial point left, skip over it */
887 /*@@@ do an error_callback() here? there's an argument for either way */
888 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
889 return false; /* the read_callback_ sets the state for us */
892 decoder->private_->has_seek_table = true;
893 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
894 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
897 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
898 unsigned real_length = length;
899 FLAC__StreamMetadata block;
901 block.is_last = last_block;
903 block.length = length;
905 if(type == FLAC__METADATA_TYPE_APPLICATION) {
906 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
907 return false; /* the read_callback_ sets the state for us */
909 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
911 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
916 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
917 return false; /* the read_callback_ sets the state for us */
921 case FLAC__METADATA_TYPE_PADDING:
922 /* skip the padding bytes */
923 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
924 return false; /* the read_callback_ sets the state for us */
926 case FLAC__METADATA_TYPE_APPLICATION:
927 /* remember, we read the ID already */
928 if(real_length > 0) {
929 if(0 == (block.data.application.data = malloc(real_length))) {
930 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
933 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
934 return false; /* the read_callback_ sets the state for us */
937 block.data.application.data = 0;
939 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
940 /* read vendor string */
941 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
942 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
943 return false; /* the read_callback_ sets the state for us */
944 if(block.data.vorbis_comment.vendor_string.length > 0) {
945 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
946 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
949 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))
950 return false; /* the read_callback_ sets the state for us */
953 block.data.vorbis_comment.vendor_string.entry = 0;
955 /* read num comments */
956 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
957 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.num_comments, read_callback_, decoder))
958 return false; /* the read_callback_ sets the state for us */
961 if(block.data.vorbis_comment.num_comments > 0) {
962 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
963 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
966 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
967 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
968 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
969 return false; /* the read_callback_ sets the state for us */
970 if(block.data.vorbis_comment.comments[i].length > 0) {
971 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
972 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
975 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))
976 return false; /* the read_callback_ sets the state for us */
979 block.data.vorbis_comment.comments[i].entry = 0;
983 block.data.vorbis_comment.comments = 0;
986 case FLAC__METADATA_TYPE_STREAMINFO:
987 case FLAC__METADATA_TYPE_SEEKTABLE:
991 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
993 /* now we have to free any malloc'ed data in the block */
995 case FLAC__METADATA_TYPE_PADDING:
997 case FLAC__METADATA_TYPE_APPLICATION:
998 if(0 != block.data.application.data)
999 free(block.data.application.data);
1001 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1002 if(0 != block.data.vorbis_comment.vendor_string.entry)
1003 free(block.data.vorbis_comment.vendor_string.entry);
1004 if(block.data.vorbis_comment.num_comments > 0)
1005 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1006 if(0 != block.data.vorbis_comment.comments[i].entry)
1007 free(block.data.vorbis_comment.comments[i].entry);
1008 if(0 != block.data.vorbis_comment.comments)
1009 free(block.data.vorbis_comment.comments);
1011 case FLAC__METADATA_TYPE_STREAMINFO:
1012 case FLAC__METADATA_TYPE_SEEKTABLE:
1020 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1025 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1030 /* skip the version and flags bytes */
1031 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1032 return false; /* the read_callback_ sets the state for us */
1033 /* get the size (in bytes) to skip */
1035 for(i = 0; i < 4; i++) {
1036 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1037 return false; /* the read_callback_ sets the state for us */
1041 /* skip the rest of the tag */
1042 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1043 return false; /* the read_callback_ sets the state for us */
1047 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1050 FLAC__bool first = true;
1052 /* If we know the total number of samples in the stream, stop if we've read that many. */
1053 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1054 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1055 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1056 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1061 /* make sure we're byte aligned */
1062 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1063 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1064 return false; /* the read_callback_ sets the state for us */
1068 if(decoder->private_->cached) {
1069 x = (FLAC__uint32)decoder->private_->lookahead;
1070 decoder->private_->cached = false;
1073 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1074 return false; /* the read_callback_ sets the state for us */
1076 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1077 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1078 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1079 return false; /* the read_callback_ sets the state for us */
1081 /* 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 */
1082 /* else we have to check if the second byte is the end of a sync code */
1083 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1084 decoder->private_->lookahead = (FLAC__byte)x;
1085 decoder->private_->cached = true;
1087 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1088 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1089 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1094 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1102 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1106 FLAC__int32 mid, side, left, right;
1107 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1110 *got_a_frame = false;
1114 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1115 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1116 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1118 if(!read_frame_header_(decoder))
1120 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1122 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1124 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1126 * first figure the correct bits-per-sample of the subframe
1128 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1129 switch(decoder->private_->frame.header.channel_assignment) {
1130 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1131 /* no adjustment needed */
1133 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1134 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1138 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1139 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1143 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1144 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1154 if(!read_subframe_(decoder, channel, bps))
1156 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1157 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1161 if(!read_zero_padding_(decoder))
1165 * Read the frame CRC-16 from the footer and check
1167 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1168 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1169 return false; /* the read_callback_ sets the state for us */
1170 if(frame_crc == (FLAC__uint16)x) {
1171 /* Undo any special channel coding */
1172 switch(decoder->private_->frame.header.channel_assignment) {
1173 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1176 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1177 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1178 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1179 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1181 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1182 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1183 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1184 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1186 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1187 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1188 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1189 mid = decoder->private_->output[0][i];
1190 side = decoder->private_->output[1][i];
1192 if(side & 1) /* i.e. if 'side' is odd... */
1196 decoder->private_->output[0][i] = left >> 1;
1197 decoder->private_->output[1][i] = right >> 1;
1206 /* Bad frame, emit error and zero the output signal */
1207 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1208 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1209 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1213 *got_a_frame = true;
1215 /* put the latest values into the public section of the decoder instance */
1216 decoder->protected_->channels = decoder->private_->frame.header.channels;
1217 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1218 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1219 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1220 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1222 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1223 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1226 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)
1229 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1233 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1237 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1238 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1239 unsigned raw_header_len;
1240 FLAC__bool is_unparseable = false;
1241 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);
1242 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);
1244 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1246 /* init the raw header with the saved bits from synchronization */
1247 raw_header[0] = decoder->private_->header_warmup[0];
1248 raw_header[1] = decoder->private_->header_warmup[1];
1252 * check to make sure that the reserved bits are 0
1254 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1255 is_unparseable = true;
1259 * Note that along the way as we read the header, we look for a sync
1260 * code inside. If we find one it would indicate that our original
1261 * sync was bad since there cannot be a sync code in a valid header.
1265 * read in the raw header as bytes so we can CRC it, and parse it on the way
1267 for(i = 0; i < 2; i++) {
1268 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1269 return false; /* the read_callback_ sets the state for us */
1270 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1271 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1272 decoder->private_->lookahead = (FLAC__byte)x;
1273 decoder->private_->cached = true;
1274 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1275 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1278 raw_header[raw_header_len++] = (FLAC__byte)x;
1281 switch(x = raw_header[2] >> 4) {
1283 if(is_known_fixed_blocksize_stream)
1284 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1286 is_unparseable = true;
1289 decoder->private_->frame.header.blocksize = 192;
1295 decoder->private_->frame.header.blocksize = 576 << (x-2);
1309 decoder->private_->frame.header.blocksize = 256 << (x-8);
1316 switch(x = raw_header[2] & 0x0f) {
1318 if(decoder->private_->has_stream_info)
1319 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1321 is_unparseable = true;
1326 is_unparseable = true;
1329 decoder->private_->frame.header.sample_rate = 8000;
1332 decoder->private_->frame.header.sample_rate = 16000;
1335 decoder->private_->frame.header.sample_rate = 22050;
1338 decoder->private_->frame.header.sample_rate = 24000;
1341 decoder->private_->frame.header.sample_rate = 32000;
1344 decoder->private_->frame.header.sample_rate = 44100;
1347 decoder->private_->frame.header.sample_rate = 48000;
1350 decoder->private_->frame.header.sample_rate = 96000;
1355 sample_rate_hint = x;
1358 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1359 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1365 x = (unsigned)(raw_header[3] >> 4);
1367 decoder->private_->frame.header.channels = 2;
1370 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1373 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1376 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1379 is_unparseable = true;
1384 decoder->private_->frame.header.channels = (unsigned)x + 1;
1385 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1388 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1390 if(decoder->private_->has_stream_info)
1391 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1393 is_unparseable = true;
1396 decoder->private_->frame.header.bits_per_sample = 8;
1399 decoder->private_->frame.header.bits_per_sample = 12;
1402 decoder->private_->frame.header.bits_per_sample = 16;
1405 decoder->private_->frame.header.bits_per_sample = 20;
1408 decoder->private_->frame.header.bits_per_sample = 24;
1412 is_unparseable = true;
1419 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1420 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1421 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1425 if(blocksize_hint && is_known_variable_blocksize_stream) {
1426 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1427 return false; /* the read_callback_ sets the state for us */
1428 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1429 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1430 decoder->private_->cached = true;
1431 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1432 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1435 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1436 decoder->private_->frame.header.number.sample_number = xx;
1439 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1440 return false; /* the read_callback_ sets the state for us */
1441 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1442 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1443 decoder->private_->cached = true;
1444 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1445 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1448 decoder->private_->last_frame_number = x;
1449 if(decoder->private_->has_stream_info) {
1450 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1451 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1454 is_unparseable = true;
1458 if(blocksize_hint) {
1459 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1460 return false; /* the read_callback_ sets the state for us */
1461 raw_header[raw_header_len++] = (FLAC__byte)x;
1462 if(blocksize_hint == 7) {
1464 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1465 return false; /* the read_callback_ sets the state for us */
1466 raw_header[raw_header_len++] = (FLAC__byte)_x;
1469 decoder->private_->frame.header.blocksize = x+1;
1472 if(sample_rate_hint) {
1473 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1474 return false; /* the read_callback_ sets the state for us */
1475 raw_header[raw_header_len++] = (FLAC__byte)x;
1476 if(sample_rate_hint != 12) {
1478 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1479 return false; /* the read_callback_ sets the state for us */
1480 raw_header[raw_header_len++] = (FLAC__byte)_x;
1483 if(sample_rate_hint == 12)
1484 decoder->private_->frame.header.sample_rate = x*1000;
1485 else if(sample_rate_hint == 13)
1486 decoder->private_->frame.header.sample_rate = x;
1488 decoder->private_->frame.header.sample_rate = x*10;
1491 /* read the CRC-8 byte */
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 crc8 = (FLAC__byte)x;
1496 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1497 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1498 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1502 if(is_unparseable) {
1503 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1510 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1513 FLAC__bool wasted_bits;
1515 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1516 return false; /* the read_callback_ sets the state for us */
1518 wasted_bits = (x & 1);
1523 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1524 return false; /* the read_callback_ sets the state for us */
1525 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1526 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1529 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1532 * Lots of magic numbers here
1535 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1536 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1540 if(!read_subframe_constant_(decoder, channel, bps))
1544 if(!read_subframe_verbatim_(decoder, channel, bps))
1548 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1552 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1556 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1560 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1566 x = decoder->private_->frame.subframes[channel].wasted_bits;
1567 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1568 decoder->private_->output[channel][i] <<= x;
1574 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1576 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1579 FLAC__int32 *output = decoder->private_->output[channel];
1581 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1583 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1584 return false; /* the read_callback_ sets the state for us */
1586 subframe->value = x;
1588 /* decode the subframe */
1589 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1595 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1597 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1602 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1604 subframe->residual = decoder->private_->residual[channel];
1605 subframe->order = order;
1607 /* read warm-up samples */
1608 for(u = 0; u < order; u++) {
1609 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1610 return false; /* the read_callback_ sets the state for us */
1611 subframe->warmup[u] = i32;
1614 /* read entropy coding method info */
1615 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1616 return false; /* the read_callback_ sets the state for us */
1617 subframe->entropy_coding_method.type = u32;
1618 switch(subframe->entropy_coding_method.type) {
1619 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1620 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1621 return false; /* the read_callback_ sets the state for us */
1622 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1625 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1630 switch(subframe->entropy_coding_method.type) {
1631 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1632 if(!read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1639 /* decode the subframe */
1640 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1641 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1646 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1648 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1653 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1655 subframe->residual = decoder->private_->residual[channel];
1656 subframe->order = order;
1658 /* read warm-up samples */
1659 for(u = 0; u < order; u++) {
1660 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1661 return false; /* the read_callback_ sets the state for us */
1662 subframe->warmup[u] = i32;
1665 /* read qlp coeff precision */
1666 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1667 return false; /* the read_callback_ sets the state for us */
1668 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1669 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1670 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1673 subframe->qlp_coeff_precision = u32+1;
1675 /* read qlp shift */
1676 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1677 return false; /* the read_callback_ sets the state for us */
1678 subframe->quantization_level = i32;
1680 /* read quantized lp coefficiencts */
1681 for(u = 0; u < order; u++) {
1682 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1683 return false; /* the read_callback_ sets the state for us */
1684 subframe->qlp_coeff[u] = i32;
1687 /* read entropy coding method info */
1688 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1689 return false; /* the read_callback_ sets the state for us */
1690 subframe->entropy_coding_method.type = u32;
1691 switch(subframe->entropy_coding_method.type) {
1692 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1693 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1694 return false; /* the read_callback_ sets the state for us */
1695 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1698 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1703 switch(subframe->entropy_coding_method.type) {
1704 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1705 if(!read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1712 /* decode the subframe */
1713 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1714 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1715 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);
1717 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);
1722 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1724 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1725 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1728 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1730 subframe->data = residual;
1732 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1733 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1734 return false; /* the read_callback_ sets the state for us */
1738 /* decode the subframe */
1739 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1744 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1746 FLAC__uint32 rice_parameter;
1748 unsigned partition, sample, u;
1749 const unsigned partition_order = partitioned_rice->order;
1750 const unsigned partitions = 1u << partition_order;
1751 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1754 for(partition = 0; partition < partitions; partition++) {
1755 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1756 return false; /* the read_callback_ sets the state for us */
1757 partitioned_rice->parameters[partition] = rice_parameter;
1758 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1759 #ifdef FLAC__SYMMETRIC_RICE
1760 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1761 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1762 return false; /* the read_callback_ sets the state for us */
1763 residual[sample] = i;
1766 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1767 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1768 return false; /* the read_callback_ sets the state for us */
1773 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1774 return false; /* the read_callback_ sets the state for us */
1775 partitioned_rice->raw_bits[partition] = rice_parameter;
1776 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1777 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1778 return false; /* the read_callback_ sets the state for us */
1779 residual[sample] = i;
1787 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1789 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1790 FLAC__uint32 zero = 0;
1791 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1792 return false; /* the read_callback_ sets the state for us */
1794 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1795 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1801 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1803 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1804 FLAC__StreamDecoderReadStatus status;
1806 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1807 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1808 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1809 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1810 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1811 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;