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 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 decoder->guts->stream_header.type = type;
441 decoder->guts->stream_header.is_last = last_block;
442 decoder->guts->stream_header.length = length;
443 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
444 return false; /* the read_callback_ sets the state for us */
445 decoder->guts->stream_header.data.encoding.min_blocksize = x;
446 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
447 return false; /* the read_callback_ sets the state for us */
448 decoder->guts->stream_header.data.encoding.max_blocksize = x;
449 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
450 return false; /* the read_callback_ sets the state for us */
451 decoder->guts->stream_header.data.encoding.min_framesize = x;
452 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
453 return false; /* the read_callback_ sets the state for us */
454 decoder->guts->stream_header.data.encoding.max_framesize = x;
455 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
456 return false; /* the read_callback_ sets the state for us */
457 decoder->guts->stream_header.data.encoding.sample_rate = x;
458 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
459 return false; /* the read_callback_ sets the state for us */
460 decoder->guts->stream_header.data.encoding.channels = x+1;
461 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
462 return false; /* the read_callback_ sets the state for us */
463 decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
464 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))
465 return false; /* the read_callback_ sets the state for us */
466 decoder->guts->has_stream_header = true;
467 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
470 /* skip other metadata blocks */
471 for(i = 0; i < length; i++) {
472 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
473 return false; /* the read_callback_ sets the state for us */
478 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
483 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
488 /* skip the version and flags bytes */
489 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
490 return false; /* the read_callback_ sets the state for us */
491 /* get the size (in bytes) to skip */
493 for(i = 0; i < 4; i++) {
494 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
495 return false; /* the read_callback_ sets the state for us */
499 /* skip the rest of the tag */
500 for(i = 0; i < skip; i++) {
501 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
502 return false; /* the read_callback_ sets the state for us */
507 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
512 /* If we know the total number of samples in the stream, stop if we've read that many. */
513 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
514 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
515 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
516 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
521 /* make sure we're byte aligned */
522 if(decoder->guts->input.consumed_bits != 0) {
523 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
524 return false; /* the read_callback_ sets the state for us */
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 */
530 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
532 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
533 return false; /* the read_callback_ sets the state for us */
534 if(!y) { /* MAGIC NUMBER for the last sync bit */
535 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
540 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
548 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
552 int32 mid, side, left, right;
554 *got_a_frame = false;
556 if(!stream_decoder_read_frame_header_(decoder))
558 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
559 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
560 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
562 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
565 if(!stream_decoder_allocate_output(decoder, decoder->guts->frame_header.blocksize))
567 for(channel = 0; channel < decoder->guts->frame_header.channels; channel++) {
568 if(!stream_decoder_read_subframe_(decoder, channel))
570 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
571 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
575 if(!stream_decoder_read_zero_padding_(decoder))
578 /* Undo any special channel coding */
579 switch(decoder->guts->frame_header.channel_assignment) {
580 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
583 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
584 assert(decoder->guts->frame_header.channels == 2);
585 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
586 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
588 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
589 assert(decoder->guts->frame_header.channels == 2);
590 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
591 decoder->guts->output[0][i] += decoder->guts->output[1][i];
593 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
594 assert(decoder->guts->frame_header.channels == 2);
595 for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
596 mid = decoder->guts->output[0][i];
597 side = decoder->guts->output[1][i];
599 if(side & 1) /* i.e. if 'side' is odd... */
603 decoder->guts->output[0][i] = left >> 1;
604 decoder->guts->output[1][i] = right >> 1;
614 /* put the latest values into the public section of the decoder instance */
615 decoder->channels = decoder->guts->frame_header.channels;
616 decoder->channel_assignment = decoder->guts->frame_header.channel_assignment;
617 decoder->bits_per_sample = decoder->guts->frame_header.bits_per_sample;
618 decoder->sample_rate = decoder->guts->frame_header.sample_rate;
619 decoder->blocksize = decoder->guts->frame_header.blocksize;
621 decoder->guts->samples_decoded += decoder->guts->frame_header.blocksize;
624 if(decoder->guts->write_callback(decoder, &decoder->guts->frame_header, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
627 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
631 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
635 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
636 byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
637 unsigned raw_header_len;
638 bool is_unparseable = false;
640 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
642 /* init the raw header with the first 8 bits of the sync code */
643 raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
647 * read in the raw header as bytes so we can CRC it, and parse it on the way
649 for(i = 0; i < 2; i++) {
650 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
651 return false; /* the read_callback_ sets the state for us */
652 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
653 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
655 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
656 return false; /* the read_callback_ sets the state for us */
657 if(!y) { /* MAGIC NUMBER for the last sync bit */
658 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
662 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
663 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
667 raw_header[raw_header_len++] = (byte)x;
669 assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
671 switch(x = raw_header[1] >> 4) {
673 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 */
674 decoder->guts->frame_header.blocksize = decoder->guts->stream_header.data.encoding.min_blocksize;
676 is_unparseable = true;
679 decoder->guts->frame_header.blocksize = 192;
685 decoder->guts->frame_header.blocksize = 576 << (x-2);
696 switch(x = raw_header[1] & 0x0f) {
698 if(decoder->guts->has_stream_header)
699 decoder->guts->frame_header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
701 is_unparseable = true;
706 is_unparseable = true;
709 decoder->guts->frame_header.sample_rate = 8000;
712 decoder->guts->frame_header.sample_rate = 16000;
715 decoder->guts->frame_header.sample_rate = 22050;
718 decoder->guts->frame_header.sample_rate = 24000;
721 decoder->guts->frame_header.sample_rate = 32000;
724 decoder->guts->frame_header.sample_rate = 44100;
727 decoder->guts->frame_header.sample_rate = 48000;
730 decoder->guts->frame_header.sample_rate = 96000;
735 sample_rate_hint = x;
738 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
739 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
745 x = (unsigned)(raw_header[2] >> 4);
747 decoder->guts->frame_header.channels = 2;
750 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
753 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
756 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
759 is_unparseable = true;
764 decoder->guts->frame_header.channels = (unsigned)x + 1;
765 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
768 switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
770 if(decoder->guts->has_stream_header)
771 decoder->guts->frame_header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
773 is_unparseable = true;
776 decoder->guts->frame_header.bits_per_sample = 8;
779 decoder->guts->frame_header.bits_per_sample = 12;
782 decoder->guts->frame_header.bits_per_sample = 16;
785 decoder->guts->frame_header.bits_per_sample = 20;
788 decoder->guts->frame_header.bits_per_sample = 24;
792 is_unparseable = true;
799 if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
800 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
801 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
806 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
807 return false; /* the read_callback_ sets the state for us */
808 if(xx == 0xffffffffffffffff) {
809 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
811 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
812 return false; /* the read_callback_ sets the state for us */
813 if(!y) { /* MAGIC NUMBER for the last sync bit */
814 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
818 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
819 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
824 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
825 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
829 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 */
830 decoder->guts->frame_header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.encoding.min_blocksize + xx;
832 decoder->guts->frame_header.number.sample_number = xx;
835 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
836 return false; /* the read_callback_ sets the state for us */
837 if(x == 0xffffffff) {
838 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
840 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
841 return false; /* the read_callback_ sets the state for us */
842 if(!y) { /* MAGIC NUMBER for the last sync bit */
843 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
847 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
848 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
853 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
854 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
858 decoder->guts->last_frame_number = x;
859 if(decoder->guts->has_stream_header) {
860 decoder->guts->frame_header.number.sample_number = (int64)decoder->guts->stream_header.data.encoding.min_blocksize * (int64)x;
863 is_unparseable = true;
868 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
869 return false; /* the read_callback_ sets the state for us */
870 raw_header[raw_header_len++] = (byte)x;
871 if(blocksize_hint == 7) {
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 */
875 raw_header[raw_header_len++] = (byte)_x;
878 decoder->guts->frame_header.blocksize = x+1;
881 if(sample_rate_hint) {
882 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
883 return false; /* the read_callback_ sets the state for us */
884 raw_header[raw_header_len++] = (byte)x;
885 if(sample_rate_hint != 12) {
887 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
888 return false; /* the read_callback_ sets the state for us */
889 raw_header[raw_header_len++] = (byte)_x;
892 if(sample_rate_hint == 12)
893 decoder->guts->frame_header.sample_rate = x*1000;
894 else if(sample_rate_hint == 13)
895 decoder->guts->frame_header.sample_rate = x;
897 decoder->guts->frame_header.sample_rate = x*10;
900 /* read the crc byte */
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 */
905 if(FLAC__crc8(raw_header, raw_header_len) != crc) {
906 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
907 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
912 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
919 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel)
923 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_HEADER_TYPE_LEN, read_callback_, decoder))
924 return false; /* the read_callback_ sets the state for us */
925 if(x & 0x01 || x & 0x80) {
926 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
927 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
931 return stream_decoder_read_subframe_constant_(decoder, channel);
934 return stream_decoder_read_subframe_verbatim_(decoder, channel);
937 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
941 return stream_decoder_read_subframe_fixed_(decoder, channel, (x>>1)&7);
944 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
948 return stream_decoder_read_subframe_lpc_(decoder, channel, ((x>>1)&31)+1);
952 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel)
957 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
958 return false; /* the read_callback_ sets the state for us */
960 for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
961 decoder->guts->output[channel][i] = x;
966 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
968 FLAC__SubframeHeader_Fixed subframe_header;
973 subframe_header.order = order;
975 /* read warm-up samples */
976 for(u = 0; u < order; u++) {
977 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
978 return false; /* the read_callback_ sets the state for us */
979 decoder->guts->output[channel][u] = i32;
982 /* read entropy coding method info */
983 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
984 return false; /* the read_callback_ sets the state for us */
985 subframe_header.entropy_coding_method.type = u32;
986 switch(subframe_header.entropy_coding_method.type) {
987 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
988 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
989 return false; /* the read_callback_ sets the state for us */
990 subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
993 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
998 switch(subframe_header.entropy_coding_method.type) {
999 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1000 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1007 /* decode the subframe */
1008 FLAC__fixed_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, order, decoder->guts->output[channel]+order);
1013 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
1015 FLAC__SubframeHeader_LPC subframe_header;
1020 subframe_header.order = order;
1022 /* read warm-up samples */
1023 for(u = 0; u < order; u++) {
1024 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1025 return false; /* the read_callback_ sets the state for us */
1026 decoder->guts->output[channel][u] = i32;
1029 /* read qlp coeff precision */
1030 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1031 return false; /* the read_callback_ sets the state for us */
1033 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1034 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1037 subframe_header.qlp_coeff_precision = u32+1;
1039 /* read qlp shift */
1040 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1041 return false; /* the read_callback_ sets the state for us */
1042 subframe_header.quantization_level = i32;
1044 /* read quantized lp coefficiencts */
1045 for(u = 0; u < order; u++) {
1046 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe_header.qlp_coeff_precision, read_callback_, decoder))
1047 return false; /* the read_callback_ sets the state for us */
1048 subframe_header.qlp_coeff[u] = i32;
1051 /* read entropy coding method info */
1052 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1053 return false; /* the read_callback_ sets the state for us */
1054 subframe_header.entropy_coding_method.type = u32;
1055 switch(subframe_header.entropy_coding_method.type) {
1056 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1057 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1058 return false; /* the read_callback_ sets the state for us */
1059 subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1062 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1067 switch(subframe_header.entropy_coding_method.type) {
1068 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1069 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1076 /* decode the subframe */
1077 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);
1082 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel)
1087 for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
1088 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1089 return false; /* the read_callback_ sets the state for us */
1090 decoder->guts->output[channel][i] = x;
1096 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order)
1098 uint32 rice_parameter;
1100 unsigned partition, sample, u;
1101 const unsigned partitions = 1u << partition_order;
1102 const unsigned partition_samples = partition_order > 0? decoder->guts->frame_header.blocksize >> partition_order : decoder->guts->frame_header.blocksize - predictor_order;
1105 for(partition = 0; partition < partitions; partition++) {
1106 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1107 return false; /* the read_callback_ sets the state for us */
1108 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1109 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1110 return false; /* the read_callback_ sets the state for us */
1111 decoder->guts->residual[sample] = i;
1118 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1120 if(decoder->guts->input.consumed_bits != 0) {
1122 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1123 return false; /* the read_callback_ sets the state for us */
1125 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1126 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1132 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1134 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1135 FLAC__StreamDecoderReadStatus status;
1136 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1137 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1138 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1139 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1140 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1141 return status == FLAC__STREAM_DECODER_READ_CONTINUE;