stream_decoder.c 82.8 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
 *
4
5
6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
7
 *
8
9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
10
 *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
30
31
32
33
 */

#include <stdio.h>
#include <stdlib.h> /* for malloc() */
Josh Coalson's avatar
Josh Coalson committed
34
#include <string.h> /* for memset/memcpy() */
35
#include "FLAC/assert.h"
36
#include "protected/stream_decoder.h"
Josh Coalson's avatar
Josh Coalson committed
37
#include "private/bitbuffer.h"
Josh Coalson's avatar
Josh Coalson committed
38
#include "private/bitmath.h"
39
#include "private/cpu.h"
Josh Coalson's avatar
Josh Coalson committed
40
41
#include "private/crc.h"
#include "private/fixed.h"
42
#include "private/format.h"
Josh Coalson's avatar
Josh Coalson committed
43
#include "private/lpc.h"
44
#include "private/memory.h"
Josh Coalson's avatar
Josh Coalson committed
45

Josh Coalson's avatar
Josh Coalson committed
46
47
48
49
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

50
51
52
53
54
#ifdef max
#undef max
#endif
#define max(a,b) ((a)>(b)?(a):(b))

55
56
57
58
59
60
/***********************************************************************
 *
 * Private static data
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
61
static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
62
63
64
65
66
67
68

/***********************************************************************
 *
 * Private class method prototypes
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
69
70
71
72
73
static void set_defaults_(FLAC__StreamDecoder *decoder);
static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
74
75
76
static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
77
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
Josh Coalson's avatar
Josh Coalson committed
78
79
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
80
static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
Josh Coalson's avatar
Josh Coalson committed
81
static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
82
83
84
85
86
static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
87
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
Josh Coalson's avatar
Josh Coalson committed
88
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson's avatar
Josh Coalson committed
89
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
90
91
92
93
94
95
96

/***********************************************************************
 *
 * Private class data
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
97
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
98
99
100
101
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
102
	/* generic 32-bit datapath: */
Josh Coalson's avatar
Josh Coalson committed
103
	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[]);
104
	/* generic 64-bit datapath: */
105
	void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
106
	/* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
Josh Coalson's avatar
Josh Coalson committed
107
	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[]);
108
109
	/* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
	void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalson's avatar
Josh Coalson committed
110
	void *client_data;
111
	FLAC__BitBuffer *input;
Josh Coalson's avatar
Josh Coalson committed
112
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
113
	FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
114
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
115
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
116
117
118
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
119
120
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
121
	FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
122
123
	FLAC__byte *metadata_filter_ids;
	unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
124
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
125
	FLAC__bool cached; /* true if there is a byte in lookahead */
126
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
127
128
	FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
	FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
129
130
	/* unaligned (original) pointers to allocated data */
	FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
Josh Coalson's avatar
Josh Coalson committed
131
132
} FLAC__StreamDecoderPrivate;

133
134
135
136
137
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
138

139
FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalson's avatar
Josh Coalson committed
140
141
142
143
144
145
146
147
	"FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
	"FLAC__STREAM_DECODER_READ_METADATA",
	"FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
	"FLAC__STREAM_DECODER_READ_FRAME",
	"FLAC__STREAM_DECODER_END_OF_STREAM",
	"FLAC__STREAM_DECODER_ABORTED",
	"FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
	"FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
148
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
149
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
150
151
152
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

153
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
154
155
156
	"FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
	"FLAC__STREAM_DECODER_READ_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
157
158
};

159
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
160
161
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
162
163
};

164
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
165
166
167
	"FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
	"FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
	"FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
Josh Coalson's avatar
Josh Coalson committed
168
169
};

170
171
172
173
174
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
175
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
176
{
177
	FLAC__StreamDecoder *decoder;
178
	unsigned i;
179
180
181

	FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */

182
	decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
183
184
	if(decoder == 0) {
		return 0;
Josh Coalson's avatar
Josh Coalson committed
185
	}
Josh Coalson's avatar
Josh Coalson committed
186

187
	decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
188
	if(decoder->protected_ == 0) {
189
190
191
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
192

193
	decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
194
195
	if(decoder->private_ == 0) {
		free(decoder->protected_);
196
197
198
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
199

200
201
202
203
204
205
206
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
207

208
	decoder->private_->metadata_filter_ids_capacity = 16;
Josh Coalson's avatar
Josh Coalson committed
209
	if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
210
211
212
213
214
215
		FLAC__bitbuffer_delete(decoder->private_->input);
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
216

217
218
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		decoder->private_->output[i] = 0;
219
		decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
220
221
222
223
224
	}

	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
	decoder->private_->has_seek_table = false;
225
226
227

	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
228

Josh Coalson's avatar
Josh Coalson committed
229
	set_defaults_(decoder);
230

231
232
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
233
234
235
	return decoder;
}

236
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
237
{
238
239
	unsigned i;

Josh Coalson's avatar
Josh Coalson committed
240
241
242
243
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
244

245
246
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
247
	if(0 != decoder->private_->metadata_filter_ids)
248
249
		free(decoder->private_->metadata_filter_ids);

250
	FLAC__bitbuffer_delete(decoder->private_->input);
251

252
253
	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
254

255
256
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
257
258
259
	free(decoder);
}

260
261
262
263
264
265
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

266
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
267
{
Josh Coalson's avatar
Josh Coalson committed
268
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
269

270
271
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
272

273
274
	if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
		return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
Josh Coalson's avatar
Josh Coalson committed
275

276
277
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
278

279
280
281
282
	decoder->private_->last_frame_number = 0;
	decoder->private_->samples_decoded = 0;
	decoder->private_->has_stream_info = false;
	decoder->private_->cached = false;
Josh Coalson's avatar
Josh Coalson committed
283

284
285
286
	/*
	 * get the CPU info and set the function pointers
	 */
287
	FLAC__cpu_info(&decoder->private_->cpuinfo);
288
	/* first default to the non-asm routines */
289
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
290
	decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
291
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
292
	decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
293
	/* now override with asm where appropriate */
294
#ifndef FLAC__NO_ASM
295
	if(decoder->private_->cpuinfo.use_asm) {
296
#ifdef FLAC__CPU_IA32
297
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
298
#ifdef FLAC__HAS_NASM
299
300
301
		if(decoder->private_->cpuinfo.data.ia32.mmx) {
			decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
			decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
302
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
303
304
		}
		else {
305
306
			decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
			decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
307
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
308
		}
309
#endif
310
311
312
313
314
315
#elif defined FLAC__CPU_PPC
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
		if(decoder->private_->cpuinfo.data.ppc.altivec) {
			decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
		}
316
#endif
317
	}
318
319
#endif

Josh Coalson's avatar
Josh Coalson committed
320
321
322
323
	if(!FLAC__stream_decoder_reset(decoder))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;

	return decoder->protected_->state;
Josh Coalson's avatar
Josh Coalson committed
324
325
}

326
FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
327
328
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
329
	FLAC__ASSERT(0 != decoder);
330
	if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson's avatar
Josh Coalson committed
331
		return;
332
	if(0 != decoder->private_->seek_table.data.seek_table.points) {
333
334
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
335
		decoder->private_->has_seek_table = false;
336
	}
337
	FLAC__bitbuffer_free(decoder->private_->input);
338
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
339
340
341
342
343
344
		/* 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 Coalson's avatar
Josh Coalson committed
345
		if(0 != decoder->private_->output[i]) {
346
347
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
348
		}
349
350
351
		if(0 != decoder->private_->residual_unaligned[i]) {
			free(decoder->private_->residual_unaligned[i]);
			decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
352
353
		}
	}
354
355
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
356

Josh Coalson's avatar
Josh Coalson committed
357
	set_defaults_(decoder);
358

359
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
360
361
}

362
FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
363
{
Josh Coalson's avatar
Josh Coalson committed
364
365
366
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
367
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
368
		return false;
369
	decoder->private_->read_callback = value;
370
371
372
	return true;
}

373
FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
374
{
Josh Coalson's avatar
Josh Coalson committed
375
376
377
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
378
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
379
		return false;
380
	decoder->private_->write_callback = value;
381
382
383
	return true;
}

384
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
385
{
Josh Coalson's avatar
Josh Coalson committed
386
387
388
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
389
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
390
		return false;
391
	decoder->private_->metadata_callback = value;
392
393
394
	return true;
}

395
FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
396
{
Josh Coalson's avatar
Josh Coalson committed
397
398
399
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
400
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
401
		return false;
402
	decoder->private_->error_callback = value;
403
404
405
	return true;
}

406
FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
407
{
Josh Coalson's avatar
Josh Coalson committed
408
409
410
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
411
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
412
		return false;
413
	decoder->private_->client_data = value;
414
415
416
	return true;
}

417
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
418
{
Josh Coalson's avatar
Josh Coalson committed
419
420
421
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
422
	FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
423
	/* double protection */
424
	if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
425
		return false;
426
427
428
429
430
431
432
433
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private_->metadata_filter[type] = true;
	if(type == FLAC__METADATA_TYPE_APPLICATION)
		decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

434
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
435
{
Josh Coalson's avatar
Josh Coalson committed
436
437
438
439
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
440
441
442
443
444
445
446
447
448
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;

	if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
		return true;

	FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);

	if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
Josh Coalson's avatar
Josh Coalson committed
449
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
450
451
452
453
454
455
456
457
458
459
			return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		decoder->private_->metadata_filter_ids_capacity *= 2;
	}

	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));
	decoder->private_->metadata_filter_ids_count++;

	return true;
}

460
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
461
{
462
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
463
464
465
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
466
467
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
468
469
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
470
471
472
473
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

474
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
475
{
Josh Coalson's avatar
Josh Coalson committed
476
477
478
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
479
	FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
480
	/* double protection */
481
	if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
482
		return false;
483
484
485
486
487
488
489
490
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private_->metadata_filter[type] = false;
	if(type == FLAC__METADATA_TYPE_APPLICATION)
		decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

491
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
492
{
Josh Coalson's avatar
Josh Coalson committed
493
494
495
496
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
497
498
499
500
501
502
503
504
505
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;

	if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
		return true;

	FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);

	if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
Josh Coalson's avatar
Josh Coalson committed
506
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
507
508
509
510
511
512
513
514
515
516
			return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		decoder->private_->metadata_filter_ids_capacity *= 2;
	}

	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));
	decoder->private_->metadata_filter_ids_count++;

	return true;
}

517
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
518
{
Josh Coalson's avatar
Josh Coalson committed
519
520
521
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
522
523
524
525
526
527
528
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

529
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
530
{
Josh Coalson's avatar
Josh Coalson committed
531
532
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
533
	return decoder->protected_->state;
534
535
}

Josh Coalson's avatar
Josh Coalson committed
536
537
538
539
540
FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
{
	return FLAC__StreamDecoderStateString[decoder->protected_->state];
}

541
FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
542
{
Josh Coalson's avatar
Josh Coalson committed
543
544
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
545
	return decoder->protected_->channels;
546
547
}

548
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
549
{
Josh Coalson's avatar
Josh Coalson committed
550
551
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
552
	return decoder->protected_->channel_assignment;
553
554
}

555
FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
556
{
Josh Coalson's avatar
Josh Coalson committed
557
558
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
559
	return decoder->protected_->bits_per_sample;
560
561
}

562
FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
563
{
Josh Coalson's avatar
Josh Coalson committed
564
565
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
566
	return decoder->protected_->sample_rate;
567
568
}

569
FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
570
{
Josh Coalson's avatar
Josh Coalson committed
571
572
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
573
	return decoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
574
575
}

576
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
577
{
Josh Coalson's avatar
Josh Coalson committed
578
579
580
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
581

582
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
583
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
584
585
		return false;
	}
586
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
587
588
589
590

	return true;
}

591
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
592
{
Josh Coalson's avatar
Josh Coalson committed
593
594
595
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
596
597

	if(!FLAC__stream_decoder_flush(decoder)) {
598
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
599
600
		return false;
	}
601
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
602

603
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
604

Josh Coalson's avatar
Josh Coalson committed
605
606
607
	return true;
}

608
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
609
{
Josh Coalson's avatar
Josh Coalson committed
610
	FLAC__bool got_a_frame;
Josh Coalson's avatar
Josh Coalson committed
611
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
612
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
613
614

	while(1) {
615
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
616
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
Josh Coalson's avatar
Josh Coalson committed
617
				if(!find_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
618
619
620
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
Josh Coalson's avatar
Josh Coalson committed
621
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
622
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
623
624
				else
					return true;
Josh Coalson's avatar
Josh Coalson committed
625
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson's avatar
Josh Coalson committed
626
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
627
628
629
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
630
				if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
Josh Coalson's avatar
Josh Coalson committed
631
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
632
633
				if(got_a_frame)
					return true; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
634
635
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
636
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
637
638
				return true;
			default:
639
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
640
				return false;
Josh Coalson's avatar
Josh Coalson committed
641
642
643
644
		}
	}
}

645
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
646
{
Josh Coalson's avatar
Josh Coalson committed
647
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
648
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
649
650

	while(1) {
651
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
652
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
Josh Coalson's avatar
Josh Coalson committed
653
				if(!find_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
654
655
656
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
Josh Coalson's avatar
Josh Coalson committed
657
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
658
659
660
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
661
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
662
			case FLAC__STREAM_DECODER_END_OF_STREAM:
663
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
664
665
				return true;
			default:
666
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
667
				return false;
Josh Coalson's avatar
Josh Coalson committed
668
669
670
671
		}
	}
}

672
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
673
{
Josh Coalson's avatar
Josh Coalson committed
674
	FLAC__bool dummy;
Josh Coalson's avatar
Josh Coalson committed
675
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
676
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
677
678

	while(1) {
679
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
680
681
682
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
				if(!find_metadata_(decoder))
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
683
				break;
Josh Coalson's avatar
Josh Coalson committed
684
685
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
686
687
688
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson's avatar
Josh Coalson committed
689
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
690
691
692
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
693
				if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
Josh Coalson's avatar
Josh Coalson committed
694
695
696
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
697
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
698
699
				return true;
			default:
700
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
701
				return false;
Josh Coalson's avatar
Josh Coalson committed
702
703
704
705
		}
	}
}

706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
{
	FLAC__bool got_a_frame;
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);

	while(1) {
		switch(decoder->protected_->state) {
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
			case FLAC__STREAM_DECODER_READ_METADATA:
				return false; /* above function sets the status for us */
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
					return false; /* above function sets the status for us */
				if(got_a_frame)
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
			case FLAC__STREAM_DECODER_ABORTED:
				return true;
			default:
				FLAC__ASSERT(0);
				return false;
		}
	}
}

737
738
739
740
741
742
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

743
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
744
{
Josh Coalson's avatar
Josh Coalson committed
745
	FLAC__ASSERT(0 != decoder);
746
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
747
748
}

749
750
751
752
753
754
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
755
void set_defaults_(FLAC__StreamDecoder *decoder)
756
757
758
759
760
761
762
763
764
765
766
767
{
	decoder->private_->read_callback = 0;
	decoder->private_->write_callback = 0;
	decoder->private_->metadata_callback = 0;
	decoder->private_->error_callback = 0;
	decoder->private_->client_data = 0;

	memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
	decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
	decoder->private_->metadata_filter_ids_count = 0;
}

Josh Coalson's avatar
Josh Coalson committed
768
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
769
770
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
771
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
772

773
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
774
775
		return true;

Josh Coalson's avatar
Josh Coalson committed
776
	/* simply using realloc() is not practical because the number of channels may change mid-stream */
777

Josh Coalson's avatar
Josh Coalson committed
778
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
779
		if(0 != decoder->private_->output[i]) {
780
			free(decoder->private_->output[i]-4);
781
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
782
		}
783
784
785
		if(0 != decoder->private_->residual_unaligned[i]) {
			free(decoder->private_->residual_unaligned[i]);
			decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
786
		}
Josh Coalson's avatar
Josh Coalson committed
787
788
	}

789
	for(i = 0; i < channels; i++) {
790
791
792
793
794
795
		/* 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.
		 */
796
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalson's avatar
Josh Coalson committed
797
		if(tmp == 0) {
798
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
799
800
			return false;
		}
801
		memset(tmp, 0, sizeof(FLAC__int32)*4);
802
		decoder->private_->output[i] = tmp + 4;
803

804
805
806
807
		/* WATCHOUT:
		 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
		 */
		if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
808
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
809
810
			return false;
		}
Josh Coalson's avatar
Josh Coalson committed
811
812
	}

813
814
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
815
816
817
818

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
819
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
820
821
822
823
824
825
826
827
828
829
830
831
832
{
	unsigned i;

	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);

	for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
		if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
			return true;

	return false;
}

Josh Coalson's avatar
Josh Coalson committed
833
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
834
{
Josh Coalson's avatar
Josh Coalson committed
835
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
836
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
837
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
838

839
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
840
841

	for(i = id = 0; i < 4; ) {
842
843
844
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
845
846
		}
		else {
847
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
848
849
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
850
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
851
			first = true;
Josh Coalson's avatar
Josh Coalson committed
852
853
854
855
856
857
858
859
			i++;
			id = 0;
			continue;
		}
		if(x == ID3V2_TAG_[id]) {
			id++;
			i = 0;
			if(id == 3) {
Josh Coalson's avatar
Josh Coalson committed
860
				if(!skip_id3v2_tag_(decoder))
Josh Coalson's avatar
Josh Coalson committed
861
862
863
864
865
					return false; /* the read_callback_ sets the state for us */
			}
			continue;
		}
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
866
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
867
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
868
				return false; /* the read_callback_ sets the state for us */
869
870
871
872

			/* 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 */
			/* else we have to check if the second byte is the end of a sync code */
			if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
873
874
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
875
876
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
877
878
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
879
880
881
882
883
				return true;
			}
		}
		i = 0;
		if(first) {
884
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
885
			first = false;
Josh Coalson's avatar
Josh Coalson committed
886
887
888
		}
	}

889
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
890
891
892
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
893
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
894
{
895
896
	FLAC__bool is_last;
	FLAC__uint32 i, x, type, length;
Josh Coalson's avatar
Josh Coalson committed
897

898
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
899

900
	if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
901
		return false; /* the read_callback_ sets the state for us */
902
903
	is_last = x? true : false;

904
	if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
905
		return false; /* the read_callback_ sets the state for us */
906

907
	if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
908
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
909

910
911
912
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
		if(!read_metadata_streaminfo_(decoder, is_last, length))
			return false;
913

914
		decoder->private_->has_stream_info = true;
915
916
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);