1 /* libFLAC - Free Lossless Audio Coder library
2 * Copyright (C) 2000 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 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
66 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
68 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
74 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
79 FLAC__StreamDecoderState FLAC__stream_decoder_init(
80 FLAC__StreamDecoder *decoder,
81 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
82 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
83 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
84 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
90 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
92 assert(read_callback != 0);
93 assert(write_callback != 0);
94 assert(metadata_callback != 0);
95 assert(error_callback != 0);
96 assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
97 assert(decoder->guts == 0);
99 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
101 decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
102 if(decoder->guts == 0)
103 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
105 decoder->guts->read_callback = read_callback;
106 decoder->guts->write_callback = write_callback;
107 decoder->guts->metadata_callback = metadata_callback;
108 decoder->guts->error_callback = error_callback;
109 decoder->guts->client_data = client_data;
111 FLAC__bitbuffer_init(&decoder->guts->input);
113 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
114 decoder->guts->output[i] = 0;
115 decoder->guts->residual = 0;
117 decoder->guts->output_capacity = 0;
118 decoder->guts->last_frame_number = 0;
119 decoder->guts->samples_decoded = 0;
120 decoder->guts->has_stream_header = false;
122 return decoder->state;
125 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
128 assert(decoder != 0);
129 if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
131 if(decoder->guts != 0) {
132 FLAC__bitbuffer_free(&decoder->guts->input);
133 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
134 if(decoder->guts->output[i] != 0) {
135 free(decoder->guts->output[i]);
136 decoder->guts->output[i] = 0;
139 if(decoder->guts->residual != 0) {
140 free(decoder->guts->residual);
141 decoder->guts->residual = 0;
146 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
149 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
151 assert(decoder != 0);
153 if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
154 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
161 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
163 assert(decoder != 0);
165 if(!FLAC__stream_decoder_flush(decoder)) {
166 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
169 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
174 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
177 assert(decoder != 0);
179 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
182 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
184 if(!FLAC__stream_decoder_reset(decoder)) {
185 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
190 switch(decoder->state) {
191 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
192 if(!stream_decoder_find_metadata_(decoder))
193 return false; /* above function sets the status for us */
195 case FLAC__STREAM_DECODER_READ_METADATA:
196 if(!stream_decoder_read_metadata_(decoder))
197 return false; /* above function sets the status for us */
199 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
200 if(!stream_decoder_frame_sync_(decoder))
201 return true; /* above function sets the status for us */
203 case FLAC__STREAM_DECODER_READ_FRAME:
204 if(!stream_decoder_read_frame_(decoder, &dummy))
205 return false; /* above function sets the status for us */
207 case FLAC__STREAM_DECODER_END_OF_STREAM:
215 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
217 assert(decoder != 0);
219 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
222 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
224 if(!FLAC__stream_decoder_reset(decoder)) {
225 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
230 switch(decoder->state) {
231 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
232 if(!stream_decoder_find_metadata_(decoder))
233 return false; /* above function sets the status for us */
235 case FLAC__STREAM_DECODER_READ_METADATA:
236 if(!stream_decoder_read_metadata_(decoder))
237 return false; /* above function sets the status for us */
239 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
242 case FLAC__STREAM_DECODER_END_OF_STREAM:
250 bool FLAC__stream_decoder_process_one_frame(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_FRAME_SYNC);
261 switch(decoder->state) {
262 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
263 if(!stream_decoder_frame_sync_(decoder))
264 return true; /* above function sets the status for us */
266 case FLAC__STREAM_DECODER_READ_FRAME:
267 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
268 return false; /* above function sets the status for us */
270 return true; /* above function sets the status for us */
272 case FLAC__STREAM_DECODER_END_OF_STREAM:
280 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
283 assert(decoder != 0);
285 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
288 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
291 switch(decoder->state) {
292 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
293 if(!stream_decoder_frame_sync_(decoder))
294 return true; /* above function sets the status for us */
296 case FLAC__STREAM_DECODER_READ_FRAME:
297 if(!stream_decoder_read_frame_(decoder, &dummy))
298 return false; /* above function sets the status for us */
300 case FLAC__STREAM_DECODER_END_OF_STREAM:
308 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
310 assert(decoder != 0);
311 return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
314 bool stream_decoder_allocate_output(FLAC__StreamDecoder *decoder, unsigned size)
319 if(size <= decoder->guts->output_capacity)
322 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
323 if(decoder->guts->output[i] != 0) {
324 free(decoder->guts->output[i]);
325 decoder->guts->output[i] = 0;
328 if(decoder->guts->residual != 0) {
329 free(decoder->guts->residual);
330 decoder->guts->residual = 0;
333 for(i = 0; i < decoder->guts->frame_header.channels; i++) {
334 tmp = (int32*)malloc(sizeof(int32)*size);
336 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
339 decoder->guts->output[i] = tmp;
341 tmp = (int32*)malloc(sizeof(int32)*size);
343 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
346 decoder->guts->residual = tmp;
348 decoder->guts->output_capacity = size;
353 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
359 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
361 for(i = id = 0; i < 4; ) {
362 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
363 return false; /* the read_callback_ sets the state for us */
364 if(x == FLAC__STREAM_SYNC_STRING[i]) {
370 if(x == ID3V2_TAG_[id]) {
374 if(!stream_decoder_skip_id3v2_tag_(decoder))
375 return false; /* the read_callback_ sets the state for us */
379 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
381 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
382 return false; /* the read_callback_ sets the state for us */
383 if(!y) { /* MAGIC NUMBER for the last sync bit */
384 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
390 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
395 decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
399 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
401 uint32 i, x, last_block, type, length;
403 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
405 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
406 return false; /* the read_callback_ sets the state for us */
407 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
408 return false; /* the read_callback_ sets the state for us */
409 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
410 return false; /* the read_callback_ sets the state for us */
411 if(type == FLAC__METADATA_TYPE_ENCODING) {
412 decoder->guts->stream_header.type = type;
413 decoder->guts->stream_header.is_last = last_block;
414 decoder->guts->stream_header.length = length;
415 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
416 return false; /* the read_callback_ sets the state for us */
417 decoder->guts->stream_header.data.encoding.min_blocksize = x;
418 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
419 return false; /* the read_callback_ sets the state for us */
420 decoder->guts->stream_header.data.encoding.max_blocksize = x;
421 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
422 return false; /* the read_callback_ sets the state for us */
423 decoder->guts->stream_header.data.encoding.min_framesize = x;
424 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
425 return false; /* the read_callback_ sets the state for us */
426 decoder->guts->stream_header.data.encoding.max_framesize = x;
427 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
428 return false; /* the read_callback_ sets the state for us */
429 decoder->guts->stream_header.data.encoding.sample_rate = x;
430 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
431 return false; /* the read_callback_ sets the state for us */
432 decoder->guts->stream_header.data.encoding.channels = x+1;
433 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
434 return false; /* the read_callback_ sets the state for us */
435 decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
436 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))
437 return false; /* the read_callback_ sets the state for us */
438 decoder->guts->has_stream_header = true;
439 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
442 /* skip other metadata blocks */
443 for(i = 0; i < length; i++) {
444 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
445 return false; /* the read_callback_ sets the state for us */
450 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
455 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
460 /* skip the version and flags bytes */
461 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
462 return false; /* the read_callback_ sets the state for us */
463 /* get the size (in bytes) to skip */
465 for(i = 0; i < 4; i++) {
466 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
467 return false; /* the read_callback_ sets the state for us */
471 /* skip the rest of the tag */
472 for(i = 0; i < skip; i++) {
473 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
474 return false; /* the read_callback_ sets the state for us */
479 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
484 /* If we know the total number of samples in the stream, stop if we've read that many. */
485 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
486 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
487 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
488 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
493 /* make sure we're byte aligned */
494 if(decoder->guts->input.consumed_bits != 0) {
495 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
496 return false; /* the read_callback_ sets the state for us */
500 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
501 return false; /* the read_callback_ sets the state for us */
502 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
504 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
505 return false; /* the read_callback_ sets the state for us */
506 if(!y) { /* MAGIC NUMBER for the last sync bit */
507 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
512 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
520 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
524 int32 mid, side, left, right;
526 *got_a_frame = false;
528 if(!stream_decoder_read_frame_header_(decoder))
530 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
531 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
532 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
534 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
537 if(!stream_decoder_allocate_output(decoder, decoder->guts->frame_header.blocksize))
539 for(channel = 0; channel < decoder->guts->frame_header.channels; channel++) {
540 if(!stream_decoder_read_subframe_(decoder, channel))
542 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
543 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
547 if(!stream_decoder_read_zero_padding_(decoder))
550 /* Undo any special channel coding */
551 switch(decoder->guts->frame_header.channel_assignment) {
552 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
555 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
556 assert(decoder->guts->frame_header.channels == 2);
557 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
558 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
560 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
561 assert(decoder->guts->frame_header.channels == 2);
562 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
563 decoder->guts->output[0][i] += decoder->guts->output[1][i];
565 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
566 assert(decoder->guts->frame_header.channels == 2);
567 for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
568 mid = decoder->guts->output[0][i];
569 side = decoder->guts->output[1][i];
571 if(side & 1) /* i.e. if 'side' is odd... */
575 decoder->guts->output[0][i] = left >> 1;
576 decoder->guts->output[1][i] = right >> 1;
586 /* put the latest values into the public section of the decoder instance */
587 decoder->channels = decoder->guts->frame_header.channels;
588 decoder->channel_assignment = decoder->guts->frame_header.channel_assignment;
589 decoder->bits_per_sample = decoder->guts->frame_header.bits_per_sample;
590 decoder->sample_rate = decoder->guts->frame_header.sample_rate;
591 decoder->blocksize = decoder->guts->frame_header.blocksize;
593 decoder->guts->samples_decoded += decoder->guts->frame_header.blocksize;
596 if(decoder->guts->write_callback(decoder, &decoder->guts->frame_header, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
599 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
603 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
607 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
608 byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
609 unsigned raw_header_len;
610 bool is_unparseable = false;
612 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
614 /* init the raw header with the first 8 bits of the sync code */
615 raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
619 * read in the raw header as bytes so we can CRC it, and parse it on the way
621 for(i = 0; i < 2; i++) {
622 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
623 return false; /* the read_callback_ sets the state for us */
624 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
625 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
627 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
628 return false; /* the read_callback_ sets the state for us */
629 if(!y) { /* MAGIC NUMBER for the last sync bit */
630 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
634 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
635 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
639 raw_header[raw_header_len++] = (byte)x;
641 assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
643 switch(x = raw_header[1] >> 4) {
645 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 */
646 decoder->guts->frame_header.blocksize = decoder->guts->stream_header.data.encoding.min_blocksize;
648 is_unparseable = true;
651 decoder->guts->frame_header.blocksize = 192;
657 decoder->guts->frame_header.blocksize = 576 << (x-2);
668 switch(x = raw_header[1] & 0x0f) {
670 if(decoder->guts->has_stream_header)
671 decoder->guts->frame_header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
673 is_unparseable = true;
678 is_unparseable = true;
681 decoder->guts->frame_header.sample_rate = 8000;
684 decoder->guts->frame_header.sample_rate = 16000;
687 decoder->guts->frame_header.sample_rate = 22050;
690 decoder->guts->frame_header.sample_rate = 24000;
693 decoder->guts->frame_header.sample_rate = 32000;
696 decoder->guts->frame_header.sample_rate = 44100;
699 decoder->guts->frame_header.sample_rate = 48000;
702 decoder->guts->frame_header.sample_rate = 96000;
707 sample_rate_hint = x;
710 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
711 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
717 x = (unsigned)(raw_header[2] >> 4);
719 decoder->guts->frame_header.channels = 2;
722 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
725 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
728 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
731 is_unparseable = true;
736 decoder->guts->frame_header.channels = (unsigned)x + 1;
737 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
740 switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
742 if(decoder->guts->has_stream_header)
743 decoder->guts->frame_header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
745 is_unparseable = true;
748 decoder->guts->frame_header.bits_per_sample = 8;
751 decoder->guts->frame_header.bits_per_sample = 12;
754 decoder->guts->frame_header.bits_per_sample = 16;
757 decoder->guts->frame_header.bits_per_sample = 20;
760 decoder->guts->frame_header.bits_per_sample = 24;
764 is_unparseable = true;
771 if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
772 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
773 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
778 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
779 return false; /* the read_callback_ sets the state for us */
780 if(xx == 0xffffffffffffffff) {
781 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
783 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
784 return false; /* the read_callback_ sets the state for us */
785 if(!y) { /* MAGIC NUMBER for the last sync bit */
786 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
790 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
791 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
796 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
797 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
801 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 */
802 decoder->guts->frame_header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.encoding.min_blocksize + xx;
804 decoder->guts->frame_header.number.sample_number = xx;
807 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
808 return false; /* the read_callback_ sets the state for us */
809 if(x == 0xffffffff) {
810 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
812 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
813 return false; /* the read_callback_ sets the state for us */
814 if(!y) { /* MAGIC NUMBER for the last sync bit */
815 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
819 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
820 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
825 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
826 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
830 decoder->guts->last_frame_number = x;
831 if(decoder->guts->has_stream_header) {
832 decoder->guts->frame_header.number.sample_number = (int64)decoder->guts->stream_header.data.encoding.min_blocksize * (int64)x;
835 is_unparseable = true;
840 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
841 return false; /* the read_callback_ sets the state for us */
842 raw_header[raw_header_len++] = (byte)x;
843 if(blocksize_hint == 7) {
845 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
846 return false; /* the read_callback_ sets the state for us */
847 raw_header[raw_header_len++] = (byte)_x;
850 decoder->guts->frame_header.blocksize = x+1;
853 if(sample_rate_hint) {
854 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
855 return false; /* the read_callback_ sets the state for us */
856 raw_header[raw_header_len++] = (byte)x;
857 if(sample_rate_hint != 12) {
859 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
860 return false; /* the read_callback_ sets the state for us */
861 raw_header[raw_header_len++] = (byte)_x;
864 if(sample_rate_hint == 12)
865 decoder->guts->frame_header.sample_rate = x*1000;
866 else if(sample_rate_hint == 13)
867 decoder->guts->frame_header.sample_rate = x;
869 decoder->guts->frame_header.sample_rate = x*10;
872 /* read the crc byte */
873 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
874 return false; /* the read_callback_ sets the state for us */
877 if(FLAC__crc8(raw_header, raw_header_len) != crc) {
878 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
879 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
884 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
891 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel)
895 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_HEADER_TYPE_LEN, read_callback_, decoder))
896 return false; /* the read_callback_ sets the state for us */
897 if(x & 0x01 || x & 0x80) {
898 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
899 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
903 return stream_decoder_read_subframe_constant_(decoder, channel);
906 return stream_decoder_read_subframe_verbatim_(decoder, channel);
909 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
913 return stream_decoder_read_subframe_fixed_(decoder, channel, (x>>1)&7);
916 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
920 return stream_decoder_read_subframe_lpc_(decoder, channel, ((x>>1)&31)+1);
924 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel)
929 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
930 return false; /* the read_callback_ sets the state for us */
932 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
933 decoder->guts->output[channel][i] = x;
938 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
940 FLAC__SubframeHeader_Fixed subframe_header;
945 subframe_header.order = order;
947 /* read warm-up samples */
948 for(u = 0; u < order; u++) {
949 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
950 return false; /* the read_callback_ sets the state for us */
951 decoder->guts->output[channel][u] = i32;
954 /* read entropy coding method info */
955 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
956 return false; /* the read_callback_ sets the state for us */
957 subframe_header.entropy_coding_method.type = u32;
958 switch(subframe_header.entropy_coding_method.type) {
959 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
960 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
961 return false; /* the read_callback_ sets the state for us */
962 subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
965 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
970 switch(subframe_header.entropy_coding_method.type) {
971 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
972 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
979 /* decode the subframe */
980 FLAC__fixed_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, order, decoder->guts->output[channel]+order);
985 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
987 FLAC__SubframeHeader_LPC subframe_header;
992 subframe_header.order = order;
994 /* read warm-up samples */
995 for(u = 0; u < order; u++) {
996 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
997 return false; /* the read_callback_ sets the state for us */
998 decoder->guts->output[channel][u] = i32;
1001 /* read qlp coeff precision */
1002 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1003 return false; /* the read_callback_ sets the state for us */
1005 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1006 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1009 subframe_header.qlp_coeff_precision = u32+1;
1011 /* read qlp shift */
1012 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1013 return false; /* the read_callback_ sets the state for us */
1014 subframe_header.quantization_level = i32;
1016 /* read quantized lp coefficiencts */
1017 for(u = 0; u < order; u++) {
1018 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe_header.qlp_coeff_precision, read_callback_, decoder))
1019 return false; /* the read_callback_ sets the state for us */
1020 subframe_header.qlp_coeff[u] = i32;
1023 /* read entropy coding method info */
1024 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1025 return false; /* the read_callback_ sets the state for us */
1026 subframe_header.entropy_coding_method.type = u32;
1027 switch(subframe_header.entropy_coding_method.type) {
1028 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1029 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1030 return false; /* the read_callback_ sets the state for us */
1031 subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1034 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1039 switch(subframe_header.entropy_coding_method.type) {
1040 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1041 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1048 /* decode the subframe */
1049 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);
1054 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel)
1059 for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
1060 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1061 return false; /* the read_callback_ sets the state for us */
1062 decoder->guts->output[channel][i] = x;
1068 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order)
1070 uint32 rice_parameter;
1072 unsigned partition, sample, u;
1073 const unsigned partitions = 1u << partition_order;
1074 const unsigned partition_samples = partition_order > 0? decoder->guts->frame_header.blocksize >> partition_order : decoder->guts->frame_header.blocksize - predictor_order;
1077 for(partition = 0; partition < partitions; partition++) {
1078 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1079 return false; /* the read_callback_ sets the state for us */
1080 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1081 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1082 return false; /* the read_callback_ sets the state for us */
1083 decoder->guts->residual[sample] = i;
1090 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1092 if(decoder->guts->input.consumed_bits != 0) {
1094 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1095 return false; /* the read_callback_ sets the state for us */
1097 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1098 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1104 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1106 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1107 FLAC__StreamDecoderReadStatus status;
1108 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1109 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1110 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1111 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1112 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1113 return status == FLAC__STREAM_DECODER_READ_CONTINUE;