stream_decoder.c 71 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
#include "private/crc.h"
#include "private/fixed.h"
29
#include "private/format.h"
Josh Coalson's avatar
Josh Coalson committed
30
31
#include "private/lpc.h"

32
33
34
35
36
#ifdef max
#undef max
#endif
#define max(a,b) ((a)>(b)?(a):(b))

37
38
39
40
41
42
/***********************************************************************
 *
 * Private static data
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
43
static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
44
45
46
47
48
49
50

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

Josh Coalson's avatar
Josh Coalson committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
65
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
Josh Coalson's avatar
Josh Coalson committed
66
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson's avatar
Josh Coalson committed
67
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
68
69
70
71
72
73
74

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

Josh Coalson's avatar
Josh Coalson committed
75
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
76
77
78
79
	FLAC__StreamDecoderReadCallback read_callback;
	FLAC__StreamDecoderWriteCallback write_callback;
	FLAC__StreamDecoderMetadataCallback metadata_callback;
	FLAC__StreamDecoderErrorCallback error_callback;
Josh Coalson's avatar
Josh Coalson committed
80
	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[]);
81
	void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalson's avatar
Josh Coalson committed
82
	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
83
	void *client_data;
84
	FLAC__BitBuffer *input;
Josh Coalson's avatar
Josh Coalson committed
85
86
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
	FLAC__int32 *residual[FLAC__MAX_CHANNELS];
87
	FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
88
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
89
90
91
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
92
93
	FLAC__StreamMetadata stream_info;
	FLAC__StreamMetadata seek_table;
94
95
96
	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 */
97
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
98
	FLAC__bool cached; /* true if there is a byte in lookahead */
99
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
100
101
	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
102
103
} FLAC__StreamDecoderPrivate;

104
105
106
107
108
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
109

110
FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalson's avatar
Josh Coalson committed
111
112
113
114
115
116
117
118
	"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",
119
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
120
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
121
122
123
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

124
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
125
126
127
	"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
128
129
};

130
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
131
132
	"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalson's avatar
Josh Coalson committed
133
134
};

135
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
136
137
138
	"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
139
140
};

141
142
143
144
145
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
146
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
147
{
148
	FLAC__StreamDecoder *decoder;
149
	unsigned i;
150
151
152

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

153
	decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
154
155
	if(decoder == 0) {
		return 0;
Josh Coalson's avatar
Josh Coalson committed
156
	}
Josh Coalson's avatar
Josh Coalson committed
157

158
	decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
159
	if(decoder->protected_ == 0) {
160
161
162
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
163

164
	decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
165
166
	if(decoder->private_ == 0) {
		free(decoder->protected_);
167
168
169
		free(decoder);
		return 0;
	}
Josh Coalson's avatar
Josh Coalson committed
170

171
172
173
174
175
176
177
	decoder->private_->input = FLAC__bitbuffer_new();
	if(decoder->private_->input == 0) {
		free(decoder->private_);
		free(decoder->protected_);
		free(decoder);
		return 0;
	}
178

179
180
181
182
183
184
185
186
	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;
	}
187

188
189
190
191
192
193
194
195
	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;
196
197
198

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

Josh Coalson's avatar
Josh Coalson committed
200
	set_defaults_(decoder);
201

202
203
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;

Josh Coalson's avatar
Josh Coalson committed
204
205
206
	return decoder;
}

207
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
208
{
209
210
	unsigned i;

Josh Coalson's avatar
Josh Coalson committed
211
212
213
214
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->private_->input);
215

216
217
	FLAC__stream_decoder_finish(decoder);

Josh Coalson's avatar
Josh Coalson committed
218
	if(0 != decoder->private_->metadata_filter_ids)
219
220
		free(decoder->private_->metadata_filter_ids);

221
	FLAC__bitbuffer_delete(decoder->private_->input);
222

223
224
	for(i = 0; i < FLAC__MAX_CHANNELS; i++)
		FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
225

226
227
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
228
229
230
	free(decoder);
}

231
232
233
234
235
236
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

237
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
238
{
Josh Coalson's avatar
Josh Coalson committed
239
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
240

241
242
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
243

244
245
	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
246

247
248
	if(!FLAC__bitbuffer_init(decoder->private_->input))
		return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
249

250
251
252
253
	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
254

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

Josh Coalson's avatar
Josh Coalson committed
282
283
284
285
	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
286
287
}

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

Josh Coalson's avatar
Josh Coalson committed
320
	set_defaults_(decoder);
321

322
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
323
324
}

325
FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
326
{
Josh Coalson's avatar
Josh Coalson committed
327
328
329
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
330
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
331
		return false;
332
	decoder->private_->read_callback = value;
333
334
335
	return true;
}

336
FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
337
{
Josh Coalson's avatar
Josh Coalson committed
338
339
340
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
341
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
342
		return false;
343
	decoder->private_->write_callback = value;
344
345
346
	return true;
}

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

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

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

380
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
381
{
Josh Coalson's avatar
Josh Coalson committed
382
383
384
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
385
386
387
388
389
390
391
392
393
	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;
}

394
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
395
{
Josh Coalson's avatar
Josh Coalson committed
396
397
398
399
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
	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;
}

420
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
421
{
422
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
423
424
425
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
426
427
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
428
429
	for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
		decoder->private_->metadata_filter[i] = true;
430
431
432
433
	decoder->private_->metadata_filter_ids_count = 0;
	return true;
}

434
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
435
{
Josh Coalson's avatar
Josh Coalson committed
436
437
438
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
439
440
441
442
443
444
445
446
447
	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;
}

448
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
449
{
Josh Coalson's avatar
Josh Coalson committed
450
451
452
453
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
	FLAC__ASSERT(0 != id);
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
	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;
}

474
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
475
{
Josh Coalson's avatar
Josh Coalson committed
476
477
478
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
479
480
481
482
483
484
485
	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;
}

486
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
487
{
Josh Coalson's avatar
Josh Coalson committed
488
489
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
490
	return decoder->protected_->state;
491
492
}

493
FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
494
{
Josh Coalson's avatar
Josh Coalson committed
495
496
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
497
	return decoder->protected_->channels;
498
499
}

500
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
501
{
Josh Coalson's avatar
Josh Coalson committed
502
503
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
504
	return decoder->protected_->channel_assignment;
505
506
}

507
FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
508
{
Josh Coalson's avatar
Josh Coalson committed
509
510
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->protected_);
511
	return decoder->protected_->bits_per_sample;
512
513
}

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

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

528
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
529
{
Josh Coalson's avatar
Josh Coalson committed
530
531
532
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
533

534
	if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
535
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
536
537
		return false;
	}
538
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
539
540
541
542

	return true;
}

543
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
544
{
Josh Coalson's avatar
Josh Coalson committed
545
546
547
	FLAC__ASSERT(0 != decoder);
	FLAC__ASSERT(0 != decoder->private_);
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
548
549

	if(!FLAC__stream_decoder_flush(decoder)) {
550
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
551
552
		return false;
	}
553
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
554

555
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
556

Josh Coalson's avatar
Josh Coalson committed
557
558
559
	return true;
}

560
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
561
{
Josh Coalson's avatar
Josh Coalson committed
562
	FLAC__bool got_a_frame;
Josh Coalson's avatar
Josh Coalson committed
563
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
564
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
565
566

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

597
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
598
{
Josh Coalson's avatar
Josh Coalson committed
599
	FLAC__ASSERT(0 != decoder);
Josh Coalson's avatar
Josh Coalson committed
600
	FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
601
602

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

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

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

658
659
660
661
662
663
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

664
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
665
{
Josh Coalson's avatar
Josh Coalson committed
666
	FLAC__ASSERT(0 != decoder);
667
	return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
668
669
}

670
671
672
673
674
675
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
676
void set_defaults_(FLAC__StreamDecoder *decoder)
677
678
679
680
681
682
683
684
685
686
687
688
{
	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
689
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
690
691
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
692
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
693

694
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
695
696
		return true;

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

Josh Coalson's avatar
Josh Coalson committed
699
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
700
		if(0 != decoder->private_->output[i]) {
701
			free(decoder->private_->output[i]-4);
702
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
703
		}
Josh Coalson's avatar
Josh Coalson committed
704
		if(0 != decoder->private_->residual[i]) {
705
706
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
707
		}
Josh Coalson's avatar
Josh Coalson committed
708
709
	}

710
	for(i = 0; i < channels; i++) {
711
712
713
714
715
716
		/* 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.
		 */
717
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalson's avatar
Josh Coalson committed
718
		if(tmp == 0) {
719
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
720
721
			return false;
		}
722
		memset(tmp, 0, sizeof(FLAC__int32)*4);
723
		decoder->private_->output[i] = tmp + 4;
724

Josh Coalson's avatar
Josh Coalson committed
725
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
726
		if(tmp == 0) {
727
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
728
729
			return false;
		}
730
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
731
732
	}

733
734
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
735
736
737
738

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
739
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
740
741
742
743
744
745
746
747
748
749
750
751
752
{
	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
753
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
754
{
Josh Coalson's avatar
Josh Coalson committed
755
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
756
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
757
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
758

759
	FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalson's avatar
Josh Coalson committed
760
761

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

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

809
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
810
811
812
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
813
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
814
{
Josh Coalson's avatar
Josh Coalson committed
815
816
	FLAC__uint32 i, x, last_block, type, length;
	FLAC__uint64 xx;
Josh Coalson's avatar
Josh Coalson committed
817

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

820
	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
821
		return false; /* the read_callback_ sets the state for us */
822
	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
823
		return false; /* the read_callback_ sets the state for us */
824
	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
825
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
826
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
827
		unsigned used_bits = 0;
828
829
830
		decoder->private_->stream_info.type = type;
		decoder->private_->stream_info.is_last = last_block;
		decoder->private_->stream_info.length = length;
831

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

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

842
		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
843
			return false; /* the read_callback_ sets the state for us */
844
		decoder->private_->stream_info.data.stream_info.min_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
845
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
846

847
		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
848
			return false; /* the read_callback_ sets the state for us */
849
		decoder->private_->stream_info.data.stream_info.max_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
850
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
851

852
		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
853
			return false; /* the read_callback_ sets the state for us */
854
		decoder->private_->stream_info.data.stream_info.sample_rate = x;
Josh Coalson's avatar
Josh Coalson committed
855
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
856

857
		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
858
			return false; /* the read_callback_ sets the state for us */
859
		decoder->private_->stream_info.data.stream_info.channels = x+1;
Josh Coalson's avatar
Josh Coalson committed
860
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
861

862
		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
863
			return false; /* the read_callback_ sets the state for us */
864
		decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
Josh Coalson's avatar
Josh Coalson committed
865
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
866

867
		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
868
			return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
869
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
870

871
872
873
		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
874

875
		/* skip the rest of the block */
876
		FLAC__ASSERT(used_bits % 8 == 0);
877
		length -= (used_bits / 8);
878
879
		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 */
880

881
		decoder->private_->has_stream_info = true;
882
883
		if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
			decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
884
885
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
886
887
888
		decoder->private_->seek_table.type = type;
		decoder->private_->seek_table.is_last = last_block;
		decoder->private_->seek_table.