stream_decoder.c 69.1 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
	}
Josh Coalson's avatar
Josh Coalson committed
149
150
	memset(decoder, 0, sizeof(FLAC__StreamDecoder));

151
152
	decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
	if(decoder->protected_ == 0) {
153
154
155
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
156
157
	memset(decoder->protected_, 0, sizeof(FLAC__StreamDecoderProtected));

158
159
160
	decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
	if(decoder->private_ == 0) {
		free(decoder->protected_);
161
162
163
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
164
165
	memset(decoder->private_, 0, sizeof(FLAC__StreamDecoderPrivate));

166
167
168
169
170
171
172
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
173

174
175
176
177
178
179
180
181
	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;
	}
182

183
184
185
186
187
188
189
190
191
	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
192
	set_defaults_(decoder);
193

194
195
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
196
197
198
	return decoder;
}

199
void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
200
{
Josh Coalson's avatar
Josh Coalson committed
201
202
203
204
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
205

206
207
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
208
	if(0 != decoder->private_->metadata_filter_ids)
209
210
		free(decoder->private_->metadata_filter_ids);

211
	FLAC__bitbuffer_delete(decoder->private_->input);
212
213
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
214
215
216
	free(decoder);
}

217
218
219
220
221
222
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

223
FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
224
{
Josh Coalson's avatar
Josh Coalson committed
225
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
226

227
228
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
229

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

233
234
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
235

236
237
238
239
	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
240

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

Josh Coalson's avatar
Josh Coalson committed
267
268
269
270
	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
271
272
273
274
275
}

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

Josh Coalson's avatar
Josh Coalson committed
300
	set_defaults_(decoder);
301

302
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
303
304
}

Josh Coalson's avatar
Josh Coalson committed
305
FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
306
{
Josh Coalson's avatar
Josh Coalson committed
307
308
309
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
310
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
311
		return false;
312
	decoder->private_->read_callback = value;
313
314
315
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
316
FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
317
{
Josh Coalson's avatar
Josh Coalson committed
318
319
320
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
321
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
322
		return false;
323
	decoder->private_->write_callback = value;
324
325
326
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
327
FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
328
{
Josh Coalson's avatar
Josh Coalson committed
329
330
331
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
332
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
333
		return false;
334
	decoder->private_->metadata_callback = value;
335
336
337
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
338
FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
339
{
Josh Coalson's avatar
Josh Coalson committed
340
341
342
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
343
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
344
		return false;
345
	decoder->private_->error_callback = value;
346
347
348
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
349
FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
350
{
Josh Coalson's avatar
Josh Coalson committed
351
352
353
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
354
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
355
		return false;
356
	decoder->private_->client_data = value;
357
358
359
	return true;
}

360
FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
361
{
Josh Coalson's avatar
Josh Coalson committed
362
363
364
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
365
366
367
368
369
370
371
372
373
	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
374
FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
375
{
Josh Coalson's avatar
Josh Coalson committed
376
377
378
379
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
	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
400
FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
401
{
402
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
403
404
405
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
406
407
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
408
409
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
410
411
412
413
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

414
FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
415
{
Josh Coalson's avatar
Josh Coalson committed
416
417
418
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
419
420
421
422
423
424
425
426
427
	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
428
FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
429
{
Josh Coalson's avatar
Josh Coalson committed
430
431
432
433
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	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
454
FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
455
{
Josh Coalson's avatar
Josh Coalson committed
456
457
458
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
459
460
461
462
463
464
465
	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;
}

466
FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
467
{
Josh Coalson's avatar
Josh Coalson committed
468
469
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
470
	return decoder->protected_->state;
471
472
}

473
unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
474
{
Josh Coalson's avatar
Josh Coalson committed
475
476
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
477
	return decoder->protected_->channels;
478
479
}

480
FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
481
{
Josh Coalson's avatar
Josh Coalson committed
482
483
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
484
	return decoder->protected_->channel_assignment;
485
486
}

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

494
unsigned FLAC__stream_decoder_get_sample_rate(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_->sample_rate;
499
500
}

501
unsigned FLAC__stream_decoder_get_blocksize(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_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
506
507
}

Josh Coalson's avatar
Josh Coalson committed
508
FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
509
{
Josh Coalson's avatar
Josh Coalson committed
510
511
512
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
513

514
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
515
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
516
517
		return false;
	}
518
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
519
520
521
522

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
523
FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
524
{
Josh Coalson's avatar
Josh Coalson committed
525
526
527
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
528
529

	if(!FLAC__stream_decoder_flush(decoder)) {
530
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
531
532
		return false;
	}
533
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
534

535
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
536

Josh Coalson's avatar
Josh Coalson committed
537
538
539
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
540
FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
541
{
Josh Coalson's avatar
Josh Coalson committed
542
	FLAC__bool got_a_frame;
Josh Coalson's avatar
Josh Coalson committed
543
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
544
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
545
546

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

Josh Coalson's avatar
Josh Coalson committed
577
FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
578
{
Josh Coalson's avatar
Josh Coalson committed
579
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
580
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
581
582

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

Josh Coalson's avatar
Josh Coalson committed
604
FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
605
{
Josh Coalson's avatar
Josh Coalson committed
606
	FLAC__bool dummy;
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
613
614
			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
615
				break;
Josh Coalson's avatar
Josh Coalson committed
616
617
			case FLAC__STREAM_DECODER_READ_METADATA:
				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:
Josh Coalson's avatar
Josh Coalson committed
621
				if(!frame_sync_(decoder))
Josh Coalson's avatar
Josh Coalson committed
622
623
624
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson's avatar
Josh Coalson committed
625
				if(!read_frame_(decoder, &dummy))
Josh Coalson's avatar
Josh Coalson committed
626
627
628
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
629
			case FLAC__STREAM_DECODER_ABORTED:
Josh Coalson's avatar
Josh Coalson committed
630
631
				return true;
			default:
632
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
633
				return false;
Josh Coalson's avatar
Josh Coalson committed
634
635
636
637
		}
	}
}

638
639
640
641
642
643
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

644
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
645
{
Josh Coalson's avatar
Josh Coalson committed
646
	FLAC__ASSERT(0 != decoder);
647
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
648
649
}

650
651
652
653
654
655
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
656
void set_defaults_(FLAC__StreamDecoder *decoder)
657
658
659
660
661
662
663
664
665
666
667
668
{
	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
669
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
670
671
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
672
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
673

674
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
675
676
		return true;

677
678
	/* @@@ should change to use realloc() */

Josh Coalson's avatar
Josh Coalson committed
679
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
680
		if(0 != decoder->private_->output[i]) {
681
682
			free(decoder->private_->output[i]);
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
683
		}
Josh Coalson's avatar
Josh Coalson committed
684
		if(0 != decoder->private_->residual[i]) {
685
686
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
687
		}
Josh Coalson's avatar
Josh Coalson committed
688
689
	}

690
	for(i = 0; i < channels; i++) {
691
692
		/* 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
693
		if(tmp == 0) {
694
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
695
696
			return false;
		}
697
		memset(tmp, 0, sizeof(FLAC__int32)*4);
698
		decoder->private_->output[i] = tmp + 4;
699

Josh Coalson's avatar
Josh Coalson committed
700
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
701
		if(tmp == 0) {
702
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
703
704
			return false;
		}
705
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
706
707
	}

708
709
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
710
711
712
713

	return true;
}

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

734
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
735
736

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

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

784
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
785
786
787
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
788
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
789
{
Josh Coalson's avatar
Josh Coalson committed
790
791
	FLAC__uint32 i, x, last_block, type, length;
	FLAC__uint64 xx;
Josh Coalson's avatar
Josh Coalson committed
792

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

795
	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
796
		return false; /* the read_callback_ sets the state for us */
797
	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
798
		return false; /* the read_callback_ sets the state for us */
799
	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
800
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
801
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
802
		unsigned used_bits = 0;
803
804
805
		decoder->private_->stream_info.type = type;
		decoder->private_->stream_info.is_last = last_block;
		decoder->private_->stream_info.length = length;
806

807
		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
808
			return false; /* the read_callback_ sets the state for us */
809
		decoder->private_->stream_info.data.stream_info.min_blocksize = x;
Josh Coalson's avatar
Josh Coalson committed
810
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
811

812
		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
813
			return false; /* the read_callback_ sets the state for us */
814
		decoder->private_->stream_info.data.stream_info.max_blocksize = x;
Josh Coalson's avatar
Josh Coalson committed
815
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
816

817
		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
818
			return false; /* the read_callback_ sets the state for us */
819
		decoder->private_->stream_info.data.stream_info.min_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
820
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
821

822
		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
823
			return false; /* the read_callback_ sets the state for us */
824
		decoder->private_->stream_info.data.stream_info.max_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
825
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
826

827
		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
828
			return false; /* the read_callback_ sets the state for us */
829
		decoder->private_->stream_info.data.stream_info.sample_rate = x;
Josh Coalson's avatar
Josh Coalson committed
830
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
831

832
		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
833
			return false; /* the read_callback_ sets the state for us */
834
		decoder->private_->stream_info.data.stream_info.channels = x+1;
Josh Coalson's avatar
Josh Coalson committed
835
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
836

837
		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
838
			return false; /* the read_callback_ sets the state for us */
839
		decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
Josh Coalson's avatar
Josh Coalson committed
840
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
841

842
		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
843
			return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
844
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
845

846
847
848
		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
849

850
		/* skip the rest of the block */
851
		FLAC__ASSERT(used_bits % 8 == 0);
852
		length -= (used_bits / 8);
853
854
		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 */
855

856
		decoder->private_->has_stream_info = true;
857
858
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
859
860
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
861
862
863
		decoder->private_->seek_table.type = type;
		decoder->private_->seek_table.is_last = last_block;
		decoder->private_->seek_table.length = length;
864

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

867
		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)))) {
868
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
869
870
			return false;
		}
871
		for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
872
			if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
873
				return false; /* the read_callback_ sets the state for us */
874
			decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
875

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

880
			if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
881
				return false; /* the read_callback_ sets the state for us */
882
			decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
883
		}
884
885
886
887
888
889
890
891
		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 */
		}