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.
22 #include <stdlib.h> /* for malloc() */
23 #include "FLAC/stream_decoder.h"
24 #include "private/bitbuffer.h"
25 #include "private/crc.h"
26 #include "private/fixed.h"
27 #include "private/lpc.h"
29 typedef struct FLAC__StreamDecoderPrivate {
30 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
31 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
32 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
33 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
35 FLAC__BitBuffer input;
36 int32 *output[FLAC__MAX_CHANNELS];
37 int32 *residual[FLAC__MAX_CHANNELS];
38 unsigned output_capacity, output_channels;
39 uint32 last_frame_number;
40 uint64 samples_decoded;
41 bool has_stream_header;
42 FLAC__StreamMetaData stream_header;
44 byte header_warmup[2]; /* contains the sync code and reserved bits */
45 byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
46 bool cached; /* true if there is a byte in lookahead */
47 } FLAC__StreamDecoderPrivate;
49 static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
51 static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
52 static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
53 static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
54 static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
55 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
56 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
57 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
58 static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
59 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
60 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
61 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
62 static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
63 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
64 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
65 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
67 const char *FLAC__StreamDecoderStateString[] = {
68 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
69 "FLAC__STREAM_DECODER_READ_METADATA",
70 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
71 "FLAC__STREAM_DECODER_READ_FRAME",
72 "FLAC__STREAM_DECODER_END_OF_STREAM",
73 "FLAC__STREAM_DECODER_ABORTED",
74 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
75 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
76 "FLAC__STREAM_DECODER_UNINITIALIZED"
79 const char *FLAC__StreamDecoderReadStatusString[] = {
80 "FLAC__STREAM_DECODER_READ_CONTINUE",
81 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
82 "FLAC__STREAM_DECODER_READ_ABORT"
85 const char *FLAC__StreamDecoderWriteStatusString[] = {
86 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
87 "FLAC__STREAM_DECODER_WRITE_ABORT"
90 const char *FLAC__StreamDecoderErrorStatusString[] = {
91 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
92 "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
93 "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
96 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
98 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
100 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
106 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
111 FLAC__StreamDecoderState FLAC__stream_decoder_init(
112 FLAC__StreamDecoder *decoder,
113 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
114 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
115 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
116 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
122 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
123 assert(decoder != 0);
124 assert(read_callback != 0);
125 assert(write_callback != 0);
126 assert(metadata_callback != 0);
127 assert(error_callback != 0);
128 assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
129 assert(decoder->guts == 0);
131 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
133 decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
134 if(decoder->guts == 0)
135 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
137 decoder->guts->read_callback = read_callback;
138 decoder->guts->write_callback = write_callback;
139 decoder->guts->metadata_callback = metadata_callback;
140 decoder->guts->error_callback = error_callback;
141 decoder->guts->client_data = client_data;
143 FLAC__bitbuffer_init(&decoder->guts->input);
145 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
146 decoder->guts->output[i] = 0;
147 decoder->guts->residual[i] = 0;
150 decoder->guts->output_capacity = 0;
151 decoder->guts->output_channels = 0;
152 decoder->guts->last_frame_number = 0;
153 decoder->guts->samples_decoded = 0;
154 decoder->guts->has_stream_header = false;
155 decoder->guts->cached = false;
157 return decoder->state;
160 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
163 assert(decoder != 0);
164 if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
166 if(decoder->guts != 0) {
167 FLAC__bitbuffer_free(&decoder->guts->input);
168 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
169 if(decoder->guts->output[i] != 0) {
170 free(decoder->guts->output[i]);
171 decoder->guts->output[i] = 0;
173 if(decoder->guts->residual[i] != 0) {
174 free(decoder->guts->residual[i]);
175 decoder->guts->residual[i] = 0;
181 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
184 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
186 assert(decoder != 0);
188 if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
189 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
196 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
198 assert(decoder != 0);
200 if(!FLAC__stream_decoder_flush(decoder)) {
201 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
204 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
209 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
212 assert(decoder != 0);
214 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
217 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
219 if(!FLAC__stream_decoder_reset(decoder)) {
220 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
225 switch(decoder->state) {
226 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
227 if(!stream_decoder_find_metadata_(decoder))
228 return false; /* above function sets the status for us */
230 case FLAC__STREAM_DECODER_READ_METADATA:
231 if(!stream_decoder_read_metadata_(decoder))
232 return false; /* above function sets the status for us */
234 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
235 if(!stream_decoder_frame_sync_(decoder))
236 return true; /* above function sets the status for us */
238 case FLAC__STREAM_DECODER_READ_FRAME:
239 if(!stream_decoder_read_frame_(decoder, &dummy))
240 return false; /* above function sets the status for us */
242 case FLAC__STREAM_DECODER_END_OF_STREAM:
250 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
252 assert(decoder != 0);
254 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
257 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
259 if(!FLAC__stream_decoder_reset(decoder)) {
260 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
265 switch(decoder->state) {
266 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
267 if(!stream_decoder_find_metadata_(decoder))
268 return false; /* above function sets the status for us */
270 case FLAC__STREAM_DECODER_READ_METADATA:
271 if(!stream_decoder_read_metadata_(decoder))
272 return false; /* above function sets the status for us */
274 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
277 case FLAC__STREAM_DECODER_END_OF_STREAM:
285 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
288 assert(decoder != 0);
290 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
293 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
296 switch(decoder->state) {
297 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
298 if(!stream_decoder_frame_sync_(decoder))
299 return true; /* above function sets the status for us */
301 case FLAC__STREAM_DECODER_READ_FRAME:
302 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
303 return false; /* above function sets the status for us */
305 return true; /* above function sets the status for us */
307 case FLAC__STREAM_DECODER_END_OF_STREAM:
315 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
318 assert(decoder != 0);
320 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
323 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
326 switch(decoder->state) {
327 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
328 if(!stream_decoder_frame_sync_(decoder))
329 return true; /* above function sets the status for us */
331 case FLAC__STREAM_DECODER_READ_FRAME:
332 if(!stream_decoder_read_frame_(decoder, &dummy))
333 return false; /* above function sets the status for us */
335 case FLAC__STREAM_DECODER_END_OF_STREAM:
343 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
345 assert(decoder != 0);
346 return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
349 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
354 if(size <= decoder->guts->output_capacity && channels <= decoder->guts->output_channels)
357 /* @@@ should change to use realloc() */
359 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
360 if(decoder->guts->output[i] != 0) {
361 free(decoder->guts->output[i]);
362 decoder->guts->output[i] = 0;
364 if(decoder->guts->residual[i] != 0) {
365 free(decoder->guts->residual[i]);
366 decoder->guts->residual[i] = 0;
370 for(i = 0; i < channels; i++) {
371 tmp = (int32*)malloc(sizeof(int32)*size);
373 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
376 decoder->guts->output[i] = tmp;
378 tmp = (int32*)malloc(sizeof(int32)*size);
380 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
383 decoder->guts->residual[i] = tmp;
386 decoder->guts->output_capacity = size;
387 decoder->guts->output_channels = channels;
392 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
398 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
400 for(i = id = 0; i < 4; ) {
401 if(decoder->guts->cached) {
402 x = (uint32)decoder->guts->lookahead;
403 decoder->guts->cached = false;
406 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
407 return false; /* the read_callback_ sets the state for us */
409 if(x == FLAC__STREAM_SYNC_STRING[i]) {
415 if(x == ID3V2_TAG_[id]) {
419 if(!stream_decoder_skip_id3v2_tag_(decoder))
420 return false; /* the read_callback_ sets the state for us */
424 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
425 decoder->guts->header_warmup[0] = (byte)x;
426 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
427 return false; /* the read_callback_ sets the state for us */
429 /* 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 */
430 /* else we have to check if the second byte is the end of a sync code */
431 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
432 decoder->guts->lookahead = (byte)x;
433 decoder->guts->cached = true;
435 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
436 decoder->guts->header_warmup[1] = (byte)x;
437 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
443 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
448 decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
452 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
454 uint32 i, x, last_block, type, length;
456 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
458 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
459 return false; /* the read_callback_ sets the state for us */
460 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
461 return false; /* the read_callback_ sets the state for us */
462 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
463 return false; /* the read_callback_ sets the state for us */
464 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
465 unsigned used_bits = 0;
466 decoder->guts->stream_header.type = type;
467 decoder->guts->stream_header.is_last = last_block;
468 decoder->guts->stream_header.length = length;
470 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
471 return false; /* the read_callback_ sets the state for us */
472 decoder->guts->stream_header.data.stream_info.min_blocksize = x;
473 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
475 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
476 return false; /* the read_callback_ sets the state for us */
477 decoder->guts->stream_header.data.stream_info.max_blocksize = x;
478 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
480 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
481 return false; /* the read_callback_ sets the state for us */
482 decoder->guts->stream_header.data.stream_info.min_framesize = x;
483 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
485 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
486 return false; /* the read_callback_ sets the state for us */
487 decoder->guts->stream_header.data.stream_info.max_framesize = x;
488 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
490 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
491 return false; /* the read_callback_ sets the state for us */
492 decoder->guts->stream_header.data.stream_info.sample_rate = x;
493 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
495 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
496 return false; /* the read_callback_ sets the state for us */
497 decoder->guts->stream_header.data.stream_info.channels = x+1;
498 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
500 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
501 return false; /* the read_callback_ sets the state for us */
502 decoder->guts->stream_header.data.stream_info.bits_per_sample = x+1;
503 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
505 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_header.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
506 return false; /* the read_callback_ sets the state for us */
507 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
509 for(i = 0; i < 16; i++) {
510 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
511 return false; /* the read_callback_ sets the state for us */
512 decoder->guts->stream_header.data.stream_info.md5sum[i] = (byte)x;
516 /* skip the rest of the block */
517 assert(used_bits % 8 == 0);
518 length -= (used_bits / 8);
519 for(i = 0; i < length; i++) {
520 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
521 return false; /* the read_callback_ sets the state for us */
524 decoder->guts->has_stream_header = true;
525 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
528 /* skip other metadata blocks */
529 for(i = 0; i < length; i++) {
530 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
531 return false; /* the read_callback_ sets the state for us */
536 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
541 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
546 /* skip the version and flags bytes */
547 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
548 return false; /* the read_callback_ sets the state for us */
549 /* get the size (in bytes) to skip */
551 for(i = 0; i < 4; i++) {
552 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
553 return false; /* the read_callback_ sets the state for us */
557 /* skip the rest of the tag */
558 for(i = 0; i < skip; i++) {
559 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
560 return false; /* the read_callback_ sets the state for us */
565 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
570 /* If we know the total number of samples in the stream, stop if we've read that many. */
571 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
572 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.total_samples) {
573 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.stream_info.total_samples) {
574 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
579 /* make sure we're byte aligned */
580 if(decoder->guts->input.consumed_bits != 0) {
581 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
582 return false; /* the read_callback_ sets the state for us */
586 if(decoder->guts->cached) {
587 x = (uint32)decoder->guts->lookahead;
588 decoder->guts->cached = false;
591 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
592 return false; /* the read_callback_ sets the state for us */
594 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
595 decoder->guts->header_warmup[0] = (byte)x;
596 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
597 return false; /* the read_callback_ sets the state for us */
599 /* 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 */
600 /* else we have to check if the second byte is the end of a sync code */
601 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
602 decoder->guts->lookahead = (byte)x;
603 decoder->guts->cached = true;
605 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
606 decoder->guts->header_warmup[1] = (byte)x;
607 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
612 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
620 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
624 int32 mid, side, left, right;
625 uint16 frame_crc; /* the one we calculate from the input stream */
628 *got_a_frame = false;
632 FLAC__CRC16_UPDATE(decoder->guts->header_warmup[0], frame_crc);
633 FLAC__CRC16_UPDATE(decoder->guts->header_warmup[1], frame_crc);
634 FLAC__bitbuffer_init_read_crc16(&decoder->guts->input, frame_crc);
636 if(!stream_decoder_read_frame_header_(decoder))
638 if(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
640 if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize, decoder->guts->frame.header.channels))
642 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
644 * first figure the correct bits-per-sample of the subframe
646 unsigned bps = decoder->guts->frame.header.bits_per_sample;
647 switch(decoder->guts->frame.header.channel_assignment) {
648 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
649 /* no adjustment needed */
651 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
652 assert(decoder->guts->frame.header.channels == 2);
656 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
657 assert(decoder->guts->frame.header.channels == 2);
661 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
662 assert(decoder->guts->frame.header.channels == 2);
672 if(!stream_decoder_read_subframe_(decoder, channel, bps))
674 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
675 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
679 if(!stream_decoder_read_zero_padding_(decoder))
683 * Read the frame CRC-16 from the footer and check
685 frame_crc = decoder->guts->input.read_crc16;
686 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
687 return false; /* the read_callback_ sets the state for us */
688 if(frame_crc == (uint16)x) {
689 /* Undo any special channel coding */
690 switch(decoder->guts->frame.header.channel_assignment) {
691 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
694 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
695 assert(decoder->guts->frame.header.channels == 2);
696 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
697 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
699 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
700 assert(decoder->guts->frame.header.channels == 2);
701 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
702 decoder->guts->output[0][i] += decoder->guts->output[1][i];
704 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
705 assert(decoder->guts->frame.header.channels == 2);
706 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
707 mid = decoder->guts->output[0][i];
708 side = decoder->guts->output[1][i];
710 if(side & 1) /* i.e. if 'side' is odd... */
714 decoder->guts->output[0][i] = left >> 1;
715 decoder->guts->output[1][i] = right >> 1;
724 /* Bad frame, emit error and zero the output signal */
725 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->guts->client_data);
726 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
727 memset(decoder->guts->output[channel], 0, sizeof(int32) * decoder->guts->frame.header.blocksize);
733 /* put the latest values into the public section of the decoder instance */
734 decoder->channels = decoder->guts->frame.header.channels;
735 decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
736 decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
737 decoder->sample_rate = decoder->guts->frame.header.sample_rate;
738 decoder->blocksize = decoder->guts->frame.header.blocksize;
740 decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
743 if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
746 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
750 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
754 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
755 byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
756 unsigned raw_header_len;
757 bool is_unparseable = false;
759 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
761 /* init the raw header with the saved bits from synchronization */
762 raw_header[0] = decoder->guts->header_warmup[0];
763 raw_header[1] = decoder->guts->header_warmup[1];
767 * check to make sure that the reserved bits are 0
769 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
770 is_unparseable = true;
774 * Note that along the way as we read the header, we look for a sync
775 * code inside. If we find one it would indicate that our original
776 * sync was bad since there cannot be a sync code in a valid header.
780 * read in the raw header as bytes so we can CRC it, and parse it on the way
782 for(i = 0; i < 2; i++) {
783 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
784 return false; /* the read_callback_ sets the state for us */
785 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
786 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
787 decoder->guts->lookahead = (byte)x;
788 decoder->guts->cached = true;
789 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
790 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
793 raw_header[raw_header_len++] = (byte)x;
796 switch(x = raw_header[2] >> 4) {
798 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.min_blocksize == decoder->guts->stream_header.data.stream_info.max_blocksize) /* i.e. it's a fixed-blocksize stream */
799 decoder->guts->frame.header.blocksize = decoder->guts->stream_header.data.stream_info.min_blocksize;
801 is_unparseable = true;
804 decoder->guts->frame.header.blocksize = 192;
810 decoder->guts->frame.header.blocksize = 576 << (x-2);
824 decoder->guts->frame.header.blocksize = 256 << (x-8);
831 switch(x = raw_header[2] & 0x0f) {
833 if(decoder->guts->has_stream_header)
834 decoder->guts->frame.header.sample_rate = decoder->guts->stream_header.data.stream_info.sample_rate;
836 is_unparseable = true;
841 is_unparseable = true;
844 decoder->guts->frame.header.sample_rate = 8000;
847 decoder->guts->frame.header.sample_rate = 16000;
850 decoder->guts->frame.header.sample_rate = 22050;
853 decoder->guts->frame.header.sample_rate = 24000;
856 decoder->guts->frame.header.sample_rate = 32000;
859 decoder->guts->frame.header.sample_rate = 44100;
862 decoder->guts->frame.header.sample_rate = 48000;
865 decoder->guts->frame.header.sample_rate = 96000;
870 sample_rate_hint = x;
873 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
874 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
880 x = (unsigned)(raw_header[3] >> 4);
882 decoder->guts->frame.header.channels = 2;
885 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
888 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
891 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
894 is_unparseable = true;
899 decoder->guts->frame.header.channels = (unsigned)x + 1;
900 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
903 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
905 if(decoder->guts->has_stream_header)
906 decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_header.data.stream_info.bits_per_sample;
908 is_unparseable = true;
911 decoder->guts->frame.header.bits_per_sample = 8;
914 decoder->guts->frame.header.bits_per_sample = 12;
917 decoder->guts->frame.header.bits_per_sample = 16;
920 decoder->guts->frame.header.bits_per_sample = 20;
923 decoder->guts->frame.header.bits_per_sample = 24;
927 is_unparseable = true;
934 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
935 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
936 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
941 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
942 return false; /* the read_callback_ sets the state for us */
943 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
944 decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
945 decoder->guts->cached = true;
946 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
947 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
950 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.min_blocksize == decoder->guts->stream_header.data.stream_info.max_blocksize) /* i.e. it's a fixed-blocksize stream */
951 decoder->guts->frame.header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.stream_info.min_blocksize + xx;
953 decoder->guts->frame.header.number.sample_number = xx;
956 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
957 return false; /* the read_callback_ sets the state for us */
958 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
959 decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
960 decoder->guts->cached = true;
961 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
962 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
965 decoder->guts->last_frame_number = x;
966 if(decoder->guts->has_stream_header) {
967 decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_header.data.stream_info.min_blocksize * (int64)x;
970 is_unparseable = true;
975 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
976 return false; /* the read_callback_ sets the state for us */
977 raw_header[raw_header_len++] = (byte)x;
978 if(blocksize_hint == 7) {
980 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
981 return false; /* the read_callback_ sets the state for us */
982 raw_header[raw_header_len++] = (byte)_x;
985 decoder->guts->frame.header.blocksize = x+1;
988 if(sample_rate_hint) {
989 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
990 return false; /* the read_callback_ sets the state for us */
991 raw_header[raw_header_len++] = (byte)x;
992 if(sample_rate_hint != 12) {
994 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
995 return false; /* the read_callback_ sets the state for us */
996 raw_header[raw_header_len++] = (byte)_x;
999 if(sample_rate_hint == 12)
1000 decoder->guts->frame.header.sample_rate = x*1000;
1001 else if(sample_rate_hint == 13)
1002 decoder->guts->frame.header.sample_rate = x;
1004 decoder->guts->frame.header.sample_rate = x*10;
1007 /* read the CRC-8 byte */
1008 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
1009 return false; /* the read_callback_ sets the state for us */
1012 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1013 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
1014 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1018 if(is_unparseable) {
1019 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1026 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1031 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1032 return false; /* the read_callback_ sets the state for us */
1034 wasted_bits = (x & 1);
1039 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->guts->input, &u, read_callback_, decoder))
1040 return false; /* the read_callback_ sets the state for us */
1041 decoder->guts->frame.subframes[channel].wasted_bits = u+1;
1044 decoder->guts->frame.subframes[channel].wasted_bits = 0;
1047 * Lots of magic numbers here
1050 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1051 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1055 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1059 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1063 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1067 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1071 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1075 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1081 x = decoder->guts->frame.subframes[channel].wasted_bits;
1082 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1083 decoder->guts->output[channel][i] <<= x;
1089 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1091 FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
1094 int32 *output = decoder->guts->output[channel];
1096 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1098 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1099 return false; /* the read_callback_ sets the state for us */
1101 subframe->value = x;
1103 /* decode the subframe */
1104 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1110 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1112 FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
1117 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1119 subframe->residual = decoder->guts->residual[channel];
1120 subframe->order = order;
1122 /* read warm-up samples */
1123 for(u = 0; u < order; u++) {
1124 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps - decoder->guts->frame.subframes[channel].wasted_bits, read_callback_, decoder))
1125 return false; /* the read_callback_ sets the state for us */
1126 subframe->warmup[u] = i32;
1129 /* read entropy coding method info */
1130 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1131 return false; /* the read_callback_ sets the state for us */
1132 subframe->entropy_coding_method.type = u32;
1133 switch(subframe->entropy_coding_method.type) {
1134 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1135 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1136 return false; /* the read_callback_ sets the state for us */
1137 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1140 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1145 switch(subframe->entropy_coding_method.type) {
1146 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1147 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1154 /* decode the subframe */
1155 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1156 FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
1161 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1163 FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
1168 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1170 subframe->residual = decoder->guts->residual[channel];
1171 subframe->order = order;
1173 /* read warm-up samples */
1174 for(u = 0; u < order; u++) {
1175 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps - decoder->guts->frame.subframes[channel].wasted_bits, read_callback_, decoder))
1176 return false; /* the read_callback_ sets the state for us */
1177 subframe->warmup[u] = i32;
1180 /* read qlp coeff precision */
1181 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1182 return false; /* the read_callback_ sets the state for us */
1184 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1185 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1188 subframe->qlp_coeff_precision = u32+1;
1190 /* read qlp shift */
1191 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1192 return false; /* the read_callback_ sets the state for us */
1193 subframe->quantization_level = i32;
1195 /* read quantized lp coefficiencts */
1196 for(u = 0; u < order; u++) {
1197 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1198 return false; /* the read_callback_ sets the state for us */
1199 subframe->qlp_coeff[u] = i32;
1202 /* read entropy coding method info */
1203 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1204 return false; /* the read_callback_ sets the state for us */
1205 subframe->entropy_coding_method.type = u32;
1206 switch(subframe->entropy_coding_method.type) {
1207 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1208 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1209 return false; /* the read_callback_ sets the state for us */
1210 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1213 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1218 switch(subframe->entropy_coding_method.type) {
1219 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1220 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1227 /* decode the subframe */
1228 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1229 FLAC__lpc_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->guts->output[channel]+order);
1234 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1236 FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
1237 int32 x, *residual = decoder->guts->residual[channel];
1240 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1242 subframe->data = residual;
1244 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
1245 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1246 return false; /* the read_callback_ sets the state for us */
1250 /* decode the subframe */
1251 memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
1256 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
1258 uint32 rice_parameter;
1260 unsigned partition, sample, u;
1261 const unsigned partitions = 1u << partition_order;
1262 const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
1265 for(partition = 0; partition < partitions; partition++) {
1266 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1267 return false; /* the read_callback_ sets the state for us */
1268 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1269 #ifdef SYMMETRIC_RICE
1270 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1271 return false; /* the read_callback_ sets the state for us */
1273 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1274 return false; /* the read_callback_ sets the state for us */
1276 residual[sample] = i;
1283 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1285 if(decoder->guts->input.consumed_bits != 0) {
1287 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1288 return false; /* the read_callback_ sets the state for us */
1290 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1291 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1297 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1299 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1300 FLAC__StreamDecoderReadStatus status;
1301 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1302 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1303 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1304 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1305 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1306 return status == FLAC__STREAM_DECODER_READ_CONTINUE;