1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memset/memcpy() */
23 #include "FLAC/assert.h"
24 #include "protected/stream_decoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/cpu.h"
27 #include "private/crc.h"
28 #include "private/fixed.h"
29 #include "private/lpc.h"
31 /***********************************************************************
35 ***********************************************************************/
37 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
39 /***********************************************************************
41 * Private class method prototypes
43 ***********************************************************************/
45 static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
46 static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
47 static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
48 static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
49 static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
50 static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
51 static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
52 static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
53 static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
54 static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
55 static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
56 static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
57 static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
58 static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
59 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
61 /***********************************************************************
65 ***********************************************************************/
67 typedef struct FLAC__StreamDecoderPrivate {
68 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
69 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
70 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
71 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
72 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[]);
73 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[]);
75 FLAC__BitBuffer *input;
76 FLAC__int32 *output[FLAC__MAX_CHANNELS];
77 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
78 unsigned output_capacity, output_channels;
79 FLAC__uint32 last_frame_number;
80 FLAC__uint64 samples_decoded;
81 FLAC__bool has_stream_info, has_seek_table;
82 FLAC__StreamMetaData stream_info;
83 FLAC__StreamMetaData seek_table;
85 FLAC__bool cached; /* true if there is a byte in lookahead */
86 FLAC__CPUInfo cpuinfo;
87 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
88 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
89 } FLAC__StreamDecoderPrivate;
91 /***********************************************************************
93 * Public static class data
95 ***********************************************************************/
97 const char *FLAC__StreamDecoderStateString[] = {
98 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
99 "FLAC__STREAM_DECODER_READ_METADATA",
100 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
101 "FLAC__STREAM_DECODER_READ_FRAME",
102 "FLAC__STREAM_DECODER_END_OF_STREAM",
103 "FLAC__STREAM_DECODER_ABORTED",
104 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
105 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
106 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
107 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
108 "FLAC__STREAM_DECODER_UNINITIALIZED"
111 const char *FLAC__StreamDecoderReadStatusString[] = {
112 "FLAC__STREAM_DECODER_READ_CONTINUE",
113 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
114 "FLAC__STREAM_DECODER_READ_ABORT"
117 const char *FLAC__StreamDecoderWriteStatusString[] = {
118 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
119 "FLAC__STREAM_DECODER_WRITE_ABORT"
122 const char *FLAC__StreamDecoderErrorStatusString[] = {
123 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
124 "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
125 "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
128 /***********************************************************************
130 * Class constructor/destructor
132 ***********************************************************************/
133 FLAC__StreamDecoder *FLAC__stream_decoder_new()
135 FLAC__StreamDecoder *decoder;
137 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
139 decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
143 decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
144 if(decoder->protected_ == 0) {
148 decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
149 if(decoder->private_ == 0) {
150 free(decoder->protected_);
154 decoder->private_->input = FLAC__bitbuffer_new();
155 if(decoder->private_->input == 0) {
156 free(decoder->private_);
157 free(decoder->protected_);
162 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
164 decoder->private_->read_callback = 0;
165 decoder->private_->write_callback = 0;
166 decoder->private_->metadata_callback = 0;
167 decoder->private_->error_callback = 0;
168 decoder->private_->client_data = 0;
173 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
175 FLAC__ASSERT(decoder != 0);
176 FLAC__ASSERT(decoder->protected_ != 0);
177 FLAC__ASSERT(decoder->private_ != 0);
178 FLAC__ASSERT(decoder->private_->input != 0);
180 FLAC__bitbuffer_delete(decoder->private_->input);
181 free(decoder->private_);
182 free(decoder->protected_);
186 /***********************************************************************
188 * Public class methods
190 ***********************************************************************/
192 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
196 FLAC__ASSERT(decoder != 0);
198 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
199 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
201 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
203 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
204 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
206 if(!FLAC__bitbuffer_init(decoder->private_->input))
207 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
209 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
210 decoder->private_->output[i] = 0;
211 decoder->private_->residual[i] = 0;
214 decoder->private_->output_capacity = 0;
215 decoder->private_->output_channels = 0;
216 decoder->private_->last_frame_number = 0;
217 decoder->private_->samples_decoded = 0;
218 decoder->private_->has_stream_info = false;
219 decoder->private_->has_seek_table = false;
220 decoder->private_->cached = false;
223 * get the CPU info and set the function pointers
225 FLAC__cpu_info(&decoder->private_->cpuinfo);
226 /* first default to the non-asm routines */
227 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
228 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
229 /* now override with asm where appropriate */
231 if(decoder->private_->cpuinfo.use_asm) {
232 #ifdef FLAC__CPU_IA32
233 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
234 #ifdef FLAC__HAS_NASM
235 if(decoder->private_->cpuinfo.data.ia32.mmx) {
236 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
237 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
240 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
241 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
248 return decoder->protected_->state;
251 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
254 FLAC__ASSERT(decoder != 0);
255 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
257 if(decoder->private_->has_seek_table) {
258 free(decoder->private_->seek_table.data.seek_table.points);
259 decoder->private_->seek_table.data.seek_table.points = 0;
261 FLAC__bitbuffer_free(decoder->private_->input);
262 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
263 /* 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. */
264 if(decoder->private_->output[i] != 0) {
265 free(decoder->private_->output[i]-4);
266 decoder->private_->output[i] = 0;
268 if(decoder->private_->residual[i] != 0) {
269 free(decoder->private_->residual[i]);
270 decoder->private_->residual[i] = 0;
273 decoder->private_->output_capacity = 0;
274 decoder->private_->output_channels = 0;
275 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
278 FLAC__bool FLAC__stream_decoder_set_read_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
280 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
282 decoder->private_->read_callback = value;
286 FLAC__bool FLAC__stream_decoder_set_write_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
288 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
290 decoder->private_->write_callback = value;
294 FLAC__bool FLAC__stream_decoder_set_metadata_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
296 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
298 decoder->private_->metadata_callback = value;
302 FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
304 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
306 decoder->private_->error_callback = value;
310 FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
312 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
314 decoder->private_->client_data = value;
318 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
320 return decoder->protected_->state;
323 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
325 return decoder->protected_->channels;
328 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
330 return decoder->protected_->channel_assignment;
333 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
335 return decoder->protected_->bits_per_sample;
338 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
340 return decoder->protected_->sample_rate;
343 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
345 return decoder->protected_->blocksize;
348 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
350 FLAC__ASSERT(decoder != 0);
352 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
353 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
356 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
361 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
363 FLAC__ASSERT(decoder != 0);
365 if(!FLAC__stream_decoder_flush(decoder)) {
366 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
369 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
371 decoder->private_->samples_decoded = 0;
376 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
379 FLAC__ASSERT(decoder != 0);
381 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
384 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
386 if(!FLAC__stream_decoder_reset(decoder)) {
387 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
392 switch(decoder->protected_->state) {
393 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
394 if(!stream_decoder_find_metadata_(decoder))
395 return false; /* above function sets the status for us */
397 case FLAC__STREAM_DECODER_READ_METADATA:
398 if(!stream_decoder_read_metadata_(decoder))
399 return false; /* above function sets the status for us */
401 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
402 if(!stream_decoder_frame_sync_(decoder))
403 return true; /* above function sets the status for us */
405 case FLAC__STREAM_DECODER_READ_FRAME:
406 if(!stream_decoder_read_frame_(decoder, &dummy))
407 return false; /* above function sets the status for us */
409 case FLAC__STREAM_DECODER_END_OF_STREAM:
417 FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
419 FLAC__ASSERT(decoder != 0);
421 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
424 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
426 if(!FLAC__stream_decoder_reset(decoder)) {
427 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
432 switch(decoder->protected_->state) {
433 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
434 if(!stream_decoder_find_metadata_(decoder))
435 return false; /* above function sets the status for us */
437 case FLAC__STREAM_DECODER_READ_METADATA:
438 if(!stream_decoder_read_metadata_(decoder))
439 return false; /* above function sets the status for us */
441 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
443 case FLAC__STREAM_DECODER_READ_FRAME:
445 case FLAC__STREAM_DECODER_END_OF_STREAM:
453 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
455 FLAC__bool got_a_frame;
456 FLAC__ASSERT(decoder != 0);
458 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
461 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
464 switch(decoder->protected_->state) {
465 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
466 if(!stream_decoder_frame_sync_(decoder))
467 return true; /* above function sets the status for us */
469 case FLAC__STREAM_DECODER_READ_FRAME:
470 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
471 return false; /* above function sets the status for us */
473 return true; /* above function sets the status for us */
475 case FLAC__STREAM_DECODER_END_OF_STREAM:
483 FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
486 FLAC__ASSERT(decoder != 0);
488 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
491 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
494 switch(decoder->protected_->state) {
495 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
496 if(!stream_decoder_frame_sync_(decoder))
497 return true; /* above function sets the status for us */
499 case FLAC__STREAM_DECODER_READ_FRAME:
500 if(!stream_decoder_read_frame_(decoder, &dummy))
501 return false; /* above function sets the status for us */
503 case FLAC__STREAM_DECODER_END_OF_STREAM:
511 /***********************************************************************
513 * Protected class methods
515 ***********************************************************************/
517 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
519 FLAC__ASSERT(decoder != 0);
520 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
523 /***********************************************************************
525 * Private class methods
527 ***********************************************************************/
529 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
534 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
537 /* @@@ should change to use realloc() */
539 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
540 if(decoder->private_->output[i] != 0) {
541 free(decoder->private_->output[i]);
542 decoder->private_->output[i] = 0;
544 if(decoder->private_->residual[i] != 0) {
545 free(decoder->private_->residual[i]);
546 decoder->private_->residual[i] = 0;
550 for(i = 0; i < channels; i++) {
551 /* 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. */
552 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
554 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
557 memset(tmp, 0, sizeof(FLAC__int32)*4);
558 decoder->private_->output[i] = tmp + 4;
560 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
562 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
565 decoder->private_->residual[i] = tmp;
568 decoder->private_->output_capacity = size;
569 decoder->private_->output_channels = channels;
574 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
578 FLAC__bool first = true;
580 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
582 for(i = id = 0; i < 4; ) {
583 if(decoder->private_->cached) {
584 x = (FLAC__uint32)decoder->private_->lookahead;
585 decoder->private_->cached = false;
588 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
589 return false; /* the read_callback_ sets the state for us */
591 if(x == FLAC__STREAM_SYNC_STRING[i]) {
597 if(x == ID3V2_TAG_[id]) {
601 if(!stream_decoder_skip_id3v2_tag_(decoder))
602 return false; /* the read_callback_ sets the state for us */
606 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
607 decoder->private_->header_warmup[0] = (FLAC__byte)x;
608 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
609 return false; /* the read_callback_ sets the state for us */
611 /* 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 */
612 /* else we have to check if the second byte is the end of a sync code */
613 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
614 decoder->private_->lookahead = (FLAC__byte)x;
615 decoder->private_->cached = true;
617 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
618 decoder->private_->header_warmup[1] = (FLAC__byte)x;
619 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
625 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
630 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
634 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
636 FLAC__uint32 i, x, last_block, type, length;
639 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
641 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
642 return false; /* the read_callback_ sets the state for us */
643 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
644 return false; /* the read_callback_ sets the state for us */
645 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
646 return false; /* the read_callback_ sets the state for us */
647 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
648 unsigned used_bits = 0;
649 decoder->private_->stream_info.type = type;
650 decoder->private_->stream_info.is_last = last_block;
651 decoder->private_->stream_info.length = length;
653 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
654 return false; /* the read_callback_ sets the state for us */
655 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
656 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
658 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
659 return false; /* the read_callback_ sets the state for us */
660 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
661 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
663 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
664 return false; /* the read_callback_ sets the state for us */
665 decoder->private_->stream_info.data.stream_info.min_framesize = x;
666 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
668 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
669 return false; /* the read_callback_ sets the state for us */
670 decoder->private_->stream_info.data.stream_info.max_framesize = x;
671 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
673 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
674 return false; /* the read_callback_ sets the state for us */
675 decoder->private_->stream_info.data.stream_info.sample_rate = x;
676 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
678 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
679 return false; /* the read_callback_ sets the state for us */
680 decoder->private_->stream_info.data.stream_info.channels = x+1;
681 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
683 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
684 return false; /* the read_callback_ sets the state for us */
685 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
686 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
688 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))
689 return false; /* the read_callback_ sets the state for us */
690 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
692 for(i = 0; i < 16; i++) {
693 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
694 return false; /* the read_callback_ sets the state for us */
695 decoder->private_->stream_info.data.stream_info.md5sum[i] = (FLAC__byte)x;
699 /* skip the rest of the block */
700 FLAC__ASSERT(used_bits % 8 == 0);
701 length -= (used_bits / 8);
702 for(i = 0; i < length; i++) {
703 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
704 return false; /* the read_callback_ sets the state for us */
707 decoder->private_->has_stream_info = true;
708 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
710 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
711 unsigned real_points;
713 decoder->private_->seek_table.type = type;
714 decoder->private_->seek_table.is_last = last_block;
715 decoder->private_->seek_table.length = length;
717 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
719 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)))) {
720 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
723 for(i = real_points = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
724 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
725 return false; /* the read_callback_ sets the state for us */
726 decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
728 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
729 return false; /* the read_callback_ sets the state for us */
730 decoder->private_->seek_table.data.seek_table.points[real_points].stream_offset = xx;
732 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
733 return false; /* the read_callback_ sets the state for us */
734 decoder->private_->seek_table.data.seek_table.points[real_points].frame_samples = x;
736 if(decoder->private_->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
739 decoder->private_->seek_table.data.seek_table.num_points = real_points;
741 decoder->private_->has_seek_table = true;
742 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
745 /* skip other metadata blocks */
746 for(i = 0; i < length; i++) {
747 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
748 return false; /* the read_callback_ sets the state for us */
753 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
758 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
763 /* skip the version and flags bytes */
764 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
765 return false; /* the read_callback_ sets the state for us */
766 /* get the size (in bytes) to skip */
768 for(i = 0; i < 4; i++) {
769 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
770 return false; /* the read_callback_ sets the state for us */
774 /* skip the rest of the tag */
775 for(i = 0; i < skip; i++) {
776 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
777 return false; /* the read_callback_ sets the state for us */
782 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
785 FLAC__bool first = true;
787 /* If we know the total number of samples in the stream, stop if we've read that many. */
788 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
789 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
790 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
791 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
796 /* make sure we're byte aligned */
797 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
798 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
799 return false; /* the read_callback_ sets the state for us */
803 if(decoder->private_->cached) {
804 x = (FLAC__uint32)decoder->private_->lookahead;
805 decoder->private_->cached = false;
808 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
809 return false; /* the read_callback_ sets the state for us */
811 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
812 decoder->private_->header_warmup[0] = (FLAC__byte)x;
813 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
814 return false; /* the read_callback_ sets the state for us */
816 /* 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 */
817 /* else we have to check if the second byte is the end of a sync code */
818 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
819 decoder->private_->lookahead = (FLAC__byte)x;
820 decoder->private_->cached = true;
822 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
823 decoder->private_->header_warmup[1] = (FLAC__byte)x;
824 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
829 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
837 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
841 FLAC__int32 mid, side, left, right;
842 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
845 *got_a_frame = false;
849 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
850 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
851 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
853 if(!stream_decoder_read_frame_header_(decoder))
855 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
857 if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
859 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
861 * first figure the correct bits-per-sample of the subframe
863 unsigned bps = decoder->private_->frame.header.bits_per_sample;
864 switch(decoder->private_->frame.header.channel_assignment) {
865 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
866 /* no adjustment needed */
868 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
869 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
873 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
874 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
878 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
879 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
889 if(!stream_decoder_read_subframe_(decoder, channel, bps))
891 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
892 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
896 if(!stream_decoder_read_zero_padding_(decoder))
900 * Read the frame CRC-16 from the footer and check
902 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
903 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
904 return false; /* the read_callback_ sets the state for us */
905 if(frame_crc == (FLAC__uint16)x) {
906 /* Undo any special channel coding */
907 switch(decoder->private_->frame.header.channel_assignment) {
908 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
911 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
912 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
913 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
914 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
916 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
917 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
918 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
919 decoder->private_->output[0][i] += decoder->private_->output[1][i];
921 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
922 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
923 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
924 mid = decoder->private_->output[0][i];
925 side = decoder->private_->output[1][i];
927 if(side & 1) /* i.e. if 'side' is odd... */
931 decoder->private_->output[0][i] = left >> 1;
932 decoder->private_->output[1][i] = right >> 1;
941 /* Bad frame, emit error and zero the output signal */
942 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private_->client_data);
943 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
944 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
950 /* put the latest values into the public section of the decoder instance */
951 decoder->protected_->channels = decoder->private_->frame.header.channels;
952 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
953 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
954 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
955 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
957 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
958 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
961 /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
962 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
965 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
969 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
973 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
974 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
975 unsigned raw_header_len;
976 FLAC__bool is_unparseable = false;
977 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);
978 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);
980 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
982 /* init the raw header with the saved bits from synchronization */
983 raw_header[0] = decoder->private_->header_warmup[0];
984 raw_header[1] = decoder->private_->header_warmup[1];
988 * check to make sure that the reserved bits are 0
990 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
991 is_unparseable = true;
995 * Note that along the way as we read the header, we look for a sync
996 * code inside. If we find one it would indicate that our original
997 * sync was bad since there cannot be a sync code in a valid header.
1001 * read in the raw header as bytes so we can CRC it, and parse it on the way
1003 for(i = 0; i < 2; i++) {
1004 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1005 return false; /* the read_callback_ sets the state for us */
1006 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1007 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1008 decoder->private_->lookahead = (FLAC__byte)x;
1009 decoder->private_->cached = true;
1010 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1011 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1014 raw_header[raw_header_len++] = (FLAC__byte)x;
1017 switch(x = raw_header[2] >> 4) {
1019 if(is_known_fixed_blocksize_stream)
1020 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1022 is_unparseable = true;
1025 decoder->private_->frame.header.blocksize = 192;
1031 decoder->private_->frame.header.blocksize = 576 << (x-2);
1045 decoder->private_->frame.header.blocksize = 256 << (x-8);
1052 switch(x = raw_header[2] & 0x0f) {
1054 if(decoder->private_->has_stream_info)
1055 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1057 is_unparseable = true;
1062 is_unparseable = true;
1065 decoder->private_->frame.header.sample_rate = 8000;
1068 decoder->private_->frame.header.sample_rate = 16000;
1071 decoder->private_->frame.header.sample_rate = 22050;
1074 decoder->private_->frame.header.sample_rate = 24000;
1077 decoder->private_->frame.header.sample_rate = 32000;
1080 decoder->private_->frame.header.sample_rate = 44100;
1083 decoder->private_->frame.header.sample_rate = 48000;
1086 decoder->private_->frame.header.sample_rate = 96000;
1091 sample_rate_hint = x;
1094 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1095 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1101 x = (unsigned)(raw_header[3] >> 4);
1103 decoder->private_->frame.header.channels = 2;
1106 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1109 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1112 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1115 is_unparseable = true;
1120 decoder->private_->frame.header.channels = (unsigned)x + 1;
1121 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1124 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1126 if(decoder->private_->has_stream_info)
1127 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1129 is_unparseable = true;
1132 decoder->private_->frame.header.bits_per_sample = 8;
1135 decoder->private_->frame.header.bits_per_sample = 12;
1138 decoder->private_->frame.header.bits_per_sample = 16;
1141 decoder->private_->frame.header.bits_per_sample = 20;
1144 decoder->private_->frame.header.bits_per_sample = 24;
1148 is_unparseable = true;
1155 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1156 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1157 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1161 if(blocksize_hint && is_known_variable_blocksize_stream) {
1162 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1163 return false; /* the read_callback_ sets the state for us */
1164 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1165 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1166 decoder->private_->cached = true;
1167 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1168 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1171 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1172 decoder->private_->frame.header.number.sample_number = xx;
1175 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1176 return false; /* the read_callback_ sets the state for us */
1177 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1178 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1179 decoder->private_->cached = true;
1180 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1181 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1184 decoder->private_->last_frame_number = x;
1185 if(decoder->private_->has_stream_info) {
1186 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1187 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1190 is_unparseable = true;
1194 if(blocksize_hint) {
1195 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1196 return false; /* the read_callback_ sets the state for us */
1197 raw_header[raw_header_len++] = (FLAC__byte)x;
1198 if(blocksize_hint == 7) {
1200 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1201 return false; /* the read_callback_ sets the state for us */
1202 raw_header[raw_header_len++] = (FLAC__byte)_x;
1205 decoder->private_->frame.header.blocksize = x+1;
1208 if(sample_rate_hint) {
1209 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1210 return false; /* the read_callback_ sets the state for us */
1211 raw_header[raw_header_len++] = (FLAC__byte)x;
1212 if(sample_rate_hint != 12) {
1214 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1215 return false; /* the read_callback_ sets the state for us */
1216 raw_header[raw_header_len++] = (FLAC__byte)_x;
1219 if(sample_rate_hint == 12)
1220 decoder->private_->frame.header.sample_rate = x*1000;
1221 else if(sample_rate_hint == 13)
1222 decoder->private_->frame.header.sample_rate = x;
1224 decoder->private_->frame.header.sample_rate = x*10;
1227 /* read the CRC-8 byte */
1228 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1229 return false; /* the read_callback_ sets the state for us */
1230 crc8 = (FLAC__byte)x;
1232 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1233 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1234 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1238 if(is_unparseable) {
1239 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1246 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1249 FLAC__bool wasted_bits;
1251 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1252 return false; /* the read_callback_ sets the state for us */
1254 wasted_bits = (x & 1);
1259 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1260 return false; /* the read_callback_ sets the state for us */
1261 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1262 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1265 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1268 * Lots of magic numbers here
1271 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1272 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1276 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1280 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1284 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1288 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1292 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1296 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1302 x = decoder->private_->frame.subframes[channel].wasted_bits;
1303 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1304 decoder->private_->output[channel][i] <<= x;
1310 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1312 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1315 FLAC__int32 *output = decoder->private_->output[channel];
1317 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1319 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1320 return false; /* the read_callback_ sets the state for us */
1322 subframe->value = x;
1324 /* decode the subframe */
1325 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1331 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1333 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1338 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1340 subframe->residual = decoder->private_->residual[channel];
1341 subframe->order = order;
1343 /* read warm-up samples */
1344 for(u = 0; u < order; u++) {
1345 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1346 return false; /* the read_callback_ sets the state for us */
1347 subframe->warmup[u] = i32;
1350 /* read entropy coding method info */
1351 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1352 return false; /* the read_callback_ sets the state for us */
1353 subframe->entropy_coding_method.type = u32;
1354 switch(subframe->entropy_coding_method.type) {
1355 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1356 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1357 return false; /* the read_callback_ sets the state for us */
1358 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1361 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1366 switch(subframe->entropy_coding_method.type) {
1367 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1368 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1375 /* decode the subframe */
1376 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1377 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1382 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1384 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1389 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1391 subframe->residual = decoder->private_->residual[channel];
1392 subframe->order = order;
1394 /* read warm-up samples */
1395 for(u = 0; u < order; u++) {
1396 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1397 return false; /* the read_callback_ sets the state for us */
1398 subframe->warmup[u] = i32;
1401 /* read qlp coeff precision */
1402 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1403 return false; /* the read_callback_ sets the state for us */
1404 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1405 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1406 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1409 subframe->qlp_coeff_precision = u32+1;
1411 /* read qlp shift */
1412 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1413 return false; /* the read_callback_ sets the state for us */
1414 subframe->quantization_level = i32;
1416 /* read quantized lp coefficiencts */
1417 for(u = 0; u < order; u++) {
1418 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1419 return false; /* the read_callback_ sets the state for us */
1420 subframe->qlp_coeff[u] = i32;
1423 /* read entropy coding method info */
1424 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1425 return false; /* the read_callback_ sets the state for us */
1426 subframe->entropy_coding_method.type = u32;
1427 switch(subframe->entropy_coding_method.type) {
1428 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1429 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1430 return false; /* the read_callback_ sets the state for us */
1431 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1434 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1439 switch(subframe->entropy_coding_method.type) {
1440 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1441 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1448 /* decode the subframe */
1449 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1450 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1451 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);
1453 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);
1458 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1460 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1461 FLAC__int32 x, *residual = decoder->private_->residual[channel];
1464 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1466 subframe->data = residual;
1468 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1469 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1470 return false; /* the read_callback_ sets the state for us */
1474 /* decode the subframe */
1475 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1480 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1482 FLAC__uint32 rice_parameter;
1484 unsigned partition, sample, u;
1485 const unsigned partition_order = partitioned_rice->order;
1486 const unsigned partitions = 1u << partition_order;
1487 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1490 for(partition = 0; partition < partitions; partition++) {
1491 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1492 return false; /* the read_callback_ sets the state for us */
1493 partitioned_rice->parameters[partition] = rice_parameter;
1494 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1495 #ifdef FLAC__SYMMETRIC_RICE
1496 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1497 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1498 return false; /* the read_callback_ sets the state for us */
1499 residual[sample] = i;
1502 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1503 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1504 return false; /* the read_callback_ sets the state for us */
1509 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1510 return false; /* the read_callback_ sets the state for us */
1511 partitioned_rice->raw_bits[partition] = rice_parameter;
1512 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1513 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1514 return false; /* the read_callback_ sets the state for us */
1515 residual[sample] = i;
1523 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1525 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1526 FLAC__uint32 zero = 0;
1527 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1528 return false; /* the read_callback_ sets the state for us */
1530 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1531 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1537 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1539 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1540 FLAC__StreamDecoderReadStatus status;
1542 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1543 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1544 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1545 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1546 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1547 return status == FLAC__STREAM_DECODER_READ_CONTINUE;