stream_decoder.c 57.6 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
145
146
147
148
149
150
151
152
153
154
155
156
	decoder->protected = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
	if(decoder->protected == 0) {
		free(decoder);
		return 0;
	}
	decoder->private = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
	if(decoder->private == 0) {
		free(decoder->protected);
		free(decoder);
		return 0;
	}

	decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;

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

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
169
170
171
172
173
	FLAC__ASSERT(decoder != 0);
	FLAC__ASSERT(decoder->protected != 0);
	FLAC__ASSERT(decoder->private != 0);

	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
	FLAC__ASSERT(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
	if(decoder->private->cpuinfo.data.ia32.mmx) {
Josh Coalson's avatar
Josh Coalson committed
226
227
		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 {
Josh Coalson's avatar
Josh Coalson committed
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
#endif

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

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

Josh Coalson's avatar
Josh Coalson committed
267
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))
268
269
270
271
272
273
274
{
	if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private->read_callback = value;
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
275
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))
276
277
278
279
280
281
282
{
	if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private->write_callback = value;
	return true;
}

Josh Coalson's avatar
Josh Coalson committed
283
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))
284
285
286
287
288
289
290
{
	if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
		return false;
	decoder->private->metadata_callback = value;
	return true;
}

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

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

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

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

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

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

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

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

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

341
342
	if(!FLAC__bitbuffer_clear(&decoder->private->input)) {
		decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson's avatar
Josh Coalson committed
343
344
345
346
347
348
		return false;
	}

	return true;
}

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

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

359
	decoder->private->samples_decoded = 0;
Josh Coalson's avatar
Josh Coalson committed
360

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return true;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

700
701
702
		decoder->private->seek_table.type = type;
		decoder->private->seek_table.is_last = last_block;
		decoder->private->seek_table.length = length;
703

704
		decoder->private->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
705

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

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

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

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

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

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

	return true;
}

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

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

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

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

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

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

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

	return true;
}

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

	*got_a_frame = false;

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

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

886
887
888
	/*
	 * Read the frame CRC-16 from the footer and check
	 */
889
890
	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))
891
		return false; /* the read_callback_ sets the state for us */
Josh Coalson's avatar
Josh Coalson committed
892
	if(frame_crc == (FLAC__uint16)x) {
893
		/* Undo any special channel coding */
894
		switch(decoder->private->frame.header.channel_assignment) {
895
896
897
898
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				/* do nothing */
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
899
900
901
				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[1][i];
902
903
				break;
			case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
904
905
906
				FLAC__ASSERT(decoder->private->frame.header.channels == 2);
				for(i = 0; i < decoder->private->frame.header.blocksize; i++)
					decoder->private->output[0][i] += decoder->private->output[1][i];
907
908
				break;
			case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
909
910
911
912
				FLAC__ASSERT(decoder->private->frame.header.channels == 2);
				for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
					mid = decoder->private->output[0][i];
					side = decoder->private->output[1][i];
913
914
915
916
917
					mid <<= 1;
					if(side & 1) /* i.e. if 'side' is odd... */
						mid++;
					left = mid + side;
					right = mid - side;
918
919
					decoder->private->output[0][i] = left >> 1;
					decoder->private->output[1][i] = right >> 1;
920
921
922
				}
				break;
			default:
923
				FLAC__ASSERT(0);
924
925
926
927
928
				break;
		}
	}
	else {
		/* Bad frame, emit error and zero the output signal */
929
930
		decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private->client_data);
		for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
Josh Coalson's avatar
Josh Coalson committed
931
			memset(decoder->private->output[channel], 0, sizeof(FLAC__int32) * decoder->private->frame.header.blocksize);
932
		}
Josh Coalson's avatar
Josh Coalson committed
933
934
935
936
937
	}

	*got_a_frame = true;

	/* put the latest values into the public section of the decoder instance */
938
939
940
941
942
	decoder->protected->channels = decoder->private->frame.header.channels;
	decoder->protected->channel_assignment = decoder->private->frame.header.channel_assignment;
	decoder->protected->bits_per_sample = decoder->private->frame.header.bits_per_sample;
	decoder->protected->sample_rate = decoder->private->frame.header.sample_rate;
	decoder->protected->blocksize = decoder->private->frame.header.blocksize;
Josh Coalson's avatar
Josh Coalson committed
943

944
	FLAC__ASSERT(decoder->private->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
945
	decoder->private->samples_decoded = decoder->private->frame.header.number.sample_number + decoder->private->frame.header.blocksize;
Josh Coalson's avatar
Josh Coalson committed
946
947

	/* write it */
948
	/* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
949
	if(decoder->private->write_callback(decoder, &decoder->private->frame, decoder->private->output, decoder->private->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)