stream_decoder.c 77.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  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h> /* for malloc() */
Josh Coalson's avatar
Josh Coalson committed
22
#include <string.h> /* for memset/memcpy() */
23
#include "FLAC/assert.h"
24
#include "protected/stream_decoder.h"
Josh Coalson's avatar
Josh Coalson committed
25
#include "private/bitbuffer.h"
Josh Coalson's avatar
Josh Coalson committed
26
#include "private/bitmath.h"
27
#include "private/cpu.h"
Josh Coalson's avatar
Josh Coalson committed
28
29
#include "private/crc.h"
#include "private/fixed.h"
30
#include "private/format.h"
Josh Coalson's avatar
Josh Coalson committed
31
32
#include "private/lpc.h"

Josh Coalson's avatar
Josh Coalson committed
33
34
35
36
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

37
38
39
40
41
#ifdef max
#undef max
#endif
#define max(a,b) ((a)>(b)?(a):(b))

42
43
44
45
46
47
/***********************************************************************
 *
 * Private static data
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
48
static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
49
50
51
52
53
54
55

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

Josh Coalson's avatar
Josh Coalson committed
56
57
58
59
60
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);
61
62
63
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);
64
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
Josh Coalson's avatar
Josh Coalson committed
65
66
67
68
69
70
71
72
73
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);
74
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
75
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson's avatar
Josh Coalson committed
76
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
77
78
79
80
81
82
83

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

Josh Coalson's avatar
Josh Coalson committed
84
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
85
86
87
88
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
Josh Coalson's avatar
Josh Coalson committed
89
	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[]);
90
	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
91
	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
92
	void *client_data;
93
	FLAC__BitBuffer *input;
Josh Coalson's avatar
Josh Coalson committed
94
95
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
	FLAC__int32 *residual[FLAC__MAX_CHANNELS];
96
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
97
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
98
99
100
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
101
102
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
103
	FLAC__bool metadata_filter[FLAC__METADATA_TYPE_UNDEFINED];
104
105
	FLAC__byte *metadata_filter_ids;
	unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
106
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
107
	FLAC__bool cached; /* true if there is a byte in lookahead */
108
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
109
110
	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
111
112
} FLAC__StreamDecoderPrivate;

113
114
115
116
117
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
118

119
FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalson's avatar
Josh Coalson committed
120
121
122
123
124
125
126
127
	"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",
128
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
129
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
130
131
132
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

133
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
134
135
136
	"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
137
138
};

139
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
140
141
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
142
143
};

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

150
151
152
153
154
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
155
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
156
{
157
	FLAC__StreamDecoder *decoder;
158
	unsigned i;
159
160
161

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

162
	decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
163
164
	if(decoder == 0) {
		return 0;
Josh Coalson's avatar
Josh Coalson committed
165
	}
Josh Coalson's avatar
Josh Coalson committed
166

167
	decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
168
	if(decoder->protected_ == 0) {
169
170
171
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
172

173
	decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
174
175
	if(decoder->private_ == 0) {
		free(decoder->protected_);
176
177
178
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
179

180
181
182
183
184
185
186
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
187

188
	decoder->private_->metadata_filter_ids_capacity = 16;
Josh Coalson's avatar
Josh Coalson committed
189
	if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
190
191
192
193
194
195
		FLAC__bitbuffer_delete(decoder->private_->input);
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
196

197
198
199
200
201
202
203
204
	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;
205
206
207

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

Josh Coalson's avatar
Josh Coalson committed
209
	set_defaults_(decoder);
210

211
212
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
213
214
215
	return decoder;
}

216
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
217
{
218
219
	unsigned i;

Josh Coalson's avatar
Josh Coalson committed
220
221
222
223
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
224

225
226
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
227
	if(0 != decoder->private_->metadata_filter_ids)
228
229
		free(decoder->private_->metadata_filter_ids);

230
	FLAC__bitbuffer_delete(decoder->private_->input);
231

232
233
	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
234

235
236
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
237
238
239
	free(decoder);
}

240
241
242
243
244
245
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

246
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
247
{
Josh Coalson's avatar
Josh Coalson committed
248
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
249

250
251
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
252

253
254
	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
255

256
257
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
258

259
260
261
262
	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
263

264
265
266
	/*
	 * get the CPU info and set the function pointers
	 */
267
	FLAC__cpu_info(&decoder->private_->cpuinfo);
268
	/* first default to the non-asm routines */
269
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
270
	decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
271
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
272
	/* now override with asm where appropriate */
273
#ifndef FLAC__NO_ASM
274
	if(decoder->private_->cpuinfo.use_asm) {
275
#ifdef FLAC__CPU_IA32
276
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
277
#ifdef FLAC__HAS_NASM
278
279
280
		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;
281
282
		}
		else {
283
284
			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;
285
		}
286
#endif
287
#endif
288
	}
289
290
#endif

Josh Coalson's avatar
Josh Coalson committed
291
292
293
294
	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
295
296
}

297
FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
298
299
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
300
	FLAC__ASSERT(0 != decoder);
301
	if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson's avatar
Josh Coalson committed
302
		return;
303
	if(0 != decoder->private_->seek_table.data.seek_table.points) {
304
305
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
306
		decoder->private_->has_seek_table = false;
307
	}
308
	FLAC__bitbuffer_free(decoder->private_->input);
309
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
310
311
312
313
314
315
		/* 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
316
		if(0 != decoder->private_->output[i]) {
317
318
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
319
		}
Josh Coalson's avatar
Josh Coalson committed
320
		if(0 != decoder->private_->residual[i]) {
321
322
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
323
324
		}
	}
325
326
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
327

Josh Coalson's avatar
Josh Coalson committed
328
	set_defaults_(decoder);
329

330
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
331
332
}

333
FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
334
{
Josh Coalson's avatar
Josh Coalson committed
335
336
337
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
338
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
339
		return false;
340
	decoder->private_->read_callback = value;
341
342
343
	return true;
}

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

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

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

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

388
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
389
{
Josh Coalson's avatar
Josh Coalson committed
390
391
392
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
393
	FLAC__ASSERT(type < FLAC__METADATA_TYPE_UNDEFINED);
394
395
396
397
398
399
400
401
	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;
}

402
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
403
{
Josh Coalson's avatar
Josh Coalson committed
404
405
406
407
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
408
409
410
411
412
413
414
415
416
	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
417
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
418
419
420
421
422
423
424
425
426
427
			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;
}

428
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
429
{
430
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
431
432
433
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
434
435
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
436
437
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
438
439
440
441
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

442
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
443
{
Josh Coalson's avatar
Josh Coalson committed
444
445
446
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
447
	FLAC__ASSERT(type < FLAC__METADATA_TYPE_UNDEFINED);
448
449
450
451
452
453
454
455
	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;
}

456
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
457
{
Josh Coalson's avatar
Josh Coalson committed
458
459
460
461
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
462
463
464
465
466
467
468
469
470
	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
471
		if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
472
473
474
475
476
477
478
479
480
481
			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;
}

482
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
483
{
Josh Coalson's avatar
Josh Coalson committed
484
485
486
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
487
488
489
490
491
492
493
	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;
}

494
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
495
{
Josh Coalson's avatar
Josh Coalson committed
496
497
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
498
	return decoder->protected_->state;
499
500
}

501
FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
502
{
Josh Coalson's avatar
Josh Coalson committed
503
504
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
505
	return decoder->protected_->channels;
506
507
}

508
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
509
{
Josh Coalson's avatar
Josh Coalson committed
510
511
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
512
	return decoder->protected_->channel_assignment;
513
514
}

515
FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
516
{
Josh Coalson's avatar
Josh Coalson committed
517
518
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
519
	return decoder->protected_->bits_per_sample;
520
521
}

522
FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
523
{
Josh Coalson's avatar
Josh Coalson committed
524
525
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
526
	return decoder->protected_->sample_rate;
527
528
}

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

536
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
537
{
Josh Coalson's avatar
Josh Coalson committed
538
539
540
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
541

542
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
543
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
544
545
		return false;
	}
546
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
547
548
549
550

	return true;
}

551
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
552
{
Josh Coalson's avatar
Josh Coalson committed
553
554
555
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
556
557

	if(!FLAC__stream_decoder_flush(decoder)) {
558
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
559
560
		return false;
	}
561
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
562

563
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
564

Josh Coalson's avatar
Josh Coalson committed
565
566
567
	return true;
}

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

	while(1) {
575
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
576
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
Josh Coalson's avatar
Josh Coalson committed
577
				if(!find_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
578
579
580
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
Josh Coalson's avatar
Josh Coalson committed
581
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
582
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
583
584
				else
					return true;
Josh Coalson's avatar
Josh Coalson committed
585
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson's avatar
Josh Coalson committed
586
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
587
588
589
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
590
				if(!read_frame_(decoder, &got_a_frame))
Josh Coalson's avatar
Josh Coalson committed
591
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
592
593
				if(got_a_frame)
					return true; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
594
595
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
596
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
597
598
				return true;
			default:
599
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
600
				return false;
Josh Coalson's avatar
Josh Coalson committed
601
602
603
604
		}
	}
}

605
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
606
{
Josh Coalson's avatar
Josh Coalson committed
607
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
608
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
609
610

	while(1) {
611
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
612
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
Josh Coalson's avatar
Josh Coalson committed
613
				if(!find_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
614
615
616
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
Josh Coalson's avatar
Josh Coalson committed
617
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
618
619
620
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
621
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
622
			case FLAC__STREAM_DECODER_END_OF_STREAM:
623
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
624
625
				return true;
			default:
626
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
627
				return false;
Josh Coalson's avatar
Josh Coalson committed
628
629
630
631
		}
	}
}

632
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
633
{
Josh Coalson's avatar
Josh Coalson committed
634
	FLAC__bool dummy;
Josh Coalson's avatar
Josh Coalson committed
635
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
636
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
637
638

	while(1) {
639
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
640
641
642
			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
643
				break;
Josh Coalson's avatar
Josh Coalson committed
644
645
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
646
647
648
					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
649
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
650
651
652
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
653
				if(!read_frame_(decoder, &dummy))
Josh Coalson's avatar
Josh Coalson committed
654
655
656
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
657
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
658
659
				return true;
			default:
660
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
661
				return false;
Josh Coalson's avatar
Josh Coalson committed
662
663
664
665
		}
	}
}

666
667
668
669
670
671
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

672
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
673
{
Josh Coalson's avatar
Josh Coalson committed
674
	FLAC__ASSERT(0 != decoder);
675
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
676
677
}

678
679
680
681
682
683
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
684
void set_defaults_(FLAC__StreamDecoder *decoder)
685
686
687
688
689
690
691
692
693
694
695
696
{
	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
697
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
698
699
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
700
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
701

702
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
703
704
		return true;

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

Josh Coalson's avatar
Josh Coalson committed
707
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
708
		if(0 != decoder->private_->output[i]) {
709
			free(decoder->private_->output[i]-4);
710
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
711
		}
Josh Coalson's avatar
Josh Coalson committed
712
		if(0 != decoder->private_->residual[i]) {
713
714
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
715
		}
Josh Coalson's avatar
Josh Coalson committed
716
717
	}

718
	for(i = 0; i < channels; i++) {
719
720
721
722
723
724
		/* 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.
		 */
725
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalson's avatar
Josh Coalson committed
726
		if(tmp == 0) {
727
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
728
729
			return false;
		}
730
		memset(tmp, 0, sizeof(FLAC__int32)*4);
731
		decoder->private_->output[i] = tmp + 4;
732

Josh Coalson's avatar
Josh Coalson committed
733
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
734
		if(tmp == 0) {
735
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
736
737
			return false;
		}
738
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
739
740
	}

741
742
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
743
744
745
746

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
747
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
748
749
750
751
752
753
754
755
756
757
758
759
760
{
	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
761
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
762
{
Josh Coalson's avatar
Josh Coalson committed
763
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
764
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
765
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
766

767
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
768
769

	for(i = id = 0; i < 4; ) {
770
771
772
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
773
774
		}
		else {
775
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
776
777
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
778
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
779
			first = true;
Josh Coalson's avatar
Josh Coalson committed
780
781
782
783
784
785
786
787
			i++;
			id = 0;
			continue;
		}
		if(x == ID3V2_TAG_[id]) {
			id++;
			i = 0;
			if(id == 3) {
Josh Coalson's avatar
Josh Coalson committed
788
				if(!skip_id3v2_tag_(decoder))
Josh Coalson's avatar
Josh Coalson committed
789
790
791
792
793
					return false; /* the read_callback_ sets the state for us */
			}
			continue;
		}
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
794
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
795
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
796
				return false; /* the read_callback_ sets the state for us */
797
798
799
800

			/* 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 */
801
802
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
803
804
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
805
806
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
807
808
809
810
811
				return true;
			}
		}
		i = 0;
		if(first) {
812
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
813
			first = false;
Josh Coalson's avatar
Josh Coalson committed
814
815
816
		}
	}

817
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
818
819
820
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
821
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
822
{
823
824
	FLAC__bool is_last;
	FLAC__uint32 i, x, type, length;
Josh Coalson's avatar
Josh Coalson committed
825

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

828
	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
829
		return false; /* the read_callback_ sets the state for us */
830
831
	is_last = x? true : false;

832
	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
833
		return false; /* the read_callback_ sets the state for us */
834

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

838
839
840
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
		if(!read_metadata_streaminfo_(decoder, is_last, length))
			return false;
841

842
		decoder->private_->has_stream_info = true;
843
844
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
845
846
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
847
		if(!read_metadata_seektable_(decoder, is_last, length))
848
			return false;
849

850
		decoder->private_->has_seek_table = true;
851
852
		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
853
854
	}
	else {
855
856
		FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
		unsigned real_length = length;
857
		FLAC__StreamMetadata block;
858

859
		block.is_last = is_last;
Josh Coalson's avatar
Josh Coalson committed
860
		block.type = (FLAC__MetadataType)type;
861
862
		block.length = length;

863
		if(type == FLAC__METADATA_TYPE_APPLICATION) {
864
			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))
865
866
867
868
				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
869
			if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
870
871
872
873
				skip_it = !skip_it;
		}

		if(skip_it) {
874
			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
875
876
				return false; /* the read_callback_ sets the state for us */
		}
877
878
879
880
		else {
			switch(type) {
				case FLAC__METADATA_TYPE_PADDING:
					/* skip the padding bytes */
881
					if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
882
883
884
885
						return false; /* the read_callback_ sets the state for us */
					break;
				case FLAC__METADATA_TYPE_APPLICATION:
					/* remember, we read the ID already */
886
					if(real_length > 0) {
Josh Coalson's avatar
Josh Coalson committed
887
						if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
888
889
890
							decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
							return false;
						}
891
						if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
892
							return false; /* the read_callback_ sets the state for us */
893
					}
894
895
					else
						block.data.application.data = 0;
896
897
					break;
				case FLAC__METADATA_TYPE_VORBIS_COMMENT: