stream_decoder.c 79.3 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
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
}

Josh Coalson's avatar
Josh Coalson committed
519
520
521
522
523
FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
{
	return FLAC__StreamDecoderStateString[decoder->protected_->state];
}

524
FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
525
{
Josh Coalson's avatar
Josh Coalson committed
526
527
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
528
	return decoder->protected_->channels;
529
530
}

531
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
532
{
Josh Coalson's avatar
Josh Coalson committed
533
534
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
535
	return decoder->protected_->channel_assignment;
536
537
}

538
FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
539
{
Josh Coalson's avatar
Josh Coalson committed
540
541
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
542
	return decoder->protected_->bits_per_sample;
543
544
}

545
FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
546
{
Josh Coalson's avatar
Josh Coalson committed
547
548
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
549
	return decoder->protected_->sample_rate;
550
551
}

552
FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
553
{
Josh Coalson's avatar
Josh Coalson committed
554
555
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
556
	return decoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
557
558
}

559
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
560
{
Josh Coalson's avatar
Josh Coalson committed
561
562
563
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
564

565
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
566
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
567
568
		return false;
	}
569
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
570
571
572
573

	return true;
}

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

	if(!FLAC__stream_decoder_flush(decoder)) {
581
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
582
583
		return false;
	}
584
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
585

586
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
587

Josh Coalson's avatar
Josh Coalson committed
588
589
590
	return true;
}

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

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

628
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
629
{
Josh Coalson's avatar
Josh Coalson committed
630
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
631
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
632
633

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

655
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
656
{
Josh Coalson's avatar
Josh Coalson committed
657
	FLAC__bool dummy;
Josh Coalson's avatar
Josh Coalson committed
658
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
659
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
660
661

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

689
690
691
692
693
694
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

695
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
696
{
Josh Coalson's avatar
Josh Coalson committed
697
	FLAC__ASSERT(0 != decoder);
698
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
699
700
}

701
702
703
704
705
706
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

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

725
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
726
727
		return true;

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

Josh Coalson's avatar
Josh Coalson committed
730
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
731
		if(0 != decoder->private_->output[i]) {
732
			free(decoder->private_->output[i]-4);
733
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
734
		}
Josh Coalson's avatar
Josh Coalson committed
735
		if(0 != decoder->private_->residual[i]) {
736
737
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
738
		}
Josh Coalson's avatar
Josh Coalson committed
739
740
	}

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

Josh Coalson's avatar
Josh Coalson committed
756
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
757
		if(tmp == 0) {
758
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
759
760
			return false;
		}
761
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
762
763
	}

764
765
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
766
767
768
769

	return true;
}

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

790
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
791
792

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

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

840
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
841
842
843
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
844
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
845
{
846
847
	FLAC__bool is_last;
	FLAC__uint32 i, x, type, length;
Josh Coalson's avatar
Josh Coalson committed
848

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

851
	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
852
		return false; /* the read_callback_ sets the state for us */
853
854
	is_last = x? true : false;

855
	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
856
		return false; /* the read_callback_ sets the state for us */
857

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

861
862
863
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
		if(!read_metadata_streaminfo_(decoder, is_last, length))
			return false;
864

865
		decoder->private_->has_stream_info = true;
866
867
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
868
869
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
870
		if(!read_metadata_seektable_(decoder, is_last, length))
871
			return false;
872

873
		decoder->private_->has_seek_table = true;
874
875
		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
876
877
	}
	else {
878
879
		FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
		unsigned real_length = length;
880
		FLAC__StreamMetadata block;
881

882
		block.is_last = is_last;
Josh Coalson's avatar
Josh Coalson committed
883
		block.type = (FLAC__MetadataType)type;
884
885
		block.length = length;

886
		if(type == FLAC__METADATA_TYPE_APPLICATION) {
887
			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))
888
889
890
891
				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
892
			if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
893
894
895
896
				skip_it = !skip_it;
		}

		if(skip_it) {
897
			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
898
899
				return false; /* the read_callback_ sets the state for us */
		}