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