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