blob: a365dd229f58ef32bcc9e88cfe6b5e1cd376638b [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson305ae2e2002-01-26 17:36:39 +00002 * Copyright (C) 2000,2001,2002 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
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.
8 *
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.
13 *
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.
18 */
19
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000020#include <stdio.h>
21#include <stdlib.h> /* for malloc() */
Josh Coalsonec1af652001-04-01 05:34:16 +000022#include <string.h> /* for memset/memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000023#include "FLAC/assert.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000024#include "protected/stream_decoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000025#include "private/bitbuffer.h"
Josh Coalsonb985bbc2001-05-23 21:59:52 +000026#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000027#include "private/crc.h"
28#include "private/fixed.h"
29#include "private/lpc.h"
30
Josh Coalson0a15c142001-06-13 17:59:57 +000031/***********************************************************************
32 *
33 * Private static data
34 *
35 ***********************************************************************/
36
Josh Coalson77e3f312001-06-23 03:03:24 +000037static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
Josh Coalson0a15c142001-06-13 17:59:57 +000038
39/***********************************************************************
40 *
41 * Private class method prototypes
42 *
43 ***********************************************************************/
44
Josh Coalson77e3f312001-06-23 03:03:24 +000045static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
Josh Coalsonbf9325f2002-05-07 05:30:27 +000046static FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
Josh Coalson77e3f312001-06-23 03:03:24 +000047static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
48static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
49static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
50static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
51static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
52static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
53static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
54static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
55static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
56static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
57static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
Josh Coalson63d99572001-07-12 21:26:57 +000058static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
Josh Coalson77e3f312001-06-23 03:03:24 +000059static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
60static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
Josh Coalson0a15c142001-06-13 17:59:57 +000061
62/***********************************************************************
63 *
64 * Private class data
65 *
66 ***********************************************************************/
67
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000068typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson77e3f312001-06-23 03:03:24 +000069 FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
70 FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000071 void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
72 void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson77e3f312001-06-23 03:03:24 +000073 void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
74 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000075 void *client_data;
Josh Coalsonaec256b2002-03-12 16:19:54 +000076 FLAC__BitBuffer *input;
Josh Coalson77e3f312001-06-23 03:03:24 +000077 FLAC__int32 *output[FLAC__MAX_CHANNELS];
78 FLAC__int32 *residual[FLAC__MAX_CHANNELS];
Josh Coalson1e1509f2001-03-23 20:20:43 +000079 unsigned output_capacity, output_channels;
Josh Coalson77e3f312001-06-23 03:03:24 +000080 FLAC__uint32 last_frame_number;
81 FLAC__uint64 samples_decoded;
82 FLAC__bool has_stream_info, has_seek_table;
Josh Coalsonc076ba52001-04-05 21:32:54 +000083 FLAC__StreamMetaData stream_info;
84 FLAC__StreamMetaData seek_table;
Josh Coalsonbf9325f2002-05-07 05:30:27 +000085 FLAC__bool metadata_filter[FLAC__METADATA_TYPE_VORBIS_COMMENT+1];
86 FLAC__byte *metadata_filter_ids;
87 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
Josh Coalson6dcea512001-01-23 23:07:36 +000088 FLAC__Frame frame;
Josh Coalson77e3f312001-06-23 03:03:24 +000089 FLAC__bool cached; /* true if there is a byte in lookahead */
Josh Coalsonb985bbc2001-05-23 21:59:52 +000090 FLAC__CPUInfo cpuinfo;
Josh Coalson77e3f312001-06-23 03:03:24 +000091 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
92 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000093} FLAC__StreamDecoderPrivate;
94
Josh Coalson0a15c142001-06-13 17:59:57 +000095/***********************************************************************
96 *
97 * Public static class data
98 *
99 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000100
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000101const char *FLAC__StreamDecoderStateString[] = {
102 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
103 "FLAC__STREAM_DECODER_READ_METADATA",
104 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
105 "FLAC__STREAM_DECODER_READ_FRAME",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000106 "FLAC__STREAM_DECODER_END_OF_STREAM",
107 "FLAC__STREAM_DECODER_ABORTED",
108 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
109 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000110 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
Josh Coalson00e53872001-06-16 07:32:25 +0000111 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000112 "FLAC__STREAM_DECODER_UNINITIALIZED"
113};
114
115const char *FLAC__StreamDecoderReadStatusString[] = {
116 "FLAC__STREAM_DECODER_READ_CONTINUE",
117 "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
118 "FLAC__STREAM_DECODER_READ_ABORT"
119};
120
121const char *FLAC__StreamDecoderWriteStatusString[] = {
122 "FLAC__STREAM_DECODER_WRITE_CONTINUE",
123 "FLAC__STREAM_DECODER_WRITE_ABORT"
124};
125
126const char *FLAC__StreamDecoderErrorStatusString[] = {
Josh Coalson215af572001-03-27 01:15:58 +0000127 "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
128 "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
129 "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000130};
131
Josh Coalson0a15c142001-06-13 17:59:57 +0000132/***********************************************************************
133 *
134 * Class constructor/destructor
135 *
136 ***********************************************************************/
137FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000138{
Josh Coalson0a15c142001-06-13 17:59:57 +0000139 FLAC__StreamDecoder *decoder;
140
141 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
142
143 decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
144 if(decoder == 0) {
145 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000146 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000147 decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
148 if(decoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000149 free(decoder);
150 return 0;
151 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000152 decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
153 if(decoder->private_ == 0) {
154 free(decoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000155 free(decoder);
156 return 0;
157 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000158 decoder->private_->input = FLAC__bitbuffer_new();
159 if(decoder->private_->input == 0) {
160 free(decoder->private_);
161 free(decoder->protected_);
162 free(decoder);
163 return 0;
164 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000165
Josh Coalsonfa697a92001-08-16 20:07:29 +0000166 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
Josh Coalson0a15c142001-06-13 17:59:57 +0000167
Josh Coalsonfa697a92001-08-16 20:07:29 +0000168 decoder->private_->read_callback = 0;
169 decoder->private_->write_callback = 0;
170 decoder->private_->metadata_callback = 0;
171 decoder->private_->error_callback = 0;
172 decoder->private_->client_data = 0;
Josh Coalson00e53872001-06-16 07:32:25 +0000173
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000174 return decoder;
175}
176
Josh Coalson0a15c142001-06-13 17:59:57 +0000177void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000178{
Josh Coalson0a15c142001-06-13 17:59:57 +0000179 FLAC__ASSERT(decoder != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000180 FLAC__ASSERT(decoder->protected_ != 0);
181 FLAC__ASSERT(decoder->private_ != 0);
Josh Coalsonaec256b2002-03-12 16:19:54 +0000182 FLAC__ASSERT(decoder->private_->input != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000183
Josh Coalsonaec256b2002-03-12 16:19:54 +0000184 FLAC__bitbuffer_delete(decoder->private_->input);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000185 free(decoder->private_);
186 free(decoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000187 free(decoder);
188}
189
Josh Coalson0a15c142001-06-13 17:59:57 +0000190/***********************************************************************
191 *
192 * Public class methods
193 *
194 ***********************************************************************/
195
Josh Coalson00e53872001-06-16 07:32:25 +0000196FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000197{
198 unsigned i;
199
Josh Coalson1b689822001-05-31 20:11:02 +0000200 FLAC__ASSERT(decoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000201
Josh Coalsonfa697a92001-08-16 20:07:29 +0000202 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
203 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000204
Josh Coalsonfa697a92001-08-16 20:07:29 +0000205 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000206
Josh Coalsonfa697a92001-08-16 20:07:29 +0000207 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
208 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000209
Josh Coalsonaec256b2002-03-12 16:19:54 +0000210 if(!FLAC__bitbuffer_init(decoder->private_->input))
211 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000212
Josh Coalson6dcea512001-01-23 23:07:36 +0000213 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000214 decoder->private_->output[i] = 0;
215 decoder->private_->residual[i] = 0;
Josh Coalson6dcea512001-01-23 23:07:36 +0000216 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000217
Josh Coalsonfa697a92001-08-16 20:07:29 +0000218 decoder->private_->output_capacity = 0;
219 decoder->private_->output_channels = 0;
220 decoder->private_->last_frame_number = 0;
221 decoder->private_->samples_decoded = 0;
222 decoder->private_->has_stream_info = false;
223 decoder->private_->has_seek_table = false;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000224
225 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
226 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
227 decoder->private_->metadata_filter_ids_capacity = 16;
228 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
229 decoder->private_->metadata_filter_ids_capacity = 0;
230 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
231 }
232 decoder->private_->metadata_filter_ids_count = 0;
233
Josh Coalsonfa697a92001-08-16 20:07:29 +0000234 decoder->private_->cached = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000235
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000236 /*
237 * get the CPU info and set the function pointers
238 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000239 FLAC__cpu_info(&decoder->private_->cpuinfo);
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000240 /* first default to the non-asm routines */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000241 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
242 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000243 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000244#ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000245 if(decoder->private_->cpuinfo.use_asm) {
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000246#ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000247 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000248#ifdef FLAC__HAS_NASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000249 if(decoder->private_->cpuinfo.data.ia32.mmx) {
250 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
251 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000252 }
253 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000254 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
255 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000256 }
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000257#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000258#endif
Josh Coalson021ad3b2001-07-18 00:25:52 +0000259 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000260#endif
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000261
Josh Coalsonfa697a92001-08-16 20:07:29 +0000262 return decoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000263}
264
265void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
266{
267 unsigned i;
Josh Coalson1b689822001-05-31 20:11:02 +0000268 FLAC__ASSERT(decoder != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000269 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000270 return;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000271 if(decoder->private_->has_seek_table) {
272 free(decoder->private_->seek_table.data.seek_table.points);
273 decoder->private_->seek_table.data.seek_table.points = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000274 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000275 FLAC__bitbuffer_free(decoder->private_->input);
Josh Coalson0a15c142001-06-13 17:59:57 +0000276 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson63d99572001-07-12 21:26:57 +0000277 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000278 if(decoder->private_->output[i] != 0) {
279 free(decoder->private_->output[i]-4);
280 decoder->private_->output[i] = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000281 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000282 if(decoder->private_->residual[i] != 0) {
283 free(decoder->private_->residual[i]);
284 decoder->private_->residual[i] = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000285 }
286 }
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000287 if(decoder->private_->metadata_filter_ids != 0) {
288 free(decoder->private_->metadata_filter_ids);
289 decoder->private_->metadata_filter_ids = 0;
290 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000291 decoder->private_->output_capacity = 0;
292 decoder->private_->output_channels = 0;
293 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
Josh Coalson0a15c142001-06-13 17:59:57 +0000294}
295
Josh Coalson77e3f312001-06-23 03:03:24 +0000296FLAC__bool FLAC__stream_decoder_set_read_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000297{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000298 FLAC__ASSERT(decoder != 0);
299 FLAC__ASSERT(decoder->private_ != 0);
300 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000301 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000302 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000303 decoder->private_->read_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000304 return true;
305}
306
Josh Coalson77e3f312001-06-23 03:03:24 +0000307FLAC__bool FLAC__stream_decoder_set_write_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000308{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000309 FLAC__ASSERT(decoder != 0);
310 FLAC__ASSERT(decoder->private_ != 0);
311 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000312 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000313 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000314 decoder->private_->write_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000315 return true;
316}
317
Josh Coalson77e3f312001-06-23 03:03:24 +0000318FLAC__bool FLAC__stream_decoder_set_metadata_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000319{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000320 FLAC__ASSERT(decoder != 0);
321 FLAC__ASSERT(decoder->private_ != 0);
322 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000323 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000324 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000325 decoder->private_->metadata_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000326 return true;
327}
328
Josh Coalson77e3f312001-06-23 03:03:24 +0000329FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000330{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000331 FLAC__ASSERT(decoder != 0);
332 FLAC__ASSERT(decoder->private_ != 0);
333 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000334 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000335 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000336 decoder->private_->error_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000337 return true;
338}
339
Josh Coalson77e3f312001-06-23 03:03:24 +0000340FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +0000341{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000342 FLAC__ASSERT(decoder != 0);
343 FLAC__ASSERT(decoder->private_ != 0);
344 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000345 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000346 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000347 decoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000348 return true;
349}
350
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000351FLAC__bool FLAC__stream_decoder_set_metadata_respond(const FLAC__StreamDecoder *decoder, FLAC__MetaDataType type)
352{
353 FLAC__ASSERT(decoder != 0);
354 FLAC__ASSERT(decoder->private_ != 0);
355 FLAC__ASSERT(decoder->protected_ != 0);
356 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
357 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
358 return false;
359 decoder->private_->metadata_filter[type] = true;
360 if(type == FLAC__METADATA_TYPE_APPLICATION)
361 decoder->private_->metadata_filter_ids_count = 0;
362 return true;
363}
364
365FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(const FLAC__StreamDecoder *decoder, FLAC__byte id[4])
366{
367 FLAC__ASSERT(decoder != 0);
368 FLAC__ASSERT(decoder->private_ != 0);
369 FLAC__ASSERT(decoder->protected_ != 0);
370 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
371 return false;
372
373 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
374 return true;
375
376 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
377
378 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
379 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
380 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
381 decoder->private_->metadata_filter_ids_capacity *= 2;
382 }
383
384 memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
385 decoder->private_->metadata_filter_ids_count++;
386
387 return true;
388}
389
390FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(const FLAC__StreamDecoder *decoder)
391{
392 FLAC__ASSERT(decoder != 0);
393 FLAC__ASSERT(decoder->private_ != 0);
394 FLAC__ASSERT(decoder->protected_ != 0);
395 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
396 return false;
397 /* OK, technically 0x01010101 != true, but it still means true */
398 memset(decoder->private_->metadata_filter, 1, sizeof(decoder->private_->metadata_filter));
399 decoder->private_->metadata_filter_ids_count = 0;
400 return true;
401}
402
403FLAC__bool FLAC__stream_decoder_set_metadata_ignore(const FLAC__StreamDecoder *decoder, FLAC__MetaDataType type)
404{
405 FLAC__ASSERT(decoder != 0);
406 FLAC__ASSERT(decoder->private_ != 0);
407 FLAC__ASSERT(decoder->protected_ != 0);
408 FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
409 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
410 return false;
411 decoder->private_->metadata_filter[type] = false;
412 if(type == FLAC__METADATA_TYPE_APPLICATION)
413 decoder->private_->metadata_filter_ids_count = 0;
414 return true;
415}
416
417FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(const FLAC__StreamDecoder *decoder, FLAC__byte id[4])
418{
419 FLAC__ASSERT(decoder != 0);
420 FLAC__ASSERT(decoder->private_ != 0);
421 FLAC__ASSERT(decoder->protected_ != 0);
422 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
423 return false;
424
425 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
426 return true;
427
428 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
429
430 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
431 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
432 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
433 decoder->private_->metadata_filter_ids_capacity *= 2;
434 }
435
436 memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
437 decoder->private_->metadata_filter_ids_count++;
438
439 return true;
440}
441
442FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(const FLAC__StreamDecoder *decoder)
443{
444 FLAC__ASSERT(decoder != 0);
445 FLAC__ASSERT(decoder->private_ != 0);
446 FLAC__ASSERT(decoder->protected_ != 0);
447 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
448 return false;
449 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
450 decoder->private_->metadata_filter_ids_count = 0;
451 return true;
452}
453
Josh Coalson00e53872001-06-16 07:32:25 +0000454FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000455{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000456 FLAC__ASSERT(decoder != 0);
457 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000458 return decoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +0000459}
460
Josh Coalson00e53872001-06-16 07:32:25 +0000461unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000462{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000463 FLAC__ASSERT(decoder != 0);
464 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000465 return decoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +0000466}
467
Josh Coalson00e53872001-06-16 07:32:25 +0000468FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000469{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000470 FLAC__ASSERT(decoder != 0);
471 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000472 return decoder->protected_->channel_assignment;
Josh Coalson0a15c142001-06-13 17:59:57 +0000473}
474
Josh Coalson00e53872001-06-16 07:32:25 +0000475unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000476{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000477 FLAC__ASSERT(decoder != 0);
478 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000479 return decoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +0000480}
481
Josh Coalson00e53872001-06-16 07:32:25 +0000482unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000483{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000484 FLAC__ASSERT(decoder != 0);
485 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000486 return decoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +0000487}
488
Josh Coalson00e53872001-06-16 07:32:25 +0000489unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000490{
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000491 FLAC__ASSERT(decoder != 0);
492 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000493 return decoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000494}
495
Josh Coalson77e3f312001-06-23 03:03:24 +0000496FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000497{
Josh Coalson1b689822001-05-31 20:11:02 +0000498 FLAC__ASSERT(decoder != 0);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000499 FLAC__ASSERT(decoder->private_ != 0);
500 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000501
Josh Coalsonaec256b2002-03-12 16:19:54 +0000502 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000503 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000504 return false;
505 }
Josh Coalsonb312f282001-11-01 05:23:48 +0000506 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000507
508 return true;
509}
510
Josh Coalson77e3f312001-06-23 03:03:24 +0000511FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000512{
Josh Coalson1b689822001-05-31 20:11:02 +0000513 FLAC__ASSERT(decoder != 0);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000514 FLAC__ASSERT(decoder->private_ != 0);
515 FLAC__ASSERT(decoder->protected_ != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000516
517 if(!FLAC__stream_decoder_flush(decoder)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000518 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000519 return false;
520 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000521 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000522
Josh Coalsonfa697a92001-08-16 20:07:29 +0000523 decoder->private_->samples_decoded = 0;
Josh Coalson671fadb2001-04-17 17:32:01 +0000524
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000525 return true;
526}
527
Josh Coalson77e3f312001-06-23 03:03:24 +0000528FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000529{
Josh Coalson77e3f312001-06-23 03:03:24 +0000530 FLAC__bool dummy;
Josh Coalson1b689822001-05-31 20:11:02 +0000531 FLAC__ASSERT(decoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000532
Josh Coalsonfa697a92001-08-16 20:07:29 +0000533 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000534 return true;
535
Josh Coalsonfa697a92001-08-16 20:07:29 +0000536 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000537
538 if(!FLAC__stream_decoder_reset(decoder)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000539 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000540 return false;
541 }
542
543 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000544 switch(decoder->protected_->state) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000545 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
546 if(!stream_decoder_find_metadata_(decoder))
547 return false; /* above function sets the status for us */
548 break;
549 case FLAC__STREAM_DECODER_READ_METADATA:
550 if(!stream_decoder_read_metadata_(decoder))
551 return false; /* above function sets the status for us */
552 break;
553 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
554 if(!stream_decoder_frame_sync_(decoder))
555 return true; /* above function sets the status for us */
556 break;
557 case FLAC__STREAM_DECODER_READ_FRAME:
558 if(!stream_decoder_read_frame_(decoder, &dummy))
559 return false; /* above function sets the status for us */
560 break;
561 case FLAC__STREAM_DECODER_END_OF_STREAM:
562 return true;
563 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000564 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000565 }
566 }
567}
568
Josh Coalson77e3f312001-06-23 03:03:24 +0000569FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000570{
Josh Coalson1b689822001-05-31 20:11:02 +0000571 FLAC__ASSERT(decoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000572
Josh Coalsonfa697a92001-08-16 20:07:29 +0000573 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000574 return true;
575
Josh Coalsonfa697a92001-08-16 20:07:29 +0000576 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000577
578 if(!FLAC__stream_decoder_reset(decoder)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000579 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000580 return false;
581 }
582
583 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000584 switch(decoder->protected_->state) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000585 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
586 if(!stream_decoder_find_metadata_(decoder))
587 return false; /* above function sets the status for us */
588 break;
589 case FLAC__STREAM_DECODER_READ_METADATA:
590 if(!stream_decoder_read_metadata_(decoder))
591 return false; /* above function sets the status for us */
592 break;
593 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
594 return true;
Josh Coalson03b189f2001-11-01 19:47:06 +0000595 case FLAC__STREAM_DECODER_READ_FRAME:
596 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000597 case FLAC__STREAM_DECODER_END_OF_STREAM:
598 return true;
599 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000600 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000601 }
602 }
603}
604
Josh Coalson77e3f312001-06-23 03:03:24 +0000605FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000606{
Josh Coalson77e3f312001-06-23 03:03:24 +0000607 FLAC__bool got_a_frame;
Josh Coalson1b689822001-05-31 20:11:02 +0000608 FLAC__ASSERT(decoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000609
Josh Coalsonfa697a92001-08-16 20:07:29 +0000610 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000611 return true;
612
Josh Coalsonfa697a92001-08-16 20:07:29 +0000613 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000614
615 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000616 switch(decoder->protected_->state) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000617 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
618 if(!stream_decoder_frame_sync_(decoder))
619 return true; /* above function sets the status for us */
620 break;
621 case FLAC__STREAM_DECODER_READ_FRAME:
622 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
623 return false; /* above function sets the status for us */
624 if(got_a_frame)
625 return true; /* above function sets the status for us */
626 break;
627 case FLAC__STREAM_DECODER_END_OF_STREAM:
628 return true;
629 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000630 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000631 }
632 }
633}
634
Josh Coalson77e3f312001-06-23 03:03:24 +0000635FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000636{
Josh Coalson77e3f312001-06-23 03:03:24 +0000637 FLAC__bool dummy;
Josh Coalson1b689822001-05-31 20:11:02 +0000638 FLAC__ASSERT(decoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000639
Josh Coalsonfa697a92001-08-16 20:07:29 +0000640 if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000641 return true;
642
Josh Coalsonfa697a92001-08-16 20:07:29 +0000643 FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000644
645 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000646 switch(decoder->protected_->state) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000647 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
648 if(!stream_decoder_frame_sync_(decoder))
649 return true; /* above function sets the status for us */
650 break;
651 case FLAC__STREAM_DECODER_READ_FRAME:
652 if(!stream_decoder_read_frame_(decoder, &dummy))
653 return false; /* above function sets the status for us */
654 break;
655 case FLAC__STREAM_DECODER_END_OF_STREAM:
656 return true;
657 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000658 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000659 }
660 }
661}
662
Josh Coalson0a15c142001-06-13 17:59:57 +0000663/***********************************************************************
664 *
665 * Protected class methods
666 *
667 ***********************************************************************/
668
Josh Coalson00e53872001-06-16 07:32:25 +0000669unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000670{
Josh Coalson1b689822001-05-31 20:11:02 +0000671 FLAC__ASSERT(decoder != 0);
Josh Coalsonaec256b2002-03-12 16:19:54 +0000672 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000673}
674
Josh Coalson0a15c142001-06-13 17:59:57 +0000675/***********************************************************************
676 *
677 * Private class methods
678 *
679 ***********************************************************************/
680
Josh Coalson77e3f312001-06-23 03:03:24 +0000681FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000682{
683 unsigned i;
Josh Coalson77e3f312001-06-23 03:03:24 +0000684 FLAC__int32 *tmp;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000685
Josh Coalsonfa697a92001-08-16 20:07:29 +0000686 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000687 return true;
688
Josh Coalson6dcea512001-01-23 23:07:36 +0000689 /* @@@ should change to use realloc() */
690
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000691 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000692 if(decoder->private_->output[i] != 0) {
693 free(decoder->private_->output[i]);
694 decoder->private_->output[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000695 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000696 if(decoder->private_->residual[i] != 0) {
697 free(decoder->private_->residual[i]);
698 decoder->private_->residual[i] = 0;
Josh Coalson6dcea512001-01-23 23:07:36 +0000699 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000700 }
701
Josh Coalson1e1509f2001-03-23 20:20:43 +0000702 for(i = 0; i < channels; i++) {
Josh Coalson63d99572001-07-12 21:26:57 +0000703 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
704 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000705 if(tmp == 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000706 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000707 return false;
708 }
Josh Coalson63d99572001-07-12 21:26:57 +0000709 memset(tmp, 0, sizeof(FLAC__int32)*4);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000710 decoder->private_->output[i] = tmp + 4;
Josh Coalson6dcea512001-01-23 23:07:36 +0000711
Josh Coalson77e3f312001-06-23 03:03:24 +0000712 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
Josh Coalson6dcea512001-01-23 23:07:36 +0000713 if(tmp == 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000714 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson6dcea512001-01-23 23:07:36 +0000715 return false;
716 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000717 decoder->private_->residual[i] = tmp;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000718 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000719
Josh Coalsonfa697a92001-08-16 20:07:29 +0000720 decoder->private_->output_capacity = size;
721 decoder->private_->output_channels = channels;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000722
723 return true;
724}
725
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000726FLAC__bool stream_decoder_has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
727{
728 unsigned i;
729
730 FLAC__ASSERT(0 != decoder);
731 FLAC__ASSERT(0 != decoder->private_);
732
733 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
734 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
735 return true;
736
737 return false;
738}
739
Josh Coalson77e3f312001-06-23 03:03:24 +0000740FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000741{
Josh Coalson77e3f312001-06-23 03:03:24 +0000742 FLAC__uint32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000743 unsigned i, id;
Josh Coalson77e3f312001-06-23 03:03:24 +0000744 FLAC__bool first = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000745
Josh Coalsonaec256b2002-03-12 16:19:54 +0000746 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000747
748 for(i = id = 0; i < 4; ) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000749 if(decoder->private_->cached) {
750 x = (FLAC__uint32)decoder->private_->lookahead;
751 decoder->private_->cached = false;
Josh Coalson215af572001-03-27 01:15:58 +0000752 }
753 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +0000754 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +0000755 return false; /* the read_callback_ sets the state for us */
756 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000757 if(x == FLAC__STREAM_SYNC_STRING[i]) {
Josh Coalson215af572001-03-27 01:15:58 +0000758 first = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000759 i++;
760 id = 0;
761 continue;
762 }
763 if(x == ID3V2_TAG_[id]) {
764 id++;
765 i = 0;
766 if(id == 3) {
767 if(!stream_decoder_skip_id3v2_tag_(decoder))
768 return false; /* the read_callback_ sets the state for us */
769 }
770 continue;
771 }
772 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000773 decoder->private_->header_warmup[0] = (FLAC__byte)x;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000774 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000775 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +0000776
777 /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
778 /* else we have to check if the second byte is the end of a sync code */
779 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000780 decoder->private_->lookahead = (FLAC__byte)x;
781 decoder->private_->cached = true;
Josh Coalson215af572001-03-27 01:15:58 +0000782 }
783 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000784 decoder->private_->header_warmup[1] = (FLAC__byte)x;
785 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000786 return true;
787 }
788 }
789 i = 0;
790 if(first) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000791 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
Josh Coalson215af572001-03-27 01:15:58 +0000792 first = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000793 }
794 }
795
Josh Coalsonfa697a92001-08-16 20:07:29 +0000796 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000797 return true;
798}
799
Josh Coalson77e3f312001-06-23 03:03:24 +0000800FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000801{
Josh Coalson77e3f312001-06-23 03:03:24 +0000802 FLAC__uint32 i, x, last_block, type, length;
803 FLAC__uint64 xx;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000804
Josh Coalsonaec256b2002-03-12 16:19:54 +0000805 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000806
Josh Coalsonaec256b2002-03-12 16:19:54 +0000807 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000808 return false; /* the read_callback_ sets the state for us */
Josh Coalsonaec256b2002-03-12 16:19:54 +0000809 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000810 return false; /* the read_callback_ sets the state for us */
Josh Coalsonaec256b2002-03-12 16:19:54 +0000811 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000812 return false; /* the read_callback_ sets the state for us */
Josh Coalson72983922001-02-23 21:03:21 +0000813 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
Josh Coalson841e27e2001-01-03 00:26:42 +0000814 unsigned used_bits = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000815 decoder->private_->stream_info.type = type;
816 decoder->private_->stream_info.is_last = last_block;
817 decoder->private_->stream_info.length = length;
Josh Coalson841e27e2001-01-03 00:26:42 +0000818
Josh Coalsonaec256b2002-03-12 16:19:54 +0000819 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000820 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000821 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
Josh Coalson72983922001-02-23 21:03:21 +0000822 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000823
Josh Coalsonaec256b2002-03-12 16:19:54 +0000824 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000825 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000826 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
Josh Coalson72983922001-02-23 21:03:21 +0000827 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000828
Josh Coalsonaec256b2002-03-12 16:19:54 +0000829 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000830 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000831 decoder->private_->stream_info.data.stream_info.min_framesize = x;
Josh Coalson72983922001-02-23 21:03:21 +0000832 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000833
Josh Coalsonaec256b2002-03-12 16:19:54 +0000834 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000835 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000836 decoder->private_->stream_info.data.stream_info.max_framesize = x;
Josh Coalson72983922001-02-23 21:03:21 +0000837 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000838
Josh Coalsonaec256b2002-03-12 16:19:54 +0000839 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000840 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000841 decoder->private_->stream_info.data.stream_info.sample_rate = x;
Josh Coalson72983922001-02-23 21:03:21 +0000842 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000843
Josh Coalsonaec256b2002-03-12 16:19:54 +0000844 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000845 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000846 decoder->private_->stream_info.data.stream_info.channels = x+1;
Josh Coalson72983922001-02-23 21:03:21 +0000847 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000848
Josh Coalsonaec256b2002-03-12 16:19:54 +0000849 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000850 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000851 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
Josh Coalson72983922001-02-23 21:03:21 +0000852 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000853
Josh Coalsonaec256b2002-03-12 16:19:54 +0000854 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000855 return false; /* the read_callback_ sets the state for us */
Josh Coalson72983922001-02-23 21:03:21 +0000856 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
Josh Coalson841e27e2001-01-03 00:26:42 +0000857
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000858 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
859 return false; /* the read_callback_ sets the state for us */
860 used_bits += 16*8;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000861
Josh Coalson841e27e2001-01-03 00:26:42 +0000862 /* skip the rest of the block */
Josh Coalson1b689822001-05-31 20:11:02 +0000863 FLAC__ASSERT(used_bits % 8 == 0);
Josh Coalson841e27e2001-01-03 00:26:42 +0000864 length -= (used_bits / 8);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000865 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
866 return false; /* the read_callback_ sets the state for us */
Josh Coalson841e27e2001-01-03 00:26:42 +0000867
Josh Coalsonfa697a92001-08-16 20:07:29 +0000868 decoder->private_->has_stream_info = true;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000869 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
870 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
Josh Coalsonc076ba52001-04-05 21:32:54 +0000871 }
872 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncff71c82001-04-13 18:20:23 +0000873 unsigned real_points;
874
Josh Coalsonfa697a92001-08-16 20:07:29 +0000875 decoder->private_->seek_table.type = type;
876 decoder->private_->seek_table.is_last = last_block;
877 decoder->private_->seek_table.length = length;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000878
Josh Coalsonf145bbd2002-05-04 17:37:51 +0000879 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000880
Josh Coalsonfa697a92001-08-16 20:07:29 +0000881 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
882 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000883 return false;
884 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000885 for(i = real_points = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +0000886 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
Josh Coalsonc076ba52001-04-05 21:32:54 +0000887 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000888 decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000889
Josh Coalsonaec256b2002-03-12 16:19:54 +0000890 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
Josh Coalsonc076ba52001-04-05 21:32:54 +0000891 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000892 decoder->private_->seek_table.data.seek_table.points[real_points].stream_offset = xx;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000893
Josh Coalsonaec256b2002-03-12 16:19:54 +0000894 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
Josh Coalsonc076ba52001-04-05 21:32:54 +0000895 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000896 decoder->private_->seek_table.data.seek_table.points[real_points].frame_samples = x;
Josh Coalsoncff71c82001-04-13 18:20:23 +0000897
Josh Coalsonfa697a92001-08-16 20:07:29 +0000898 if(decoder->private_->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
Josh Coalsoncff71c82001-04-13 18:20:23 +0000899 real_points++;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000900 }
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000901 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000902 decoder->private_->seek_table.data.seek_table.num_points = real_points;
Josh Coalsonc076ba52001-04-05 21:32:54 +0000903
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000904 /* if there is a partial point left, skip over it */
905 if(length > 0) {
906 /*@@@ do an error_callback() here? there's an argument for either way */
907 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
908 return false; /* the read_callback_ sets the state for us */
909 }
910
Josh Coalsonfa697a92001-08-16 20:07:29 +0000911 decoder->private_->has_seek_table = true;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000912 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
913 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000914 }
915 else {
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000916 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
917 unsigned real_length = length;
918 FLAC__StreamMetaData block;
919
920 block.is_last = last_block;
921 block.type = type;
922 block.length = length;
923
924 if(type == FLAC__METADATA_TYPE_APPLICATION && decoder->private_->metadata_filter_ids_count > 0) {
925 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000926 return false; /* the read_callback_ sets the state for us */
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000927
928 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
929
930 if(stream_decoder_has_id_filtered_(decoder, block.data.application.id))
931 skip_it = !skip_it;
932 }
933
934 if(skip_it) {
935 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
936 return false; /* the read_callback_ sets the state for us */
937 }
938 else {
939 switch(type) {
940 case FLAC__METADATA_TYPE_PADDING:
941 /* skip the padding bytes */
942 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder))
943 return false; /* the read_callback_ sets the state for us */
944 break;
945 case FLAC__METADATA_TYPE_APPLICATION:
946 /* remember, we read the ID already */
947 if(0 == (block.data.application.data = malloc(real_length))) {
948 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
949 return false;
950 }
951 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
952 return false; /* the read_callback_ sets the state for us */
953 break;
954 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
955 /* read vendor string */
956 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, read_callback_, decoder))
957 return false; /* the read_callback_ sets the state for us */
958 if(block.data.vorbis_comment.vendor_string.length > 0) {
959 if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) {
960 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
961 return false;
962 }
963 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.vendor_string.entry, block.data.vorbis_comment.vendor_string.length, read_callback_, decoder))
964 return false; /* the read_callback_ sets the state for us */
965 }
966 else
967 block.data.vorbis_comment.vendor_string.entry = 0;
968
969 /* read num comments */
970 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &block.data.vorbis_comment.num_comments, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN, read_callback_, decoder))
971 return false; /* the read_callback_ sets the state for us */
972
973 /* read comments */
974 if(block.data.vorbis_comment.num_comments > 0) {
975 if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)))) {
976 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
977 return false;
978 }
979 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) {
980 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, read_callback_, decoder))
981 return false; /* the read_callback_ sets the state for us */
982 if(block.data.vorbis_comment.comments[i].length > 0) {
983 if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) {
984 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
985 return false;
986 }
987 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.comments[i].entry, block.data.vorbis_comment.comments[i].length, read_callback_, decoder))
988 return false; /* the read_callback_ sets the state for us */
989 }
990 else
991 block.data.vorbis_comment.comments[i].entry = 0;
992 }
993 }
994 else {
995 block.data.vorbis_comment.comments = 0;
996 }
997 break;
998 case FLAC__METADATA_TYPE_STREAMINFO:
999 case FLAC__METADATA_TYPE_SEEKTABLE:
1000 default:
1001 FLAC__ASSERT(0);
1002 }
1003 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1004
1005 /* now we have to free any malloc'ed data in the block */
1006 switch(type) {
1007 case FLAC__METADATA_TYPE_PADDING:
1008 break;
1009 case FLAC__METADATA_TYPE_APPLICATION:
1010 if(0 != block.data.application.data)
1011 free(block.data.application.data);
1012 break;
1013 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1014 if(0 != block.data.vorbis_comment.vendor_string.entry)
1015 free(block.data.vorbis_comment.vendor_string.entry);
1016 if(block.data.vorbis_comment.num_comments > 0)
1017 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1018 if(0 != block.data.vorbis_comment.comments[i].entry)
1019 free(block.data.vorbis_comment.comments[i].entry);
1020 if(0 != block.data.vorbis_comment.comments)
1021 free(block.data.vorbis_comment.comments);
1022 break;
1023 case FLAC__METADATA_TYPE_STREAMINFO:
1024 case FLAC__METADATA_TYPE_SEEKTABLE:
1025 default:
1026 FLAC__ASSERT(0);
1027 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001028 }
1029 }
1030
1031 if(last_block)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001032 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001033
1034 return true;
1035}
1036
Josh Coalson77e3f312001-06-23 03:03:24 +00001037FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001038{
Josh Coalson77e3f312001-06-23 03:03:24 +00001039 FLAC__uint32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001040 unsigned i, skip;
1041
1042 /* skip the version and flags bytes */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001043 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001044 return false; /* the read_callback_ sets the state for us */
1045 /* get the size (in bytes) to skip */
1046 skip = 0;
1047 for(i = 0; i < 4; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001048 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001049 return false; /* the read_callback_ sets the state for us */
1050 skip <<= 7;
1051 skip |= (x & 0x7f);
1052 }
1053 /* skip the rest of the tag */
Josh Coalsonbf9325f2002-05-07 05:30:27 +00001054 if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder))
1055 return false; /* the read_callback_ sets the state for us */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001056 return true;
1057}
1058
Josh Coalson77e3f312001-06-23 03:03:24 +00001059FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001060{
Josh Coalson77e3f312001-06-23 03:03:24 +00001061 FLAC__uint32 x;
1062 FLAC__bool first = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001063
1064 /* If we know the total number of samples in the stream, stop if we've read that many. */
1065 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001066 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1067 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1068 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001069 return true;
1070 }
1071 }
1072
1073 /* make sure we're byte aligned */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001074 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1075 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001076 return false; /* the read_callback_ sets the state for us */
1077 }
1078
1079 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001080 if(decoder->private_->cached) {
1081 x = (FLAC__uint32)decoder->private_->lookahead;
1082 decoder->private_->cached = false;
Josh Coalson215af572001-03-27 01:15:58 +00001083 }
1084 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001085 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001086 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001087 }
1088 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001089 decoder->private_->header_warmup[0] = (FLAC__byte)x;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001090 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +00001091 return false; /* the read_callback_ sets the state for us */
1092
1093 /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
1094 /* else we have to check if the second byte is the end of a sync code */
1095 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001096 decoder->private_->lookahead = (FLAC__byte)x;
1097 decoder->private_->cached = true;
Josh Coalson215af572001-03-27 01:15:58 +00001098 }
1099 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001100 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1101 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001102 return true;
1103 }
1104 }
1105 if(first) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001106 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
Josh Coalson38c68542002-02-12 22:58:59 +00001107 first = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001108 }
1109 }
1110
1111 return true;
1112}
1113
Josh Coalson77e3f312001-06-23 03:03:24 +00001114FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001115{
1116 unsigned channel;
1117 unsigned i;
Josh Coalson77e3f312001-06-23 03:03:24 +00001118 FLAC__int32 mid, side, left, right;
1119 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1120 FLAC__uint32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001121
1122 *got_a_frame = false;
1123
Josh Coalson215af572001-03-27 01:15:58 +00001124 /* init the CRC */
1125 frame_crc = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001126 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1127 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
Josh Coalsonaec256b2002-03-12 16:19:54 +00001128 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
Josh Coalson215af572001-03-27 01:15:58 +00001129
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001130 if(!stream_decoder_read_frame_header_(decoder))
1131 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001132 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001133 return true;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001134 if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001135 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001136 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001137 /*
1138 * first figure the correct bits-per-sample of the subframe
1139 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001140 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1141 switch(decoder->private_->frame.header.channel_assignment) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001142 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1143 /* no adjustment needed */
1144 break;
1145 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001146 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001147 if(channel == 1)
1148 bps++;
1149 break;
1150 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001151 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001152 if(channel == 0)
1153 bps++;
1154 break;
1155 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001156 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001157 if(channel == 1)
1158 bps++;
1159 break;
1160 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001161 FLAC__ASSERT(0);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001162 }
1163 /*
1164 * now read it
1165 */
1166 if(!stream_decoder_read_subframe_(decoder, channel, bps))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001167 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001168 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1169 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001170 return true;
1171 }
1172 }
1173 if(!stream_decoder_read_zero_padding_(decoder))
1174 return false;
1175
Josh Coalson215af572001-03-27 01:15:58 +00001176 /*
1177 * Read the frame CRC-16 from the footer and check
1178 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001179 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1180 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +00001181 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001182 if(frame_crc == (FLAC__uint16)x) {
Josh Coalson215af572001-03-27 01:15:58 +00001183 /* Undo any special channel coding */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001184 switch(decoder->private_->frame.header.channel_assignment) {
Josh Coalson215af572001-03-27 01:15:58 +00001185 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1186 /* do nothing */
1187 break;
1188 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001189 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1190 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1191 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
Josh Coalson215af572001-03-27 01:15:58 +00001192 break;
1193 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001194 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1195 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1196 decoder->private_->output[0][i] += decoder->private_->output[1][i];
Josh Coalson215af572001-03-27 01:15:58 +00001197 break;
1198 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001199 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1200 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1201 mid = decoder->private_->output[0][i];
1202 side = decoder->private_->output[1][i];
Josh Coalson215af572001-03-27 01:15:58 +00001203 mid <<= 1;
1204 if(side & 1) /* i.e. if 'side' is odd... */
1205 mid++;
1206 left = mid + side;
1207 right = mid - side;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001208 decoder->private_->output[0][i] = left >> 1;
1209 decoder->private_->output[1][i] = right >> 1;
Josh Coalson215af572001-03-27 01:15:58 +00001210 }
1211 break;
1212 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001213 FLAC__ASSERT(0);
Josh Coalson215af572001-03-27 01:15:58 +00001214 break;
1215 }
1216 }
1217 else {
1218 /* Bad frame, emit error and zero the output signal */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001219 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1220 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1221 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
Josh Coalson215af572001-03-27 01:15:58 +00001222 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001223 }
1224
1225 *got_a_frame = true;
1226
1227 /* put the latest values into the public section of the decoder instance */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001228 decoder->protected_->channels = decoder->private_->frame.header.channels;
1229 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1230 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1231 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1232 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001233
Josh Coalsonfa697a92001-08-16 20:07:29 +00001234 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1235 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001236
1237 /* write it */
Josh Coalsoneae4dde2001-04-16 05:33:22 +00001238 /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001239 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001240 return false;
1241
Josh Coalsonfa697a92001-08-16 20:07:29 +00001242 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001243 return true;
1244}
1245
Josh Coalson77e3f312001-06-23 03:03:24 +00001246FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001247{
Josh Coalson77e3f312001-06-23 03:03:24 +00001248 FLAC__uint32 x;
1249 FLAC__uint64 xx;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001250 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
Josh Coalson77e3f312001-06-23 03:03:24 +00001251 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001252 unsigned raw_header_len;
Josh Coalson77e3f312001-06-23 03:03:24 +00001253 FLAC__bool is_unparseable = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001254 const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
1255 const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001256
Josh Coalsonaec256b2002-03-12 16:19:54 +00001257 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001258
Josh Coalson215af572001-03-27 01:15:58 +00001259 /* init the raw header with the saved bits from synchronization */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001260 raw_header[0] = decoder->private_->header_warmup[0];
1261 raw_header[1] = decoder->private_->header_warmup[1];
Josh Coalson215af572001-03-27 01:15:58 +00001262 raw_header_len = 2;
1263
1264 /*
1265 * check to make sure that the reserved bits are 0
1266 */
1267 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1268 is_unparseable = true;
1269 }
1270
1271 /*
1272 * Note that along the way as we read the header, we look for a sync
1273 * code inside. If we find one it would indicate that our original
1274 * sync was bad since there cannot be a sync code in a valid header.
1275 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001276
1277 /*
1278 * read in the raw header as bytes so we can CRC it, and parse it on the way
1279 */
1280 for(i = 0; i < 2; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001281 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001282 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001283 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001284 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001285 decoder->private_->lookahead = (FLAC__byte)x;
1286 decoder->private_->cached = true;
1287 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1288 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson215af572001-03-27 01:15:58 +00001289 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001290 }
Josh Coalson77e3f312001-06-23 03:03:24 +00001291 raw_header[raw_header_len++] = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001292 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001293
Josh Coalson215af572001-03-27 01:15:58 +00001294 switch(x = raw_header[2] >> 4) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001295 case 0:
Josh Coalson7531b002001-04-14 00:24:32 +00001296 if(is_known_fixed_blocksize_stream)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001297 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001298 else
1299 is_unparseable = true;
1300 break;
1301 case 1:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001302 decoder->private_->frame.header.blocksize = 192;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001303 break;
1304 case 2:
1305 case 3:
1306 case 4:
1307 case 5:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001308 decoder->private_->frame.header.blocksize = 576 << (x-2);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001309 break;
1310 case 6:
1311 case 7:
1312 blocksize_hint = x;
1313 break;
Josh Coalson215af572001-03-27 01:15:58 +00001314 case 8:
1315 case 9:
1316 case 10:
1317 case 11:
1318 case 12:
1319 case 13:
1320 case 14:
1321 case 15:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001322 decoder->private_->frame.header.blocksize = 256 << (x-8);
Josh Coalson215af572001-03-27 01:15:58 +00001323 break;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001324 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001325 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001326 break;
1327 }
1328
Josh Coalson215af572001-03-27 01:15:58 +00001329 switch(x = raw_header[2] & 0x0f) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001330 case 0:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001331 if(decoder->private_->has_stream_info)
1332 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001333 else
1334 is_unparseable = true;
1335 break;
1336 case 1:
1337 case 2:
1338 case 3:
1339 is_unparseable = true;
1340 break;
1341 case 4:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001342 decoder->private_->frame.header.sample_rate = 8000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001343 break;
1344 case 5:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001345 decoder->private_->frame.header.sample_rate = 16000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001346 break;
1347 case 6:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001348 decoder->private_->frame.header.sample_rate = 22050;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001349 break;
1350 case 7:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001351 decoder->private_->frame.header.sample_rate = 24000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001352 break;
1353 case 8:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001354 decoder->private_->frame.header.sample_rate = 32000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001355 break;
1356 case 9:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001357 decoder->private_->frame.header.sample_rate = 44100;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001358 break;
1359 case 10:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001360 decoder->private_->frame.header.sample_rate = 48000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001361 break;
1362 case 11:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001363 decoder->private_->frame.header.sample_rate = 96000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001364 break;
1365 case 12:
1366 case 13:
1367 case 14:
1368 sample_rate_hint = x;
1369 break;
1370 case 15:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001371 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1372 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001373 return true;
1374 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001375 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001376 }
1377
Josh Coalson215af572001-03-27 01:15:58 +00001378 x = (unsigned)(raw_header[3] >> 4);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001379 if(x & 8) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001380 decoder->private_->frame.header.channels = 2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001381 switch(x & 7) {
1382 case 0:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001383 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001384 break;
1385 case 1:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001386 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001387 break;
1388 case 2:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001389 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001390 break;
1391 default:
1392 is_unparseable = true;
1393 break;
1394 }
1395 }
1396 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001397 decoder->private_->frame.header.channels = (unsigned)x + 1;
1398 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001399 }
1400
Josh Coalson215af572001-03-27 01:15:58 +00001401 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001402 case 0:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001403 if(decoder->private_->has_stream_info)
1404 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001405 else
1406 is_unparseable = true;
1407 break;
1408 case 1:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001409 decoder->private_->frame.header.bits_per_sample = 8;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001410 break;
1411 case 2:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001412 decoder->private_->frame.header.bits_per_sample = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001413 break;
1414 case 4:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001415 decoder->private_->frame.header.bits_per_sample = 16;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001416 break;
1417 case 5:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001418 decoder->private_->frame.header.bits_per_sample = 20;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001419 break;
1420 case 6:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001421 decoder->private_->frame.header.bits_per_sample = 24;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001422 break;
1423 case 3:
1424 case 7:
1425 is_unparseable = true;
1426 break;
1427 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001428 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001429 break;
1430 }
1431
Josh Coalson215af572001-03-27 01:15:58 +00001432 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001433 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1434 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001435 return true;
1436 }
1437
Josh Coalson7531b002001-04-14 00:24:32 +00001438 if(blocksize_hint && is_known_variable_blocksize_stream) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001439 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001440 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001441 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001442 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1443 decoder->private_->cached = true;
1444 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1445 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson215af572001-03-27 01:15:58 +00001446 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001447 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001448 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1449 decoder->private_->frame.header.number.sample_number = xx;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001450 }
1451 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001452 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001453 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001454 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001455 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1456 decoder->private_->cached = true;
1457 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1458 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson215af572001-03-27 01:15:58 +00001459 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001460 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001461 decoder->private_->last_frame_number = x;
1462 if(decoder->private_->has_stream_info) {
1463 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1464 decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001465 }
1466 else {
1467 is_unparseable = true;
1468 }
1469 }
1470
1471 if(blocksize_hint) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001472 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001473 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001474 raw_header[raw_header_len++] = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001475 if(blocksize_hint == 7) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001476 FLAC__uint32 _x;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001477 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001478 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001479 raw_header[raw_header_len++] = (FLAC__byte)_x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001480 x = (x << 8) | _x;
1481 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001482 decoder->private_->frame.header.blocksize = x+1;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001483 }
1484
1485 if(sample_rate_hint) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001486 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001487 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001488 raw_header[raw_header_len++] = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001489 if(sample_rate_hint != 12) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001490 FLAC__uint32 _x;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001491 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001492 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001493 raw_header[raw_header_len++] = (FLAC__byte)_x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001494 x = (x << 8) | _x;
1495 }
1496 if(sample_rate_hint == 12)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001497 decoder->private_->frame.header.sample_rate = x*1000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001498 else if(sample_rate_hint == 13)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001499 decoder->private_->frame.header.sample_rate = x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001500 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001501 decoder->private_->frame.header.sample_rate = x*10;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001502 }
1503
Josh Coalson215af572001-03-27 01:15:58 +00001504 /* read the CRC-8 byte */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001505 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001506 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001507 crc8 = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001508
Josh Coalson215af572001-03-27 01:15:58 +00001509 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001510 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1511 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001512 return true;
1513 }
1514
1515 if(is_unparseable) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001516 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001517 return false;
1518 }
1519
1520 return true;
1521}
1522
Josh Coalson77e3f312001-06-23 03:03:24 +00001523FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001524{
Josh Coalson77e3f312001-06-23 03:03:24 +00001525 FLAC__uint32 x;
1526 FLAC__bool wasted_bits;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001527
Josh Coalsonaec256b2002-03-12 16:19:54 +00001528 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001529 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001530
1531 wasted_bits = (x & 1);
1532 x &= 0xfe;
1533
1534 if(wasted_bits) {
1535 unsigned u;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001536 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +00001537 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001538 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1539 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
Josh Coalson215af572001-03-27 01:15:58 +00001540 }
1541 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001542 decoder->private_->frame.subframes[channel].wasted_bits = 0;
Josh Coalson215af572001-03-27 01:15:58 +00001543
Josh Coalson859bc542001-03-27 22:22:27 +00001544 /*
1545 * Lots of magic numbers here
1546 */
Josh Coalson215af572001-03-27 01:15:58 +00001547 if(x & 0x80) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001548 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1549 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001550 return true;
1551 }
1552 else if(x == 0) {
Josh Coalson215af572001-03-27 01:15:58 +00001553 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1554 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001555 }
1556 else if(x == 2) {
Josh Coalson215af572001-03-27 01:15:58 +00001557 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1558 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001559 }
1560 else if(x < 16) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001561 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001562 return false;
1563 }
1564 else if(x <= 24) {
Josh Coalson215af572001-03-27 01:15:58 +00001565 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1566 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001567 }
1568 else if(x < 64) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001569 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001570 return false;
1571 }
1572 else {
Josh Coalson215af572001-03-27 01:15:58 +00001573 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1574 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001575 }
Josh Coalson215af572001-03-27 01:15:58 +00001576
1577 if(wasted_bits) {
Josh Coalson859bc542001-03-27 22:22:27 +00001578 unsigned i;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001579 x = decoder->private_->frame.subframes[channel].wasted_bits;
1580 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1581 decoder->private_->output[channel][i] <<= x;
Josh Coalson215af572001-03-27 01:15:58 +00001582 }
Josh Coalson859bc542001-03-27 22:22:27 +00001583
Josh Coalson215af572001-03-27 01:15:58 +00001584 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001585}
1586
Josh Coalson77e3f312001-06-23 03:03:24 +00001587FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001588{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001589 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
Josh Coalson77e3f312001-06-23 03:03:24 +00001590 FLAC__int32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001591 unsigned i;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001592 FLAC__int32 *output = decoder->private_->output[channel];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001593
Josh Coalsonfa697a92001-08-16 20:07:29 +00001594 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
Josh Coalson6dcea512001-01-23 23:07:36 +00001595
Josh Coalsonaec256b2002-03-12 16:19:54 +00001596 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001597 return false; /* the read_callback_ sets the state for us */
1598
Josh Coalson6dcea512001-01-23 23:07:36 +00001599 subframe->value = x;
1600
1601 /* decode the subframe */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001602 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
Josh Coalson6dcea512001-01-23 23:07:36 +00001603 output[i] = x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001604
1605 return true;
1606}
1607
Josh Coalson77e3f312001-06-23 03:03:24 +00001608FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001609{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001610 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
Josh Coalson77e3f312001-06-23 03:03:24 +00001611 FLAC__int32 i32;
1612 FLAC__uint32 u32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001613 unsigned u;
1614
Josh Coalsonfa697a92001-08-16 20:07:29 +00001615 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
Josh Coalson6dcea512001-01-23 23:07:36 +00001616
Josh Coalsonfa697a92001-08-16 20:07:29 +00001617 subframe->residual = decoder->private_->residual[channel];
Josh Coalson6dcea512001-01-23 23:07:36 +00001618 subframe->order = order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001619
1620 /* read warm-up samples */
1621 for(u = 0; u < order; u++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001622 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001623 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001624 subframe->warmup[u] = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001625 }
1626
1627 /* read entropy coding method info */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001628 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001629 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001630 subframe->entropy_coding_method.type = u32;
1631 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001632 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsonaec256b2002-03-12 16:19:54 +00001633 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001634 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001635 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001636 break;
1637 default:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001638 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001639 return false;
1640 }
1641
1642 /* read residual */
Josh Coalson6dcea512001-01-23 23:07:36 +00001643 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001644 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001645 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001646 return false;
1647 break;
1648 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001649 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001650 }
1651
1652 /* decode the subframe */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001653 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1654 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001655
1656 return true;
1657}
1658
Josh Coalson77e3f312001-06-23 03:03:24 +00001659FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001660{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001661 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
Josh Coalson77e3f312001-06-23 03:03:24 +00001662 FLAC__int32 i32;
1663 FLAC__uint32 u32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001664 unsigned u;
1665
Josh Coalsonfa697a92001-08-16 20:07:29 +00001666 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
Josh Coalson6dcea512001-01-23 23:07:36 +00001667
Josh Coalsonfa697a92001-08-16 20:07:29 +00001668 subframe->residual = decoder->private_->residual[channel];
Josh Coalson6dcea512001-01-23 23:07:36 +00001669 subframe->order = order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001670
1671 /* read warm-up samples */
1672 for(u = 0; u < order; u++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001673 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001674 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001675 subframe->warmup[u] = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001676 }
1677
1678 /* read qlp coeff precision */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001679 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001680 return false; /* the read_callback_ sets the state for us */
Josh Coalson3372cc02001-04-13 18:44:46 +00001681 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001682 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1683 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001684 return true;
1685 }
Josh Coalson6dcea512001-01-23 23:07:36 +00001686 subframe->qlp_coeff_precision = u32+1;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001687
1688 /* read qlp shift */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001689 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001690 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001691 subframe->quantization_level = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001692
1693 /* read quantized lp coefficiencts */
1694 for(u = 0; u < order; u++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001695 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001696 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001697 subframe->qlp_coeff[u] = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001698 }
1699
1700 /* read entropy coding method info */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001701 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001702 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001703 subframe->entropy_coding_method.type = u32;
1704 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001705 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsonaec256b2002-03-12 16:19:54 +00001706 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001707 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001708 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001709 break;
1710 default:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001711 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001712 return false;
1713 }
1714
1715 /* read residual */
Josh Coalson6dcea512001-01-23 23:07:36 +00001716 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001717 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001718 if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001719 return false;
1720 break;
1721 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001722 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001723 }
1724
1725 /* decode the subframe */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001726 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
Josh Coalson92d42402001-05-31 20:53:19 +00001727 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001728 decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
Josh Coalson92d42402001-05-31 20:53:19 +00001729 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001730 decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001731
1732 return true;
1733}
1734
Josh Coalson77e3f312001-06-23 03:03:24 +00001735FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001736{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001737 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1738 FLAC__int32 x, *residual = decoder->private_->residual[channel];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001739 unsigned i;
1740
Josh Coalsonfa697a92001-08-16 20:07:29 +00001741 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
Josh Coalson6dcea512001-01-23 23:07:36 +00001742
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001743 subframe->data = residual;
Josh Coalson6dcea512001-01-23 23:07:36 +00001744
Josh Coalsonfa697a92001-08-16 20:07:29 +00001745 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001746 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001747 return false; /* the read_callback_ sets the state for us */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001748 residual[i] = x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001749 }
1750
Josh Coalson6dcea512001-01-23 23:07:36 +00001751 /* decode the subframe */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001752 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
Josh Coalson6dcea512001-01-23 23:07:36 +00001753
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001754 return true;
1755}
1756
Josh Coalson63d99572001-07-12 21:26:57 +00001757FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001758{
Josh Coalson77e3f312001-06-23 03:03:24 +00001759 FLAC__uint32 rice_parameter;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001760 int i;
1761 unsigned partition, sample, u;
Josh Coalson63d99572001-07-12 21:26:57 +00001762 const unsigned partition_order = partitioned_rice->order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001763 const unsigned partitions = 1u << partition_order;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001764 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001765
1766 sample = 0;
1767 for(partition = 0; partition < partitions; partition++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001768 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001769 return false; /* the read_callback_ sets the state for us */
Josh Coalson63d99572001-07-12 21:26:57 +00001770 partitioned_rice->parameters[partition] = rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001771 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001772#ifdef FLAC__SYMMETRIC_RICE
Josh Coalsonb28559a2002-04-25 05:21:09 +00001773 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001774 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
Josh Coalson2051dd42001-04-12 22:22:34 +00001775 return false; /* the read_callback_ sets the state for us */
Josh Coalson2051dd42001-04-12 22:22:34 +00001776 residual[sample] = i;
1777 }
Josh Coalsonb28559a2002-04-25 05:21:09 +00001778#else
1779 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1780 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1781 return false; /* the read_callback_ sets the state for us */
1782 sample += u;
1783#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001784 }
1785 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001786 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
Josh Coalson2051dd42001-04-12 22:22:34 +00001787 return false; /* the read_callback_ sets the state for us */
Josh Coalson63d99572001-07-12 21:26:57 +00001788 partitioned_rice->raw_bits[partition] = rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001789 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001790 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
Josh Coalson2051dd42001-04-12 22:22:34 +00001791 return false; /* the read_callback_ sets the state for us */
1792 residual[sample] = i;
1793 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001794 }
1795 }
1796
1797 return true;
1798}
1799
Josh Coalson77e3f312001-06-23 03:03:24 +00001800FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001801{
Josh Coalsonaec256b2002-03-12 16:19:54 +00001802 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001803 FLAC__uint32 zero = 0;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001804 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001805 return false; /* the read_callback_ sets the state for us */
1806 if(zero != 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001807 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1808 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001809 }
1810 }
1811 return true;
1812}
1813
Josh Coalson77e3f312001-06-23 03:03:24 +00001814FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001815{
1816 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1817 FLAC__StreamDecoderReadStatus status;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001818
Josh Coalsonfa697a92001-08-16 20:07:29 +00001819 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001820 if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001821 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001822 else if(status == FLAC__STREAM_DECODER_READ_ABORT)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001823 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001824 return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1825}