stream_decoder.c 58.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
static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
57
static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
Josh Coalson's avatar
Josh Coalson committed
58
59
static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
60
61
62
63
64
65
66

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

Josh Coalson's avatar
Josh Coalson committed
67
typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson's avatar
Josh Coalson committed
68
69
	FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
	FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
Josh Coalson's avatar
Josh Coalson committed
70
71
	void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
	void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson's avatar
Josh Coalson committed
72
73
	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
74
75
	void *client_data;
	FLAC__BitBuffer input;
Josh Coalson's avatar
Josh Coalson committed
76
77
	FLAC__int32 *output[FLAC__MAX_CHANNELS];
	FLAC__int32 *residual[FLAC__MAX_CHANNELS];
78
	unsigned output_capacity, output_channels;
Josh Coalson's avatar
Josh Coalson committed
79
80
81
	FLAC__uint32 last_frame_number;
	FLAC__uint64 samples_decoded;
	FLAC__bool has_stream_info, has_seek_table;
82
83
	FLAC__StreamMetaData stream_info;
	FLAC__StreamMetaData seek_table;
84
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
85
	FLAC__bool cached; /* true if there is a byte in lookahead */
86
	FLAC__CPUInfo cpuinfo;
Josh Coalson's avatar
Josh Coalson committed
87
88
	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
89
90
} FLAC__StreamDecoderPrivate;

91
92
93
94
95
/***********************************************************************
 *
 * Public static class data
 *
 ***********************************************************************/
Josh Coalson's avatar
Josh Coalson committed
96

Josh Coalson's avatar
Josh Coalson committed
97
98
99
100
101
102
103
104
105
const char *FLAC__StreamDecoderStateString[] = {
	"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",
106
	"FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
107
	"FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalson's avatar
Josh Coalson committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	"FLAC__STREAM_DECODER_UNINITIALIZED"
};

const char *FLAC__StreamDecoderReadStatusString[] = {
	"FLAC__STREAM_DECODER_READ_CONTINUE",
	"FLAC__STREAM_DECODER_READ_END_OF_STREAM",
	"FLAC__STREAM_DECODER_READ_ABORT"
};

const char *FLAC__StreamDecoderWriteStatusString[] = {
	"FLAC__STREAM_DECODER_WRITE_CONTINUE",
	"FLAC__STREAM_DECODER_WRITE_ABORT"
};

const char *FLAC__StreamDecoderErrorStatusString[] = {
123
124
125
	"FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
	"FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
	"FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
Josh Coalson's avatar
Josh Coalson committed
126
127
};

128
129
130
131
132
133
/***********************************************************************
 *
 * Class constructor/destructor
 *
 ***********************************************************************/
FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalson's avatar
Josh Coalson committed
134
{
135
136
137
138
139
140
141
	FLAC__StreamDecoder *decoder;

	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
142
	}
143
144
	decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
	if(decoder->protected_ == 0) {
145
146
147
		free(decoder);
		return 0;
	}
148
149
150
	decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
	if(decoder->private_ == 0) {
		free(decoder->protected_);
151
152
153
154
		free(decoder);
		return 0;
	}

155
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
156

157
158
159
160
161
	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;
162

Josh Coalson's avatar
Josh Coalson committed
163
164
165
	return decoder;
}

166
void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
167
{
168
	FLAC__ASSERT(decoder != 0);
169
170
	FLAC__ASSERT(decoder->protected_ != 0);
	FLAC__ASSERT(decoder->private_ != 0);
171

172
173
	free(decoder->private_);
	free(decoder->protected_);
Josh Coalson's avatar
Josh Coalson committed
174
175
176
	free(decoder);
}

177
178
179
180
181
182
/***********************************************************************
 *
 * Public class methods
 *
 ***********************************************************************/

183
FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
184
185
186
{
	unsigned i;

187
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
188

189
190
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalson's avatar
Josh Coalson committed
191

192
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
193

194
195
	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
196

197
	FLAC__bitbuffer_init(&decoder->private_->input);
Josh Coalson's avatar
Josh Coalson committed
198

199
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
200
201
		decoder->private_->output[i] = 0;
		decoder->private_->residual[i] = 0;
202
	}
Josh Coalson's avatar
Josh Coalson committed
203

204
205
206
207
208
209
210
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
	decoder->private_->last_frame_number = 0;
	decoder->private_->samples_decoded = 0;
	decoder->private_->has_stream_info = false;
	decoder->private_->has_seek_table = false;
	decoder->private_->cached = false;
Josh Coalson's avatar
Josh Coalson committed
211

212
213
214
	/*
	 * get the CPU info and set the function pointers
	 */
215
	FLAC__cpu_info(&decoder->private_->cpuinfo);
216
	/* first default to the non-asm routines */
217
218
	decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
	decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
219
	/* now override with asm where appropriate */
220
#ifndef FLAC__NO_ASM
221
	if(decoder->private_->cpuinfo.use_asm) {
222
#ifdef FLAC__CPU_IA32
223
		FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
224
#ifdef FLAC__HAS_NASM
225
226
227
		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;
228
229
		}
		else {
230
231
			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;
232
		}
233
#endif
234
#endif
235
	}
236
237
#endif

238
	return decoder->protected_->state;
Josh Coalson's avatar
Josh Coalson committed
239
240
241
242
243
}

void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
{
	unsigned i;
244
	FLAC__ASSERT(decoder != 0);
245
	if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson's avatar
Josh Coalson committed
246
		return;
247
248
249
	if(decoder->private_->has_seek_table) {
		free(decoder->private_->seek_table.data.seek_table.points);
		decoder->private_->seek_table.data.seek_table.points = 0;
250
	}
251
	FLAC__bitbuffer_free(&decoder->private_->input);
252
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
253
		/* 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. */
254
255
256
		if(decoder->private_->output[i] != 0) {
			free(decoder->private_->output[i]-4);
			decoder->private_->output[i] = 0;
257
		}
258
259
260
		if(decoder->private_->residual[i] != 0) {
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
261
262
		}
	}
263
264
265
	decoder->private_->output_capacity = 0;
	decoder->private_->output_channels = 0;
	decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
266
267
}

Josh Coalson's avatar
Josh Coalson committed
268
FLAC__bool FLAC__stream_decoder_set_read_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
269
{
270
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
271
		return false;
272
	decoder->private_->read_callback = value;
273
274
275
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
276
FLAC__bool FLAC__stream_decoder_set_write_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
277
{
278
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
279
		return false;
280
	decoder->private_->write_callback = value;
281
282
283
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
284
FLAC__bool FLAC__stream_decoder_set_metadata_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
285
{
286
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
287
		return false;
288
	decoder->private_->metadata_callback = value;
289
290
291
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
292
FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
293
{
294
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
295
		return false;
296
	decoder->private_->error_callback = value;
297
298
299
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
300
FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
301
{
302
	if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
303
		return false;
304
	decoder->private_->client_data = value;
305
306
307
308
	return true;
}

FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
309
{
310
	return decoder->protected_->state;
311
312
}

313
unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
314
{
315
	return decoder->protected_->channels;
316
317
}

318
FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
319
{
320
	return decoder->protected_->channel_assignment;
321
322
}

323
unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
324
{
325
	return decoder->protected_->bits_per_sample;
326
327
}

328
unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
329
{
330
	return decoder->protected_->sample_rate;
331
332
}

333
unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
334
{
335
	return decoder->protected_->blocksize;
Josh Coalson's avatar
Josh Coalson committed
336
337
}

Josh Coalson's avatar
Josh Coalson committed
338
FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
339
{
340
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
341

342
343
	if(!FLAC__bitbuffer_clear(&decoder->private_->input)) {
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
344
345
		return false;
	}
346
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
347
348
349
350

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
351
FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
352
{
353
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
354
355

	if(!FLAC__stream_decoder_flush(decoder)) {
356
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
357
358
		return false;
	}
359
	decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalson's avatar
Josh Coalson committed
360

361
	decoder->private_->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
362

Josh Coalson's avatar
Josh Coalson committed
363
364
365
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
366
FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
367
{
Josh Coalson's avatar
Josh Coalson committed
368
	FLAC__bool dummy;
369
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
370

371
	if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalson's avatar
Josh Coalson committed
372
373
		return true;

374
	FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
Josh Coalson's avatar
Josh Coalson committed
375
376

	if(!FLAC__stream_decoder_reset(decoder)) {
377
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
378
379
380
381
		return false;
	}

	while(1) {
382
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
				if(!stream_decoder_find_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!stream_decoder_read_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!stream_decoder_frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!stream_decoder_read_frame_(decoder, &dummy))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
				return true;
			default:
402
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
403
404
405
406
		}
	}
}

Josh Coalson's avatar
Josh Coalson committed
407
FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
408
{
409
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
410

411
	if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalson's avatar
Josh Coalson committed
412
413
		return true;

414
	FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
Josh Coalson's avatar
Josh Coalson committed
415
416

	if(!FLAC__stream_decoder_reset(decoder)) {
417
		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
418
419
420
421
		return false;
	}

	while(1) {
422
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
423
424
425
426
427
428
429
430
431
432
			case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
				if(!stream_decoder_find_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_METADATA:
				if(!stream_decoder_read_metadata_(decoder))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				return true;
433
434
			case FLAC__STREAM_DECODER_READ_FRAME:
				return true;
Josh Coalson's avatar
Josh Coalson committed
435
436
437
			case FLAC__STREAM_DECODER_END_OF_STREAM:
				return true;
			default:
438
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
439
440
441
442
		}
	}
}

Josh Coalson's avatar
Josh Coalson committed
443
FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
444
{
Josh Coalson's avatar
Josh Coalson committed
445
	FLAC__bool got_a_frame;
446
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
447

448
	if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalson's avatar
Josh Coalson committed
449
450
		return true;

451
	FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
Josh Coalson's avatar
Josh Coalson committed
452
453

	while(1) {
454
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
455
456
457
458
459
460
461
462
463
464
465
466
467
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!stream_decoder_frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!stream_decoder_read_frame_(decoder, &got_a_frame))
					return false; /* above function sets the status for us */
				if(got_a_frame)
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
				return true;
			default:
468
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
469
470
471
472
		}
	}
}

Josh Coalson's avatar
Josh Coalson committed
473
FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
474
{
Josh Coalson's avatar
Josh Coalson committed
475
	FLAC__bool dummy;
476
	FLAC__ASSERT(decoder != 0);
Josh Coalson's avatar
Josh Coalson committed
477

478
	if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
Josh Coalson's avatar
Josh Coalson committed
479
480
		return true;

481
	FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
Josh Coalson's avatar
Josh Coalson committed
482
483

	while(1) {
484
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
485
486
487
488
489
490
491
492
493
494
495
			case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
				if(!stream_decoder_frame_sync_(decoder))
					return true; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_READ_FRAME:
				if(!stream_decoder_read_frame_(decoder, &dummy))
					return false; /* above function sets the status for us */
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
				return true;
			default:
496
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
497
498
499
500
		}
	}
}

501
502
503
504
505
506
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

507
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
508
{
509
	FLAC__ASSERT(decoder != 0);
510
	return decoder->private_->input.bytes - decoder->private_->input.consumed_bytes;
Josh Coalson's avatar
Josh Coalson committed
511
512
}

513
514
515
516
517
518
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

Josh Coalson's avatar
Josh Coalson committed
519
FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalson's avatar
Josh Coalson committed
520
521
{
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
522
	FLAC__int32 *tmp;
Josh Coalson's avatar
Josh Coalson committed
523

524
	if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalson's avatar
Josh Coalson committed
525
526
		return true;

527
528
	/* @@@ should change to use realloc() */

Josh Coalson's avatar
Josh Coalson committed
529
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
530
531
532
		if(decoder->private_->output[i] != 0) {
			free(decoder->private_->output[i]);
			decoder->private_->output[i] = 0;
Josh Coalson's avatar
Josh Coalson committed
533
		}
534
535
536
		if(decoder->private_->residual[i] != 0) {
			free(decoder->private_->residual[i]);
			decoder->private_->residual[i] = 0;
537
		}
Josh Coalson's avatar
Josh Coalson committed
538
539
	}

540
	for(i = 0; i < channels; i++) {
541
542
		/* 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
543
		if(tmp == 0) {
544
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
545
546
			return false;
		}
547
		memset(tmp, 0, sizeof(FLAC__int32)*4);
548
		decoder->private_->output[i] = tmp + 4;
549

Josh Coalson's avatar
Josh Coalson committed
550
		tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
551
		if(tmp == 0) {
552
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
553
554
			return false;
		}
555
		decoder->private_->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
556
557
	}

558
559
	decoder->private_->output_capacity = size;
	decoder->private_->output_channels = channels;
Josh Coalson's avatar
Josh Coalson committed
560
561
562
563

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
564
FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
565
{
Josh Coalson's avatar
Josh Coalson committed
566
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
567
	unsigned i, id;
Josh Coalson's avatar
Josh Coalson committed
568
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
569

570
	FLAC__ASSERT(decoder->private_->input.consumed_bits == 0); /* make sure we're byte aligned */
Josh Coalson's avatar
Josh Coalson committed
571
572

	for(i = id = 0; i < 4; ) {
573
574
575
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
576
577
		}
		else {
578
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
579
580
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
581
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
582
			first = true;
Josh Coalson's avatar
Josh Coalson committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
			i++;
			id = 0;
			continue;
		}
		if(x == ID3V2_TAG_[id]) {
			id++;
			i = 0;
			if(id == 3) {
				if(!stream_decoder_skip_id3v2_tag_(decoder))
					return false; /* the read_callback_ sets the state for us */
			}
			continue;
		}
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
597
598
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
599
				return false; /* the read_callback_ sets the state for us */
600
601
602
603

			/* 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 */
604
605
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
606
607
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
608
609
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
610
611
612
613
614
				return true;
			}
		}
		i = 0;
		if(first) {
615
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
616
			first = false;
Josh Coalson's avatar
Josh Coalson committed
617
618
619
		}
	}

620
	decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalson's avatar
Josh Coalson committed
621
622
623
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
624
FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
625
{
Josh Coalson's avatar
Josh Coalson committed
626
627
	FLAC__uint32 i, x, last_block, type, length;
	FLAC__uint64 xx;
Josh Coalson's avatar
Josh Coalson committed
628

629
	FLAC__ASSERT(decoder->private_->input.consumed_bits == 0); /* make sure we're byte aligned */
Josh Coalson's avatar
Josh Coalson committed
630

631
	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
632
		return false; /* the read_callback_ sets the state for us */
633
	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
634
		return false; /* the read_callback_ sets the state for us */
635
	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
636
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
637
	if(type == FLAC__METADATA_TYPE_STREAMINFO) {
638
		unsigned used_bits = 0;
639
640
641
		decoder->private_->stream_info.type = type;
		decoder->private_->stream_info.is_last = last_block;
		decoder->private_->stream_info.length = length;
642

643
		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
644
			return false; /* the read_callback_ sets the state for us */
645
		decoder->private_->stream_info.data.stream_info.min_blocksize = x;
Josh Coalson's avatar
Josh Coalson committed
646
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
647

648
		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
649
			return false; /* the read_callback_ sets the state for us */
650
		decoder->private_->stream_info.data.stream_info.max_blocksize = x;
Josh Coalson's avatar
Josh Coalson committed
651
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
652

653
		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
654
			return false; /* the read_callback_ sets the state for us */
655
		decoder->private_->stream_info.data.stream_info.min_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
656
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
657

658
		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
659
			return false; /* the read_callback_ sets the state for us */
660
		decoder->private_->stream_info.data.stream_info.max_framesize = x;
Josh Coalson's avatar
Josh Coalson committed
661
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
662

663
		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
664
			return false; /* the read_callback_ sets the state for us */
665
		decoder->private_->stream_info.data.stream_info.sample_rate = x;
Josh Coalson's avatar
Josh Coalson committed
666
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
667

668
		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
669
			return false; /* the read_callback_ sets the state for us */
670
		decoder->private_->stream_info.data.stream_info.channels = x+1;
Josh Coalson's avatar
Josh Coalson committed
671
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
672

673
		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
674
			return false; /* the read_callback_ sets the state for us */
675
		decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
Josh Coalson's avatar
Josh Coalson committed
676
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
677

678
		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
679
			return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
680
		used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
681

Josh Coalson's avatar
Josh Coalson committed
682
		for(i = 0; i < 16; i++) {
683
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
684
				return false; /* the read_callback_ sets the state for us */
685
			decoder->private_->stream_info.data.stream_info.md5sum[i] = (FLAC__byte)x;
Josh Coalson's avatar
Josh Coalson committed
686
		}
687
		used_bits += i*8;
Josh Coalson's avatar
Josh Coalson committed
688

689
		/* skip the rest of the block */
690
		FLAC__ASSERT(used_bits % 8 == 0);
691
692
		length -= (used_bits / 8);
		for(i = 0; i < length; i++) {
693
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
694
695
696
				return false; /* the read_callback_ sets the state for us */
		}

697
698
		decoder->private_->has_stream_info = true;
		decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
699
700
	}
	else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalson's avatar
Josh Coalson committed
701
702
		unsigned real_points;

703
704
705
		decoder->private_->seek_table.type = type;
		decoder->private_->seek_table.is_last = last_block;
		decoder->private_->seek_table.length = length;
706

707
		decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
708

709
710
		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)))) {
			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
711
712
			return false;
		}
713
714
		for(i = real_points = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
			if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
715
				return false; /* the read_callback_ sets the state for us */
716
			decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
717

718
			if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
719
				return false; /* the read_callback_ sets the state for us */
720
			decoder->private_->seek_table.data.seek_table.points[real_points].stream_offset = xx;
721

722
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
723
				return false; /* the read_callback_ sets the state for us */
724
			decoder->private_->seek_table.data.seek_table.points[real_points].frame_samples = x;
Josh Coalson's avatar
Josh Coalson committed
725

726
			if(decoder->private_->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
Josh Coalson's avatar
Josh Coalson committed
727
				real_points++;
728
		}
729
		decoder->private_->seek_table.data.seek_table.num_points = real_points;
730

731
732
		decoder->private_->has_seek_table = true;
		decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
Josh Coalson's avatar
Josh Coalson committed
733
734
735
736
	}
	else {
		/* skip other metadata blocks */
		for(i = 0; i < length; i++) {
737
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
738
739
740
741
742
				return false; /* the read_callback_ sets the state for us */
		}
	}

	if(last_block)
743
		decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
744
745
746
747

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
748
FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
749
{
Josh Coalson's avatar
Josh Coalson committed
750
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
751
752
753
	unsigned i, skip;

	/* skip the version and flags bytes */
754
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 24, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
755
756
757
758
		return false; /* the read_callback_ sets the state for us */
	/* get the size (in bytes) to skip */
	skip = 0;
	for(i = 0; i < 4; i++) {
759
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
760
761
762
763
764
765
			return false; /* the read_callback_ sets the state for us */
		skip <<= 7;
		skip |= (x & 0x7f);
	}
	/* skip the rest of the tag */
	for(i = 0; i < skip; i++) {
766
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
767
768
769
770
771
			return false; /* the read_callback_ sets the state for us */
	}
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
772
FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
Josh Coalson's avatar
Josh Coalson committed
773
{
Josh Coalson's avatar
Josh Coalson committed
774
775
	FLAC__uint32 x;
	FLAC__bool first = true;
Josh Coalson's avatar
Josh Coalson committed
776
777
778

	/* If we know the total number of samples in the stream, stop if we've read that many. */
	/* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
779
780
781
	if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
		if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
			decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
Josh Coalson's avatar
Josh Coalson committed
782
783
784
785
786
			return true;
		}
	}

	/* make sure we're byte aligned */
787
788
	if(decoder->private_->input.consumed_bits != 0) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8-decoder->private_->input.consumed_bits, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
789
790
791
792
			return false; /* the read_callback_ sets the state for us */
	}

	while(1) {
793
794
795
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
796
797
		}
		else {
798
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
799
800
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
801
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
802
803
			decoder->private_->header_warmup[0] = (FLAC__byte)x;
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
804
				return false; /* the read_callback_ sets the state for us */
805
806
807
808

			/* 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 */
809
810
				decoder->private_->lookahead = (FLAC__byte)x;
				decoder->private_->cached = true;
811
812
			}
			else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
813
814
				decoder->private_->header_warmup[1] = (FLAC__byte)x;
				decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalson's avatar
Josh Coalson committed
815
816
817
818
				return true;
			}
		}
		if(first) {
819
			decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
Josh Coalson's avatar
Josh Coalson committed
820
			first = false;
Josh Coalson's avatar
Josh Coalson committed
821
822
823
824
825
826
		}
	}

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
827
FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
Josh Coalson's avatar
Josh Coalson committed
828
829
830
{
	unsigned channel;
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
831
832
833
	FLAC__int32 mid, side, left, right;
	FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
	FLAC__uint32 x;
Josh Coalson's avatar
Josh Coalson committed
834
835
836

	*got_a_frame = false;

837
838
	/* init the CRC */
	frame_crc = 0;
839
840
841
	FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
	FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
	FLAC__bitbuffer_init_read_crc16(&decoder->private_->input, frame_crc);
842

Josh Coalson's avatar
Josh Coalson committed
843
844
	if(!stream_decoder_read_frame_header_(decoder))
		return false;
845
	if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
Josh Coalson's avatar
Josh Coalson committed
846
		return true;
847
	if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
Josh Coalson's avatar
Josh Coalson committed
848
		return false;
849
	for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
850
851
852
		/*
		 * first figure the correct bits-per-sample of the subframe
		 */
853
854
		unsigned bps = decoder->private_->frame.header.bits_per_sample;
		switch(decoder->private_->frame.header.channel_assignment) {
855
856
857
858
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				/* no adjustment needed */
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
859
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
860
861
862
863
				if(channel == 1)
					bps++;
				break;
			case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
864
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
865
866
867
868
				if(channel == 0)
					bps++;
				break;
			case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
869
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
870
871
872
873
				if(channel == 1)
					bps++;
				break;
			default:
874
				FLAC__ASSERT(0);
875
876
877
878
879
		}
		/*
		 * now read it
		 */
		if(!stream_decoder_read_subframe_(decoder, channel, bps))
Josh Coalson's avatar
Josh Coalson committed
880
			return false;
881
882
		if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
			decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson's avatar
Josh Coalson committed
883
884
885
886
887
888
			return true;
		}
	}
	if(!stream_decoder_read_zero_padding_(decoder))
		return false;

889
890
891
	/*
	 * Read the frame CRC-16 from the footer and check
	 */
892
893
	frame_crc = decoder->private_->input.read_crc16;
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
894
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
895
	if(frame_crc == (FLAC__uint16)x) {
896
		/* Undo any special channel coding */
897
		switch(decoder->private_->frame.header.channel_assignment) {