stream_decoder.c 78.9 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001,2002,2003  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
44
#include "private/lpc.h"

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

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

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

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

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

Josh Coalson's avatar
Josh Coalson committed
68
69
70
71
72
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);
73
74
75
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);
76
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
Josh Coalson's avatar
Josh Coalson committed
77
78
79
80
81
82
83
84
85
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
86
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
87
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson's avatar
Josh Coalson committed
88
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
89
90
91
92
93
94
95

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

Josh Coalson's avatar
Josh Coalson committed
96
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
97
98
99
100
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
Josh Coalson's avatar
Josh Coalson committed
101
	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[]);
102
	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[]);
Josh Coalson's avatar
Josh Coalson committed
103
	void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalson's avatar
Josh Coalson committed
104
	void *client_data;
105
	FLAC__BitBuffer *input;
Josh Coalson's avatar
Josh Coalson committed
106
107
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
	FLAC__int32 *residual[FLAC__MAX_CHANNELS];
108
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
109
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
110
111
112
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
113
114
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
115
	FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
116
117
	FLAC__byte *metadata_filter_ids;
	unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
118
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
119
	FLAC__bool cached; /* true if there is a byte in lookahead */
120
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
121
122
	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 */
Josh Coalson's avatar
Josh Coalson committed
123
124
} FLAC__StreamDecoderPrivate;

125
126
127
128
129
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
130

131
FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalson's avatar
Josh Coalson committed
132
133
134
135
136
137
138
139
	"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",
140
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
141
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
142
143
144
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

145
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
146
147
148
	"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
149
150
};

151
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
152
153
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
154
155
};

156
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
157
158
159
	"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
160
161
};

162
163
164
165
166
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
167
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
168
{
169
	FLAC__StreamDecoder *decoder;
170
	unsigned i;
171
172
173

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

174
	decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
175
176
	if(decoder == 0) {
		return 0;
Josh Coalson's avatar
Josh Coalson committed
177
	}
Josh Coalson's avatar
Josh Coalson committed
178

179
	decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
180
	if(decoder->protected_ == 0) {
181
182
183
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
184

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

192
193
194
195
196
197
198
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
199

200
	decoder->private_->metadata_filter_ids_capacity = 16;
Josh Coalson's avatar
Josh Coalson committed
201
	if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
202
203
204
205
206
207
		FLAC__bitbuffer_delete(decoder->private_->input);
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
208

209
210
211
212
213
214
215
216
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		decoder->private_->output[i] = 0;
		decoder->private_->residual[i] = 0;
	}

	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
	decoder->private_->has_seek_table = false;
217
218
219

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

Josh Coalson's avatar
Josh Coalson committed
221
	set_defaults_(decoder);
222

223
224
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
225
226
227
	return decoder;
}

228
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
229
{
230
231
	unsigned i;

Josh Coalson's avatar
Josh Coalson committed
232
233
234
235
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
236

237
238
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
239
	if(0 != decoder->private_->metadata_filter_ids)
240
241
		free(decoder->private_->metadata_filter_ids);

242
	FLAC__bitbuffer_delete(decoder->private_->input);
243

244
245
	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
246

247
248
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
249
250
251
	free(decoder);
}

252
253
254
255
256
257
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

258
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
259
{
Josh Coalson's avatar
Josh Coalson committed
260
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
261

262
263
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
264

265
266
	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
267

268
269
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
270

271
272
273
274
	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
275

276
277
278
	/*
	 * get the CPU info and set the function pointers
	 */
279
	FLAC__cpu_info(&decoder->private_->cpuinfo);
280
	/* first default to the non-asm routines */
281
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
282
	decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
283
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
284
	/* now override with asm where appropriate */
285
#ifndef FLAC__NO_ASM
286
	if(decoder->private_->cpuinfo.use_asm) {
287
#ifdef FLAC__CPU_IA32
288
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
289
#ifdef FLAC__HAS_NASM
290
291
292
		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;
293
294
		}
		else {
295
296
			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;
297
		}
298
#endif
299
#endif
300
	}
301
302
#endif

Josh Coalson's avatar
Josh Coalson committed
303
304
305
306
	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
307
308
}

309
FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
310
311
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
312
	FLAC__ASSERT(0 != decoder);
313
	if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson's avatar
Josh Coalson committed
314
		return;
315
	if(0 != decoder->private_->seek_table.data.seek_table.points) {
316
317
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
318
		decoder->private_->has_seek_table = false;
319
	}
320
	FLAC__bitbuffer_free(decoder->private_->input);
321
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
322
323
324
325
326
327
		/* 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
328
		if(0 != decoder->private_->output[i]) {
329
330
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
331
		}
Josh Coalson's avatar
Josh Coalson committed
332
		if(0 != decoder->private_->residual[i]) {
333
334
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
335
336
		}
	}
337
338
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
339

Josh Coalson's avatar
Josh Coalson committed
340
	set_defaults_(decoder);
341

342
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
343
344
}

345
FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
346
{
Josh Coalson's avatar
Josh Coalson committed
347
348
349
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
350
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
351
		return false;
352
	decoder->private_->read_callback = value;
353
354
355
	return true;
}

356
FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
357
{
Josh Coalson's avatar
Josh Coalson committed
358
359
360
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
361
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
362
		return false;
363
	decoder->private_->write_callback = value;
364
365
366
	return true;
}

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

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

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

400
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
401
{
Josh Coalson's avatar
Josh Coalson committed
402
403
404
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
405
	FLAC__ASSERT((unsigned)type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
406
	/* double protection */
Josh Coalson's avatar
Josh Coalson committed
407
	if((unsigned)type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
408
		return false;
409
410
411
412
413
414
415
416
	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;
}

417
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
418
{
Josh Coalson's avatar
Josh Coalson committed
419
420
421
422
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
423
424
425
426
427
428
429
430
431
	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
432
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
433
434
435
436
437
438
439
440
441
442
			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;
}

443
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
444
{
445
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
446
447
448
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
449
450
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
451
452
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
453
454
455
456
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

457
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
458
{
Josh Coalson's avatar
Josh Coalson committed
459
460
461
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
462
	FLAC__ASSERT((unsigned)type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
463
	/* double protection */
Josh Coalson's avatar
Josh Coalson committed
464
	if((unsigned)type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
465
		return false;
466
467
468
469
470
471
472
473
	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;
}

474
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
475
{
Josh Coalson's avatar
Josh Coalson committed
476
477
478
479
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
480
481
482
483
484
485
486
487
488
	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
489
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
490
491
492
493
494
495
496
497
498
499
			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;
}

500
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
501
{
Josh Coalson's avatar
Josh Coalson committed
502
503
504
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
505
506
507
508
509
510
511
	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;
}

512
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
513
{
Josh Coalson's avatar
Josh Coalson committed
514
515
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
516
	return decoder->protected_->state;
517
518
}

519
FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
520
{
Josh Coalson's avatar
Josh Coalson committed
521
522
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
523
	return decoder->protected_->channels;
524
525
}

526
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
527
{
Josh Coalson's avatar
Josh Coalson committed
528
529
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
530
	return decoder->protected_->channel_assignment;
531
532
}

533
FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
534
{
Josh Coalson's avatar
Josh Coalson committed
535
536
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
537
	return decoder->protected_->bits_per_sample;
538
539
}

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

547
FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
548
{
Josh Coalson's avatar
Josh Coalson committed
549
550
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
551
	return decoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
552
553
}

554
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
555
{
Josh Coalson's avatar
Josh Coalson committed
556
557
558
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
559

560
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
561
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
562
563
		return false;
	}
564
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
565
566
567
568

	return true;
}

569
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
570
{
Josh Coalson's avatar
Josh Coalson committed
571
572
573
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
574
575

	if(!FLAC__stream_decoder_flush(decoder)) {
576
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
577
578
		return false;
	}
579
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
580

581
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
582

Josh Coalson's avatar
Josh Coalson committed
583
584
585
	return true;
}

586
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
587
{
Josh Coalson's avatar
Josh Coalson committed
588
	FLAC__bool got_a_frame;
Josh Coalson's avatar
Josh Coalson committed
589
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
590
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
591
592

	while(1) {
593
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
594
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
Josh Coalson's avatar
Josh Coalson committed
595
				if(!find_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
596
597
598
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
Josh Coalson's avatar
Josh Coalson committed
599
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
600
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
601
602
				else
					return true;
Josh Coalson's avatar
Josh Coalson committed
603
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson's avatar
Josh Coalson committed
604
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
605
606
607
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
608
				if(!read_frame_(decoder, &got_a_frame))
Josh Coalson's avatar
Josh Coalson committed
609
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
610
611
				if(got_a_frame)
					return true; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
612
613
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
614
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
615
616
				return true;
			default:
617
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
618
				return false;
Josh Coalson's avatar
Josh Coalson committed
619
620
621
622
		}
	}
}

623
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
624
{
Josh Coalson's avatar
Josh Coalson committed
625
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
626
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
627
628

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

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

	while(1) {
657
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
658
659
660
			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
661
				break;
Josh Coalson's avatar
Josh Coalson committed
662
663
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
664
665
666
					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
667
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
668
669
670
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
671
				if(!read_frame_(decoder, &dummy))
Josh Coalson's avatar
Josh Coalson committed
672
673
674
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
675
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
676
677
				return true;
			default:
678
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
679
				return false;
Josh Coalson's avatar
Josh Coalson committed
680
681
682
683
		}
	}
}

684
685
686
687
688
689
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

690
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
691
{
Josh Coalson's avatar
Josh Coalson committed
692
	FLAC__ASSERT(0 != decoder);
693
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
694
695
}

696
697
698
699
700
701
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
702
void set_defaults_(FLAC__StreamDecoder *decoder)
703
704
705
706
707
708
709
710
711
712
713
714
{
	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
715
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
716
717
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
718
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
719

720
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
721
722
		return true;

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

Josh Coalson's avatar
Josh Coalson committed
725
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
726
		if(0 != decoder->private_->output[i]) {
727
			free(decoder->private_->output[i]-4);
728
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
729
		}
Josh Coalson's avatar
Josh Coalson committed
730
		if(0 != decoder->private_->residual[i]) {
731
732
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
733
		}
Josh Coalson's avatar
Josh Coalson committed
734
735
	}

736
	for(i = 0; i < channels; i++) {
737
738
739
740
741
742
		/* 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.
		 */
743
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalson's avatar
Josh Coalson committed
744
		if(tmp == 0) {
745
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
746
747
			return false;
		}
748
		memset(tmp, 0, sizeof(FLAC__int32)*4);
749
		decoder->private_->output[i] = tmp + 4;
750

Josh Coalson's avatar
Josh Coalson committed
751
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
752
		if(tmp == 0) {
753
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
754
755
			return false;
		}
756
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
757
758
	}

759
760
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
761
762
763
764

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
765
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
766
767
768
769
770
771
772
773
774
775
776
777
778
{
	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
779
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
780
{
Josh Coalson's avatar
Josh Coalson committed
781
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
782
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
783
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
784

785
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
786
787

	for(i = id = 0; i < 4; ) {
788
789
790
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
791
792
		}
		else {
793
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
794
795
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
796
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
797
			first = true;
Josh Coalson's avatar
Josh Coalson committed
798
799
800
801
802
803
804
805
			i++;
			id = 0;
			continue;
		}
		if(x == ID3V2_TAG_[id]) {
			id++;
			i = 0;
			if(id == 3) {
Josh Coalson's avatar
Josh Coalson committed
806
				if(!skip_id3v2_tag_(decoder))
Josh Coalson's avatar
Josh Coalson committed
807
808
809
810
811
					return false; /* the read_callback_ sets the state for us */
			}
			continue;
		}
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
812
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
813
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
814
				return false; /* the read_callback_ sets the state for us */
815
816
817
818

			/* 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 */
819
820
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
821
822
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
823
824
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
825
826
827
828
829
				return true;
			}
		}
		i = 0;
		if(first) {
830
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
831
			first = false;
Josh Coalson's avatar
Josh Coalson committed
832
833
834
		}
	}

835
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
836
837
838
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
839
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
840
{
841
842
	FLAC__bool is_last;
	FLAC__uint32 i, x, type, length;
Josh Coalson's avatar
Josh Coalson committed
843

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

846
	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
847
		return false; /* the read_callback_ sets the state for us */
848
849
	is_last = x? true : false;

850
	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
851
		return false; /* the read_callback_ sets the state for us */
852

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

856
857
858
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
		if(!read_metadata_streaminfo_(decoder, is_last, length))
			return false;
859

860
		decoder->private_->has_stream_info = true;
861
862
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
863
864
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
865
		if(!read_metadata_seektable_(decoder, is_last, length))
866
			return false;
867

868
		decoder->private_->has_seek_table = true;
869
870
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
			decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
Josh Coalson's avatar
Josh Coalson committed
871
872
	}
	else {
873
874
		FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
		unsigned real_length = length;
875
		FLAC__StreamMetadata block;
876

877
		block.is_last = is_last;
Josh Coalson's avatar
Josh Coalson committed
878
		block.type = (FLAC__MetadataType)type;
879
880
		block.length = length;

881
		if(type == FLAC__METADATA_TYPE_APPLICATION) {
882
			if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
883
884
885
886
				return false; /* the read_callback_ sets the state for us */

			real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;

Josh Coalson's avatar
Josh Coalson committed
887
			if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
888
889
890
891
				skip_it = !skip_it;
		}

		if(skip_it) {
892
			if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
893
894
				return false; /* the read_callback_ sets the state for us */
		}
895
896
897
898
		else {
			switch(type) {
				case FLAC__METADATA_TYPE_PADDING:
					/* skip the padding bytes */
899
					if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input