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_info, has_seek_table;
43 FLAC__StreamMetaData stream_info;
44 FLAC__StreamMetaData seek_table;
46 byte header_warmup[2]; /* contains the sync code and reserved bits */
47 byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
48 bool cached; /* true if there is a byte in lookahead */
49 } FLAC__StreamDecoderPrivate;
51 static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
53 static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
54 static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
55 static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
56 static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
57 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
58 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
59 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
60 static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
61 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
62 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
63 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
64 static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
65 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
66 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
67 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
69 const char *FLAC__StreamDecoderStateString[] = {
70 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
71 "FLAC__STREAM_DECODER_READ_METADATA",
72 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
73 "FLAC__STREAM_DECODER_READ_FRAME",
74 "FLAC__STREAM_DECODER_END_OF_STREAM",
75 "FLAC__STREAM_DECODER_ABORTED",
76 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
77 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
78 "FLAC__STREAM_DECODER_UNINITIALIZED"
81 const char *FLAC__StreamDecoderReadStatusString[] = {
82 "FLAC__STREAM_DECODER_READ_CONTINUE",
83 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
84 "FLAC__STREAM_DECODER_READ_ABORT"
87 const char *FLAC__StreamDecoderWriteStatusString[] = {
88 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
89 "FLAC__STREAM_DECODER_WRITE_ABORT"
92 const char *FLAC__StreamDecoderErrorStatusString[] = {
93 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
94 "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
95 "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
98 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
100 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
102 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
108 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
113 FLAC__StreamDecoderState FLAC__stream_decoder_init(
114 FLAC__StreamDecoder *decoder,
115 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
116 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
117 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
118 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
124 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
125 assert(decoder != 0);
126 assert(read_callback != 0);
127 assert(write_callback != 0);
128 assert(metadata_callback != 0);
129 assert(error_callback != 0);
130 assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
131 assert(decoder->guts == 0);
133 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
135 decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
136 if(decoder->guts == 0)
137 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
139 decoder->guts->read_callback = read_callback;
140 decoder->guts->write_callback = write_callback;
141 decoder->guts->metadata_callback = metadata_callback;
142 decoder->guts->error_callback = error_callback;
143 decoder->guts->client_data = client_data;
145 FLAC__bitbuffer_init(&decoder->guts->input);
147 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
148 decoder->guts->output[i] = 0;
149 decoder->guts->residual[i] = 0;
152 decoder->guts->output_capacity = 0;
153 decoder->guts->output_channels = 0;
154 decoder->guts->last_frame_number = 0;
155 decoder->guts->samples_decoded = 0;
156 decoder->guts->has_stream_info = false;
157 decoder->guts->has_seek_table = false;
158 decoder->guts->cached = false;
160 return decoder->state;
163 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
166 assert(decoder != 0);
167 if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
169 if(decoder->guts != 0) {
170 if(decoder->guts->has_seek_table) {
171 free(decoder->guts->seek_table.data.seek_table.points);
172 decoder->guts->seek_table.data.seek_table.points = 0;
174 FLAC__bitbuffer_free(&decoder->guts->input);
175 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
176 if(decoder->guts->output[i] != 0) {
177 free(decoder->guts->output[i]);
178 decoder->guts->output[i] = 0;
180 if(decoder->guts->residual[i] != 0) {
181 free(decoder->guts->residual[i]);
182 decoder->guts->residual[i] = 0;
188 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
191 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
193 assert(decoder != 0);
195 if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
196 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
203 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
205 assert(decoder != 0);
207 if(!FLAC__stream_decoder_flush(decoder)) {
208 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
211 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
216 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
219 assert(decoder != 0);
221 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
224 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
226 if(!FLAC__stream_decoder_reset(decoder)) {
227 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
232 switch(decoder->state) {
233 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
234 if(!stream_decoder_find_metadata_(decoder))
235 return false; /* above function sets the status for us */
237 case FLAC__STREAM_DECODER_READ_METADATA:
238 if(!stream_decoder_read_metadata_(decoder))
239 return false; /* above function sets the status for us */
241 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
242 if(!stream_decoder_frame_sync_(decoder))
243 return true; /* above function sets the status for us */
245 case FLAC__STREAM_DECODER_READ_FRAME:
246 if(!stream_decoder_read_frame_(decoder, &dummy))
247 return false; /* above function sets the status for us */
249 case FLAC__STREAM_DECODER_END_OF_STREAM:
257 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
259 assert(decoder != 0);
261 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
264 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
266 if(!FLAC__stream_decoder_reset(decoder)) {
267 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
272 switch(decoder->state) {
273 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
274 if(!stream_decoder_find_metadata_(decoder))
275 return false; /* above function sets the status for us */
277 case FLAC__STREAM_DECODER_READ_METADATA:
278 if(!stream_decoder_read_metadata_(decoder))
279 return false; /* above function sets the status for us */
281 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
284 case FLAC__STREAM_DECODER_END_OF_STREAM:
292 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
295 assert(decoder != 0);
297 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
300 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
303 switch(decoder->state) {
304 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
305 if(!stream_decoder_frame_sync_(decoder))
306 return true; /* above function sets the status for us */
308 case FLAC__STREAM_DECODER_READ_FRAME:
309 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
310 return false; /* above function sets the status for us */
312 return true; /* above function sets the status for us */
314 case FLAC__STREAM_DECODER_END_OF_STREAM:
322 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
325 assert(decoder != 0);
327 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
330 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
333 switch(decoder->state) {
334 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
335 if(!stream_decoder_frame_sync_(decoder))
336 return true; /* above function sets the status for us */
338 case FLAC__STREAM_DECODER_READ_FRAME:
339 if(!stream_decoder_read_frame_(decoder, &dummy))
340 return false; /* above function sets the status for us */
342 case FLAC__STREAM_DECODER_END_OF_STREAM:
350 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
352 assert(decoder != 0);
353 return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
356 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
361 if(size <= decoder->guts->output_capacity && channels <= decoder->guts->output_channels)
364 /* @@@ should change to use realloc() */
366 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
367 if(decoder->guts->output[i] != 0) {
368 free(decoder->guts->output[i]);
369 decoder->guts->output[i] = 0;
371 if(decoder->guts->residual[i] != 0) {
372 free(decoder->guts->residual[i]);
373 decoder->guts->residual[i] = 0;
377 for(i = 0; i < channels; i++) {
378 tmp = (int32*)malloc(sizeof(int32)*size);
380 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
383 decoder->guts->output[i] = tmp;
385 tmp = (int32*)malloc(sizeof(int32)*size);
387 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
390 decoder->guts->residual[i] = tmp;
393 decoder->guts->output_capacity = size;
394 decoder->guts->output_channels = channels;
399 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
405 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
407 for(i = id = 0; i < 4; ) {
408 if(decoder->guts->cached) {
409 x = (uint32)decoder->guts->lookahead;
410 decoder->guts->cached = false;
413 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
414 return false; /* the read_callback_ sets the state for us */
416 if(x == FLAC__STREAM_SYNC_STRING[i]) {
422 if(x == ID3V2_TAG_[id]) {
426 if(!stream_decoder_skip_id3v2_tag_(decoder))
427 return false; /* the read_callback_ sets the state for us */
431 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
432 decoder->guts->header_warmup[0] = (byte)x;
433 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
434 return false; /* the read_callback_ sets the state for us */
436 /* 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 */
437 /* else we have to check if the second byte is the end of a sync code */
438 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
439 decoder->guts->lookahead = (byte)x;
440 decoder->guts->cached = true;
442 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
443 decoder->guts->header_warmup[1] = (byte)x;
444 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
450 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
455 decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
459 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
461 uint32 i, x, last_block, type, length;
464 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
466 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
467 return false; /* the read_callback_ sets the state for us */
468 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
469 return false; /* the read_callback_ sets the state for us */
470 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
471 return false; /* the read_callback_ sets the state for us */
472 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
473 unsigned used_bits = 0;
474 decoder->guts->stream_info.type = type;
475 decoder->guts->stream_info.is_last = last_block;
476 decoder->guts->stream_info.length = length;
478 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
479 return false; /* the read_callback_ sets the state for us */
480 decoder->guts->stream_info.data.stream_info.min_blocksize = x;
481 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
483 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
484 return false; /* the read_callback_ sets the state for us */
485 decoder->guts->stream_info.data.stream_info.max_blocksize = x;
486 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
488 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
489 return false; /* the read_callback_ sets the state for us */
490 decoder->guts->stream_info.data.stream_info.min_framesize = x;
491 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
493 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
494 return false; /* the read_callback_ sets the state for us */
495 decoder->guts->stream_info.data.stream_info.max_framesize = x;
496 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
498 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
499 return false; /* the read_callback_ sets the state for us */
500 decoder->guts->stream_info.data.stream_info.sample_rate = x;
501 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
503 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
504 return false; /* the read_callback_ sets the state for us */
505 decoder->guts->stream_info.data.stream_info.channels = x+1;
506 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
508 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
509 return false; /* the read_callback_ sets the state for us */
510 decoder->guts->stream_info.data.stream_info.bits_per_sample = x+1;
511 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
513 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
514 return false; /* the read_callback_ sets the state for us */
515 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
517 for(i = 0; i < 16; i++) {
518 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
519 return false; /* the read_callback_ sets the state for us */
520 decoder->guts->stream_info.data.stream_info.md5sum[i] = (byte)x;
524 /* skip the rest of the block */
525 assert(used_bits % 8 == 0);
526 length -= (used_bits / 8);
527 for(i = 0; i < length; i++) {
528 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
529 return false; /* the read_callback_ sets the state for us */
532 decoder->guts->has_stream_info = true;
533 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_info, decoder->guts->client_data);
535 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
536 unsigned real_points;
538 decoder->guts->seek_table.type = type;
539 decoder->guts->seek_table.is_last = last_block;
540 decoder->guts->seek_table.length = length;
542 decoder->guts->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
544 if(0 == (decoder->guts->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->guts->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
545 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
548 for(i = real_points = 0; i < decoder->guts->seek_table.data.seek_table.num_points; i++) {
549 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
550 return false; /* the read_callback_ sets the state for us */
551 decoder->guts->seek_table.data.seek_table.points[real_points].sample_number = xx;
553 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
554 return false; /* the read_callback_ sets the state for us */
555 decoder->guts->seek_table.data.seek_table.points[real_points].stream_offset = xx;
557 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
558 return false; /* the read_callback_ sets the state for us */
559 decoder->guts->seek_table.data.seek_table.points[real_points].frame_samples = x;
561 if(decoder->guts->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
564 decoder->guts->seek_table.data.seek_table.num_points = real_points;
566 decoder->guts->has_seek_table = true;
567 decoder->guts->metadata_callback(decoder, &decoder->guts->seek_table, decoder->guts->client_data);
570 /* skip other metadata blocks */
571 for(i = 0; i < length; i++) {
572 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
573 return false; /* the read_callback_ sets the state for us */
578 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
583 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
588 /* skip the version and flags bytes */
589 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
590 return false; /* the read_callback_ sets the state for us */
591 /* get the size (in bytes) to skip */
593 for(i = 0; i < 4; i++) {
594 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
595 return false; /* the read_callback_ sets the state for us */
599 /* skip the rest of the tag */
600 for(i = 0; i < skip; i++) {
601 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
602 return false; /* the read_callback_ sets the state for us */
607 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
612 /* If we know the total number of samples in the stream, stop if we've read that many. */
613 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
614 if(decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.total_samples) {
615 if(decoder->guts->samples_decoded >= decoder->guts->stream_info.data.stream_info.total_samples) {
616 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
621 /* make sure we're byte aligned */
622 if(decoder->guts->input.consumed_bits != 0) {
623 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
624 return false; /* the read_callback_ sets the state for us */
628 if(decoder->guts->cached) {
629 x = (uint32)decoder->guts->lookahead;
630 decoder->guts->cached = false;
633 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
634 return false; /* the read_callback_ sets the state for us */
636 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
637 decoder->guts->header_warmup[0] = (byte)x;
638 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
639 return false; /* the read_callback_ sets the state for us */
641 /* 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 */
642 /* else we have to check if the second byte is the end of a sync code */
643 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
644 decoder->guts->lookahead = (byte)x;
645 decoder->guts->cached = true;
647 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
648 decoder->guts->header_warmup[1] = (byte)x;
649 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
654 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
662 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
666 int32 mid, side, left, right;
667 uint16 frame_crc; /* the one we calculate from the input stream */
670 *got_a_frame = false;
674 FLAC__CRC16_UPDATE(decoder->guts->header_warmup[0], frame_crc);
675 FLAC__CRC16_UPDATE(decoder->guts->header_warmup[1], frame_crc);
676 FLAC__bitbuffer_init_read_crc16(&decoder->guts->input, frame_crc);
678 if(!stream_decoder_read_frame_header_(decoder))
680 if(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
682 if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize, decoder->guts->frame.header.channels))
684 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
686 * first figure the correct bits-per-sample of the subframe
688 unsigned bps = decoder->guts->frame.header.bits_per_sample;
689 switch(decoder->guts->frame.header.channel_assignment) {
690 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
691 /* no adjustment needed */
693 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
694 assert(decoder->guts->frame.header.channels == 2);
698 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
699 assert(decoder->guts->frame.header.channels == 2);
703 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
704 assert(decoder->guts->frame.header.channels == 2);
714 if(!stream_decoder_read_subframe_(decoder, channel, bps))
716 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
717 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
721 if(!stream_decoder_read_zero_padding_(decoder))
725 * Read the frame CRC-16 from the footer and check
727 frame_crc = decoder->guts->input.read_crc16;
728 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
729 return false; /* the read_callback_ sets the state for us */
730 if(frame_crc == (uint16)x) {
731 /* Undo any special channel coding */
732 switch(decoder->guts->frame.header.channel_assignment) {
733 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
736 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
737 assert(decoder->guts->frame.header.channels == 2);
738 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
739 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
741 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
742 assert(decoder->guts->frame.header.channels == 2);
743 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
744 decoder->guts->output[0][i] += decoder->guts->output[1][i];
746 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
747 assert(decoder->guts->frame.header.channels == 2);
748 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
749 mid = decoder->guts->output[0][i];
750 side = decoder->guts->output[1][i];
752 if(side & 1) /* i.e. if 'side' is odd... */
756 decoder->guts->output[0][i] = left >> 1;
757 decoder->guts->output[1][i] = right >> 1;
766 /* Bad frame, emit error and zero the output signal */
767 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->guts->client_data);
768 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
769 memset(decoder->guts->output[channel], 0, sizeof(int32) * decoder->guts->frame.header.blocksize);
775 /* put the latest values into the public section of the decoder instance */
776 decoder->channels = decoder->guts->frame.header.channels;
777 decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
778 decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
779 decoder->sample_rate = decoder->guts->frame.header.sample_rate;
780 decoder->blocksize = decoder->guts->frame.header.blocksize;
782 decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
785 if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
788 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
792 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
796 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
797 byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
798 unsigned raw_header_len;
799 bool is_unparseable = false;
800 const bool is_known_variable_blocksize_stream = (decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.min_blocksize != decoder->guts->stream_info.data.stream_info.max_blocksize);
801 const bool is_known_fixed_blocksize_stream = (decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.min_blocksize == decoder->guts->stream_info.data.stream_info.max_blocksize);
803 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
805 /* init the raw header with the saved bits from synchronization */
806 raw_header[0] = decoder->guts->header_warmup[0];
807 raw_header[1] = decoder->guts->header_warmup[1];
811 * check to make sure that the reserved bits are 0
813 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
814 is_unparseable = true;
818 * Note that along the way as we read the header, we look for a sync
819 * code inside. If we find one it would indicate that our original
820 * sync was bad since there cannot be a sync code in a valid header.
824 * read in the raw header as bytes so we can CRC it, and parse it on the way
826 for(i = 0; i < 2; i++) {
827 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
828 return false; /* the read_callback_ sets the state for us */
829 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
830 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
831 decoder->guts->lookahead = (byte)x;
832 decoder->guts->cached = true;
833 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
834 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
837 raw_header[raw_header_len++] = (byte)x;
840 switch(x = raw_header[2] >> 4) {
842 if(is_known_fixed_blocksize_stream)
843 decoder->guts->frame.header.blocksize = decoder->guts->stream_info.data.stream_info.min_blocksize;
845 is_unparseable = true;
848 decoder->guts->frame.header.blocksize = 192;
854 decoder->guts->frame.header.blocksize = 576 << (x-2);
868 decoder->guts->frame.header.blocksize = 256 << (x-8);
875 switch(x = raw_header[2] & 0x0f) {
877 if(decoder->guts->has_stream_info)
878 decoder->guts->frame.header.sample_rate = decoder->guts->stream_info.data.stream_info.sample_rate;
880 is_unparseable = true;
885 is_unparseable = true;
888 decoder->guts->frame.header.sample_rate = 8000;
891 decoder->guts->frame.header.sample_rate = 16000;
894 decoder->guts->frame.header.sample_rate = 22050;
897 decoder->guts->frame.header.sample_rate = 24000;
900 decoder->guts->frame.header.sample_rate = 32000;
903 decoder->guts->frame.header.sample_rate = 44100;
906 decoder->guts->frame.header.sample_rate = 48000;
909 decoder->guts->frame.header.sample_rate = 96000;
914 sample_rate_hint = x;
917 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
918 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
924 x = (unsigned)(raw_header[3] >> 4);
926 decoder->guts->frame.header.channels = 2;
929 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
932 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
935 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
938 is_unparseable = true;
943 decoder->guts->frame.header.channels = (unsigned)x + 1;
944 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
947 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
949 if(decoder->guts->has_stream_info)
950 decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_info.data.stream_info.bits_per_sample;
952 is_unparseable = true;
955 decoder->guts->frame.header.bits_per_sample = 8;
958 decoder->guts->frame.header.bits_per_sample = 12;
961 decoder->guts->frame.header.bits_per_sample = 16;
964 decoder->guts->frame.header.bits_per_sample = 20;
967 decoder->guts->frame.header.bits_per_sample = 24;
971 is_unparseable = true;
978 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
979 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
980 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
984 if(blocksize_hint && is_known_variable_blocksize_stream) {
985 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
986 return false; /* the read_callback_ sets the state for us */
987 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
988 decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
989 decoder->guts->cached = true;
990 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
991 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
994 decoder->guts->frame.header.number.sample_number = xx;
997 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
998 return false; /* the read_callback_ sets the state for us */
999 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1000 decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1001 decoder->guts->cached = true;
1002 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
1003 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1006 decoder->guts->last_frame_number = x;
1007 if(decoder->guts->has_stream_info) {
1008 decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_info.data.stream_info.min_blocksize * (int64)x;
1011 is_unparseable = true;
1015 if(blocksize_hint) {
1016 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
1017 return false; /* the read_callback_ sets the state for us */
1018 raw_header[raw_header_len++] = (byte)x;
1019 if(blocksize_hint == 7) {
1021 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
1022 return false; /* the read_callback_ sets the state for us */
1023 raw_header[raw_header_len++] = (byte)_x;
1026 decoder->guts->frame.header.blocksize = x+1;
1029 if(sample_rate_hint) {
1030 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
1031 return false; /* the read_callback_ sets the state for us */
1032 raw_header[raw_header_len++] = (byte)x;
1033 if(sample_rate_hint != 12) {
1035 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
1036 return false; /* the read_callback_ sets the state for us */
1037 raw_header[raw_header_len++] = (byte)_x;
1040 if(sample_rate_hint == 12)
1041 decoder->guts->frame.header.sample_rate = x*1000;
1042 else if(sample_rate_hint == 13)
1043 decoder->guts->frame.header.sample_rate = x;
1045 decoder->guts->frame.header.sample_rate = x*10;
1048 /* read the CRC-8 byte */
1049 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
1050 return false; /* the read_callback_ sets the state for us */
1053 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1054 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
1055 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1059 if(is_unparseable) {
1060 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1067 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1072 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1073 return false; /* the read_callback_ sets the state for us */
1075 wasted_bits = (x & 1);
1080 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->guts->input, &u, read_callback_, decoder))
1081 return false; /* the read_callback_ sets the state for us */
1082 decoder->guts->frame.subframes[channel].wasted_bits = u+1;
1083 bps -= decoder->guts->frame.subframes[channel].wasted_bits;
1086 decoder->guts->frame.subframes[channel].wasted_bits = 0;
1089 * Lots of magic numbers here
1092 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1093 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1097 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1101 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1105 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1109 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1113 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1117 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1123 x = decoder->guts->frame.subframes[channel].wasted_bits;
1124 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1125 decoder->guts->output[channel][i] <<= x;
1131 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1133 FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
1136 int32 *output = decoder->guts->output[channel];
1138 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1140 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1141 return false; /* the read_callback_ sets the state for us */
1143 subframe->value = x;
1145 /* decode the subframe */
1146 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1152 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1154 FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
1159 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1161 subframe->residual = decoder->guts->residual[channel];
1162 subframe->order = order;
1164 /* read warm-up samples */
1165 for(u = 0; u < order; u++) {
1166 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1167 return false; /* the read_callback_ sets the state for us */
1168 subframe->warmup[u] = i32;
1171 /* read entropy coding method info */
1172 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1173 return false; /* the read_callback_ sets the state for us */
1174 subframe->entropy_coding_method.type = u32;
1175 switch(subframe->entropy_coding_method.type) {
1176 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1177 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1178 return false; /* the read_callback_ sets the state for us */
1179 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1182 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1187 switch(subframe->entropy_coding_method.type) {
1188 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1189 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1196 /* decode the subframe */
1197 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1198 FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
1203 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1205 FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
1210 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1212 subframe->residual = decoder->guts->residual[channel];
1213 subframe->order = order;
1215 /* read warm-up samples */
1216 for(u = 0; u < order; u++) {
1217 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1218 return false; /* the read_callback_ sets the state for us */
1219 subframe->warmup[u] = i32;
1222 /* read qlp coeff precision */
1223 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1224 return false; /* the read_callback_ sets the state for us */
1225 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1226 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1227 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1230 subframe->qlp_coeff_precision = u32+1;
1232 /* read qlp shift */
1233 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1234 return false; /* the read_callback_ sets the state for us */
1235 subframe->quantization_level = i32;
1237 /* read quantized lp coefficiencts */
1238 for(u = 0; u < order; u++) {
1239 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1240 return false; /* the read_callback_ sets the state for us */
1241 subframe->qlp_coeff[u] = i32;
1244 /* read entropy coding method info */
1245 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1246 return false; /* the read_callback_ sets the state for us */
1247 subframe->entropy_coding_method.type = u32;
1248 switch(subframe->entropy_coding_method.type) {
1249 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1250 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1251 return false; /* the read_callback_ sets the state for us */
1252 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1255 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1260 switch(subframe->entropy_coding_method.type) {
1261 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1262 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1269 /* decode the subframe */
1270 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1271 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);
1276 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1278 FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
1279 int32 x, *residual = decoder->guts->residual[channel];
1282 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1284 subframe->data = residual;
1286 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
1287 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1288 return false; /* the read_callback_ sets the state for us */
1292 /* decode the subframe */
1293 memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
1298 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
1300 uint32 rice_parameter;
1302 unsigned partition, sample, u;
1303 const unsigned partitions = 1u << partition_order;
1304 const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
1307 for(partition = 0; partition < partitions; partition++) {
1308 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1309 return false; /* the read_callback_ sets the state for us */
1310 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1311 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1312 #ifdef SYMMETRIC_RICE
1313 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1314 return false; /* the read_callback_ sets the state for us */
1316 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1317 return false; /* the read_callback_ sets the state for us */
1319 residual[sample] = i;
1323 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1324 return false; /* the read_callback_ sets the state for us */
1325 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1326 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1327 return false; /* the read_callback_ sets the state for us */
1328 residual[sample] = i;
1336 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1338 if(decoder->guts->input.consumed_bits != 0) {
1340 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1341 return false; /* the read_callback_ sets the state for us */
1343 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1344 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1350 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1352 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1353 FLAC__StreamDecoderReadStatus status;
1354 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1355 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1356 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1357 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1358 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1359 return status == FLAC__STREAM_DECODER_READ_CONTINUE;