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 <string.h> /* for memset/memcpy() */
24 #include "FLAC/stream_decoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/crc.h"
27 #include "private/fixed.h"
28 #include "private/lpc.h"
30 typedef struct FLAC__StreamDecoderPrivate {
31 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
32 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
33 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
34 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
36 FLAC__BitBuffer input;
37 int32 *output[FLAC__MAX_CHANNELS];
38 int32 *residual[FLAC__MAX_CHANNELS];
39 unsigned output_capacity, output_channels;
40 uint32 last_frame_number;
41 uint64 samples_decoded;
42 bool has_stream_header;
43 FLAC__StreamMetaData stream_header;
45 byte header_warmup[2]; /* contains the sync code and reserved bits */
46 byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
47 bool cached; /* true if there is a byte in lookahead */
48 } FLAC__StreamDecoderPrivate;
50 static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
52 static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
53 static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
54 static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
55 static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
56 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
57 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
58 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
59 static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
60 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
61 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
62 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
63 static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
64 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
65 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
66 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
68 const char *FLAC__StreamDecoderStateString[] = {
69 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
70 "FLAC__STREAM_DECODER_READ_METADATA",
71 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
72 "FLAC__STREAM_DECODER_READ_FRAME",
73 "FLAC__STREAM_DECODER_END_OF_STREAM",
74 "FLAC__STREAM_DECODER_ABORTED",
75 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
76 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
77 "FLAC__STREAM_DECODER_UNINITIALIZED"
80 const char *FLAC__StreamDecoderReadStatusString[] = {
81 "FLAC__STREAM_DECODER_READ_CONTINUE",
82 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
83 "FLAC__STREAM_DECODER_READ_ABORT"
86 const char *FLAC__StreamDecoderWriteStatusString[] = {
87 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
88 "FLAC__STREAM_DECODER_WRITE_ABORT"
91 const char *FLAC__StreamDecoderErrorStatusString[] = {
92 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
93 "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
94 "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
97 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
99 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
101 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
107 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
112 FLAC__StreamDecoderState FLAC__stream_decoder_init(
113 FLAC__StreamDecoder *decoder,
114 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
115 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
116 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
117 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
123 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
124 assert(decoder != 0);
125 assert(read_callback != 0);
126 assert(write_callback != 0);
127 assert(metadata_callback != 0);
128 assert(error_callback != 0);
129 assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
130 assert(decoder->guts == 0);
132 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
134 decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
135 if(decoder->guts == 0)
136 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
138 decoder->guts->read_callback = read_callback;
139 decoder->guts->write_callback = write_callback;
140 decoder->guts->metadata_callback = metadata_callback;
141 decoder->guts->error_callback = error_callback;
142 decoder->guts->client_data = client_data;
144 FLAC__bitbuffer_init(&decoder->guts->input);
146 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
147 decoder->guts->output[i] = 0;
148 decoder->guts->residual[i] = 0;
151 decoder->guts->output_capacity = 0;
152 decoder->guts->output_channels = 0;
153 decoder->guts->last_frame_number = 0;
154 decoder->guts->samples_decoded = 0;
155 decoder->guts->has_stream_header = false;
156 decoder->guts->cached = false;
158 return decoder->state;
161 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
164 assert(decoder != 0);
165 if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
167 if(decoder->guts != 0) {
168 FLAC__bitbuffer_free(&decoder->guts->input);
169 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
170 if(decoder->guts->output[i] != 0) {
171 free(decoder->guts->output[i]);
172 decoder->guts->output[i] = 0;
174 if(decoder->guts->residual[i] != 0) {
175 free(decoder->guts->residual[i]);
176 decoder->guts->residual[i] = 0;
182 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
185 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
187 assert(decoder != 0);
189 if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
190 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
197 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
199 assert(decoder != 0);
201 if(!FLAC__stream_decoder_flush(decoder)) {
202 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
205 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
210 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
213 assert(decoder != 0);
215 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
218 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
220 if(!FLAC__stream_decoder_reset(decoder)) {
221 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
226 switch(decoder->state) {
227 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
228 if(!stream_decoder_find_metadata_(decoder))
229 return false; /* above function sets the status for us */
231 case FLAC__STREAM_DECODER_READ_METADATA:
232 if(!stream_decoder_read_metadata_(decoder))
233 return false; /* above function sets the status for us */
235 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
236 if(!stream_decoder_frame_sync_(decoder))
237 return true; /* above function sets the status for us */
239 case FLAC__STREAM_DECODER_READ_FRAME:
240 if(!stream_decoder_read_frame_(decoder, &dummy))
241 return false; /* above function sets the status for us */
243 case FLAC__STREAM_DECODER_END_OF_STREAM:
251 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
253 assert(decoder != 0);
255 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
258 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
260 if(!FLAC__stream_decoder_reset(decoder)) {
261 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
266 switch(decoder->state) {
267 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
268 if(!stream_decoder_find_metadata_(decoder))
269 return false; /* above function sets the status for us */
271 case FLAC__STREAM_DECODER_READ_METADATA:
272 if(!stream_decoder_read_metadata_(decoder))
273 return false; /* above function sets the status for us */
275 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
278 case FLAC__STREAM_DECODER_END_OF_STREAM:
286 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
289 assert(decoder != 0);
291 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
294 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
297 switch(decoder->state) {
298 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
299 if(!stream_decoder_frame_sync_(decoder))
300 return true; /* above function sets the status for us */
302 case FLAC__STREAM_DECODER_READ_FRAME:
303 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
304 return false; /* above function sets the status for us */
306 return true; /* above function sets the status for us */
308 case FLAC__STREAM_DECODER_END_OF_STREAM:
316 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
319 assert(decoder != 0);
321 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
324 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
327 switch(decoder->state) {
328 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
329 if(!stream_decoder_frame_sync_(decoder))
330 return true; /* above function sets the status for us */
332 case FLAC__STREAM_DECODER_READ_FRAME:
333 if(!stream_decoder_read_frame_(decoder, &dummy))
334 return false; /* above function sets the status for us */
336 case FLAC__STREAM_DECODER_END_OF_STREAM:
344 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
346 assert(decoder != 0);
347 return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
350 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
355 if(size <= decoder->guts->output_capacity && channels <= decoder->guts->output_channels)
358 /* @@@ should change to use realloc() */
360 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
361 if(decoder->guts->output[i] != 0) {
362 free(decoder->guts->output[i]);
363 decoder->guts->output[i] = 0;
365 if(decoder->guts->residual[i] != 0) {
366 free(decoder->guts->residual[i]);
367 decoder->guts->residual[i] = 0;
371 for(i = 0; i < channels; i++) {
372 tmp = (int32*)malloc(sizeof(int32)*size);
374 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
377 decoder->guts->output[i] = tmp;
379 tmp = (int32*)malloc(sizeof(int32)*size);
381 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
384 decoder->guts->residual[i] = tmp;
387 decoder->guts->output_capacity = size;
388 decoder->guts->output_channels = channels;
393 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
399 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
401 for(i = id = 0; i < 4; ) {
402 if(decoder->guts->cached) {
403 x = (uint32)decoder->guts->lookahead;
404 decoder->guts->cached = false;
407 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
408 return false; /* the read_callback_ sets the state for us */
410 if(x == FLAC__STREAM_SYNC_STRING[i]) {
416 if(x == ID3V2_TAG_[id]) {
420 if(!stream_decoder_skip_id3v2_tag_(decoder))
421 return false; /* the read_callback_ sets the state for us */
425 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
426 decoder->guts->header_warmup[0] = (byte)x;
427 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
428 return false; /* the read_callback_ sets the state for us */
430 /* 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 */
431 /* else we have to check if the second byte is the end of a sync code */
432 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
433 decoder->guts->lookahead = (byte)x;
434 decoder->guts->cached = true;
436 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
437 decoder->guts->header_warmup[1] = (byte)x;
438 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
444 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
449 decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
453 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
455 uint32 i, x, last_block, type, length;
457 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
459 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
460 return false; /* the read_callback_ sets the state for us */
461 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
462 return false; /* the read_callback_ sets the state for us */
463 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
464 return false; /* the read_callback_ sets the state for us */
465 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
466 unsigned used_bits = 0;
467 decoder->guts->stream_header.type = type;
468 decoder->guts->stream_header.is_last = last_block;
469 decoder->guts->stream_header.length = length;
471 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
472 return false; /* the read_callback_ sets the state for us */
473 decoder->guts->stream_header.data.stream_info.min_blocksize = x;
474 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
476 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
477 return false; /* the read_callback_ sets the state for us */
478 decoder->guts->stream_header.data.stream_info.max_blocksize = x;
479 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
481 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
482 return false; /* the read_callback_ sets the state for us */
483 decoder->guts->stream_header.data.stream_info.min_framesize = x;
484 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
486 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
487 return false; /* the read_callback_ sets the state for us */
488 decoder->guts->stream_header.data.stream_info.max_framesize = x;
489 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
491 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
492 return false; /* the read_callback_ sets the state for us */
493 decoder->guts->stream_header.data.stream_info.sample_rate = x;
494 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
496 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
497 return false; /* the read_callback_ sets the state for us */
498 decoder->guts->stream_header.data.stream_info.channels = x+1;
499 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
501 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
502 return false; /* the read_callback_ sets the state for us */
503 decoder->guts->stream_header.data.stream_info.bits_per_sample = x+1;
504 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
506 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))
507 return false; /* the read_callback_ sets the state for us */
508 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
510 for(i = 0; i < 16; i++) {
511 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
512 return false; /* the read_callback_ sets the state for us */
513 decoder->guts->stream_header.data.stream_info.md5sum[i] = (byte)x;
517 /* skip the rest of the block */
518 assert(used_bits % 8 == 0);
519 length -= (used_bits / 8);
520 for(i = 0; i < length; i++) {
521 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
522 return false; /* the read_callback_ sets the state for us */
525 decoder->guts->has_stream_header = true;
526 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
529 /* skip other metadata blocks */
530 for(i = 0; i < length; i++) {
531 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
532 return false; /* the read_callback_ sets the state for us */
537 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
542 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
547 /* skip the version and flags bytes */
548 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
549 return false; /* the read_callback_ sets the state for us */
550 /* get the size (in bytes) to skip */
552 for(i = 0; i < 4; i++) {
553 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
554 return false; /* the read_callback_ sets the state for us */
558 /* skip the rest of the tag */
559 for(i = 0; i < skip; i++) {
560 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
561 return false; /* the read_callback_ sets the state for us */
566 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
571 /* If we know the total number of samples in the stream, stop if we've read that many. */
572 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
573 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.total_samples) {
574 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.stream_info.total_samples) {
575 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
580 /* make sure we're byte aligned */
581 if(decoder->guts->input.consumed_bits != 0) {
582 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
583 return false; /* the read_callback_ sets the state for us */
587 if(decoder->guts->cached) {
588 x = (uint32)decoder->guts->lookahead;
589 decoder->guts->cached = false;
592 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
593 return false; /* the read_callback_ sets the state for us */
595 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
596 decoder->guts->header_warmup[0] = (byte)x;
597 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
598 return false; /* the read_callback_ sets the state for us */
600 /* 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 */
601 /* else we have to check if the second byte is the end of a sync code */
602 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
603 decoder->guts->lookahead = (byte)x;
604 decoder->guts->cached = true;
606 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
607 decoder->guts->header_warmup[1] = (byte)x;
608 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
613 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
621 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
625 int32 mid, side, left, right;
626 uint16 frame_crc; /* the one we calculate from the input stream */
629 *got_a_frame = false;
633 FLAC__CRC16_UPDATE(decoder->guts->header_warmup[0], frame_crc);
634 FLAC__CRC16_UPDATE(decoder->guts->header_warmup[1], frame_crc);
635 FLAC__bitbuffer_init_read_crc16(&decoder->guts->input, frame_crc);
637 if(!stream_decoder_read_frame_header_(decoder))
639 if(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
641 if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize, decoder->guts->frame.header.channels))
643 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
645 * first figure the correct bits-per-sample of the subframe
647 unsigned bps = decoder->guts->frame.header.bits_per_sample;
648 switch(decoder->guts->frame.header.channel_assignment) {
649 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
650 /* no adjustment needed */
652 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
653 assert(decoder->guts->frame.header.channels == 2);
657 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
658 assert(decoder->guts->frame.header.channels == 2);
662 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
663 assert(decoder->guts->frame.header.channels == 2);
673 if(!stream_decoder_read_subframe_(decoder, channel, bps))
675 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
676 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
680 if(!stream_decoder_read_zero_padding_(decoder))
684 * Read the frame CRC-16 from the footer and check
686 frame_crc = decoder->guts->input.read_crc16;
687 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
688 return false; /* the read_callback_ sets the state for us */
689 if(frame_crc == (uint16)x) {
690 /* Undo any special channel coding */
691 switch(decoder->guts->frame.header.channel_assignment) {
692 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
695 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
696 assert(decoder->guts->frame.header.channels == 2);
697 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
698 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
700 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
701 assert(decoder->guts->frame.header.channels == 2);
702 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
703 decoder->guts->output[0][i] += decoder->guts->output[1][i];
705 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
706 assert(decoder->guts->frame.header.channels == 2);
707 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
708 mid = decoder->guts->output[0][i];
709 side = decoder->guts->output[1][i];
711 if(side & 1) /* i.e. if 'side' is odd... */
715 decoder->guts->output[0][i] = left >> 1;
716 decoder->guts->output[1][i] = right >> 1;
725 /* Bad frame, emit error and zero the output signal */
726 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->guts->client_data);
727 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
728 memset(decoder->guts->output[channel], 0, sizeof(int32) * decoder->guts->frame.header.blocksize);
734 /* put the latest values into the public section of the decoder instance */
735 decoder->channels = decoder->guts->frame.header.channels;
736 decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
737 decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
738 decoder->sample_rate = decoder->guts->frame.header.sample_rate;
739 decoder->blocksize = decoder->guts->frame.header.blocksize;
741 decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
744 if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
747 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
751 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
755 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
756 byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
757 unsigned raw_header_len;
758 bool is_unparseable = false;
760 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
762 /* init the raw header with the saved bits from synchronization */
763 raw_header[0] = decoder->guts->header_warmup[0];
764 raw_header[1] = decoder->guts->header_warmup[1];
768 * check to make sure that the reserved bits are 0
770 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
771 is_unparseable = true;
775 * Note that along the way as we read the header, we look for a sync
776 * code inside. If we find one it would indicate that our original
777 * sync was bad since there cannot be a sync code in a valid header.
781 * read in the raw header as bytes so we can CRC it, and parse it on the way
783 for(i = 0; i < 2; i++) {
784 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
785 return false; /* the read_callback_ sets the state for us */
786 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
787 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
788 decoder->guts->lookahead = (byte)x;
789 decoder->guts->cached = true;
790 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
791 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
794 raw_header[raw_header_len++] = (byte)x;
797 switch(x = raw_header[2] >> 4) {
799 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 */
800 decoder->guts->frame.header.blocksize = decoder->guts->stream_header.data.stream_info.min_blocksize;
802 is_unparseable = true;
805 decoder->guts->frame.header.blocksize = 192;
811 decoder->guts->frame.header.blocksize = 576 << (x-2);
825 decoder->guts->frame.header.blocksize = 256 << (x-8);
832 switch(x = raw_header[2] & 0x0f) {
834 if(decoder->guts->has_stream_header)
835 decoder->guts->frame.header.sample_rate = decoder->guts->stream_header.data.stream_info.sample_rate;
837 is_unparseable = true;
842 is_unparseable = true;
845 decoder->guts->frame.header.sample_rate = 8000;
848 decoder->guts->frame.header.sample_rate = 16000;
851 decoder->guts->frame.header.sample_rate = 22050;
854 decoder->guts->frame.header.sample_rate = 24000;
857 decoder->guts->frame.header.sample_rate = 32000;
860 decoder->guts->frame.header.sample_rate = 44100;
863 decoder->guts->frame.header.sample_rate = 48000;
866 decoder->guts->frame.header.sample_rate = 96000;
871 sample_rate_hint = x;
874 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
875 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
881 x = (unsigned)(raw_header[3] >> 4);
883 decoder->guts->frame.header.channels = 2;
886 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
889 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
892 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
895 is_unparseable = true;
900 decoder->guts->frame.header.channels = (unsigned)x + 1;
901 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
904 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
906 if(decoder->guts->has_stream_header)
907 decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_header.data.stream_info.bits_per_sample;
909 is_unparseable = true;
912 decoder->guts->frame.header.bits_per_sample = 8;
915 decoder->guts->frame.header.bits_per_sample = 12;
918 decoder->guts->frame.header.bits_per_sample = 16;
921 decoder->guts->frame.header.bits_per_sample = 20;
924 decoder->guts->frame.header.bits_per_sample = 24;
928 is_unparseable = true;
935 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
936 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
937 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
942 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
943 return false; /* the read_callback_ sets the state for us */
944 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
945 decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
946 decoder->guts->cached = true;
947 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
948 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
951 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 */
952 decoder->guts->frame.header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.stream_info.min_blocksize + xx;
954 decoder->guts->frame.header.number.sample_number = xx;
957 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
958 return false; /* the read_callback_ sets the state for us */
959 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
960 decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
961 decoder->guts->cached = true;
962 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
963 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
966 decoder->guts->last_frame_number = x;
967 if(decoder->guts->has_stream_header) {
968 decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_header.data.stream_info.min_blocksize * (int64)x;
971 is_unparseable = true;
976 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
977 return false; /* the read_callback_ sets the state for us */
978 raw_header[raw_header_len++] = (byte)x;
979 if(blocksize_hint == 7) {
981 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
982 return false; /* the read_callback_ sets the state for us */
983 raw_header[raw_header_len++] = (byte)_x;
986 decoder->guts->frame.header.blocksize = x+1;
989 if(sample_rate_hint) {
990 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
991 return false; /* the read_callback_ sets the state for us */
992 raw_header[raw_header_len++] = (byte)x;
993 if(sample_rate_hint != 12) {
995 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
996 return false; /* the read_callback_ sets the state for us */
997 raw_header[raw_header_len++] = (byte)_x;
1000 if(sample_rate_hint == 12)
1001 decoder->guts->frame.header.sample_rate = x*1000;
1002 else if(sample_rate_hint == 13)
1003 decoder->guts->frame.header.sample_rate = x;
1005 decoder->guts->frame.header.sample_rate = x*10;
1008 /* read the CRC-8 byte */
1009 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
1010 return false; /* the read_callback_ sets the state for us */
1013 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1014 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
1015 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1019 if(is_unparseable) {
1020 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1027 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1032 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1033 return false; /* the read_callback_ sets the state for us */
1035 wasted_bits = (x & 1);
1040 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->guts->input, &u, read_callback_, decoder))
1041 return false; /* the read_callback_ sets the state for us */
1042 decoder->guts->frame.subframes[channel].wasted_bits = u+1;
1043 bps -= decoder->guts->frame.subframes[channel].wasted_bits;
1046 decoder->guts->frame.subframes[channel].wasted_bits = 0;
1049 * Lots of magic numbers here
1052 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1053 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1057 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1061 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1065 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1069 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1073 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1077 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1083 x = decoder->guts->frame.subframes[channel].wasted_bits;
1084 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1085 decoder->guts->output[channel][i] <<= x;
1091 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1093 FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
1096 int32 *output = decoder->guts->output[channel];
1098 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1100 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1101 return false; /* the read_callback_ sets the state for us */
1103 subframe->value = x;
1105 /* decode the subframe */
1106 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1112 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1114 FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
1119 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1121 subframe->residual = decoder->guts->residual[channel];
1122 subframe->order = order;
1124 /* read warm-up samples */
1125 for(u = 0; u < order; u++) {
1126 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1127 return false; /* the read_callback_ sets the state for us */
1128 subframe->warmup[u] = i32;
1131 /* read entropy coding method info */
1132 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1133 return false; /* the read_callback_ sets the state for us */
1134 subframe->entropy_coding_method.type = u32;
1135 switch(subframe->entropy_coding_method.type) {
1136 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1137 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1138 return false; /* the read_callback_ sets the state for us */
1139 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1142 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1147 switch(subframe->entropy_coding_method.type) {
1148 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1149 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1156 /* decode the subframe */
1157 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1158 FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
1163 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1165 FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
1170 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1172 subframe->residual = decoder->guts->residual[channel];
1173 subframe->order = order;
1175 /* read warm-up samples */
1176 for(u = 0; u < order; u++) {
1177 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1178 return false; /* the read_callback_ sets the state for us */
1179 subframe->warmup[u] = i32;
1182 /* read qlp coeff precision */
1183 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1184 return false; /* the read_callback_ sets the state for us */
1186 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1187 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1190 subframe->qlp_coeff_precision = u32+1;
1192 /* read qlp shift */
1193 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1194 return false; /* the read_callback_ sets the state for us */
1195 subframe->quantization_level = i32;
1197 /* read quantized lp coefficiencts */
1198 for(u = 0; u < order; u++) {
1199 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1200 return false; /* the read_callback_ sets the state for us */
1201 subframe->qlp_coeff[u] = i32;
1204 /* read entropy coding method info */
1205 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1206 return false; /* the read_callback_ sets the state for us */
1207 subframe->entropy_coding_method.type = u32;
1208 switch(subframe->entropy_coding_method.type) {
1209 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1210 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1211 return false; /* the read_callback_ sets the state for us */
1212 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1215 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1220 switch(subframe->entropy_coding_method.type) {
1221 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1222 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1229 /* decode the subframe */
1230 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1231 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);
1236 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1238 FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
1239 int32 x, *residual = decoder->guts->residual[channel];
1242 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1244 subframe->data = residual;
1246 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
1247 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1248 return false; /* the read_callback_ sets the state for us */
1252 /* decode the subframe */
1253 memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
1258 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
1260 uint32 rice_parameter;
1262 unsigned partition, sample, u;
1263 const unsigned partitions = 1u << partition_order;
1264 const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
1267 for(partition = 0; partition < partitions; partition++) {
1268 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1269 return false; /* the read_callback_ sets the state for us */
1270 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1271 #ifdef SYMMETRIC_RICE
1272 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1273 return false; /* the read_callback_ sets the state for us */
1275 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1276 return false; /* the read_callback_ sets the state for us */
1278 residual[sample] = i;
1285 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1287 if(decoder->guts->input.consumed_bits != 0) {
1289 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1290 return false; /* the read_callback_ sets the state for us */
1292 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1293 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1299 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1301 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1302 FLAC__StreamDecoderReadStatus status;
1303 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1304 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1305 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1306 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1307 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1308 return status == FLAC__STREAM_DECODER_READ_CONTINUE;