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