1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <stdlib.h> /* for malloc() */
23 #include "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__Frame *frame, 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];
37 int32 *residual[FLAC__MAX_CHANNELS];
38 unsigned output_capacity, output_channels;
39 uint32 last_frame_number;
40 uint64 samples_decoded;
41 bool has_stream_header;
42 FLAC__StreamMetaData stream_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, unsigned channels);
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, unsigned bps);
56 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
57 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
58 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
59 static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
60 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
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__Frame *frame, 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[i] = 0;
146 decoder->guts->output_capacity = 0;
147 decoder->guts->output_channels = 0;
148 decoder->guts->last_frame_number = 0;
149 decoder->guts->samples_decoded = 0;
150 decoder->guts->has_stream_header = false;
152 return decoder->state;
155 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
158 assert(decoder != 0);
159 if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
161 if(decoder->guts != 0) {
162 FLAC__bitbuffer_free(&decoder->guts->input);
163 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
164 if(decoder->guts->output[i] != 0) {
165 free(decoder->guts->output[i]);
166 decoder->guts->output[i] = 0;
168 if(decoder->guts->residual[i] != 0) {
169 free(decoder->guts->residual[i]);
170 decoder->guts->residual[i] = 0;
176 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
179 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
181 assert(decoder != 0);
183 if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
184 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
191 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
193 assert(decoder != 0);
195 if(!FLAC__stream_decoder_flush(decoder)) {
196 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
199 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
204 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
207 assert(decoder != 0);
209 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
212 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
214 if(!FLAC__stream_decoder_reset(decoder)) {
215 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
220 switch(decoder->state) {
221 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
222 if(!stream_decoder_find_metadata_(decoder))
223 return false; /* above function sets the status for us */
225 case FLAC__STREAM_DECODER_READ_METADATA:
226 if(!stream_decoder_read_metadata_(decoder))
227 return false; /* above function sets the status for us */
229 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
230 if(!stream_decoder_frame_sync_(decoder))
231 return true; /* above function sets the status for us */
233 case FLAC__STREAM_DECODER_READ_FRAME:
234 if(!stream_decoder_read_frame_(decoder, &dummy))
235 return false; /* above function sets the status for us */
237 case FLAC__STREAM_DECODER_END_OF_STREAM:
245 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
247 assert(decoder != 0);
249 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
252 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
254 if(!FLAC__stream_decoder_reset(decoder)) {
255 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
260 switch(decoder->state) {
261 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
262 if(!stream_decoder_find_metadata_(decoder))
263 return false; /* above function sets the status for us */
265 case FLAC__STREAM_DECODER_READ_METADATA:
266 if(!stream_decoder_read_metadata_(decoder))
267 return false; /* above function sets the status for us */
269 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
272 case FLAC__STREAM_DECODER_END_OF_STREAM:
280 bool FLAC__stream_decoder_process_one_frame(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, &got_a_frame))
298 return false; /* above function sets the status for us */
300 return true; /* above function sets the status for us */
302 case FLAC__STREAM_DECODER_END_OF_STREAM:
310 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
313 assert(decoder != 0);
315 if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
318 assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
321 switch(decoder->state) {
322 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
323 if(!stream_decoder_frame_sync_(decoder))
324 return true; /* above function sets the status for us */
326 case FLAC__STREAM_DECODER_READ_FRAME:
327 if(!stream_decoder_read_frame_(decoder, &dummy))
328 return false; /* above function sets the status for us */
330 case FLAC__STREAM_DECODER_END_OF_STREAM:
338 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
340 assert(decoder != 0);
341 return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
344 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
349 if(size <= decoder->guts->output_capacity && channels <= decoder->guts->output_channels)
352 /* @@@ should change to use realloc() */
354 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
355 if(decoder->guts->output[i] != 0) {
356 free(decoder->guts->output[i]);
357 decoder->guts->output[i] = 0;
359 if(decoder->guts->residual[i] != 0) {
360 free(decoder->guts->residual[i]);
361 decoder->guts->residual[i] = 0;
365 for(i = 0; i < channels; i++) {
366 tmp = (int32*)malloc(sizeof(int32)*size);
368 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
371 decoder->guts->output[i] = tmp;
373 tmp = (int32*)malloc(sizeof(int32)*size);
375 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
378 decoder->guts->residual[i] = tmp;
381 decoder->guts->output_capacity = size;
382 decoder->guts->output_channels = channels;
387 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
393 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
395 for(i = id = 0; i < 4; ) {
396 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
397 return false; /* the read_callback_ sets the state for us */
398 if(x == FLAC__STREAM_SYNC_STRING[i]) {
404 if(x == ID3V2_TAG_[id]) {
408 if(!stream_decoder_skip_id3v2_tag_(decoder))
409 return false; /* the read_callback_ sets the state for us */
413 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
415 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
416 return false; /* the read_callback_ sets the state for us */
417 if(!y) { /* MAGIC NUMBER for the last sync bit */
418 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
424 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
429 decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
433 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
435 uint32 i, x, last_block, type, length;
437 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
439 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
440 return false; /* the read_callback_ sets the state for us */
441 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
442 return false; /* the read_callback_ sets the state for us */
443 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
444 return false; /* the read_callback_ sets the state for us */
445 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
446 unsigned used_bits = 0;
447 decoder->guts->stream_header.type = type;
448 decoder->guts->stream_header.is_last = last_block;
449 decoder->guts->stream_header.length = length;
451 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
452 return false; /* the read_callback_ sets the state for us */
453 decoder->guts->stream_header.data.stream_info.min_blocksize = x;
454 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
456 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
457 return false; /* the read_callback_ sets the state for us */
458 decoder->guts->stream_header.data.stream_info.max_blocksize = x;
459 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
461 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
462 return false; /* the read_callback_ sets the state for us */
463 decoder->guts->stream_header.data.stream_info.min_framesize = x;
464 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
466 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
467 return false; /* the read_callback_ sets the state for us */
468 decoder->guts->stream_header.data.stream_info.max_framesize = x;
469 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
471 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
472 return false; /* the read_callback_ sets the state for us */
473 decoder->guts->stream_header.data.stream_info.sample_rate = x;
474 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
476 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
477 return false; /* the read_callback_ sets the state for us */
478 decoder->guts->stream_header.data.stream_info.channels = x+1;
479 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
481 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
482 return false; /* the read_callback_ sets the state for us */
483 decoder->guts->stream_header.data.stream_info.bits_per_sample = x+1;
484 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
486 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_header.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
487 return false; /* the read_callback_ sets the state for us */
488 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
490 for(i = 0; i < 16; i++) {
491 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
492 return false; /* the read_callback_ sets the state for us */
493 decoder->guts->stream_header.data.stream_info.md5sum[i] = (byte)x;
497 /* skip the rest of the block */
498 assert(used_bits % 8 == 0);
499 length -= (used_bits / 8);
500 for(i = 0; i < length; 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 */
505 decoder->guts->has_stream_header = true;
506 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
509 /* skip other metadata blocks */
510 for(i = 0; i < length; i++) {
511 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
512 return false; /* the read_callback_ sets the state for us */
517 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
522 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
527 /* skip the version and flags bytes */
528 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
529 return false; /* the read_callback_ sets the state for us */
530 /* get the size (in bytes) to skip */
532 for(i = 0; i < 4; i++) {
533 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
534 return false; /* the read_callback_ sets the state for us */
538 /* skip the rest of the tag */
539 for(i = 0; i < skip; i++) {
540 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
541 return false; /* the read_callback_ sets the state for us */
546 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
551 /* If we know the total number of samples in the stream, stop if we've read that many. */
552 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
553 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.total_samples) {
554 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.stream_info.total_samples) {
555 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
560 /* make sure we're byte aligned */
561 if(decoder->guts->input.consumed_bits != 0) {
562 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
563 return false; /* the read_callback_ sets the state for us */
567 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
568 return false; /* the read_callback_ sets the state for us */
569 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
571 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
572 return false; /* the read_callback_ sets the state for us */
573 if(!y) { /* MAGIC NUMBER for the last sync bit */
574 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
579 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
587 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
591 int32 mid, side, left, right;
593 *got_a_frame = false;
595 if(!stream_decoder_read_frame_header_(decoder))
597 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
598 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
599 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
601 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
604 if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize, decoder->guts->frame.header.channels))
606 for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
608 * first figure the correct bits-per-sample of the subframe
610 unsigned bps = decoder->guts->frame.header.bits_per_sample;
611 switch(decoder->guts->frame.header.channel_assignment) {
612 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
613 /* no adjustment needed */
615 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
619 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
623 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
633 if(!stream_decoder_read_subframe_(decoder, channel, bps))
635 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
636 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
640 if(!stream_decoder_read_zero_padding_(decoder))
643 /* Undo any special channel coding */
644 switch(decoder->guts->frame.header.channel_assignment) {
645 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
648 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
649 assert(decoder->guts->frame.header.channels == 2);
650 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
651 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
653 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
654 assert(decoder->guts->frame.header.channels == 2);
655 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
656 decoder->guts->output[0][i] += decoder->guts->output[1][i];
658 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
659 assert(decoder->guts->frame.header.channels == 2);
660 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
661 mid = decoder->guts->output[0][i];
662 side = decoder->guts->output[1][i];
664 if(side & 1) /* i.e. if 'side' is odd... */
668 decoder->guts->output[0][i] = left >> 1;
669 decoder->guts->output[1][i] = right >> 1;
679 /* put the latest values into the public section of the decoder instance */
680 decoder->channels = decoder->guts->frame.header.channels;
681 decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
682 decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
683 decoder->sample_rate = decoder->guts->frame.header.sample_rate;
684 decoder->blocksize = decoder->guts->frame.header.blocksize;
686 decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
689 if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
692 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
696 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
700 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
701 byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
702 unsigned raw_header_len;
703 bool is_unparseable = false;
705 assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
707 /* init the raw header with the first 8 bits of the sync code */
708 raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
712 * read in the raw header as bytes so we can CRC it, and parse it on the way
714 for(i = 0; i < 2; i++) {
715 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
716 return false; /* the read_callback_ sets the state for us */
717 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
718 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
720 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
721 return false; /* the read_callback_ sets the state for us */
722 if(!y) { /* MAGIC NUMBER for the last sync bit */
723 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
727 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
728 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
732 raw_header[raw_header_len++] = (byte)x;
734 assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
736 switch(x = raw_header[1] >> 4) {
738 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.min_blocksize == decoder->guts->stream_header.data.stream_info.max_blocksize) /* i.e. it's a fixed-blocksize stream */
739 decoder->guts->frame.header.blocksize = decoder->guts->stream_header.data.stream_info.min_blocksize;
741 is_unparseable = true;
744 decoder->guts->frame.header.blocksize = 192;
750 decoder->guts->frame.header.blocksize = 576 << (x-2);
761 switch(x = raw_header[1] & 0x0f) {
763 if(decoder->guts->has_stream_header)
764 decoder->guts->frame.header.sample_rate = decoder->guts->stream_header.data.stream_info.sample_rate;
766 is_unparseable = true;
771 is_unparseable = true;
774 decoder->guts->frame.header.sample_rate = 8000;
777 decoder->guts->frame.header.sample_rate = 16000;
780 decoder->guts->frame.header.sample_rate = 22050;
783 decoder->guts->frame.header.sample_rate = 24000;
786 decoder->guts->frame.header.sample_rate = 32000;
789 decoder->guts->frame.header.sample_rate = 44100;
792 decoder->guts->frame.header.sample_rate = 48000;
795 decoder->guts->frame.header.sample_rate = 96000;
800 sample_rate_hint = x;
803 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
804 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
810 x = (unsigned)(raw_header[2] >> 4);
812 decoder->guts->frame.header.channels = 2;
815 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
818 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
821 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
824 is_unparseable = true;
829 decoder->guts->frame.header.channels = (unsigned)x + 1;
830 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
833 switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
835 if(decoder->guts->has_stream_header)
836 decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_header.data.stream_info.bits_per_sample;
838 is_unparseable = true;
841 decoder->guts->frame.header.bits_per_sample = 8;
844 decoder->guts->frame.header.bits_per_sample = 12;
847 decoder->guts->frame.header.bits_per_sample = 16;
850 decoder->guts->frame.header.bits_per_sample = 20;
853 decoder->guts->frame.header.bits_per_sample = 24;
857 is_unparseable = true;
864 if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
865 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
866 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
871 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
872 return false; /* the read_callback_ sets the state for us */
873 if(xx == 0xffffffffffffffff) {
874 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
876 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
877 return false; /* the read_callback_ sets the state for us */
878 if(!y) { /* MAGIC NUMBER for the last sync bit */
879 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
883 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
884 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
889 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
890 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
894 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.stream_info.min_blocksize == decoder->guts->stream_header.data.stream_info.max_blocksize) /* i.e. it's a fixed-blocksize stream */
895 decoder->guts->frame.header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.stream_info.min_blocksize + xx;
897 decoder->guts->frame.header.number.sample_number = xx;
900 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
901 return false; /* the read_callback_ sets the state for us */
902 if(x == 0xffffffff) {
903 if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
905 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
906 return false; /* the read_callback_ sets the state for us */
907 if(!y) { /* MAGIC NUMBER for the last sync bit */
908 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
912 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
913 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
918 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
919 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
923 decoder->guts->last_frame_number = x;
924 if(decoder->guts->has_stream_header) {
925 decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_header.data.stream_info.min_blocksize * (int64)x;
928 is_unparseable = true;
933 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
934 return false; /* the read_callback_ sets the state for us */
935 raw_header[raw_header_len++] = (byte)x;
936 if(blocksize_hint == 7) {
938 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
939 return false; /* the read_callback_ sets the state for us */
940 raw_header[raw_header_len++] = (byte)_x;
943 decoder->guts->frame.header.blocksize = x+1;
946 if(sample_rate_hint) {
947 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
948 return false; /* the read_callback_ sets the state for us */
949 raw_header[raw_header_len++] = (byte)x;
950 if(sample_rate_hint != 12) {
952 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
953 return false; /* the read_callback_ sets the state for us */
954 raw_header[raw_header_len++] = (byte)_x;
957 if(sample_rate_hint == 12)
958 decoder->guts->frame.header.sample_rate = x*1000;
959 else if(sample_rate_hint == 13)
960 decoder->guts->frame.header.sample_rate = x;
962 decoder->guts->frame.header.sample_rate = x*10;
965 /* read the crc byte */
966 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
967 return false; /* the read_callback_ sets the state for us */
970 if(FLAC__crc8(raw_header, raw_header_len) != crc) {
971 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
972 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
977 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
984 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
988 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_TYPE_LEN, read_callback_, decoder))
989 return false; /* the read_callback_ sets the state for us */
990 if(x & 0x01 || x & 0x80) {
991 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
992 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
996 return stream_decoder_read_subframe_constant_(decoder, channel, bps);
999 return stream_decoder_read_subframe_verbatim_(decoder, channel, bps);
1002 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1006 return stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7);
1009 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1013 return stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1);
1017 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1019 FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
1022 int32 *output = decoder->guts->output[channel];
1024 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1026 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1027 return false; /* the read_callback_ sets the state for us */
1029 subframe->value = x;
1031 /* decode the subframe */
1032 for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1038 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1040 FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
1045 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1047 subframe->residual = decoder->guts->residual[channel];
1048 subframe->order = order;
1050 /* read warm-up samples */
1051 for(u = 0; u < order; u++) {
1052 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1053 return false; /* the read_callback_ sets the state for us */
1054 subframe->warmup[u] = i32;
1057 /* read entropy coding method info */
1058 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1059 return false; /* the read_callback_ sets the state for us */
1060 subframe->entropy_coding_method.type = u32;
1061 switch(subframe->entropy_coding_method.type) {
1062 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1063 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1064 return false; /* the read_callback_ sets the state for us */
1065 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1068 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1073 switch(subframe->entropy_coding_method.type) {
1074 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1075 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1082 /* decode the subframe */
1083 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1084 FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
1089 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1091 FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
1096 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1098 subframe->residual = decoder->guts->residual[channel];
1099 subframe->order = order;
1101 /* read warm-up samples */
1102 for(u = 0; u < order; u++) {
1103 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1104 return false; /* the read_callback_ sets the state for us */
1105 subframe->warmup[u] = i32;
1108 /* read qlp coeff precision */
1109 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1110 return false; /* the read_callback_ sets the state for us */
1112 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1113 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1116 subframe->qlp_coeff_precision = u32+1;
1118 /* read qlp shift */
1119 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1120 return false; /* the read_callback_ sets the state for us */
1121 subframe->quantization_level = i32;
1123 /* read quantized lp coefficiencts */
1124 for(u = 0; u < order; u++) {
1125 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1126 return false; /* the read_callback_ sets the state for us */
1127 subframe->qlp_coeff[u] = i32;
1130 /* read entropy coding method info */
1131 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1132 return false; /* the read_callback_ sets the state for us */
1133 subframe->entropy_coding_method.type = u32;
1134 switch(subframe->entropy_coding_method.type) {
1135 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1136 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1137 return false; /* the read_callback_ sets the state for us */
1138 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1141 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1146 switch(subframe->entropy_coding_method.type) {
1147 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1148 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
1155 /* decode the subframe */
1156 memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1157 FLAC__lpc_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->guts->output[channel]+order);
1162 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1164 FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
1165 int32 x, *residual = decoder->guts->residual[channel];
1168 decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1170 subframe->data = residual;
1172 for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
1173 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1174 return false; /* the read_callback_ sets the state for us */
1178 /* decode the subframe */
1179 memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
1184 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
1186 uint32 rice_parameter;
1188 unsigned partition, sample, u;
1189 const unsigned partitions = 1u << partition_order;
1190 const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
1193 for(partition = 0; partition < partitions; partition++) {
1194 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1195 return false; /* the read_callback_ sets the state for us */
1196 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1197 #ifdef SYMMETRIC_RICE
1198 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1199 return false; /* the read_callback_ sets the state for us */
1201 if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1202 return false; /* the read_callback_ sets the state for us */
1204 residual[sample] = i;
1211 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1213 if(decoder->guts->input.consumed_bits != 0) {
1215 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1216 return false; /* the read_callback_ sets the state for us */
1218 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1219 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1225 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1227 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1228 FLAC__StreamDecoderReadStatus status;
1229 status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1230 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1231 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1232 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1233 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1234 return status == FLAC__STREAM_DECODER_READ_CONTINUE;