stream_decoder.c 83.7 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
61
/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif

62
63
64
65
66
67
/***********************************************************************
 *
 * Private static data
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
68
static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
69
70
71
72
73
74
75

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

Josh Coalson's avatar
Josh Coalson committed
76
77
78
79
80
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);
81
82
83
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);
84
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
Josh Coalson's avatar
Josh Coalson committed
85
86
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
87
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
88
static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
89
90
91
92
93
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);
94
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
95
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson's avatar
Josh Coalson committed
96
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
97
98
99
100
101
102
103

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

Josh Coalson's avatar
Josh Coalson committed
104
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
105
106
107
108
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
109
	/* generic 32-bit datapath: */
Josh Coalson's avatar
Josh Coalson committed
110
	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[]);
111
	/* generic 64-bit datapath: */
112
	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[]);
113
	/* 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
114
	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[]);
115
116
	/* 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
117
	void *client_data;
118
	FLAC__BitBuffer *input;
Josh Coalson's avatar
Josh Coalson committed
119
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
120
	FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
121
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
122
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
123
124
125
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
126
127
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
128
	FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
129
130
	FLAC__byte *metadata_filter_ids;
	unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
131
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
132
	FLAC__bool cached; /* true if there is a byte in lookahead */
133
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
134
135
	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 */
136
137
	/* unaligned (original) pointers to allocated data */
	FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
Josh Coalson's avatar
Josh Coalson committed
138
139
} FLAC__StreamDecoderPrivate;

140
141
142
143
144
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
145

146
FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalson's avatar
Josh Coalson committed
147
148
149
150
151
152
153
154
	"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",
155
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
156
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
157
158
159
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

160
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
161
162
163
	"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
164
165
};

166
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
167
168
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
169
170
};

171
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
172
173
174
	"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
175
176
};

177
178
179
180
181
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
182
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
183
{
184
	FLAC__StreamDecoder *decoder;
185
	unsigned i;
186
187
188

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

189
	decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
190
191
	if(decoder == 0) {
		return 0;
Josh Coalson's avatar
Josh Coalson committed
192
	}
Josh Coalson's avatar
Josh Coalson committed
193

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

200
	decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
201
202
	if(decoder->private_ == 0) {
		free(decoder->protected_);
203
204
205
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
206

207
208
209
210
211
212
213
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
214

215
	decoder->private_->metadata_filter_ids_capacity = 16;
Josh Coalson's avatar
Josh Coalson committed
216
	if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
217
218
219
220
221
222
		FLAC__bitbuffer_delete(decoder->private_->input);
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
223

224
225
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		decoder->private_->output[i] = 0;
226
		decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
227
228
229
230
231
	}

	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
	decoder->private_->has_seek_table = false;
232
233
234

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

Josh Coalson's avatar
Josh Coalson committed
236
	set_defaults_(decoder);
237

238
239
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
240
241
242
	return decoder;
}

243
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
244
{
245
246
	unsigned i;

Josh Coalson's avatar
Josh Coalson committed
247
248
249
250
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
251

252
253
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
254
	if(0 != decoder->private_->metadata_filter_ids)
255
256
		free(decoder->private_->metadata_filter_ids);

257
	FLAC__bitbuffer_delete(decoder->private_->input);
258

259
260
	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
261

262
263
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
264
265
266
	free(decoder);
}

267
268
269
270
271
272
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

273
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
274
{
Josh Coalson's avatar
Josh Coalson committed
275
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
276

277
278
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
279

280
281
	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
282

283
284
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
285

286
287
288
289
	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
290

291
292
293
	/*
	 * get the CPU info and set the function pointers
	 */
294
	FLAC__cpu_info(&decoder->private_->cpuinfo);
295
	/* first default to the non-asm routines */
296
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
297
	decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
298
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
299
	decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
300
	/* now override with asm where appropriate */
301
#ifndef FLAC__NO_ASM
302
	if(decoder->private_->cpuinfo.use_asm) {
303
#ifdef FLAC__CPU_IA32
304
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
305
#ifdef FLAC__HAS_NASM
306
307
308
		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;
309
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
310
311
		}
		else {
312
313
			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;
314
			decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
315
		}
316
#endif
317
318
319
320
321
322
#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;
		}
323
#endif
324
	}
325
326
#endif

Josh Coalson's avatar
Josh Coalson committed
327
328
329
330
	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
331
332
}

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

Josh Coalson's avatar
Josh Coalson committed
364
	set_defaults_(decoder);
365

366
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
367
368
}

369
FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
370
{
Josh Coalson's avatar
Josh Coalson committed
371
372
373
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
374
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
375
		return false;
376
	decoder->private_->read_callback = value;
377
378
379
	return true;
}

380
FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
381
{
Josh Coalson's avatar
Josh Coalson committed
382
383
384
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
385
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
386
		return false;
387
	decoder->private_->write_callback = value;
388
389
390
	return true;
}

391
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
392
{
Josh Coalson's avatar
Josh Coalson committed
393
394
395
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
396
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
397
		return false;
398
	decoder->private_->metadata_callback = value;
399
400
401
	return true;
}

402
FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
403
{
Josh Coalson's avatar
Josh Coalson committed
404
405
406
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
407
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
408
		return false;
409
	decoder->private_->error_callback = value;
410
411
412
	return true;
}

413
FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
414
{
Josh Coalson's avatar
Josh Coalson committed
415
416
417
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
418
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
419
		return false;
420
	decoder->private_->client_data = value;
421
422
423
	return true;
}

424
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
425
{
Josh Coalson's avatar
Josh Coalson committed
426
427
428
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
429
	FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
430
	/* double protection */
431
	if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
432
		return false;
433
434
435
436
437
438
439
440
	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;
}

441
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
442
{
Josh Coalson's avatar
Josh Coalson committed
443
444
445
446
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
447
448
449
450
451
452
453
454
455
	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
456
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
457
458
459
460
461
462
463
464
465
466
			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;
}

467
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
468
{
469
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
470
471
472
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
473
474
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
475
476
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
477
478
479
480
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

481
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
482
{
Josh Coalson's avatar
Josh Coalson committed
483
484
485
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
486
	FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
487
	/* double protection */
488
	if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
489
		return false;
490
491
492
493
494
495
496
497
	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;
}

498
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
499
{
Josh Coalson's avatar
Josh Coalson committed
500
501
502
503
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
504
505
506
507
508
509
510
511
512
	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
513
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
514
515
516
517
518
519
520
521
522
523
			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;
}

524
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
525
{
Josh Coalson's avatar
Josh Coalson committed
526
527
528
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
529
530
531
532
533
534
535
	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;
}

536
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
537
{
Josh Coalson's avatar
Josh Coalson committed
538
539
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
540
	return decoder->protected_->state;
541
542
}

Josh Coalson's avatar
Josh Coalson committed
543
544
545
546
547
FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
{
	return FLAC__StreamDecoderStateString[decoder->protected_->state];
}

548
FLAC_API unsigned FLAC__stream_decoder_get_channels(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_->channels;
553
554
}

555
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(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_->channel_assignment;
560
561
}

562
FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(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_->bits_per_sample;
567
568
}

569
FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(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_->sample_rate;
574
575
}

576
FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
577
{
Josh Coalson's avatar
Josh Coalson committed
578
579
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
580
	return decoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
581
582
}

583
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
584
{
Josh Coalson's avatar
Josh Coalson committed
585
586
587
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
588

589
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
590
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
591
592
		return false;
	}
593
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
594
595
596
597

	return true;
}

598
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
599
{
Josh Coalson's avatar
Josh Coalson committed
600
601
602
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
603
604

	if(!FLAC__stream_decoder_flush(decoder)) {
605
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
606
607
		return false;
	}
608
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
609

610
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
611

Josh Coalson's avatar
Josh Coalson committed
612
613
614
	return true;
}

615
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
616
{
Josh Coalson's avatar
Josh Coalson committed
617
	FLAC__bool got_a_frame;
Josh Coalson's avatar
Josh Coalson committed
618
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
619
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
620
621

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

652
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
653
{
Josh Coalson's avatar
Josh Coalson committed
654
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
655
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
656
657

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

679
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
680
{
Josh Coalson's avatar
Josh Coalson committed
681
	FLAC__bool dummy;
Josh Coalson's avatar
Josh Coalson committed
682
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
683
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
684
685

	while(1) {
686
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
687
688
689
			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
690
				break;
Josh Coalson's avatar
Josh Coalson committed
691
692
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
693
694
695
					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
696
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
697
698
699
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
700
				if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
Josh Coalson's avatar
Josh Coalson committed
701
702
703
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
704
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
705
706
				return true;
			default:
707
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
708
				return false;
Josh Coalson's avatar
Josh Coalson committed
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
737
738
739
740
741
742
743
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;
		}
	}
}

744
745
746
747
748
749
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

750
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
751
{
Josh Coalson's avatar
Josh Coalson committed
752
	FLAC__ASSERT(0 != decoder);
753
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
754
755
}

756
757
758
759
760
761
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
762
void set_defaults_(FLAC__StreamDecoder *decoder)
763
764
765
766
767
768
769
770
771
772
773
774
{
	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
775
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
776
777
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
778
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
779

780
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
781
782
		return true;

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

Josh Coalson's avatar
Josh Coalson committed
785
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
786
		if(0 != decoder->private_->output[i]) {
787
			free(decoder->private_->output[i]-4);
788
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
789
		}
790
791
792
		if(0 != decoder->private_->residual_unaligned[i]) {
			free(decoder->private_->residual_unaligned[i]);
			decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
793
		}
Josh Coalson's avatar
Josh Coalson committed
794
795
	}

796
	for(i = 0; i < channels; i++) {
797
798
799
800
801
802
		/* 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.
		 */
803
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalson's avatar
Josh Coalson committed
804
		if(tmp == 0) {
805
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
806
807
			return false;
		}
808
		memset(tmp, 0, sizeof(FLAC__int32)*4);
809
		decoder->private_->output[i] = tmp + 4;
810

811
812
813
814
		/* 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])) {
815
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
816
817
			return false;
		}
Josh Coalson's avatar
Josh Coalson committed
818
819
	}

820
821
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
822
823
824
825

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
826
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
827
828
829
830
831
832
833
834
835
836
837
838
839
{
	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
840
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
841
{
Josh Coalson's avatar
Josh Coalson committed
842
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
843
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
844
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
845

846
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
847
848

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

			/* 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 */
880
881
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
882
883
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
884
885
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
886
887
888
889
890
				return true;
			}
		}
		i = 0;
		if(first) {
891
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
892
			first = false;
Josh Coalson's avatar
Josh Coalson committed
893
894
895
		}
	}

896
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
897
898
899
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
900
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
901
{
902
903
	FLAC__bool is_last;
	FLAC__uint32 i, x, type, length;
Josh Coalson's avatar
Josh Coalson committed
904

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

907
	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
908
		return false; /* the read_callback_ sets the state for us */
909
910
	is_last = x? true : false;

911
	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
912
		return false; /* the read_callback_ sets the state for us */
913

914
	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
915
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
916

917
918
919
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
		if(!read_metadata_streaminfo_(decoder, is_last, length))
			return false;
920

921
		decoder->private_->has_stream_info = true;