1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 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);
155 decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;
157 decoder->private->read_callback = 0;
158 decoder->private->write_callback = 0;
159 decoder->private->metadata_callback = 0;
160 decoder->private->error_callback = 0;
161 decoder->private->client_data = 0;
166 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
168 FLAC__ASSERT(decoder != 0);
169 FLAC__ASSERT(decoder->protected != 0);
170 FLAC__ASSERT(decoder->private != 0);
172 free(decoder->private);
173 free(decoder->protected);
177 /***********************************************************************
179 * Public class methods
181 ***********************************************************************/
183 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
187 FLAC__ASSERT(decoder != 0);
189 if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
190 return decoder->protected->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
192 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
194 if(0 == decoder->private->read_callback || 0 == decoder->private->write_callback || 0 == decoder->private->metadata_callback || 0 == decoder->private->error_callback)
195 return decoder->protected->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
197 FLAC__bitbuffer_init(&decoder->private->input);
199 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
200 decoder->private->output[i] = 0;
201 decoder->private->residual[i] = 0;
204 decoder->private->output_capacity = 0;
205 decoder->private->output_channels = 0;
206 decoder->private->last_frame_number = 0;
207 decoder->private->samples_decoded = 0;
208 decoder->private->has_stream_info = false;
209 decoder->private->has_seek_table = false;
210 decoder->private->cached = false;
213 * get the CPU info and set the function pointers
215 FLAC__cpu_info(&decoder->private->cpuinfo);
216 /* first default to the non-asm routines */
217 decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal;
218 decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
219 /* now override with asm where appropriate */
221 FLAC__ASSERT(decoder->private->cpuinfo.use_asm);
222 #ifdef FLAC__CPU_IA32
223 FLAC__ASSERT(decoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
224 #ifdef FLAC__HAS_NASM
225 if(decoder->private->cpuinfo.data.ia32.mmx) {
226 decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
227 decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
230 decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
231 decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
237 return decoder->protected->state;
240 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
243 FLAC__ASSERT(decoder != 0);
244 if(decoder->protected->state == FLAC__STREAM_DECODER_UNINITIALIZED)
246 if(decoder->private->has_seek_table) {
247 free(decoder->private->seek_table.data.seek_table.points);
248 decoder->private->seek_table.data.seek_table.points = 0;
250 FLAC__bitbuffer_free(&decoder->private->input);
251 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
252 /* 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. */
253 if(decoder->private->output[i] != 0) {
254 free(decoder->private->output[i]-4);
255 decoder->private->output[i] = 0;
257 if(decoder->private->residual[i] != 0) {
258 free(decoder->private->residual[i]);
259 decoder->private->residual[i] = 0;
262 decoder->private->output_capacity = 0;
263 decoder->private->output_channels = 0;
264 decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;
267 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))
269 if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
271 decoder->private->read_callback = value;
275 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))
277 if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
279 decoder->private->write_callback = value;
283 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))
285 if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
287 decoder->private->metadata_callback = value;
291 FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
293 if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
295 decoder->private->error_callback = value;
299 FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
301 if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
303 decoder->private->client_data = value;
307 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
309 return decoder->protected->state;
312 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
314 return decoder->protected->channels;
317 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
319 return decoder->protected->channel_assignment;
322 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
324 return decoder->protected->bits_per_sample;
327 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
329 return decoder->protected->sample_rate;
332 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
334 return decoder->protected->blocksize;
337 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
339 FLAC__ASSERT(decoder != 0);
341 if(!FLAC__bitbuffer_clear(&decoder->private->input)) {
342 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
349 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
351 FLAC__ASSERT(decoder != 0);
353 if(!FLAC__stream_decoder_flush(decoder)) {
354 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
357 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
359 decoder->private->samples_decoded = 0;
364 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
367 FLAC__ASSERT(decoder != 0);
369 if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
372 FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
374 if(!FLAC__stream_decoder_reset(decoder)) {
375 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
380 switch(decoder->protected->state) {
381 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
382 if(!stream_decoder_find_metadata_(decoder))
383 return false; /* above function sets the status for us */
385 case FLAC__STREAM_DECODER_READ_METADATA:
386 if(!stream_decoder_read_metadata_(decoder))
387 return false; /* above function sets the status for us */
389 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
390 if(!stream_decoder_frame_sync_(decoder))
391 return true; /* above function sets the status for us */
393 case FLAC__STREAM_DECODER_READ_FRAME:
394 if(!stream_decoder_read_frame_(decoder, &dummy))
395 return false; /* above function sets the status for us */
397 case FLAC__STREAM_DECODER_END_OF_STREAM:
405 FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
407 FLAC__ASSERT(decoder != 0);
409 if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
412 FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
414 if(!FLAC__stream_decoder_reset(decoder)) {
415 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
420 switch(decoder->protected->state) {
421 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
422 if(!stream_decoder_find_metadata_(decoder))
423 return false; /* above function sets the status for us */
425 case FLAC__STREAM_DECODER_READ_METADATA:
426 if(!stream_decoder_read_metadata_(decoder))
427 return false; /* above function sets the status for us */
429 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
432 case FLAC__STREAM_DECODER_END_OF_STREAM:
440 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
442 FLAC__bool got_a_frame;
443 FLAC__ASSERT(decoder != 0);
445 if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
448 FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
451 switch(decoder->protected->state) {
452 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
453 if(!stream_decoder_frame_sync_(decoder))
454 return true; /* above function sets the status for us */
456 case FLAC__STREAM_DECODER_READ_FRAME:
457 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
458 return false; /* above function sets the status for us */
460 return true; /* above function sets the status for us */
462 case FLAC__STREAM_DECODER_END_OF_STREAM:
470 FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
473 FLAC__ASSERT(decoder != 0);
475 if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
478 FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
481 switch(decoder->protected->state) {
482 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
483 if(!stream_decoder_frame_sync_(decoder))
484 return true; /* above function sets the status for us */
486 case FLAC__STREAM_DECODER_READ_FRAME:
487 if(!stream_decoder_read_frame_(decoder, &dummy))
488 return false; /* above function sets the status for us */
490 case FLAC__STREAM_DECODER_END_OF_STREAM:
498 /***********************************************************************
500 * Protected class methods
502 ***********************************************************************/
504 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
506 FLAC__ASSERT(decoder != 0);
507 return decoder->private->input.bytes - decoder->private->input.consumed_bytes;
510 /***********************************************************************
512 * Private class methods
514 ***********************************************************************/
516 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
521 if(size <= decoder->private->output_capacity && channels <= decoder->private->output_channels)
524 /* @@@ should change to use realloc() */
526 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
527 if(decoder->private->output[i] != 0) {
528 free(decoder->private->output[i]);
529 decoder->private->output[i] = 0;
531 if(decoder->private->residual[i] != 0) {
532 free(decoder->private->residual[i]);
533 decoder->private->residual[i] = 0;
537 for(i = 0; i < channels; i++) {
538 /* 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. */
539 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
541 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
544 memset(tmp, 0, sizeof(FLAC__int32)*4);
545 decoder->private->output[i] = tmp + 4;
547 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
549 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
552 decoder->private->residual[i] = tmp;
555 decoder->private->output_capacity = size;
556 decoder->private->output_channels = channels;
561 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
565 FLAC__bool first = true;
567 FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
569 for(i = id = 0; i < 4; ) {
570 if(decoder->private->cached) {
571 x = (FLAC__uint32)decoder->private->lookahead;
572 decoder->private->cached = false;
575 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
576 return false; /* the read_callback_ sets the state for us */
578 if(x == FLAC__STREAM_SYNC_STRING[i]) {
584 if(x == ID3V2_TAG_[id]) {
588 if(!stream_decoder_skip_id3v2_tag_(decoder))
589 return false; /* the read_callback_ sets the state for us */
593 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
594 decoder->private->header_warmup[0] = (FLAC__byte)x;
595 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
596 return false; /* the read_callback_ sets the state for us */
598 /* 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 */
599 /* else we have to check if the second byte is the end of a sync code */
600 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
601 decoder->private->lookahead = (FLAC__byte)x;
602 decoder->private->cached = true;
604 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
605 decoder->private->header_warmup[1] = (FLAC__byte)x;
606 decoder->protected->state = FLAC__STREAM_DECODER_READ_FRAME;
612 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
617 decoder->protected->state = FLAC__STREAM_DECODER_READ_METADATA;
621 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
623 FLAC__uint32 i, x, last_block, type, length;
626 FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
628 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
629 return false; /* the read_callback_ sets the state for us */
630 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
631 return false; /* the read_callback_ sets the state for us */
632 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
633 return false; /* the read_callback_ sets the state for us */
634 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
635 unsigned used_bits = 0;
636 decoder->private->stream_info.type = type;
637 decoder->private->stream_info.is_last = last_block;
638 decoder->private->stream_info.length = length;
640 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
641 return false; /* the read_callback_ sets the state for us */
642 decoder->private->stream_info.data.stream_info.min_blocksize = x;
643 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
645 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
646 return false; /* the read_callback_ sets the state for us */
647 decoder->private->stream_info.data.stream_info.max_blocksize = x;
648 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
650 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
651 return false; /* the read_callback_ sets the state for us */
652 decoder->private->stream_info.data.stream_info.min_framesize = x;
653 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
655 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
656 return false; /* the read_callback_ sets the state for us */
657 decoder->private->stream_info.data.stream_info.max_framesize = x;
658 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
660 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
661 return false; /* the read_callback_ sets the state for us */
662 decoder->private->stream_info.data.stream_info.sample_rate = x;
663 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
665 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
666 return false; /* the read_callback_ sets the state for us */
667 decoder->private->stream_info.data.stream_info.channels = x+1;
668 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
670 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
671 return false; /* the read_callback_ sets the state for us */
672 decoder->private->stream_info.data.stream_info.bits_per_sample = x+1;
673 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
675 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))
676 return false; /* the read_callback_ sets the state for us */
677 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
679 for(i = 0; i < 16; i++) {
680 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
681 return false; /* the read_callback_ sets the state for us */
682 decoder->private->stream_info.data.stream_info.md5sum[i] = (FLAC__byte)x;
686 /* skip the rest of the block */
687 FLAC__ASSERT(used_bits % 8 == 0);
688 length -= (used_bits / 8);
689 for(i = 0; i < length; i++) {
690 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
691 return false; /* the read_callback_ sets the state for us */
694 decoder->private->has_stream_info = true;
695 decoder->private->metadata_callback(decoder, &decoder->private->stream_info, decoder->private->client_data);
697 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
698 unsigned real_points;
700 decoder->private->seek_table.type = type;
701 decoder->private->seek_table.is_last = last_block;
702 decoder->private->seek_table.length = length;
704 decoder->private->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
706 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)))) {
707 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
710 for(i = real_points = 0; i < decoder->private->seek_table.data.seek_table.num_points; i++) {
711 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
712 return false; /* the read_callback_ sets the state for us */
713 decoder->private->seek_table.data.seek_table.points[real_points].sample_number = xx;
715 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
716 return false; /* the read_callback_ sets the state for us */
717 decoder->private->seek_table.data.seek_table.points[real_points].stream_offset = xx;
719 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
720 return false; /* the read_callback_ sets the state for us */
721 decoder->private->seek_table.data.seek_table.points[real_points].frame_samples = x;
723 if(decoder->private->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
726 decoder->private->seek_table.data.seek_table.num_points = real_points;
728 decoder->private->has_seek_table = true;
729 decoder->private->metadata_callback(decoder, &decoder->private->seek_table, decoder->private->client_data);
732 /* skip other metadata blocks */
733 for(i = 0; i < length; i++) {
734 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
735 return false; /* the read_callback_ sets the state for us */
740 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
745 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
750 /* skip the version and flags bytes */
751 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 24, read_callback_, decoder))
752 return false; /* the read_callback_ sets the state for us */
753 /* get the size (in bytes) to skip */
755 for(i = 0; i < 4; i++) {
756 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
757 return false; /* the read_callback_ sets the state for us */
761 /* skip the rest of the tag */
762 for(i = 0; i < skip; i++) {
763 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
764 return false; /* the read_callback_ sets the state for us */
769 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
772 FLAC__bool first = true;
774 /* If we know the total number of samples in the stream, stop if we've read that many. */
775 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
776 if(decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.total_samples) {
777 if(decoder->private->samples_decoded >= decoder->private->stream_info.data.stream_info.total_samples) {
778 decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
783 /* make sure we're byte aligned */
784 if(decoder->private->input.consumed_bits != 0) {
785 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
786 return false; /* the read_callback_ sets the state for us */
790 if(decoder->private->cached) {
791 x = (FLAC__uint32)decoder->private->lookahead;
792 decoder->private->cached = false;
795 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
796 return false; /* the read_callback_ sets the state for us */
798 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
799 decoder->private->header_warmup[0] = (FLAC__byte)x;
800 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
801 return false; /* the read_callback_ sets the state for us */
803 /* 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 */
804 /* else we have to check if the second byte is the end of a sync code */
805 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
806 decoder->private->lookahead = (FLAC__byte)x;
807 decoder->private->cached = true;
809 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
810 decoder->private->header_warmup[1] = (FLAC__byte)x;
811 decoder->protected->state = FLAC__STREAM_DECODER_READ_FRAME;
816 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
824 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
828 FLAC__int32 mid, side, left, right;
829 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
832 *got_a_frame = false;
836 FLAC__CRC16_UPDATE(decoder->private->header_warmup[0], frame_crc);
837 FLAC__CRC16_UPDATE(decoder->private->header_warmup[1], frame_crc);
838 FLAC__bitbuffer_init_read_crc16(&decoder->private->input, frame_crc);
840 if(!stream_decoder_read_frame_header_(decoder))
842 if(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
844 if(!stream_decoder_allocate_output_(decoder, decoder->private->frame.header.blocksize, decoder->private->frame.header.channels))
846 for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
848 * first figure the correct bits-per-sample of the subframe
850 unsigned bps = decoder->private->frame.header.bits_per_sample;
851 switch(decoder->private->frame.header.channel_assignment) {
852 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
853 /* no adjustment needed */
855 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
856 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
860 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
861 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
865 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
866 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
876 if(!stream_decoder_read_subframe_(decoder, channel, bps))
878 if(decoder->protected->state != FLAC__STREAM_DECODER_READ_FRAME) {
879 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
883 if(!stream_decoder_read_zero_padding_(decoder))
887 * Read the frame CRC-16 from the footer and check
889 frame_crc = decoder->private->input.read_crc16;
890 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
891 return false; /* the read_callback_ sets the state for us */
892 if(frame_crc == (FLAC__uint16)x) {
893 /* Undo any special channel coding */
894 switch(decoder->private->frame.header.channel_assignment) {
895 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
898 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
899 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
900 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
901 decoder->private->output[1][i] = decoder->private->output[0][i] - decoder->private->output[1][i];
903 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
904 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
905 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
906 decoder->private->output[0][i] += decoder->private->output[1][i];
908 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
909 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
910 for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
911 mid = decoder->private->output[0][i];
912 side = decoder->private->output[1][i];
914 if(side & 1) /* i.e. if 'side' is odd... */
918 decoder->private->output[0][i] = left >> 1;
919 decoder->private->output[1][i] = right >> 1;
928 /* Bad frame, emit error and zero the output signal */
929 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private->client_data);
930 for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
931 memset(decoder->private->output[channel], 0, sizeof(FLAC__int32) * decoder->private->frame.header.blocksize);
937 /* put the latest values into the public section of the decoder instance */
938 decoder->protected->channels = decoder->private->frame.header.channels;
939 decoder->protected->channel_assignment = decoder->private->frame.header.channel_assignment;
940 decoder->protected->bits_per_sample = decoder->private->frame.header.bits_per_sample;
941 decoder->protected->sample_rate = decoder->private->frame.header.sample_rate;
942 decoder->protected->blocksize = decoder->private->frame.header.blocksize;
944 decoder->private->samples_decoded = decoder->private->frame.header.number.sample_number + decoder->private->frame.header.blocksize;
947 /* 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: */
948 if(decoder->private->write_callback(decoder, &decoder->private->frame, decoder->private->output, decoder->private->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
951 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
955 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
959 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
960 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
961 unsigned raw_header_len;
962 FLAC__bool is_unparseable = false;
963 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);
964 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);
966 FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
968 /* init the raw header with the saved bits from synchronization */
969 raw_header[0] = decoder->private->header_warmup[0];
970 raw_header[1] = decoder->private->header_warmup[1];
974 * check to make sure that the reserved bits are 0
976 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
977 is_unparseable = true;
981 * Note that along the way as we read the header, we look for a sync
982 * code inside. If we find one it would indicate that our original
983 * sync was bad since there cannot be a sync code in a valid header.
987 * read in the raw header as bytes so we can CRC it, and parse it on the way
989 for(i = 0; i < 2; i++) {
990 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
991 return false; /* the read_callback_ sets the state for us */
992 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
993 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
994 decoder->private->lookahead = (FLAC__byte)x;
995 decoder->private->cached = true;
996 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
997 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1000 raw_header[raw_header_len++] = (FLAC__byte)x;
1003 switch(x = raw_header[2] >> 4) {
1005 if(is_known_fixed_blocksize_stream)
1006 decoder->private->frame.header.blocksize = decoder->private->stream_info.data.stream_info.min_blocksize;
1008 is_unparseable = true;
1011 decoder->private->frame.header.blocksize = 192;
1017 decoder->private->frame.header.blocksize = 576 << (x-2);
1031 decoder->private->frame.header.blocksize = 256 << (x-8);
1038 switch(x = raw_header[2] & 0x0f) {
1040 if(decoder->private->has_stream_info)
1041 decoder->private->frame.header.sample_rate = decoder->private->stream_info.data.stream_info.sample_rate;
1043 is_unparseable = true;
1048 is_unparseable = true;
1051 decoder->private->frame.header.sample_rate = 8000;
1054 decoder->private->frame.header.sample_rate = 16000;
1057 decoder->private->frame.header.sample_rate = 22050;
1060 decoder->private->frame.header.sample_rate = 24000;
1063 decoder->private->frame.header.sample_rate = 32000;
1066 decoder->private->frame.header.sample_rate = 44100;
1069 decoder->private->frame.header.sample_rate = 48000;
1072 decoder->private->frame.header.sample_rate = 96000;
1077 sample_rate_hint = x;
1080 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1081 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1087 x = (unsigned)(raw_header[3] >> 4);
1089 decoder->private->frame.header.channels = 2;
1092 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1095 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1098 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1101 is_unparseable = true;
1106 decoder->private->frame.header.channels = (unsigned)x + 1;
1107 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1110 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1112 if(decoder->private->has_stream_info)
1113 decoder->private->frame.header.bits_per_sample = decoder->private->stream_info.data.stream_info.bits_per_sample;
1115 is_unparseable = true;
1118 decoder->private->frame.header.bits_per_sample = 8;
1121 decoder->private->frame.header.bits_per_sample = 12;
1124 decoder->private->frame.header.bits_per_sample = 16;
1127 decoder->private->frame.header.bits_per_sample = 20;
1130 decoder->private->frame.header.bits_per_sample = 24;
1134 is_unparseable = true;
1141 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1142 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1143 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1147 if(blocksize_hint && is_known_variable_blocksize_stream) {
1148 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->private->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1149 return false; /* the read_callback_ sets the state for us */
1150 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1151 decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1152 decoder->private->cached = true;
1153 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1154 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1157 decoder->private->frame.header.number.sample_number = xx;
1160 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->private->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1161 return false; /* the read_callback_ sets the state for us */
1162 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1163 decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1164 decoder->private->cached = true;
1165 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1166 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1169 decoder->private->last_frame_number = x;
1170 if(decoder->private->has_stream_info) {
1171 decoder->private->frame.header.number.sample_number = (FLAC__int64)decoder->private->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1174 is_unparseable = true;
1178 if(blocksize_hint) {
1179 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1180 return false; /* the read_callback_ sets the state for us */
1181 raw_header[raw_header_len++] = (FLAC__byte)x;
1182 if(blocksize_hint == 7) {
1184 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
1185 return false; /* the read_callback_ sets the state for us */
1186 raw_header[raw_header_len++] = (FLAC__byte)_x;
1189 decoder->private->frame.header.blocksize = x+1;
1192 if(sample_rate_hint) {
1193 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1194 return false; /* the read_callback_ sets the state for us */
1195 raw_header[raw_header_len++] = (FLAC__byte)x;
1196 if(sample_rate_hint != 12) {
1198 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
1199 return false; /* the read_callback_ sets the state for us */
1200 raw_header[raw_header_len++] = (FLAC__byte)_x;
1203 if(sample_rate_hint == 12)
1204 decoder->private->frame.header.sample_rate = x*1000;
1205 else if(sample_rate_hint == 13)
1206 decoder->private->frame.header.sample_rate = x;
1208 decoder->private->frame.header.sample_rate = x*10;
1211 /* read the CRC-8 byte */
1212 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1213 return false; /* the read_callback_ sets the state for us */
1214 crc8 = (FLAC__byte)x;
1216 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1217 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1218 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1222 if(is_unparseable) {
1223 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1230 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1233 FLAC__bool wasted_bits;
1235 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1236 return false; /* the read_callback_ sets the state for us */
1238 wasted_bits = (x & 1);
1243 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->private->input, &u, read_callback_, decoder))
1244 return false; /* the read_callback_ sets the state for us */
1245 decoder->private->frame.subframes[channel].wasted_bits = u+1;
1246 bps -= decoder->private->frame.subframes[channel].wasted_bits;
1249 decoder->private->frame.subframes[channel].wasted_bits = 0;
1252 * Lots of magic numbers here
1255 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1256 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1260 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1264 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1268 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1272 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1276 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1280 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1286 x = decoder->private->frame.subframes[channel].wasted_bits;
1287 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
1288 decoder->private->output[channel][i] <<= x;
1294 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1296 FLAC__Subframe_Constant *subframe = &decoder->private->frame.subframes[channel].data.constant;
1299 FLAC__int32 *output = decoder->private->output[channel];
1301 decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1303 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
1304 return false; /* the read_callback_ sets the state for us */
1306 subframe->value = x;
1308 /* decode the subframe */
1309 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
1315 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1317 FLAC__Subframe_Fixed *subframe = &decoder->private->frame.subframes[channel].data.fixed;
1322 decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1324 subframe->residual = decoder->private->residual[channel];
1325 subframe->order = order;
1327 /* read warm-up samples */
1328 for(u = 0; u < order; u++) {
1329 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
1330 return false; /* the read_callback_ sets the state for us */
1331 subframe->warmup[u] = i32;
1334 /* read entropy coding method info */
1335 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1336 return false; /* the read_callback_ sets the state for us */
1337 subframe->entropy_coding_method.type = u32;
1338 switch(subframe->entropy_coding_method.type) {
1339 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1340 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1341 return false; /* the read_callback_ sets the state for us */
1342 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1345 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1350 switch(subframe->entropy_coding_method.type) {
1351 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1352 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private->residual[channel]))
1359 /* decode the subframe */
1360 memcpy(decoder->private->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1361 FLAC__fixed_restore_signal(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, order, decoder->private->output[channel]+order);
1366 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1368 FLAC__Subframe_LPC *subframe = &decoder->private->frame.subframes[channel].data.lpc;
1373 decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1375 subframe->residual = decoder->private->residual[channel];
1376 subframe->order = order;
1378 /* read warm-up samples */
1379 for(u = 0; u < order; u++) {
1380 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
1381 return false; /* the read_callback_ sets the state for us */
1382 subframe->warmup[u] = i32;
1385 /* read qlp coeff precision */
1386 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1387 return false; /* the read_callback_ sets the state for us */
1388 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1389 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1390 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1393 subframe->qlp_coeff_precision = u32+1;
1395 /* read qlp shift */
1396 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1397 return false; /* the read_callback_ sets the state for us */
1398 subframe->quantization_level = i32;
1400 /* read quantized lp coefficiencts */
1401 for(u = 0; u < order; u++) {
1402 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1403 return false; /* the read_callback_ sets the state for us */
1404 subframe->qlp_coeff[u] = i32;
1407 /* read entropy coding method info */
1408 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1409 return false; /* the read_callback_ sets the state for us */
1410 subframe->entropy_coding_method.type = u32;
1411 switch(subframe->entropy_coding_method.type) {
1412 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1413 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1414 return false; /* the read_callback_ sets the state for us */
1415 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1418 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1423 switch(subframe->entropy_coding_method.type) {
1424 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1425 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private->residual[channel]))
1432 /* decode the subframe */
1433 memcpy(decoder->private->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1434 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1435 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);
1437 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);
1442 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1444 FLAC__Subframe_Verbatim *subframe = &decoder->private->frame.subframes[channel].data.verbatim;
1445 FLAC__int32 x, *residual = decoder->private->residual[channel];
1448 decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1450 subframe->data = residual;
1452 for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
1453 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
1454 return false; /* the read_callback_ sets the state for us */
1458 /* decode the subframe */
1459 memcpy(decoder->private->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private->frame.header.blocksize);
1464 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1466 FLAC__uint32 rice_parameter;
1468 unsigned partition, sample, u;
1469 const unsigned partition_order = partitioned_rice->order;
1470 const unsigned partitions = 1u << partition_order;
1471 const unsigned partition_samples = partition_order > 0? decoder->private->frame.header.blocksize >> partition_order : decoder->private->frame.header.blocksize - predictor_order;
1474 for(partition = 0; partition < partitions; partition++) {
1475 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1476 return false; /* the read_callback_ sets the state for us */
1477 partitioned_rice->parameters[partition] = rice_parameter;
1478 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1479 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1480 #ifdef FLAC__SYMMETRIC_RICE
1481 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1482 return false; /* the read_callback_ sets the state for us */
1484 if(!FLAC__bitbuffer_read_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1485 return false; /* the read_callback_ sets the state for us */
1487 residual[sample] = i;
1491 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1492 return false; /* the read_callback_ sets the state for us */
1493 partitioned_rice->raw_bits[partition] = rice_parameter;
1494 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1495 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1496 return false; /* the read_callback_ sets the state for us */
1497 residual[sample] = i;
1505 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1507 if(decoder->private->input.consumed_bits != 0) {
1508 FLAC__uint32 zero = 0;
1509 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &zero, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
1510 return false; /* the read_callback_ sets the state for us */
1512 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1513 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1519 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1521 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1522 FLAC__StreamDecoderReadStatus status;
1523 status = decoder->private->read_callback(decoder, buffer, bytes, decoder->private->client_data);
1524 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1525 decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1526 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1527 decoder->protected->state = FLAC__STREAM_DECODER_ABORTED;
1528 return status == FLAC__STREAM_DECODER_READ_CONTINUE;