stream_decoder.c 68.9 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2000,2001,2002  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"
26
#include "private/cpu.h"
Josh Coalson's avatar
Josh Coalson committed
27
28
29
30
#include "private/crc.h"
#include "private/fixed.h"
#include "private/lpc.h"

31
32
33
34
35
36
/***********************************************************************
 *
 * Private static data
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
37
static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
38
39
40
41
42
43
44

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

Josh Coalson's avatar
Josh Coalson committed
45
46
47
48
49
50
51
52
53
54
55
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);
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);
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson's avatar
Josh Coalson committed
61
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
62
63
64
65
66
67
68

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

Josh Coalson's avatar
Josh Coalson committed
69
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
70
71
72
73
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
Josh Coalson's avatar
Josh Coalson committed
74
75
	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[]);
	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
76
	void *client_data;
77
	FLAC__BitBuffer *input;
Josh Coalson's avatar
Josh Coalson committed
78
79
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
	FLAC__int32 *residual[FLAC__MAX_CHANNELS];
80
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
81
82
83
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
84
85
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
86
87
88
	FLAC__bool metadata_filter[FLAC__METADATA_TYPE_VORBIS_COMMENT+1];
	FLAC__byte *metadata_filter_ids;
	unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
89
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
90
	FLAC__bool cached; /* true if there is a byte in lookahead */
91
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
92
93
	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
94
95
} FLAC__StreamDecoderPrivate;

96
97
98
99
100
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
101

102
const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalson's avatar
Josh Coalson committed
103
104
105
106
107
108
109
110
	"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",
111
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
112
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
113
114
115
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

116
const char * const FLAC__StreamDecoderReadStatusString[] = {
117
118
119
	"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
120
121
};

122
const char * const FLAC__StreamDecoderWriteStatusString[] = {
123
124
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
125
126
};

127
const char * const FLAC__StreamDecoderErrorStatusString[] = {
128
129
130
	"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
131
132
};

133
134
135
136
137
138
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
139
{
140
	FLAC__StreamDecoder *decoder;
141
	unsigned i;
142
143
144
145
146
147

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

	decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
	if(decoder == 0) {
		return 0;
Josh Coalson's avatar
Josh Coalson committed
148
	}
149
150
	decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
	if(decoder->protected_ == 0) {
151
152
153
		free(decoder);
		return 0;
	}
154
155
156
	decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
	if(decoder->private_ == 0) {
		free(decoder->protected_);
157
158
159
		free(decoder);
		return 0;
	}
160
161
162
163
164
165
166
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
167

168
169
170
171
172
173
174
175
	decoder->private_->metadata_filter_ids_capacity = 16;
	if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
		FLAC__bitbuffer_delete(decoder->private_->input);
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
176

177
178
179
180
181
182
183
184
185
	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;

Josh Coalson's avatar
Josh Coalson committed
186
	set_defaults_(decoder);
187

188
189
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
190
191
192
	return decoder;
}

193
void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
194
{
Josh Coalson's avatar
Josh Coalson committed
195
196
197
198
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
199

200
201
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
202
	if(0 != decoder->private_->metadata_filter_ids)
203
204
		free(decoder->private_->metadata_filter_ids);

205
	FLAC__bitbuffer_delete(decoder->private_->input);
206
207
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
208
209
210
	free(decoder);
}

211
212
213
214
215
216
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

217
FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
218
{
Josh Coalson's avatar
Josh Coalson committed
219
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
220

221
222
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
223

224
225
	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
226

227
228
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
229

230
231
232
233
	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
234

235
236
237
	/*
	 * get the CPU info and set the function pointers
	 */
238
	FLAC__cpu_info(&decoder->private_->cpuinfo);
239
	/* first default to the non-asm routines */
240
241
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
242
	/* now override with asm where appropriate */
243
#ifndef FLAC__NO_ASM
244
	if(decoder->private_->cpuinfo.use_asm) {
245
#ifdef FLAC__CPU_IA32
246
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
247
#ifdef FLAC__HAS_NASM
248
249
250
		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;
251
252
		}
		else {
253
254
			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;
255
		}
256
#endif
257
#endif
258
	}
259
260
#endif

Josh Coalson's avatar
Josh Coalson committed
261
262
263
264
	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
265
266
267
268
269
}

void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
270
	FLAC__ASSERT(0 != decoder);
271
	if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson's avatar
Josh Coalson committed
272
		return;
273
	if(decoder->private_->has_seek_table) {
Josh Coalson's avatar
Josh Coalson committed
274
		FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
275
276
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
277
		decoder->private_->has_seek_table = false;
278
	}
279
	FLAC__bitbuffer_free(decoder->private_->input);
280
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
281
		/* 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
282
		if(0 != decoder->private_->output[i]) {
283
284
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
285
		}
Josh Coalson's avatar
Josh Coalson committed
286
		if(0 != decoder->private_->residual[i]) {
287
288
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
289
290
		}
	}
291
292
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
293

Josh Coalson's avatar
Josh Coalson committed
294
	set_defaults_(decoder);
295

296
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
297
298
}

Josh Coalson's avatar
Josh Coalson committed
299
FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
300
{
Josh Coalson's avatar
Josh Coalson committed
301
302
303
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
304
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
305
		return false;
306
	decoder->private_->read_callback = value;
307
308
309
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
310
FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
311
{
Josh Coalson's avatar
Josh Coalson committed
312
313
314
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
315
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
316
		return false;
317
	decoder->private_->write_callback = value;
318
319
320
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
321
FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
322
{
Josh Coalson's avatar
Josh Coalson committed
323
324
325
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
326
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
327
		return false;
328
	decoder->private_->metadata_callback = value;
329
330
331
	return true;
}

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

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

354
FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
355
{
Josh Coalson's avatar
Josh Coalson committed
356
357
358
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
359
360
361
362
363
364
365
366
367
	FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
	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;
}

Josh Coalson's avatar
Josh Coalson committed
368
FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
369
{
Josh Coalson's avatar
Josh Coalson committed
370
371
372
373
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
	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) {
		if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
			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;
}

Josh Coalson's avatar
Josh Coalson committed
394
FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
395
{
396
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
397
398
399
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
400
401
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
402
403
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
404
405
406
407
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

408
FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
409
{
Josh Coalson's avatar
Josh Coalson committed
410
411
412
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
413
414
415
416
417
418
419
420
421
	FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
	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;
}

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

Josh Coalson's avatar
Josh Coalson committed
448
FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
449
{
Josh Coalson's avatar
Josh Coalson committed
450
451
452
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
453
454
455
456
457
458
459
	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;
}

460
FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
461
{
Josh Coalson's avatar
Josh Coalson committed
462
463
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
464
	return decoder->protected_->state;
465
466
}

467
unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
468
{
Josh Coalson's avatar
Josh Coalson committed
469
470
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
471
	return decoder->protected_->channels;
472
473
}

474
FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
475
{
Josh Coalson's avatar
Josh Coalson committed
476
477
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
478
	return decoder->protected_->channel_assignment;
479
480
}

481
unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
482
{
Josh Coalson's avatar
Josh Coalson committed
483
484
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
485
	return decoder->protected_->bits_per_sample;
486
487
}

488
unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
489
{
Josh Coalson's avatar
Josh Coalson committed
490
491
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
492
	return decoder->protected_->sample_rate;
493
494
}

495
unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
496
{
Josh Coalson's avatar
Josh Coalson committed
497
498
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
499
	return decoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
500
501
}

Josh Coalson's avatar
Josh Coalson committed
502
FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
503
{
Josh Coalson's avatar
Josh Coalson committed
504
505
506
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
507

508
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
509
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
510
511
		return false;
	}
512
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
513
514
515
516

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
517
FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
518
{
Josh Coalson's avatar
Josh Coalson committed
519
520
521
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
522
523

	if(!FLAC__stream_decoder_flush(decoder)) {
524
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
525
526
		return false;
	}
527
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
528

529
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
530

Josh Coalson's avatar
Josh Coalson committed
531
532
533
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
534
FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
535
{
Josh Coalson's avatar
Josh Coalson committed
536
	FLAC__bool got_a_frame;
Josh Coalson's avatar
Josh Coalson committed
537
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
538
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
539
540

	while(1) {
541
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
542
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
Josh Coalson's avatar
Josh Coalson committed
543
				if(!find_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
544
545
546
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
Josh Coalson's avatar
Josh Coalson committed
547
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
548
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
549
550
				else
					return true;
Josh Coalson's avatar
Josh Coalson committed
551
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson's avatar
Josh Coalson committed
552
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
553
554
555
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
556
				if(!read_frame_(decoder, &got_a_frame))
Josh Coalson's avatar
Josh Coalson committed
557
					return false; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
558
559
				if(got_a_frame)
					return true; /* above function sets the status for us */
Josh Coalson's avatar
Josh Coalson committed
560
561
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
562
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
563
564
				return true;
			default:
565
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
566
				return false;
Josh Coalson's avatar
Josh Coalson committed
567
568
569
570
		}
	}
}

Josh Coalson's avatar
Josh Coalson committed
571
FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
572
{
Josh Coalson's avatar
Josh Coalson committed
573
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
574
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
575
576

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

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

	while(1) {
605
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
606
607
608
			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
609
				break;
Josh Coalson's avatar
Josh Coalson committed
610
611
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!read_metadata_(decoder))
Josh Coalson's avatar
Josh Coalson committed
612
613
614
					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
615
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
616
617
618
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
619
				if(!read_frame_(decoder, &dummy))
Josh Coalson's avatar
Josh Coalson committed
620
621
622
					return false; /* above function sets the status for us */
				break;
			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
633
634
635
636
637
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

638
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
639
{
Josh Coalson's avatar
Josh Coalson committed
640
	FLAC__ASSERT(0 != decoder);
641
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
642
643
}

644
645
646
647
648
649
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
650
void set_defaults_(FLAC__StreamDecoder *decoder)
651
652
653
654
655
656
657
658
659
660
661
662
{
	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
663
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
664
665
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
666
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
667

668
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
669
670
		return true;

671
672
	/* @@@ should change to use realloc() */

Josh Coalson's avatar
Josh Coalson committed
673
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
674
		if(0 != decoder->private_->output[i]) {
675
676
			free(decoder->private_->output[i]);
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
677
		}
Josh Coalson's avatar
Josh Coalson committed
678
		if(0 != decoder->private_->residual[i]) {
679
680
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
681
		}
Josh Coalson's avatar
Josh Coalson committed
682
683
	}

684
	for(i = 0; i < channels; i++) {
685
686
		/* 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. */
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalson's avatar
Josh Coalson committed
687
		if(tmp == 0) {
688
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
689
690
			return false;
		}
691
		memset(tmp, 0, sizeof(FLAC__int32)*4);
692
		decoder->private_->output[i] = tmp + 4;
693

Josh Coalson's avatar
Josh Coalson committed
694
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
695
		if(tmp == 0) {
696
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
697
698
			return false;
		}
699
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
700
701
	}

702
703
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
704
705
706
707

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
708
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
709
710
711
712
713
714
715
716
717
718
719
720
721
{
	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
722
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
723
{
Josh Coalson's avatar
Josh Coalson committed
724
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
725
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
726
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
727

728
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
729
730

	for(i = id = 0; i < 4; ) {
731
732
733
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
734
735
		}
		else {
736
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
737
738
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
739
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
740
			first = true;
Josh Coalson's avatar
Josh Coalson committed
741
742
743
744
745
746
747
748
			i++;
			id = 0;
			continue;
		}
		if(x == ID3V2_TAG_[id]) {
			id++;
			i = 0;
			if(id == 3) {
Josh Coalson's avatar
Josh Coalson committed
749
				if(!skip_id3v2_tag_(decoder))
Josh Coalson's avatar
Josh Coalson committed
750
751
752
753
754
					return false; /* the read_callback_ sets the state for us */
			}
			continue;
		}
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
755
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
756
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
757
				return false; /* the read_callback_ sets the state for us */
758
759
760
761

			/* 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 */
762
763
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
764
765
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
766
767
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
768
769
770
771
772
				return true;
			}
		}
		i = 0;
		if(first) {
773
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
774
			first = false;
Josh Coalson's avatar
Josh Coalson committed
775
776
777
		}
	}

778
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
779
780
781
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
782
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
783
{
Josh Coalson's avatar
Josh Coalson committed
784
785
	FLAC__uint32 i, x, last_block, type, length;
	FLAC__uint64 xx;
Josh Coalson's avatar
Josh Coalson committed
786

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

789
	if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
790
		return false; /* the read_callback_ sets the state for us */
791
	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
792
		return false; /* the read_callback_ sets the state for us */
793
	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
794
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
795
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
796
		unsigned used_bits = 0;
797
798
799
		decoder->private_->stream_info.type = type;
		decoder->private_->stream_info.is_last = last_block;
		decoder->private_->stream_info.length = length;
800

801
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
802
			return false; /* the read_callback_ sets the state for us */
803
		decoder->private_->stream_info.data.stream_info.min_blocksize = x;
Josh Coalson's avatar
Josh Coalson committed
804
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
805

806
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
807
			return false; /* the read_callback_ sets the state for us */
808
		decoder->private_->stream_info.data.stream_info.max_blocksize = x;
Josh Coalson's avatar
Josh Coalson committed
809
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
810

811
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
812
			return false; /* the read_callback_ sets the state for us */
813
		decoder->private_->stream_info.data.stream_info.min_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
814
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
815

816
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
817
			return false; /* the read_callback_ sets the state for us */
818
		decoder->private_->stream_info.data.stream_info.max_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
819
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
820

821
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
822
			return false; /* the read_callback_ sets the state for us */
823
		decoder->private_->stream_info.data.stream_info.sample_rate = x;
Josh Coalson's avatar
Josh Coalson committed
824
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
825

826
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
827
			return false; /* the read_callback_ sets the state for us */
828
		decoder->private_->stream_info.data.stream_info.channels = x+1;
Josh Coalson's avatar
Josh Coalson committed
829
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
830

831
		if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
832
			return false; /* the read_callback_ sets the state for us */
833
		decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
Josh Coalson's avatar
Josh Coalson committed
834
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
835

836
		if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
837
			return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
838
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
839

840
841
842
		if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		used_bits += 16*8;
Josh Coalson's avatar
Josh Coalson committed
843

844
		/* skip the rest of the block */
845
		FLAC__ASSERT(used_bits % 8 == 0);
846
		length -= (used_bits / 8);
847
848
		if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
849

850
		decoder->private_->has_stream_info = true;
851
852
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
853
854
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
855
856
857
		decoder->private_->seek_table.type = type;
		decoder->private_->seek_table.is_last = last_block;
		decoder->private_->seek_table.length = length;
858

Josh Coalson's avatar
Josh Coalson committed
859
		decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
860

861
		if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
862
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
863
864
			return false;
		}
865
		for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
866
			if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
867
				return false; /* the read_callback_ sets the state for us */
868
			decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
869

870
			if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
871
				return false; /* the read_callback_ sets the state for us */
872
			decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
873

874
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
875
				return false; /* the read_callback_ sets the state for us */
876
			decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
877
		}
878
879
880
881
882
883
884
885
		length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
		/* if there is a partial point left, skip over it */
		if(length > 0) {
			/*@@@ do an error_callback() here?  there's an argument for either way */
			if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
		}

886
		decoder->private_->has_seek_table = true;
887
888
		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
889
890
	}
	else {
Josh Coalson's avatar