change Coder to Codec everywhere
[flac.git] / src / libFLAC / stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h> /* for malloc() */
23 #include "FLAC/stream_decoder.h"
24 #include "private/bitbuffer.h"
25 #include "private/crc.h"
26 #include "private/fixed.h"
27 #include "private/lpc.h"
28
29 typedef struct FLAC__StreamDecoderPrivate {
30         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);
32         void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
33         void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
34         void *client_data;
35         FLAC__BitBuffer input;
36         int32 *output[FLAC__MAX_CHANNELS];
37         int32 *residual[FLAC__MAX_CHANNELS];
38         unsigned output_capacity;
39         uint32 last_frame_number;
40         uint64 samples_decoded;
41         bool has_stream_header;
42         FLAC__StreamMetaData stream_header;
43         FLAC__Frame frame;
44 } FLAC__StreamDecoderPrivate;
45
46 static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
47
48 static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size);
49 static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
50 static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
51 static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
52 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
53 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
54 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
55 static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
56 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
57 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
58 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
59 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);
61 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
62 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
63
64 const char *FLAC__StreamDecoderStateString[] = {
65         "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
66         "FLAC__STREAM_DECODER_READ_METADATA",
67         "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
68         "FLAC__STREAM_DECODER_READ_FRAME",
69         "FLAC__STREAM_DECODER_RESYNC_IN_HEADER",
70         "FLAC__STREAM_DECODER_END_OF_STREAM",
71         "FLAC__STREAM_DECODER_ABORTED",
72         "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
73         "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
74         "FLAC__STREAM_DECODER_UNINITIALIZED"
75 };
76
77 const char *FLAC__StreamDecoderReadStatusString[] = {
78         "FLAC__STREAM_DECODER_READ_CONTINUE",
79         "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
80         "FLAC__STREAM_DECODER_READ_ABORT"
81 };
82
83 const char *FLAC__StreamDecoderWriteStatusString[] = {
84         "FLAC__STREAM_DECODER_WRITE_CONTINUE",
85         "FLAC__STREAM_DECODER_WRITE_ABORT"
86 };
87
88 const char *FLAC__StreamDecoderErrorStatusString[] = {
89         "FLAC__STREAM_DECODER_ERROR_LOST_SYNC"
90 };
91
92 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
93 {
94         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
95         if(decoder != 0) {
96                 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
97                 decoder->guts = 0;
98         }
99         return decoder;
100 }
101
102 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
103 {
104         free(decoder);
105 }
106
107 FLAC__StreamDecoderState FLAC__stream_decoder_init(
108         FLAC__StreamDecoder *decoder,
109         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),
111         void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
112         void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
113         void *client_data
114 )
115 {
116         unsigned i;
117
118         assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
119         assert(decoder != 0);
120         assert(read_callback != 0);
121         assert(write_callback != 0);
122         assert(metadata_callback != 0);
123         assert(error_callback != 0);
124         assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
125         assert(decoder->guts == 0);
126
127         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
128
129         decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
130         if(decoder->guts == 0)
131                 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
132
133         decoder->guts->read_callback = read_callback;
134         decoder->guts->write_callback = write_callback;
135         decoder->guts->metadata_callback = metadata_callback;
136         decoder->guts->error_callback = error_callback;
137         decoder->guts->client_data = client_data;
138
139         FLAC__bitbuffer_init(&decoder->guts->input);
140
141         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
142                 decoder->guts->output[i] = 0;
143                 decoder->guts->residual[i] = 0;
144         }
145
146         decoder->guts->output_capacity = 0;
147         decoder->guts->last_frame_number = 0;
148         decoder->guts->samples_decoded = 0;
149         decoder->guts->has_stream_header = false;
150
151         return decoder->state;
152 }
153
154 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
155 {
156         unsigned i;
157         assert(decoder != 0);
158         if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
159                 return;
160         if(decoder->guts != 0) {
161                 FLAC__bitbuffer_free(&decoder->guts->input);
162                 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
163                         if(decoder->guts->output[i] != 0) {
164                                 free(decoder->guts->output[i]);
165                                 decoder->guts->output[i] = 0;
166                         }
167                         if(decoder->guts->residual[i] != 0) {
168                                 free(decoder->guts->residual[i]);
169                                 decoder->guts->residual[i] = 0;
170                         }
171                 }
172                 free(decoder->guts);
173                 decoder->guts = 0;
174         }
175         decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
176 }
177
178 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
179 {
180         assert(decoder != 0);
181
182         if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
183                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
184                 return false;
185         }
186
187         return true;
188 }
189
190 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
191 {
192         assert(decoder != 0);
193
194         if(!FLAC__stream_decoder_flush(decoder)) {
195                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
196                 return false;
197         }
198         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
199
200         return true;
201 }
202
203 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
204 {
205         bool dummy;
206         assert(decoder != 0);
207
208         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
209                 return true;
210
211         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
212
213         if(!FLAC__stream_decoder_reset(decoder)) {
214                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
215                 return false;
216         }
217
218         while(1) {
219                 switch(decoder->state) {
220                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
221                                 if(!stream_decoder_find_metadata_(decoder))
222                                         return false; /* above function sets the status for us */
223                                 break;
224                         case FLAC__STREAM_DECODER_READ_METADATA:
225                                 if(!stream_decoder_read_metadata_(decoder))
226                                         return false; /* above function sets the status for us */
227                                 break;
228                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
229                                 if(!stream_decoder_frame_sync_(decoder))
230                                         return true; /* above function sets the status for us */
231                                 break;
232                         case FLAC__STREAM_DECODER_READ_FRAME:
233                                 if(!stream_decoder_read_frame_(decoder, &dummy))
234                                         return false; /* above function sets the status for us */
235                                 break;
236                         case FLAC__STREAM_DECODER_END_OF_STREAM:
237                                 return true;
238                         default:
239                                 assert(0);
240                 }
241         }
242 }
243
244 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
245 {
246         assert(decoder != 0);
247
248         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
249                 return true;
250
251         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
252
253         if(!FLAC__stream_decoder_reset(decoder)) {
254                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
255                 return false;
256         }
257
258         while(1) {
259                 switch(decoder->state) {
260                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
261                                 if(!stream_decoder_find_metadata_(decoder))
262                                         return false; /* above function sets the status for us */
263                                 break;
264                         case FLAC__STREAM_DECODER_READ_METADATA:
265                                 if(!stream_decoder_read_metadata_(decoder))
266                                         return false; /* above function sets the status for us */
267                                 break;
268                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
269                                 return true;
270                                 break;
271                         case FLAC__STREAM_DECODER_END_OF_STREAM:
272                                 return true;
273                         default:
274                                 assert(0);
275                 }
276         }
277 }
278
279 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
280 {
281         bool got_a_frame;
282         assert(decoder != 0);
283
284         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
285                 return true;
286
287         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
288
289         while(1) {
290                 switch(decoder->state) {
291                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
292                                 if(!stream_decoder_frame_sync_(decoder))
293                                         return true; /* above function sets the status for us */
294                                 break;
295                         case FLAC__STREAM_DECODER_READ_FRAME:
296                                 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
297                                         return false; /* above function sets the status for us */
298                                 if(got_a_frame)
299                                         return true; /* above function sets the status for us */
300                                 break;
301                         case FLAC__STREAM_DECODER_END_OF_STREAM:
302                                 return true;
303                         default:
304                                 assert(0);
305                 }
306         }
307 }
308
309 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
310 {
311         bool dummy;
312         assert(decoder != 0);
313
314         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
315                 return true;
316
317         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
318
319         while(1) {
320                 switch(decoder->state) {
321                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
322                                 if(!stream_decoder_frame_sync_(decoder))
323                                         return true; /* above function sets the status for us */
324                                 break;
325                         case FLAC__STREAM_DECODER_READ_FRAME:
326                                 if(!stream_decoder_read_frame_(decoder, &dummy))
327                                         return false; /* above function sets the status for us */
328                                 break;
329                         case FLAC__STREAM_DECODER_END_OF_STREAM:
330                                 return true;
331                         default:
332                                 assert(0);
333                 }
334         }
335 }
336
337 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
338 {
339         assert(decoder != 0);
340         return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
341 }
342
343 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size)
344 {
345         unsigned i;
346         int32 *tmp;
347
348         if(size <= decoder->guts->output_capacity)
349                 return true;
350
351         /* @@@ should change to use realloc() */
352
353         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
354                 if(decoder->guts->output[i] != 0) {
355                         free(decoder->guts->output[i]);
356                         decoder->guts->output[i] = 0;
357                 }
358                 if(decoder->guts->residual[i] != 0) {
359                         free(decoder->guts->residual[i]);
360                         decoder->guts->residual[i] = 0;
361                 }
362         }
363
364         for(i = 0; i < decoder->guts->frame.header.channels; i++) {
365                 tmp = (int32*)malloc(sizeof(int32)*size);
366                 if(tmp == 0) {
367                         decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
368                         return false;
369                 }
370                 decoder->guts->output[i] = tmp;
371
372                 tmp = (int32*)malloc(sizeof(int32)*size);
373                 if(tmp == 0) {
374                         decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
375                         return false;
376                 }
377                 decoder->guts->residual[i] = tmp;
378         }
379
380         decoder->guts->output_capacity = size;
381
382         return true;
383 }
384
385 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
386 {
387         uint32 x;
388         unsigned i, id;
389         bool first = 1;
390
391         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
392
393         for(i = id = 0; i < 4; ) {
394                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
395                         return false; /* the read_callback_ sets the state for us */
396                 if(x == FLAC__STREAM_SYNC_STRING[i]) {
397                         first = 1;
398                         i++;
399                         id = 0;
400                         continue;
401                 }
402                 if(x == ID3V2_TAG_[id]) {
403                         id++;
404                         i = 0;
405                         if(id == 3) {
406                                 if(!stream_decoder_skip_id3v2_tag_(decoder))
407                                         return false; /* the read_callback_ sets the state for us */
408                         }
409                         continue;
410                 }
411                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
412                         unsigned y;
413                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
414                                 return false; /* the read_callback_ sets the state for us */
415                         if(!y) { /* MAGIC NUMBER for the last sync bit */
416                                 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
417                                 return true;
418                         }
419                 }
420                 i = 0;
421                 if(first) {
422                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
423                         first = 0;
424                 }
425         }
426
427         decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
428         return true;
429 }
430
431 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
432 {
433         uint32 i, x, last_block, type, length;
434
435         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
436
437         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
438                 return false; /* the read_callback_ sets the state for us */
439         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
440                 return false; /* the read_callback_ sets the state for us */
441         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
442                 return false; /* the read_callback_ sets the state for us */
443         if(type == FLAC__METADATA_TYPE_ENCODING) {
444                 unsigned used_bits = 0;
445                 decoder->guts->stream_header.type = type;
446                 decoder->guts->stream_header.is_last = last_block;
447                 decoder->guts->stream_header.length = length;
448
449                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
450                         return false; /* the read_callback_ sets the state for us */
451                 decoder->guts->stream_header.data.encoding.min_blocksize = x;
452                 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN;
453
454                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
455                         return false; /* the read_callback_ sets the state for us */
456                 decoder->guts->stream_header.data.encoding.max_blocksize = x;
457                 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN;
458
459                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
460                         return false; /* the read_callback_ sets the state for us */
461                 decoder->guts->stream_header.data.encoding.min_framesize = x;
462                 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN;
463
464                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
465                         return false; /* the read_callback_ sets the state for us */
466                 decoder->guts->stream_header.data.encoding.max_framesize = x;
467                 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN;
468
469                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
470                         return false; /* the read_callback_ sets the state for us */
471                 decoder->guts->stream_header.data.encoding.sample_rate = x;
472                 used_bits += FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN;
473
474                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
475                         return false; /* the read_callback_ sets the state for us */
476                 decoder->guts->stream_header.data.encoding.channels = x+1;
477                 used_bits += FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN;
478
479                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
480                         return false; /* the read_callback_ sets the state for us */
481                 decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
482                 used_bits += FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN;
483
484                 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))
485                         return false; /* the read_callback_ sets the state for us */
486                 used_bits += FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN;
487
488                 for(i = 0; i < 16; i++) {
489                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
490                                 return false; /* the read_callback_ sets the state for us */
491                         decoder->guts->stream_header.data.encoding.md5sum[i] = (byte)x;
492                 }
493                 used_bits += 128;
494
495                 /* skip the rest of the block */
496                 assert(used_bits % 8 == 0);
497                 length -= (used_bits / 8);
498                 for(i = 0; i < length; i++) {
499                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
500                                 return false; /* the read_callback_ sets the state for us */
501                 }
502
503                 decoder->guts->has_stream_header = true;
504                 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
505         }
506         else {
507                 /* skip other metadata blocks */
508                 for(i = 0; i < length; i++) {
509                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
510                                 return false; /* the read_callback_ sets the state for us */
511                 }
512         }
513
514         if(last_block)
515                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
516
517         return true;
518 }
519
520 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
521 {
522         uint32 x;
523         unsigned i, skip;
524
525         /* skip the version and flags bytes */
526         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
527                 return false; /* the read_callback_ sets the state for us */
528         /* get the size (in bytes) to skip */
529         skip = 0;
530         for(i = 0; i < 4; i++) {
531                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
532                         return false; /* the read_callback_ sets the state for us */
533                 skip <<= 7;
534                 skip |= (x & 0x7f);
535         }
536         /* skip the rest of the tag */
537         for(i = 0; i < skip; i++) {
538                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
539                         return false; /* the read_callback_ sets the state for us */
540         }
541         return true;
542 }
543
544 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
545 {
546         uint32 x;
547         bool first = 1;
548
549         /* If we know the total number of samples in the stream, stop if we've read that many. */
550         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
551         if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
552                 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
553                         decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
554                         return true;
555                 }
556         }
557
558         /* make sure we're byte aligned */
559         if(decoder->guts->input.consumed_bits != 0) {
560                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
561                         return false; /* the read_callback_ sets the state for us */
562         }
563
564         while(1) {
565                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
566                         return false; /* the read_callback_ sets the state for us */
567                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
568                         unsigned y;
569                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
570                                 return false; /* the read_callback_ sets the state for us */
571                         if(!y) { /* MAGIC NUMBER for the last sync bit */
572                                 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
573                                 return true;
574                         }
575                 }
576                 if(first) {
577                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
578                         first = 0;
579                 }
580         }
581
582         return true;
583 }
584
585 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
586 {
587         unsigned channel;
588         unsigned i;
589         int32 mid, side, left, right;
590
591         *got_a_frame = false;
592
593         if(!stream_decoder_read_frame_header_(decoder))
594                 return false;
595         if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
596                 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
597                         decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
598                 else
599                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
600                 return true;
601         }
602         if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize))
603                 return false;
604         for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
605                 /*
606                  * first figure the correct bits-per-sample of the subframe
607                  */
608                 unsigned bps = decoder->guts->frame.header.bits_per_sample;
609                 switch(decoder->guts->frame.header.channel_assignment) {
610                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
611                                 /* no adjustment needed */
612                                 break;
613                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
614                                 if(channel == 1)
615                                         bps++;
616                                 break;
617                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
618                                 if(channel == 0)
619                                         bps++;
620                                 break;
621                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
622                                 if(channel == 1)
623                                         bps++;
624                                 break;
625                         default:
626                                 assert(0);
627                 }
628                 /*
629                  * now read it
630                  */
631                 if(!stream_decoder_read_subframe_(decoder, channel, bps))
632                         return false;
633                 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
634                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
635                         return true;
636                 }
637         }
638         if(!stream_decoder_read_zero_padding_(decoder))
639                 return false;
640
641         /* Undo any special channel coding */
642         switch(decoder->guts->frame.header.channel_assignment) {
643                 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
644                         /* do nothing */
645                         break;
646                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
647                         assert(decoder->guts->frame.header.channels == 2);
648                         for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
649                                 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
650                         break;
651                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
652                         assert(decoder->guts->frame.header.channels == 2);
653                         for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
654                                 decoder->guts->output[0][i] += decoder->guts->output[1][i];
655                         break;
656                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
657                         assert(decoder->guts->frame.header.channels == 2);
658                         for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
659                                 mid = decoder->guts->output[0][i];
660                                 side = decoder->guts->output[1][i];
661                                 mid <<= 1;
662                                 if(side & 1) /* i.e. if 'side' is odd... */
663                                         mid++;
664                                 left = mid + side;
665                                 right = mid - side;
666                                 decoder->guts->output[0][i] = left >> 1;
667                                 decoder->guts->output[1][i] = right >> 1;
668                         }
669                         break;
670                 default:
671                         assert(0);
672                         break;
673         }
674
675         *got_a_frame = true;
676
677         /* put the latest values into the public section of the decoder instance */
678         decoder->channels = decoder->guts->frame.header.channels;
679         decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
680         decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
681         decoder->sample_rate = decoder->guts->frame.header.sample_rate;
682         decoder->blocksize = decoder->guts->frame.header.blocksize;
683
684         decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
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)
688                 return false;
689
690         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
691         return true;
692 }
693
694 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
695 {
696         uint32 x;
697         uint64 xx;
698         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
699         byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
700         unsigned raw_header_len;
701         bool is_unparseable = false;
702
703         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
704
705         /* init the raw header with the first 8 bits of the sync code */
706         raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
707         raw_header_len = 1;
708
709         /*
710          * read in the raw header as bytes so we can CRC it, and parse it on the way
711          */
712         for(i = 0; i < 2; i++) {
713                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
714                         return false; /* the read_callback_ sets the state for us */
715                 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
716                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
717                         uint32 y;
718                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
719                                 return false; /* the read_callback_ sets the state for us */
720                         if(!y) { /* MAGIC NUMBER for the last sync bit */
721                                 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
722                                 return true;
723                         }
724                         else {
725                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
726                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
727                                 return true;
728                         }
729                 }
730                 raw_header[raw_header_len++] = (byte)x;
731         }
732         assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
733
734         switch(x = raw_header[1] >> 4) {
735                 case 0:
736                         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;
738                         else
739                                 is_unparseable = true;
740                         break;
741                 case 1:
742                         decoder->guts->frame.header.blocksize = 192;
743                         break;
744                 case 2:
745                 case 3:
746                 case 4:
747                 case 5:
748                         decoder->guts->frame.header.blocksize = 576 << (x-2);
749                         break;
750                 case 6:
751                 case 7:
752                         blocksize_hint = x;
753                         break;
754                 default:
755                         assert(0);
756                         break;
757         }
758
759         switch(x = raw_header[1] & 0x0f) {
760                 case 0:
761                         if(decoder->guts->has_stream_header)
762                                 decoder->guts->frame.header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
763                         else
764                                 is_unparseable = true;
765                         break;
766                 case 1:
767                 case 2:
768                 case 3:
769                         is_unparseable = true;
770                         break;
771                 case 4:
772                         decoder->guts->frame.header.sample_rate = 8000;
773                         break;
774                 case 5:
775                         decoder->guts->frame.header.sample_rate = 16000;
776                         break;
777                 case 6:
778                         decoder->guts->frame.header.sample_rate = 22050;
779                         break;
780                 case 7:
781                         decoder->guts->frame.header.sample_rate = 24000;
782                         break;
783                 case 8:
784                         decoder->guts->frame.header.sample_rate = 32000;
785                         break;
786                 case 9:
787                         decoder->guts->frame.header.sample_rate = 44100;
788                         break;
789                 case 10:
790                         decoder->guts->frame.header.sample_rate = 48000;
791                         break;
792                 case 11:
793                         decoder->guts->frame.header.sample_rate = 96000;
794                         break;
795                 case 12:
796                 case 13:
797                 case 14:
798                         sample_rate_hint = x;
799                         break;
800                 case 15:
801                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
802                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
803                         return true;
804                 default:
805                         assert(0);
806         }
807
808         x = (unsigned)(raw_header[2] >> 4);
809         if(x & 8) {
810                 decoder->guts->frame.header.channels = 2;
811                 switch(x & 7) {
812                         case 0:
813                                 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
814                                 break;
815                         case 1:
816                                 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
817                                 break;
818                         case 2:
819                                 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
820                                 break;
821                         default:
822                                 is_unparseable = true;
823                                 break;
824                 }
825         }
826         else {
827                 decoder->guts->frame.header.channels = (unsigned)x + 1;
828                 decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
829         }
830
831         switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
832                 case 0:
833                         if(decoder->guts->has_stream_header)
834                                 decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
835                         else
836                                 is_unparseable = true;
837                         break;
838                 case 1:
839                         decoder->guts->frame.header.bits_per_sample = 8;
840                         break;
841                 case 2:
842                         decoder->guts->frame.header.bits_per_sample = 12;
843                         break;
844                 case 4:
845                         decoder->guts->frame.header.bits_per_sample = 16;
846                         break;
847                 case 5:
848                         decoder->guts->frame.header.bits_per_sample = 20;
849                         break;
850                 case 6:
851                         decoder->guts->frame.header.bits_per_sample = 24;
852                         break;
853                 case 3:
854                 case 7:
855                         is_unparseable = true;
856                         break;
857                 default:
858                         assert(0);
859                         break;
860         }
861
862         if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
863                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
864                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
865                 return true;
866         }
867
868         if(blocksize_hint) {
869                 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
870                         return false; /* the read_callback_ sets the state for us */
871                 if(xx == 0xffffffffffffffff) {
872                         if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
873                                 uint32 y;
874                                 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
875                                         return false; /* the read_callback_ sets the state for us */
876                                 if(!y) { /* MAGIC NUMBER for the last sync bit */
877                                         decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
878                                         return true;
879                                 }
880                                 else {
881                                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
882                                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
883                                         return true;
884                                 }
885                         }
886                         else {
887                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
888                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
889                                 return true;
890                         }
891                 }
892                 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;
894                 else
895                         decoder->guts->frame.header.number.sample_number = xx;
896         }
897         else {
898                 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
899                         return false; /* the read_callback_ sets the state for us */
900                 if(x == 0xffffffff) {
901                         if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
902                                 uint32 y;
903                                 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
904                                         return false; /* the read_callback_ sets the state for us */
905                                 if(!y) { /* MAGIC NUMBER for the last sync bit */
906                                         decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
907                                         return true;
908                                 }
909                                 else {
910                                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
911                                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
912                                         return true;
913                                 }
914                         }
915                         else {
916                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
917                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
918                                 return true;
919                         }
920                 }
921                 decoder->guts->last_frame_number = x;
922                 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;
924                 }
925                 else {
926                         is_unparseable = true;
927                 }
928         }
929
930         if(blocksize_hint) {
931                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
932                         return false; /* the read_callback_ sets the state for us */
933                 raw_header[raw_header_len++] = (byte)x;
934                 if(blocksize_hint == 7) {
935                         uint32 _x;
936                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
937                                 return false; /* the read_callback_ sets the state for us */
938                         raw_header[raw_header_len++] = (byte)_x;
939                         x = (x << 8) | _x;
940                 }
941                 decoder->guts->frame.header.blocksize = x+1;
942         }
943
944         if(sample_rate_hint) {
945                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
946                         return false; /* the read_callback_ sets the state for us */
947                 raw_header[raw_header_len++] = (byte)x;
948                 if(sample_rate_hint != 12) {
949                         uint32 _x;
950                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
951                                 return false; /* the read_callback_ sets the state for us */
952                         raw_header[raw_header_len++] = (byte)_x;
953                         x = (x << 8) | _x;
954                 }
955                 if(sample_rate_hint == 12)
956                         decoder->guts->frame.header.sample_rate = x*1000;
957                 else if(sample_rate_hint == 13)
958                         decoder->guts->frame.header.sample_rate = x;
959                 else
960                         decoder->guts->frame.header.sample_rate = x*10;
961         }
962
963         /* read the crc byte */
964         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
965                 return false; /* the read_callback_ sets the state for us */
966         crc = (byte)x;
967
968         if(FLAC__crc8(raw_header, raw_header_len) != crc) {
969                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
970                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
971                 return true;
972         }
973
974         if(is_unparseable) {
975                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
976                 return false;
977         }
978
979         return true;
980 }
981
982 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
983 {
984         uint32 x;
985
986         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_TYPE_LEN, read_callback_, decoder))
987                 return false; /* the read_callback_ sets the state for us */
988         if(x & 0x01 || x & 0x80) {
989                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
990                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
991                 return true;
992         }
993         else if(x == 0) {
994                 return stream_decoder_read_subframe_constant_(decoder, channel, bps);
995         }
996         else if(x == 2) {
997                 return stream_decoder_read_subframe_verbatim_(decoder, channel, bps);
998         }
999         else if(x < 16) {
1000                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1001                 return false;
1002         }
1003         else if(x <= 24) {
1004                 return stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7);
1005         }
1006         else if(x < 64) {
1007                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1008                 return false;
1009         }
1010         else {
1011                 return stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1);
1012         }
1013 }
1014
1015 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1016 {
1017         FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
1018         int32 x;
1019         unsigned i;
1020         int32 *output = decoder->guts->output[channel];
1021
1022         decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1023
1024         if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
1025                 return false; /* the read_callback_ sets the state for us */
1026
1027         subframe->value = x;
1028
1029         /* decode the subframe */
1030         for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
1031                 output[i] = x;
1032
1033         return true;
1034 }
1035
1036 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1037 {
1038         FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
1039         int32 i32;
1040         uint32 u32;
1041         unsigned u;
1042
1043         decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1044
1045         subframe->residual = decoder->guts->residual[channel];
1046         subframe->order = order;
1047
1048         /* read warm-up samples */
1049         for(u = 0; u < order; u++) {
1050                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1051                         return false; /* the read_callback_ sets the state for us */
1052                 subframe->warmup[u] = i32;
1053         }
1054
1055         /* read entropy coding method info */
1056         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1057                 return false; /* the read_callback_ sets the state for us */
1058         subframe->entropy_coding_method.type = u32;
1059         switch(subframe->entropy_coding_method.type) {
1060                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1061                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1062                                 return false; /* the read_callback_ sets the state for us */
1063                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1064                         break;
1065                 default:
1066                         decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1067                         return false;
1068         }
1069
1070         /* read residual */
1071         switch(subframe->entropy_coding_method.type) {
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]))
1074                                 return false;
1075                         break;
1076                 default:
1077                         assert(0);
1078         }
1079
1080         /* decode the subframe */
1081         memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1082         FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
1083
1084         return true;
1085 }
1086
1087 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1088 {
1089         FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
1090         int32 i32;
1091         uint32 u32;
1092         unsigned u;
1093
1094         decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1095
1096         subframe->residual = decoder->guts->residual[channel];
1097         subframe->order = order;
1098
1099         /* read warm-up samples */
1100         for(u = 0; u < order; u++) {
1101                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
1102                         return false; /* the read_callback_ sets the state for us */
1103                 subframe->warmup[u] = i32;
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))
1108                 return false; /* the read_callback_ sets the state for us */
1109         if(u32 == 15) {
1110                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1111                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1112                 return true;
1113         }
1114         subframe->qlp_coeff_precision = u32+1;
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))
1118                 return false; /* the read_callback_ sets the state for us */
1119         subframe->quantization_level = i32;
1120
1121         /* read quantized lp coefficiencts */
1122         for(u = 0; u < order; u++) {
1123                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1124                         return false; /* the read_callback_ sets the state for us */
1125                 subframe->qlp_coeff[u] = i32;
1126         }
1127
1128         /* read entropy coding method info */
1129         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1130                 return false; /* the read_callback_ sets the state for us */
1131         subframe->entropy_coding_method.type = u32;
1132         switch(subframe->entropy_coding_method.type) {
1133                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1134                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1135                                 return false; /* the read_callback_ sets the state for us */
1136                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1137                         break;
1138                 default:
1139                         decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1140                         return false;
1141         }
1142
1143         /* read residual */
1144         switch(subframe->entropy_coding_method.type) {
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]))
1147                                 return false;
1148                         break;
1149                 default:
1150                         assert(0);
1151         }
1152
1153         /* decode the subframe */
1154         memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
1155         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);
1156
1157         return true;
1158 }
1159
1160 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1161 {
1162         FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
1163         int32 x, *residual = decoder->guts->residual[channel];
1164         unsigned i;
1165
1166         decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1167
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))
1172                         return false; /* the read_callback_ sets the state for us */
1173                 residual[i] = x;
1174         }
1175
1176         /* decode the subframe */
1177         memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
1178
1179         return true;
1180 }
1181
1182 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
1183 {
1184         uint32 rice_parameter;
1185         int i;
1186         unsigned partition, sample, u;
1187         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;
1189
1190         sample = 0;
1191         for(partition = 0; partition < partitions; partition++) {
1192                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1193                         return false; /* the read_callback_ sets the state for us */
1194                 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1195                         if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1196                                 return false; /* the read_callback_ sets the state for us */
1197                         residual[sample] = i;
1198                 }
1199         }
1200
1201         return true;
1202 }
1203
1204 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1205 {
1206         if(decoder->guts->input.consumed_bits != 0) {
1207                 uint32 zero = 0;
1208                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1209                         return false; /* the read_callback_ sets the state for us */
1210                 if(zero != 0) {
1211                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1212                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1213                 }
1214         }
1215         return true;
1216 }
1217
1218 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1219 {
1220         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1221         FLAC__StreamDecoderReadStatus status;
1222         status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1223         if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1224                 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1225         else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1226                 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1227         return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1228 }