stream_decoder.c 42.9 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* libFLAC - Free Lossless Audio Coder 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
22
23
24
25
26
27
28
29
30
 *
 * 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 <assert.h>
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include "FLAC/stream_decoder.h"
#include "private/bitbuffer.h"
#include "private/crc.h"
#include "private/fixed.h"
#include "private/lpc.h"

typedef struct FLAC__StreamDecoderPrivate {
	FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
31
	FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
Josh Coalson's avatar
Josh Coalson committed
32
33
34
35
36
	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);
	void *client_data;
	FLAC__BitBuffer input;
	int32 *output[FLAC__MAX_CHANNELS];
37
	int32 *residual[FLAC__MAX_CHANNELS];
Josh Coalson's avatar
Josh Coalson committed
38
39
40
41
42
	unsigned output_capacity;
	uint32 last_frame_number;
	uint64 samples_decoded;
	bool has_stream_header;
	FLAC__StreamMetaData stream_header;
43
	FLAC__Frame frame;
Josh Coalson's avatar
Josh Coalson committed
44
45
46
47
} FLAC__StreamDecoderPrivate;

static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };

48
static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size);
Josh Coalson's avatar
Josh Coalson committed
49
50
51
52
53
54
static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
55
56
57
58
59
static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
60
static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
Josh Coalson's avatar
Josh Coalson committed
61
62
63
static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);

Josh Coalson's avatar
Josh Coalson committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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_RESYNC_IN_HEADER",
	"FLAC__STREAM_DECODER_END_OF_STREAM",
	"FLAC__STREAM_DECODER_ABORTED",
	"FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
	"FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
	"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[] = {
	"FLAC__STREAM_DECODER_ERROR_LOST_SYNC"
};

Josh Coalson's avatar
Josh Coalson committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
{
	FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
	if(decoder != 0) {
		decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
		decoder->guts = 0;
	}
	return decoder;
}

void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
{
	free(decoder);
}

FLAC__StreamDecoderState FLAC__stream_decoder_init(
	FLAC__StreamDecoder *decoder,
	FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
110
	FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
Josh Coalson's avatar
Josh Coalson committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	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),
	void *client_data
)
{
	unsigned i;

	assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
	assert(decoder != 0);
	assert(read_callback != 0);
	assert(write_callback != 0);
	assert(metadata_callback != 0);
	assert(error_callback != 0);
	assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
	assert(decoder->guts == 0);

	decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;

	decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
	if(decoder->guts == 0)
		return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;

	decoder->guts->read_callback = read_callback;
	decoder->guts->write_callback = write_callback;
	decoder->guts->metadata_callback = metadata_callback;
	decoder->guts->error_callback = error_callback;
	decoder->guts->client_data = client_data;

	FLAC__bitbuffer_init(&decoder->guts->input);

141
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson's avatar
Josh Coalson committed
142
		decoder->guts->output[i] = 0;
143
144
		decoder->guts->residual[i] = 0;
	}
Josh Coalson's avatar
Josh Coalson committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

	decoder->guts->output_capacity = 0;
	decoder->guts->last_frame_number = 0;
	decoder->guts->samples_decoded = 0;
	decoder->guts->has_stream_header = false;

	return decoder->state;
}

void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
{
	unsigned i;
	assert(decoder != 0);
	if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
		return;
	if(decoder->guts != 0) {
		FLAC__bitbuffer_free(&decoder->guts->input);
		for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
			if(decoder->guts->output[i] != 0) {
				free(decoder->guts->output[i]);
				decoder->guts->output[i] = 0;
			}
167
168
169
170
			if(decoder->guts->residual[i] != 0) {
				free(decoder->guts->residual[i]);
				decoder->guts->residual[i] = 0;
			}
Josh Coalson's avatar
Josh Coalson committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
		}
		free(decoder->guts);
		decoder->guts = 0;
	}
	decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
}

bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
{
	assert(decoder != 0);

	if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
		decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	return true;
}

bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
{
	assert(decoder != 0);

	if(!FLAC__stream_decoder_flush(decoder)) {
		decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}
	decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;

	return true;
}

bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
{
	bool dummy;
	assert(decoder != 0);

	if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
		return true;

	assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);

	if(!FLAC__stream_decoder_reset(decoder)) {
		decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	while(1) {
		switch(decoder->state) {
			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:
				assert(0);
		}
	}
}

bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
{
	assert(decoder != 0);

	if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
		return true;

	assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);

	if(!FLAC__stream_decoder_reset(decoder)) {
		decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
		return false;
	}

	while(1) {
		switch(decoder->state) {
			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:
				assert(0);
		}
	}
}

bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
{
	bool got_a_frame;
	assert(decoder != 0);

	if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
		return true;

	assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);

	while(1) {
		switch(decoder->state) {
			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:
				assert(0);
		}
	}
}

bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
{
	bool dummy;
	assert(decoder != 0);

	if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
		return true;

	assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);

	while(1) {
		switch(decoder->state) {
			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:
				assert(0);
		}
	}
}

unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
{
	assert(decoder != 0);
	return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
}

343
bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size)
Josh Coalson's avatar
Josh Coalson committed
344
345
346
347
348
349
350
{
	unsigned i;
	int32 *tmp;

	if(size <= decoder->guts->output_capacity)
		return true;

351
352
	/* @@@ should change to use realloc() */

Josh Coalson's avatar
Josh Coalson committed
353
354
355
356
357
	for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
		if(decoder->guts->output[i] != 0) {
			free(decoder->guts->output[i]);
			decoder->guts->output[i] = 0;
		}
358
359
360
361
		if(decoder->guts->residual[i] != 0) {
			free(decoder->guts->residual[i]);
			decoder->guts->residual[i] = 0;
		}
Josh Coalson's avatar
Josh Coalson committed
362
363
	}

364
	for(i = 0; i < decoder->guts->frame.header.channels; i++) {
Josh Coalson's avatar
Josh Coalson committed
365
366
367
368
369
370
		tmp = (int32*)malloc(sizeof(int32)*size);
		if(tmp == 0) {
			decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		decoder->guts->output[i] = tmp;
371
372
373
374
375
376
377

		tmp = (int32*)malloc(sizeof(int32)*size);
		if(tmp == 0) {
			decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
			return false;
		}
		decoder->guts->residual[i] = tmp;
Josh Coalson's avatar
Josh Coalson committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
	}

	decoder->guts->output_capacity = size;

	return true;
}

bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
{
	uint32 x;
	unsigned i, id;
	bool first = 1;

	assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */

	for(i = id = 0; i < 4; ) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		if(x == FLAC__STREAM_SYNC_STRING[i]) {
			first = 1;
			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 */
			unsigned y;
			if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
			if(!y) { /* MAGIC NUMBER for the last sync bit */
				decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
				return true;
			}
		}
		i = 0;
		if(first) {
			decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
			first = 0;
		}
	}

	decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
	return true;
}

bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
{
	uint32 i, x, last_block, type, length;

	assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */

	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
		return false; /* the read_callback_ sets the state for us */
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
		return false; /* the read_callback_ sets the state for us */
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
		return false; /* the read_callback_ sets the state for us */
	if(type == FLAC__METADATA_TYPE_ENCODING) {
444
		unsigned used_bits = 0;
Josh Coalson's avatar
Josh Coalson committed
445
446
447
		decoder->guts->stream_header.type = type;
		decoder->guts->stream_header.is_last = last_block;
		decoder->guts->stream_header.length = length;
448

Josh Coalson's avatar
Josh Coalson committed
449
450
451
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.min_blocksize = x;
452
453
		used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN;

Josh Coalson's avatar
Josh Coalson committed
454
455
456
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.max_blocksize = x;
457
458
		used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN;

Josh Coalson's avatar
Josh Coalson committed
459
460
461
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.min_framesize = x;
462
463
		used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN;

Josh Coalson's avatar
Josh Coalson committed
464
465
466
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.max_framesize = x;
467
468
		used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN;

Josh Coalson's avatar
Josh Coalson committed
469
470
471
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.sample_rate = x;
472
473
		used_bits += FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN;

Josh Coalson's avatar
Josh Coalson committed
474
475
476
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.channels = x+1;
477
478
		used_bits += FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN;

Josh Coalson's avatar
Josh Coalson committed
479
480
481
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
482
483
		used_bits += FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN;

Josh Coalson's avatar
Josh Coalson committed
484
485
		if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_header.data.encoding.total_samples, FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
486
487
		used_bits += FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN;

Josh Coalson's avatar
Josh Coalson committed
488
489
490
491
492
493
494
		for(i = 0; i < 16; i++) {
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
			decoder->guts->stream_header.data.encoding.md5sum[i] = (byte)x;
		}
		used_bits += 128;

495
496
497
498
499
500
501
502
		/* skip the rest of the block */
		assert(used_bits % 8 == 0);
		length -= (used_bits / 8);
		for(i = 0; i < length; i++) {
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
		}

Josh Coalson's avatar
Josh Coalson committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
		decoder->guts->has_stream_header = true;
		decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
	}
	else {
		/* skip other metadata blocks */
		for(i = 0; i < length; i++) {
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
		}
	}

	if(last_block)
		decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;

	return true;
}

bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
{
	uint32 x;
	unsigned i, skip;

	/* skip the version and flags bytes */
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
		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++) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			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++) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
	}
	return true;
}

bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
{
	uint32 x;
	bool first = 1;

	/* 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. */
	if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
		if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
			decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
			return true;
		}
	}

	/* make sure we're byte aligned */
	if(decoder->guts->input.consumed_bits != 0) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
	}

	while(1) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
			unsigned y;
			if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
			if(!y) { /* MAGIC NUMBER for the last sync bit */
				decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
				return true;
			}
		}
		if(first) {
			decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
			first = 0;
		}
	}

	return true;
}

bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
{
	unsigned channel;
	unsigned i;
	int32 mid, side, left, right;

	*got_a_frame = false;

	if(!stream_decoder_read_frame_header_(decoder))
		return false;
	if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
		if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
			decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
		else
			decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
602
	if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize))
Josh Coalson's avatar
Josh Coalson committed
603
		return false;
604
	for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
		/*
		 * first figure the correct bits-per-sample of the subframe
		 */
		unsigned bps = decoder->guts->frame.header.bits_per_sample;
		switch(decoder->guts->frame.header.channel_assignment) {
			case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
				/* no adjustment needed */
				break;
			case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
				if(channel == 1)
					bps++;
				break;
			case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
				if(channel == 0)
					bps++;
				break;
			case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
				if(channel == 1)
					bps++;
				break;
			default:
				assert(0);
		}
		/*
		 * now read it
		 */
		if(!stream_decoder_read_subframe_(decoder, channel, bps))
Josh Coalson's avatar
Josh Coalson committed
632
633
634
635
636
637
638
639
640
641
			return false;
		if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
			decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		}
	}
	if(!stream_decoder_read_zero_padding_(decoder))
		return false;

	/* Undo any special channel coding */
642
	switch(decoder->guts->frame.header.channel_assignment) {
Josh Coalson's avatar
Josh Coalson committed
643
644
645
646
		case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
			/* do nothing */
			break;
		case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
647
648
			assert(decoder->guts->frame.header.channels == 2);
			for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
Josh Coalson's avatar
Josh Coalson committed
649
650
651
				decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
			break;
		case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
652
653
			assert(decoder->guts->frame.header.channels == 2);
			for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
Josh Coalson's avatar
Josh Coalson committed
654
655
656
				decoder->guts->output[0][i] += decoder->guts->output[1][i];
			break;
		case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
657
658
			assert(decoder->guts->frame.header.channels == 2);
			for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
Josh Coalson's avatar
Josh Coalson committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
				mid = decoder->guts->output[0][i];
				side = decoder->guts->output[1][i];
				mid <<= 1;
				if(side & 1) /* i.e. if 'side' is odd... */
					mid++;
				left = mid + side;
				right = mid - side;
				decoder->guts->output[0][i] = left >> 1;
				decoder->guts->output[1][i] = right >> 1;
			}
			break;
		default:
			assert(0);
			break;
	}

	*got_a_frame = true;

	/* put the latest values into the public section of the decoder instance */
678
679
680
681
682
	decoder->channels = decoder->guts->frame.header.channels;
	decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
	decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
	decoder->sample_rate = decoder->guts->frame.header.sample_rate;
	decoder->blocksize = decoder->guts->frame.header.blocksize;
Josh Coalson's avatar
Josh Coalson committed
683

684
	decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
Josh Coalson's avatar
Josh Coalson committed
685
686

	/* write it */
687
	if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
Josh Coalson's avatar
Josh Coalson committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
		return false;

	decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
	return true;
}

bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
{
	uint32 x;
	uint64 xx;
	unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
	byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
	unsigned raw_header_len;
	bool is_unparseable = false;

	assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */

	/* init the raw header with the first 8 bits of the sync code */
	raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
	raw_header_len = 1;

	/*
	 * read in the raw header as bytes so we can CRC it, and parse it on the way
	 */
	for(i = 0; i < 2; i++) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
			/* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
			uint32 y;
			if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
			if(!y) { /* MAGIC NUMBER for the last sync bit */
				decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
				return true;
			}
			else {
				decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
				decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
				return true;
			}
		}
		raw_header[raw_header_len++] = (byte)x;
	}
	assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */

	switch(x = raw_header[1] >> 4) {
		case 0:
			if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.min_blocksize == decoder->guts->stream_header.data.encoding.max_blocksize) /* i.e. it's a fixed-blocksize stream */
737
				decoder->guts->frame.header.blocksize = decoder->guts->stream_header.data.encoding.min_blocksize;
Josh Coalson's avatar
Josh Coalson committed
738
739
740
741
			else
				is_unparseable = true;
			break;
		case 1:
742
			decoder->guts->frame.header.blocksize = 192;
Josh Coalson's avatar
Josh Coalson committed
743
744
745
746
747
			break;
		case 2:
		case 3:
		case 4:
		case 5:
748
			decoder->guts->frame.header.blocksize = 576 << (x-2);
Josh Coalson's avatar
Josh Coalson committed
749
750
751
752
753
754
755
756
757
758
759
760
761
			break;
		case 6:
		case 7:
			blocksize_hint = x;
			break;
		default:
			assert(0);
			break;
	}

	switch(x = raw_header[1] & 0x0f) {
		case 0:
			if(decoder->guts->has_stream_header)
762
				decoder->guts->frame.header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
Josh Coalson's avatar
Josh Coalson committed
763
764
765
766
767
768
769
770
771
			else
				is_unparseable = true;
			break;
		case 1:
		case 2:
		case 3:
			is_unparseable = true;
			break;
		case 4:
772
			decoder->guts->frame.header.sample_rate = 8000;
Josh Coalson's avatar
Josh Coalson committed
773
774
			break;
		case 5:
775
			decoder->guts->frame.header.sample_rate = 16000;
Josh Coalson's avatar
Josh Coalson committed
776
777
			break;
		case 6:
778
			decoder->guts->frame.header.sample_rate = 22050;
Josh Coalson's avatar
Josh Coalson committed
779
780
			break;
		case 7:
781
			decoder->guts->frame.header.sample_rate = 24000;
Josh Coalson's avatar
Josh Coalson committed
782
783
			break;
		case 8:
784
			decoder->guts->frame.header.sample_rate = 32000;
Josh Coalson's avatar
Josh Coalson committed
785
786
			break;
		case 9:
787
			decoder->guts->frame.header.sample_rate = 44100;
Josh Coalson's avatar
Josh Coalson committed
788
789
			break;
		case 10:
790
			decoder->guts->frame.header.sample_rate = 48000;
Josh Coalson's avatar
Josh Coalson committed
791
792
			break;
		case 11:
793
			decoder->guts->frame.header.sample_rate = 96000;
Josh Coalson's avatar
Josh Coalson committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
			break;
		case 12:
		case 13:
		case 14:
			sample_rate_hint = x;
			break;
		case 15:
			decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
			decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
			return true;
		default:
			assert(0);
	}

	x = (unsigned)(raw_header[2] >> 4);
	if(x & 8) {
810
		decoder->guts->frame.header.channels = 2;
Josh Coalson's avatar
Josh Coalson committed
811
812
		switch(x & 7) {
			case 0:
813
				decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
Josh Coalson's avatar
Josh Coalson committed
814
815
				break;
			case 1:
816
				decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
Josh Coalson's avatar
Josh Coalson committed
817
818
				break;
			case 2:
819
				decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
Josh Coalson's avatar
Josh Coalson committed
820
821
822
823
824
825
826
				break;
			default:
				is_unparseable = true;
				break;
		}
	}
	else {
827
828
		decoder->guts->frame.header.channels = (unsigned)x + 1;
		decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
Josh Coalson's avatar
Josh Coalson committed
829
830
831
832
833
	}

	switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
		case 0:
			if(decoder->guts->has_stream_header)
834
				decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
Josh Coalson's avatar
Josh Coalson committed
835
836
837
838
			else
				is_unparseable = true;
			break;
		case 1:
839
			decoder->guts->frame.header.bits_per_sample = 8;
Josh Coalson's avatar
Josh Coalson committed
840
841
			break;
		case 2:
842
			decoder->guts->frame.header.bits_per_sample = 12;
Josh Coalson's avatar
Josh Coalson committed
843
844
			break;
		case 4:
845
			decoder->guts->frame.header.bits_per_sample = 16;
Josh Coalson's avatar
Josh Coalson committed
846
847
			break;
		case 5:
848
			decoder->guts->frame.header.bits_per_sample = 20;
Josh Coalson's avatar
Josh Coalson committed
849
850
			break;
		case 6:
851
			decoder->guts->frame.header.bits_per_sample = 24;
Josh Coalson's avatar
Josh Coalson committed
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
			break;
		case 3:
		case 7:
			is_unparseable = true;
			break;
		default:
			assert(0);
			break;
	}

	if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
		decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
		decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}

	if(blocksize_hint) {
		if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
			return false; /* the read_callback_ sets the state for us */
		if(xx == 0xffffffffffffffff) {
			if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
				uint32 y;
				if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
					return false; /* the read_callback_ sets the state for us */
				if(!y) { /* MAGIC NUMBER for the last sync bit */
					decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
					return true;
				}
				else {
					decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
					decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
					return true;
				}
			}
			else {
				decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
				decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
				return true;
			}
		}
		if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.min_blocksize == decoder->guts->stream_header.data.encoding.max_blocksize) /* i.e. it's a fixed-blocksize stream */
893
			decoder->guts->frame.header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.encoding.min_blocksize + xx;
Josh Coalson's avatar
Josh Coalson committed
894
		else
895
			decoder->guts->frame.header.number.sample_number = xx;
Josh Coalson's avatar
Josh Coalson committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
	}
	else {
		if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
			return false; /* the read_callback_ sets the state for us */
		if(x == 0xffffffff) {
			if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
				uint32 y;
				if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
					return false; /* the read_callback_ sets the state for us */
				if(!y) { /* MAGIC NUMBER for the last sync bit */
					decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
					return true;
				}
				else {
					decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
					decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
					return true;
				}
			}
			else {
				decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
				decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
				return true;
			}
		}
		decoder->guts->last_frame_number = x;
		if(decoder->guts->has_stream_header) {
923
			decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_header.data.encoding.min_blocksize * (int64)x;
Josh Coalson's avatar
Josh Coalson committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
		}
		else {
			is_unparseable = true;
		}
	}

	if(blocksize_hint) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		raw_header[raw_header_len++] = (byte)x;
		if(blocksize_hint == 7) {
			uint32 _x;
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
			raw_header[raw_header_len++] = (byte)_x;
			x = (x << 8) | _x;
		}
941
		decoder->guts->frame.header.blocksize = x+1;
Josh Coalson's avatar
Josh Coalson committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
	}

	if(sample_rate_hint) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		raw_header[raw_header_len++] = (byte)x;
		if(sample_rate_hint != 12) {
			uint32 _x;
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
			raw_header[raw_header_len++] = (byte)_x;
			x = (x << 8) | _x;
		}
		if(sample_rate_hint == 12)
956
			decoder->guts->frame.header.sample_rate = x*1000;
Josh Coalson's avatar
Josh Coalson committed
957
		else if(sample_rate_hint == 13)
958
			decoder->guts->frame.header.sample_rate = x;
Josh Coalson's avatar
Josh Coalson committed
959
		else
960
			decoder->guts->frame.header.sample_rate = x*10;
Josh Coalson's avatar
Josh Coalson committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
	}

	/* read the crc byte */
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
		return false; /* the read_callback_ sets the state for us */
	crc = (byte)x;

	if(FLAC__crc8(raw_header, raw_header_len) != crc) {
		decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
		decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}

	if(is_unparseable) {
		decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
		return false;
	}

	return true;
}

982
bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
Josh Coalson's avatar
Josh Coalson committed
983
984
985
{
	uint32 x;

986
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_TYPE_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
987
988
989
990
991
992
993
		return false; /* the read_callback_ sets the state for us */
	if(x & 0x01 || x & 0x80) {
		decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
		decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
	else if(x == 0) {
994
		return stream_decoder_read_subframe_constant_(decoder, channel, bps);
Josh Coalson's avatar
Josh Coalson committed
995
996
	}
	else if(x == 2) {
997
		return stream_decoder_read_subframe_verbatim_(decoder, channel, bps);
Josh Coalson's avatar
Josh Coalson committed
998
999
1000
1001
1002
1003
	}
	else if(x < 16) {
		decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
		return false;
	}
	else if(x <= 24) {
1004
		return stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7);
Josh Coalson's avatar
Josh Coalson committed
1005
1006
1007
1008
1009
1010
	}
	else if(x < 64) {
		decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
		return false;
	}
	else {
1011
		return stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1);
Josh Coalson's avatar
Josh Coalson committed
1012
1013
1014
	}
}

1015
bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
Josh Coalson's avatar
Josh Coalson committed
1016
{
1017
	FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
Josh Coalson's avatar
Josh Coalson committed
1018
1019
	int32 x;
	unsigned i;
1020
	int32 *output = decoder->guts->output[channel];
Josh Coalson's avatar
Josh Coalson committed
1021

1022
1023
	decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;

1024
	if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1025
1026
		return false; /* the read_callback_ sets the state for us */

1027
1028
1029
1030
1031
	subframe->value = x;

	/* decode the subframe */
	for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
		output[i] = x;
Josh Coalson's avatar
Josh Coalson committed
1032
1033
1034
1035

	return true;
}

1036
bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
Josh Coalson's avatar
Josh Coalson committed
1037
{
1038
	FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
Josh Coalson's avatar
Josh Coalson committed
1039
1040
1041
1042
	int32 i32;
	uint32 u32;
	unsigned u;

1043
1044
1045
1046
	decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;

	subframe->residual = decoder->guts->residual[channel];
	subframe->order = order;
Josh Coalson's avatar
Josh Coalson committed
1047
1048
1049

	/* read warm-up samples */
	for(u = 0; u < order; u++) {
1050
		if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1051
			return false; /* the read_callback_ sets the state for us */
1052
		subframe->warmup[u] = i32;
Josh Coalson's avatar
Josh Coalson committed
1053
1054
1055
1056
1057
	}

	/* read entropy coding method info */
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
		return false; /* the read_callback_ sets the state for us */
1058
1059
	subframe->entropy_coding_method.type = u32;
	switch(subframe->entropy_coding_method.type) {
Josh Coalson's avatar
Josh Coalson committed
1060
1061
1062
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
1063
			subframe->entropy_coding_method.data.partitioned_rice.order = u32;
Josh Coalson's avatar
Josh Coalson committed
1064
1065
1066
1067
1068
1069
1070
			break;
		default:
			decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
			return false;
	}

	/* read residual */
1071
	switch(subframe->entropy_coding_method.type) {
Josh Coalson's avatar
Josh Coalson committed
1072
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1073
			if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
Josh Coalson's avatar
Josh Coalson committed
1074
1075
1076
1077
1078
1079
1080
				return false;
			break;
		default:
			assert(0);
	}

	/* decode the subframe */
1081
1082
	memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
	FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
Josh Coalson's avatar
Josh Coalson committed
1083
1084
1085
1086

	return true;
}

1087
bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
Josh Coalson's avatar
Josh Coalson committed
1088
{
1089
	FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
Josh Coalson's avatar
Josh Coalson committed
1090
1091
1092
1093
	int32 i32;
	uint32 u32;
	unsigned u;

1094
1095
1096
1097
	decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;

	subframe->residual = decoder->guts->residual[channel];
	subframe->order = order;
Josh Coalson's avatar
Josh Coalson committed
1098
1099
1100

	/* read warm-up samples */
	for(u = 0; u < order; u++) {
1101
		if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1102
			return false; /* the read_callback_ sets the state for us */
1103
		subframe->warmup[u] = i32;
Josh Coalson's avatar
Josh Coalson committed
1104
1105
1106
	}

	/* read qlp coeff precision */
1107
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1108
1109
1110
1111
1112
1113
		return false; /* the read_callback_ sets the state for us */
	if(u32 == 15) {
		decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
		decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		return true;
	}
1114
	subframe->qlp_coeff_precision = u32+1;
Josh Coalson's avatar
Josh Coalson committed
1115
1116

	/* read qlp shift */
1117
	if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1118
		return false; /* the read_callback_ sets the state for us */
1119
	subframe->quantization_level = i32;
Josh Coalson's avatar
Josh Coalson committed
1120
1121
1122

	/* read quantized lp coefficiencts */
	for(u = 0; u < order; u++) {
1123
		if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1124
			return false; /* the read_callback_ sets the state for us */
1125
		subframe->qlp_coeff[u] = i32;
Josh Coalson's avatar
Josh Coalson committed
1126
1127
1128
1129
1130
	}

	/* read entropy coding method info */
	if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
		return false; /* the read_callback_ sets the state for us */
1131
1132
	subframe->entropy_coding_method.type = u32;
	switch(subframe->entropy_coding_method.type) {
Josh Coalson's avatar
Josh Coalson committed
1133
1134
1135
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
			if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
1136
			subframe->entropy_coding_method.data.partitioned_rice.order = u32;
Josh Coalson's avatar
Josh Coalson committed
1137
1138
1139
1140
1141
1142
1143
			break;
		default:
			decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
			return false;
	}

	/* read residual */
1144
	switch(subframe->entropy_coding_method.type) {
Josh Coalson's avatar
Josh Coalson committed
1145
		case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1146
			if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
Josh Coalson's avatar
Josh Coalson committed
1147
1148
1149
1150
1151
1152
1153
				return false;
			break;
		default:
			assert(0);
	}

	/* decode the subframe */
1154
1155
	memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
	FLAC__lpc_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->guts->output[channel]+order);
Josh Coalson's avatar
Josh Coalson committed
1156
1157
1158
1159

	return true;
}

1160
bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
Josh Coalson's avatar
Josh Coalson committed
1161
{
1162
	FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
1163
	int32 x, *residual = decoder->guts->residual[channel];
Josh Coalson's avatar
Josh Coalson committed
1164
1165
	unsigned i;

1166
1167
	decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;

1168
	subframe->data = residual;
1169
1170

	for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
1171
		if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
Josh Coalson's avatar
Josh Coalson committed
1172
			return false; /* the read_callback_ sets the state for us */
1173
		residual[i] = x;
Josh Coalson's avatar
Josh Coalson committed
1174
1175
	}

1176
1177
1178
	/* decode the subframe */
	memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);

Josh Coalson's avatar
Josh Coalson committed
1179
1180
1181
	return true;
}

1182
bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
Josh Coalson's avatar
Josh Coalson committed
1183
1184
1185
1186
1187
{
	uint32 rice_parameter;
	int i;
	unsigned partition, sample, u;
	const unsigned partitions = 1u << partition_order;
1188
	const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
Josh Coalson's avatar
Josh Coalson committed
1189
1190
1191
1192
1193
1194
1195
1196

	sample = 0;
	for(partition = 0; partition < partitions; partition++) {
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
			if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
				return false; /* the read_callback_ sets the state for us */
1197
			residual[sample] = i;
Josh Coalson's avatar
Josh Coalson committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
		}
	}

	return true;
}

bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
{
	if(decoder->guts->input.consumed_bits != 0) {
Josh Coalson's avatar
Josh Coalson committed
1207
		uint32 zero = 0;
Josh Coalson's avatar
Josh Coalson committed
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
		if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
			return false; /* the read_callback_ sets the state for us */
		if(zero != 0) {
			decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
			decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
		}
	}
	return true;
}

bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
{
	FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
	FLAC__StreamDecoderReadStatus status;
	status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
	if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
		decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
	else if(status == FLAC__STREAM_DECODER_READ_ABORT)
		decoder->state = FLAC__STREAM_DECODER_ABORTED;
	return status == FLAC__STREAM_DECODER_READ_CONTINUE;
}