1 /* libFLAC - Free Lossless Audio Coder 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__FrameHeader *header, 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];
38 unsigned output_capacity;
39 uint32 last_frame_number;
40 uint64 samples_decoded;
41 bool has_stream_header;
42 FLAC__StreamMetaData stream_header;
43 FLAC__FrameHeader frame_header;
44 } FLAC__StreamDecoderPrivate;
46 static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
48 static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size);
49 static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
50 static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
51 static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
52 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
53 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
54 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
55 static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel);
56 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel);
57 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order);
58 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order);
59 static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel);
60 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order);
61 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
62 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
64 const char *FLAC__StreamDecoderStateString[] = {
65 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
66 "FLAC__STREAM_DECODER_READ_METADATA",
67 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
68 "FLAC__STREAM_DECODER_READ_FRAME",
69 "FLAC__STREAM_DECODER_RESYNC_IN_HEADER",
70 "FLAC__STREAM_DECODER_END_OF_STREAM",
71 "FLAC__STREAM_DECODER_ABORTED",
72 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
73 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
74 "FLAC__STREAM_DECODER_UNINITIALIZED"
77 const char *FLAC__StreamDecoderReadStatusString[] = {
78 "FLAC__STREAM_DECODER_READ_CONTINUE",
79 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
80 "FLAC__STREAM_DECODER_READ_ABORT"
83 const char *FLAC__StreamDecoderWriteStatusString[] = {
84 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
85 "FLAC__STREAM_DECODER_WRITE_ABORT"
88 const char *FLAC__StreamDecoderErrorStatusString[] = {
89 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC"
92 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
94 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
96 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
102 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
107 FLAC__StreamDecoderState FLAC__stream_decoder_init(
108 FLAC__StreamDecoder *decoder,
109 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
110 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
111 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
112 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
118 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
119 assert(decoder != 0);
120 assert(read_callback != 0);
121 assert(write_callback != 0);
122 assert(metadata_callback != 0);
123 assert(error_callback != 0);
124 assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
125 assert(decoder->guts == 0);
127 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
129 decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
130 if(decoder->guts == 0)
131 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
133 decoder->guts->read_callback = read_callback;
134 decoder->guts->write_callback = write_callback;
135 decoder->guts->metadata_callback = metadata_callback;
136 decoder->guts->error_callback = error_callback;
137 decoder->guts->client_data = client_data;
139 FLAC__bitbuffer_init(&decoder->guts->input);
141 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
142 decoder->guts->output[i] = 0;
143 decoder->guts->residual = 0;
145 decoder->guts->output_capacity = 0;
146 decoder->guts->last_frame_number = 0;
147 decoder->guts->samples_decoded = 0;
148 decoder->guts->has_stream_header = false;
150 return decoder->state;
153 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
156 assert(decoder != 0);
157 if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
159 if(decoder->guts != 0) {
160 FLAC__bitbuffer_free(&decoder->guts->input);
161 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
162 if(decoder->guts->output[i] != 0) {
163 free(decoder->guts->output[i]);
164 decoder->guts->output[i] = 0;
167 if(decoder->guts->residual != 0) {
168 free(decoder->guts->residual);
169 decoder->guts->residual = 0;
174 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
177 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
179 assert(decoder != 0);
181 if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
182 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
189 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
191 assert(decoder != 0);
193 if(!FLAC__stream_decoder_flush(decoder)) {
194 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
197 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
202 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
205 assert(decoder != 0);
207 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
210 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
212 if(!FLAC__stream_decoder_reset(decoder)) {
213 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
218 switch(decoder->state) {
219 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
220 if(!stream_decoder_find_metadata_(decoder))
221 return false; /* above function sets the status for us */
223 case FLAC__STREAM_DECODER_READ_METADATA:
224 if(!stream_decoder_read_metadata_(decoder))
225 return false; /* above function sets the status for us */
227 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
228 if(!stream_decoder_frame_sync_(decoder))
229 return true; /* above function sets the status for us */
231 case FLAC__STREAM_DECODER_READ_FRAME:
232 if(!stream_decoder_read_frame_(decoder, &dummy))
233 return false; /* above function sets the status for us */
235 case FLAC__STREAM_DECODER_END_OF_STREAM:
243 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
245 assert(decoder != 0);
247 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
250 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
252 if(!FLAC__stream_decoder_reset(decoder)) {
253 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
258 switch(decoder->state) {
259 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
260 if(!stream_decoder_find_metadata_(decoder))
261 return false; /* above function sets the status for us */
263 case FLAC__STREAM_DECODER_READ_METADATA:
264 if(!stream_decoder_read_metadata_(decoder))
265 return false; /* above function sets the status for us */
267 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
270 case FLAC__STREAM_DECODER_END_OF_STREAM:
278 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
281 assert(decoder != 0);
283 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
286 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
289 switch(decoder->state) {
290 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
291 if(!stream_decoder_frame_sync_(decoder))
292 return true; /* above function sets the status for us */
294 case FLAC__STREAM_DECODER_READ_FRAME:
295 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
296 return false; /* above function sets the status for us */
298 return true; /* above function sets the status for us */
300 case FLAC__STREAM_DECODER_END_OF_STREAM:
308 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
311 assert(decoder != 0);
313 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
316 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
319 switch(decoder->state) {
320 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
321 if(!stream_decoder_frame_sync_(decoder))
322 return true; /* above function sets the status for us */
324 case FLAC__STREAM_DECODER_READ_FRAME:
325 if(!stream_decoder_read_frame_(decoder, &dummy))
326 return false; /* above function sets the status for us */
328 case FLAC__STREAM_DECODER_END_OF_STREAM:
336 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
338 assert(decoder != 0);
339 return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
342 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size)
347 if(size <= decoder->guts->output_capacity)
350 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
351 if(decoder->guts->output[i] != 0) {
352 free(decoder->guts->output[i]);
353 decoder->guts->output[i] = 0;
356 if(decoder->guts->residual != 0) {
357 free(decoder->guts->residual);
358 decoder->guts->residual = 0;
361 for(i = 0; i < decoder->guts->frame_header.channels; i++) {
362 tmp = (int32*)malloc(sizeof(int32)*size);
364 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
367 decoder->guts->output[i] = tmp;
369 tmp = (int32*)malloc(sizeof(int32)*size);
371 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
374 decoder->guts->residual = tmp;
376 decoder->guts->output_capacity = size;
381 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
387 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
389 for(i = id = 0; i < 4; ) {
390 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
391 return false; /* the read_callback_ sets the state for us */
392 if(x == FLAC__STREAM_SYNC_STRING[i]) {
398 if(x == ID3V2_TAG_[id]) {
402 if(!stream_decoder_skip_id3v2_tag_(decoder))
403 return false; /* the read_callback_ sets the state for us */
407 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
409 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
410 return false; /* the read_callback_ sets the state for us */
411 if(!y) { /* MAGIC NUMBER for the last sync bit */
412 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
418 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
423 decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
427 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
429 uint32 i, x, last_block, type, length;
431 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
433 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
434 return false; /* the read_callback_ sets the state for us */
435 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
436 return false; /* the read_callback_ sets the state for us */
437 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
438 return false; /* the read_callback_ sets the state for us */
439 if(type == FLAC__METADATA_TYPE_ENCODING) {
440 unsigned used_bits = 0;
441 decoder->guts->stream_header.type = type;
442 decoder->guts->stream_header.is_last = last_block;
443 decoder->guts->stream_header.length = length;
445 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
446 return false; /* the read_callback_ sets the state for us */
447 decoder->guts->stream_header.data.encoding.min_blocksize = x;
448 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN;
450 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
451 return false; /* the read_callback_ sets the state for us */
452 decoder->guts->stream_header.data.encoding.max_blocksize = x;
453 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN;
455 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
456 return false; /* the read_callback_ sets the state for us */
457 decoder->guts->stream_header.data.encoding.min_framesize = x;
458 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN;
460 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
461 return false; /* the read_callback_ sets the state for us */
462 decoder->guts->stream_header.data.encoding.max_framesize = x;
463 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN;
465 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
466 return false; /* the read_callback_ sets the state for us */
467 decoder->guts->stream_header.data.encoding.sample_rate = x;
468 used_bits += FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN;
470 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
471 return false; /* the read_callback_ sets the state for us */
472 decoder->guts->stream_header.data.encoding.channels = x+1;
473 used_bits += FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN;
475 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
476 return false; /* the read_callback_ sets the state for us */
477 decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
478 used_bits += FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN;
480 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_header.data.encoding.total_samples, FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN, read_callback_, decoder))
481 return false; /* the read_callback_ sets the state for us */
482 used_bits += FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN;
484 for(i = 0; i < 16; i++) {
485 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
486 return false; /* the read_callback_ sets the state for us */
487 decoder->guts->stream_header.data.encoding.md5sum[i] = (byte)x;
491 /* skip the rest of the block */
492 assert(used_bits % 8 == 0);
493 length -= (used_bits / 8);
494 for(i = 0; i < length; i++) {
495 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
496 return false; /* the read_callback_ sets the state for us */
499 decoder->guts->has_stream_header = true;
500 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
503 /* skip other metadata blocks */
504 for(i = 0; i < length; i++) {
505 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
506 return false; /* the read_callback_ sets the state for us */
511 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
516 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
521 /* skip the version and flags bytes */
522 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
523 return false; /* the read_callback_ sets the state for us */
524 /* get the size (in bytes) to skip */
526 for(i = 0; i < 4; i++) {
527 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
528 return false; /* the read_callback_ sets the state for us */
532 /* skip the rest of the tag */
533 for(i = 0; i < skip; i++) {
534 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
535 return false; /* the read_callback_ sets the state for us */
540 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
545 /* If we know the total number of samples in the stream, stop if we've read that many. */
546 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
547 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
548 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
549 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
554 /* make sure we're byte aligned */
555 if(decoder->guts->input.consumed_bits != 0) {
556 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
557 return false; /* the read_callback_ sets the state for us */
561 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
562 return false; /* the read_callback_ sets the state for us */
563 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
565 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
566 return false; /* the read_callback_ sets the state for us */
567 if(!y) { /* MAGIC NUMBER for the last sync bit */
568 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
573 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
581 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
585 int32 mid, side, left, right;
587 *got_a_frame = false;
589 if(!stream_decoder_read_frame_header_(decoder))
591 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
592 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
593 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
595 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
598 if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame_header.blocksize))
600 for(channel = 0; channel < decoder->guts->frame_header.channels; channel++) {
601 if(!stream_decoder_read_subframe_(decoder, channel))
603 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
604 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
608 if(!stream_decoder_read_zero_padding_(decoder))
611 /* Undo any special channel coding */
612 switch(decoder->guts->frame_header.channel_assignment) {
613 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
616 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
617 assert(decoder->guts->frame_header.channels == 2);
618 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
619 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
621 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
622 assert(decoder->guts->frame_header.channels == 2);
623 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
624 decoder->guts->output[0][i] += decoder->guts->output[1][i];
626 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
627 assert(decoder->guts->frame_header.channels == 2);
628 for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
629 mid = decoder->guts->output[0][i];
630 side = decoder->guts->output[1][i];
632 if(side & 1) /* i.e. if 'side' is odd... */
636 decoder->guts->output[0][i] = left >> 1;
637 decoder->guts->output[1][i] = right >> 1;
647 /* put the latest values into the public section of the decoder instance */
648 decoder->channels = decoder->guts->frame_header.channels;
649 decoder->channel_assignment = decoder->guts->frame_header.channel_assignment;
650 decoder->bits_per_sample = decoder->guts->frame_header.bits_per_sample;
651 decoder->sample_rate = decoder->guts->frame_header.sample_rate;
652 decoder->blocksize = decoder->guts->frame_header.blocksize;
654 decoder->guts->samples_decoded += decoder->guts->frame_header.blocksize;
657 if(decoder->guts->write_callback(decoder, &decoder->guts->frame_header, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
660 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
664 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
668 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
669 byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
670 unsigned raw_header_len;
671 bool is_unparseable = false;
673 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
675 /* init the raw header with the first 8 bits of the sync code */
676 raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
680 * read in the raw header as bytes so we can CRC it, and parse it on the way
682 for(i = 0; i < 2; i++) {
683 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
684 return false; /* the read_callback_ sets the state for us */
685 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
686 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
688 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
689 return false; /* the read_callback_ sets the state for us */
690 if(!y) { /* MAGIC NUMBER for the last sync bit */
691 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
695 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
696 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
700 raw_header[raw_header_len++] = (byte)x;
702 assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
704 switch(x = raw_header[1] >> 4) {
706 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.min_blocksize == decoder->guts->stream_header.data.encoding.max_blocksize) /* i.e. it's a fixed-blocksize stream */
707 decoder->guts->frame_header.blocksize = decoder->guts->stream_header.data.encoding.min_blocksize;
709 is_unparseable = true;
712 decoder->guts->frame_header.blocksize = 192;
718 decoder->guts->frame_header.blocksize = 576 << (x-2);
729 switch(x = raw_header[1] & 0x0f) {
731 if(decoder->guts->has_stream_header)
732 decoder->guts->frame_header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
734 is_unparseable = true;
739 is_unparseable = true;
742 decoder->guts->frame_header.sample_rate = 8000;
745 decoder->guts->frame_header.sample_rate = 16000;
748 decoder->guts->frame_header.sample_rate = 22050;
751 decoder->guts->frame_header.sample_rate = 24000;
754 decoder->guts->frame_header.sample_rate = 32000;
757 decoder->guts->frame_header.sample_rate = 44100;
760 decoder->guts->frame_header.sample_rate = 48000;
763 decoder->guts->frame_header.sample_rate = 96000;
768 sample_rate_hint = x;
771 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
772 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
778 x = (unsigned)(raw_header[2] >> 4);
780 decoder->guts->frame_header.channels = 2;
783 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
786 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
789 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
792 is_unparseable = true;
797 decoder->guts->frame_header.channels = (unsigned)x + 1;
798 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
801 switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
803 if(decoder->guts->has_stream_header)
804 decoder->guts->frame_header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
806 is_unparseable = true;
809 decoder->guts->frame_header.bits_per_sample = 8;
812 decoder->guts->frame_header.bits_per_sample = 12;
815 decoder->guts->frame_header.bits_per_sample = 16;
818 decoder->guts->frame_header.bits_per_sample = 20;
821 decoder->guts->frame_header.bits_per_sample = 24;
825 is_unparseable = true;
832 if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
833 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
834 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
839 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
840 return false; /* the read_callback_ sets the state for us */
841 if(xx == 0xffffffffffffffff) {
842 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
844 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
845 return false; /* the read_callback_ sets the state for us */
846 if(!y) { /* MAGIC NUMBER for the last sync bit */
847 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
851 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
852 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
857 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
858 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
862 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.min_blocksize == decoder->guts->stream_header.data.encoding.max_blocksize) /* i.e. it's a fixed-blocksize stream */
863 decoder->guts->frame_header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.encoding.min_blocksize + xx;
865 decoder->guts->frame_header.number.sample_number = xx;
868 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
869 return false; /* the read_callback_ sets the state for us */
870 if(x == 0xffffffff) {
871 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
873 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
874 return false; /* the read_callback_ sets the state for us */
875 if(!y) { /* MAGIC NUMBER for the last sync bit */
876 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
880 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
881 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
886 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
887 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
891 decoder->guts->last_frame_number = x;
892 if(decoder->guts->has_stream_header) {
893 decoder->guts->frame_header.number.sample_number = (int64)decoder->guts->stream_header.data.encoding.min_blocksize * (int64)x;
896 is_unparseable = true;
901 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
902 return false; /* the read_callback_ sets the state for us */
903 raw_header[raw_header_len++] = (byte)x;
904 if(blocksize_hint == 7) {
906 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
907 return false; /* the read_callback_ sets the state for us */
908 raw_header[raw_header_len++] = (byte)_x;
911 decoder->guts->frame_header.blocksize = x+1;
914 if(sample_rate_hint) {
915 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
916 return false; /* the read_callback_ sets the state for us */
917 raw_header[raw_header_len++] = (byte)x;
918 if(sample_rate_hint != 12) {
920 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
921 return false; /* the read_callback_ sets the state for us */
922 raw_header[raw_header_len++] = (byte)_x;
925 if(sample_rate_hint == 12)
926 decoder->guts->frame_header.sample_rate = x*1000;
927 else if(sample_rate_hint == 13)
928 decoder->guts->frame_header.sample_rate = x;
930 decoder->guts->frame_header.sample_rate = x*10;
933 /* read the crc byte */
934 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
935 return false; /* the read_callback_ sets the state for us */
938 if(FLAC__crc8(raw_header, raw_header_len) != crc) {
939 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
940 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
945 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
952 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel)
956 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_HEADER_TYPE_LEN, read_callback_, decoder))
957 return false; /* the read_callback_ sets the state for us */
958 if(x & 0x01 || x & 0x80) {
959 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
960 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
964 return stream_decoder_read_subframe_constant_(decoder, channel);
967 return stream_decoder_read_subframe_verbatim_(decoder, channel);
970 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
974 return stream_decoder_read_subframe_fixed_(decoder, channel, (x>>1)&7);
977 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
981 return stream_decoder_read_subframe_lpc_(decoder, channel, ((x>>1)&31)+1);
985 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel)
990 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
991 return false; /* the read_callback_ sets the state for us */
993 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
994 decoder->guts->output[channel][i] = x;
999 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
1001 FLAC__SubframeHeader_Fixed subframe_header;
1006 subframe_header.order = order;
1008 /* read warm-up samples */
1009 for(u = 0; u < order; u++) {
1010 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1011 return false; /* the read_callback_ sets the state for us */
1012 decoder->guts->output[channel][u] = i32;
1015 /* read entropy coding method info */
1016 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1017 return false; /* the read_callback_ sets the state for us */
1018 subframe_header.entropy_coding_method.type = u32;
1019 switch(subframe_header.entropy_coding_method.type) {
1020 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1021 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1022 return false; /* the read_callback_ sets the state for us */
1023 subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1026 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1031 switch(subframe_header.entropy_coding_method.type) {
1032 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1033 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1040 /* decode the subframe */
1041 FLAC__fixed_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, order, decoder->guts->output[channel]+order);
1046 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
1048 FLAC__SubframeHeader_LPC subframe_header;
1053 subframe_header.order = order;
1055 /* read warm-up samples */
1056 for(u = 0; u < order; u++) {
1057 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1058 return false; /* the read_callback_ sets the state for us */
1059 decoder->guts->output[channel][u] = i32;
1062 /* read qlp coeff precision */
1063 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1064 return false; /* the read_callback_ sets the state for us */
1066 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1067 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1070 subframe_header.qlp_coeff_precision = u32+1;
1072 /* read qlp shift */
1073 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1074 return false; /* the read_callback_ sets the state for us */
1075 subframe_header.quantization_level = i32;
1077 /* read quantized lp coefficiencts */
1078 for(u = 0; u < order; u++) {
1079 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe_header.qlp_coeff_precision, read_callback_, decoder))
1080 return false; /* the read_callback_ sets the state for us */
1081 subframe_header.qlp_coeff[u] = i32;
1084 /* read entropy coding method info */
1085 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1086 return false; /* the read_callback_ sets the state for us */
1087 subframe_header.entropy_coding_method.type = u32;
1088 switch(subframe_header.entropy_coding_method.type) {
1089 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1090 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1091 return false; /* the read_callback_ sets the state for us */
1092 subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1095 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1100 switch(subframe_header.entropy_coding_method.type) {
1101 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1102 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1109 /* decode the subframe */
1110 FLAC__lpc_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, subframe_header.qlp_coeff, order, subframe_header.quantization_level, decoder->guts->output[channel]+order);
1115 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel)
1120 for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
1121 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1122 return false; /* the read_callback_ sets the state for us */
1123 decoder->guts->output[channel][i] = x;
1129 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order)
1131 uint32 rice_parameter;
1133 unsigned partition, sample, u;
1134 const unsigned partitions = 1u << partition_order;
1135 const unsigned partition_samples = partition_order > 0? decoder->guts->frame_header.blocksize >> partition_order : decoder->guts->frame_header.blocksize - predictor_order;
1138 for(partition = 0; partition < partitions; partition++) {
1139 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1140 return false; /* the read_callback_ sets the state for us */
1141 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1142 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1143 return false; /* the read_callback_ sets the state for us */
1144 decoder->guts->residual[sample] = i;
1151 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1153 if(decoder->guts->input.consumed_bits != 0) {
1155 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1156 return false; /* the read_callback_ sets the state for us */
1158 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1159 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1165 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1167 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1168 FLAC__StreamDecoderReadStatus status;
1169 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1170 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1171 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1172 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1173 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1174 return status == FLAC__STREAM_DECODER_READ_CONTINUE;