stream_decoder.c 58 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001  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
346
347
348
349
		return false;
	}

	return true;
}

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

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

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

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

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

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

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

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

	while(1) {
381
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
			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:
401
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
402
403
404
405
		}
	}
}

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

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

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

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

	while(1) {
421
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
			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;
				break;
			case FLAC__STREAM_DECODER_END_OF_STREAM:
				return true;
			default:
436
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
437
438
439
440
		}
	}
}

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

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

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

	while(1) {
452
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
453
454
455
456
457
458
459
460
461
462
463
464
465
			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:
466
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
467
468
469
470
		}
	}
}

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

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

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

	while(1) {
482
		switch(decoder->protected_->state) {
Josh Coalson's avatar
Josh Coalson committed
483
484
485
486
487
488
489
490
491
492
493
			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:
494
				FLAC__ASSERT(0);
Josh Coalson's avatar
Josh Coalson committed
495
496
497
498
		}
	}
}

499
500
501
502
503
504
/***********************************************************************
 *
 * Protected class methods
 *
 ***********************************************************************/

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

511
512
513
514
515
516
/***********************************************************************
 *
 * Private class methods
 *
 ***********************************************************************/

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

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

525
526
	/* @@@ should change to use realloc() */

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

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

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

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

	return true;
}

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

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

	for(i = id = 0; i < 4; ) {
571
572
573
		if(decoder->private_->cached) {
			x = (FLAC__uint32)decoder->private_->lookahead;
			decoder->private_->cached = false;
574
575
		}
		else {
576
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
577
578
				return false; /* the read_callback_ sets the state for us */
		}
Josh Coalson's avatar
Josh Coalson committed
579
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
580
			first = true;
Josh Coalson's avatar
Josh Coalson committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
			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 */
595
596
			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
597
				return false; /* the read_callback_ sets the state for us */
598
599
600
601

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

701
702
703
		decoder->private_->seek_table.type = type;
		decoder->private_->seek_table.is_last = last_block;
		decoder->private_->seek_table.length = length;
704

705
		decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
706

707
708
		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;
709
710
			return false;
		}
711
712
		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))
713
				return false; /* the read_callback_ sets the state for us */
714
			decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
715

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

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

724
			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
725
				real_points++;
726
		}
727
		decoder->private_->seek_table.data.seek_table.num_points = real_points;
728

729
730
		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
731
732
733
734
	}
	else {
		/* skip other metadata blocks */
		for(i = 0; i < length; i++) {
735
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
736
737
738
739
740
				return false; /* the read_callback_ sets the state for us */
		}
	}

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

	return true;
}

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

	/* skip the version and flags bytes */
752
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 24, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
753
754
755
756
		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++) {
757
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
758
759
760
761
762
763
			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++) {
764
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
765
766
767
768
769
			return false; /* the read_callback_ sets the state for us */
	}
	return true;
}

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

	/* 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. */
777
778
779
	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
780
781
782
783
784
			return true;
		}
	}

	/* make sure we're byte aligned */
785
786
	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
787
788
789
790
			return false; /* the read_callback_ sets the state for us */
	}

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

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

	return true;
}

Josh Coalson's avatar
Josh Coalson committed
825
FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
Josh Coalson's avatar
Josh Coalson committed
826
827
828
{
	unsigned channel;
	unsigned i;
Josh Coalson's avatar
Josh Coalson committed
829
830
831
	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
832
833
834

	*got_a_frame = false;

835
836
	/* init the CRC */
	frame_crc = 0;
837
838
839
	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);
840

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

887
888
889
	/*
	 * Read the frame CRC-16 from the footer and check
	 */
890
891
	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))
892
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
893
	if(frame_crc == (FLAC__uint16)x) {
894
		/* Undo any special channel coding */
895
		switch(decoder->private_->frame.header.channel_assignment) {
896
897
898
899
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				/* do nothing */
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
900
901
902
				FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
				for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
					decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[