add reserved space detection to metadata decoding
[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                 unsigned used_bits = 0;
441                 decoder->guts->stream_header.type = type;
442                 decoder->guts->stream_header.is_last = last_block;
443                 decoder->guts->stream_header.length = length;
444
445                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
446                         return false; /* the read_callback_ sets the state for us */
447                 decoder->guts->stream_header.data.encoding.min_blocksize = x;
448                 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN;
449
450                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
451                         return false; /* the read_callback_ sets the state for us */
452                 decoder->guts->stream_header.data.encoding.max_blocksize = x;
453                 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN;
454
455                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
456                         return false; /* the read_callback_ sets the state for us */
457                 decoder->guts->stream_header.data.encoding.min_framesize = x;
458                 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN;
459
460                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
461                         return false; /* the read_callback_ sets the state for us */
462                 decoder->guts->stream_header.data.encoding.max_framesize = x;
463                 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN;
464
465                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
466                         return false; /* the read_callback_ sets the state for us */
467                 decoder->guts->stream_header.data.encoding.sample_rate = x;
468                 used_bits += FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN;
469
470                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
471                         return false; /* the read_callback_ sets the state for us */
472                 decoder->guts->stream_header.data.encoding.channels = x+1;
473                 used_bits += FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN;
474
475                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
476                         return false; /* the read_callback_ sets the state for us */
477                 decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
478                 used_bits += FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN;
479
480                 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))
481                         return false; /* the read_callback_ sets the state for us */
482                 used_bits += FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN;
483
484                 /* skip the rest of the block */
485                 assert(used_bits % 8 == 0);
486                 length -= (used_bits / 8);
487                 for(i = 0; i < length; i++) {
488                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
489                                 return false; /* the read_callback_ sets the state for us */
490                 }
491
492                 decoder->guts->has_stream_header = true;
493                 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
494         }
495         else {
496                 /* skip other metadata blocks */
497                 for(i = 0; i < length; i++) {
498                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
499                                 return false; /* the read_callback_ sets the state for us */
500                 }
501         }
502
503         if(last_block)
504                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
505
506         return true;
507 }
508
509 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
510 {
511         uint32 x;
512         unsigned i, skip;
513
514         /* skip the version and flags bytes */
515         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
516                 return false; /* the read_callback_ sets the state for us */
517         /* get the size (in bytes) to skip */
518         skip = 0;
519         for(i = 0; i < 4; i++) {
520                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
521                         return false; /* the read_callback_ sets the state for us */
522                 skip <<= 7;
523                 skip |= (x & 0x7f);
524         }
525         /* skip the rest of the tag */
526         for(i = 0; i < skip; i++) {
527                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
528                         return false; /* the read_callback_ sets the state for us */
529         }
530         return true;
531 }
532
533 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
534 {
535         uint32 x;
536         bool first = 1;
537
538         /* If we know the total number of samples in the stream, stop if we've read that many. */
539         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
540         if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
541                 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
542                         decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
543                         return true;
544                 }
545         }
546
547         /* make sure we're byte aligned */
548         if(decoder->guts->input.consumed_bits != 0) {
549                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
550                         return false; /* the read_callback_ sets the state for us */
551         }
552
553         while(1) {
554                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
555                         return false; /* the read_callback_ sets the state for us */
556                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
557                         unsigned y;
558                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
559                                 return false; /* the read_callback_ sets the state for us */
560                         if(!y) { /* MAGIC NUMBER for the last sync bit */
561                                 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
562                                 return true;
563                         }
564                 }
565                 if(first) {
566                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
567                         first = 0;
568                 }
569         }
570
571         return true;
572 }
573
574 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
575 {
576         unsigned channel;
577         unsigned i;
578         int32 mid, side, left, right;
579
580         *got_a_frame = false;
581
582         if(!stream_decoder_read_frame_header_(decoder))
583                 return false;
584         if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
585                 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
586                         decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
587                 else
588                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
589                 return true;
590         }
591         if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame_header.blocksize))
592                 return false;
593         for(channel = 0; channel < decoder->guts->frame_header.channels; channel++) {
594                 if(!stream_decoder_read_subframe_(decoder, channel))
595                         return false;
596                 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
597                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
598                         return true;
599                 }
600         }
601         if(!stream_decoder_read_zero_padding_(decoder))
602                 return false;
603
604         /* Undo any special channel coding */
605         switch(decoder->guts->frame_header.channel_assignment) {
606                 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
607                         /* do nothing */
608                         break;
609                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
610                         assert(decoder->guts->frame_header.channels == 2);
611                         for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
612                                 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
613                         break;
614                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
615                         assert(decoder->guts->frame_header.channels == 2);
616                         for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
617                                 decoder->guts->output[0][i] += decoder->guts->output[1][i];
618                         break;
619                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
620                         assert(decoder->guts->frame_header.channels == 2);
621                         for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
622                                 mid = decoder->guts->output[0][i];
623                                 side = decoder->guts->output[1][i];
624                                 mid <<= 1;
625                                 if(side & 1) /* i.e. if 'side' is odd... */
626                                         mid++;
627                                 left = mid + side;
628                                 right = mid - side;
629                                 decoder->guts->output[0][i] = left >> 1;
630                                 decoder->guts->output[1][i] = right >> 1;
631                         }
632                         break;
633                 default:
634                         assert(0);
635                         break;
636         }
637
638         *got_a_frame = true;
639
640         /* put the latest values into the public section of the decoder instance */
641         decoder->channels = decoder->guts->frame_header.channels;
642         decoder->channel_assignment = decoder->guts->frame_header.channel_assignment;
643         decoder->bits_per_sample = decoder->guts->frame_header.bits_per_sample;
644         decoder->sample_rate = decoder->guts->frame_header.sample_rate;
645         decoder->blocksize = decoder->guts->frame_header.blocksize;
646
647         decoder->guts->samples_decoded += decoder->guts->frame_header.blocksize;
648
649         /* write it */
650         if(decoder->guts->write_callback(decoder, &decoder->guts->frame_header, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
651                 return false;
652
653         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
654         return true;
655 }
656
657 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
658 {
659         uint32 x;
660         uint64 xx;
661         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
662         byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
663         unsigned raw_header_len;
664         bool is_unparseable = false;
665
666         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
667
668         /* init the raw header with the first 8 bits of the sync code */
669         raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
670         raw_header_len = 1;
671
672         /*
673          * read in the raw header as bytes so we can CRC it, and parse it on the way
674          */
675         for(i = 0; i < 2; i++) {
676                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
677                         return false; /* the read_callback_ sets the state for us */
678                 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
679                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
680                         uint32 y;
681                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
682                                 return false; /* the read_callback_ sets the state for us */
683                         if(!y) { /* MAGIC NUMBER for the last sync bit */
684                                 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
685                                 return true;
686                         }
687                         else {
688                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
689                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
690                                 return true;
691                         }
692                 }
693                 raw_header[raw_header_len++] = (byte)x;
694         }
695         assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
696
697         switch(x = raw_header[1] >> 4) {
698                 case 0:
699                         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 */
700                                 decoder->guts->frame_header.blocksize = decoder->guts->stream_header.data.encoding.min_blocksize;
701                         else
702                                 is_unparseable = true;
703                         break;
704                 case 1:
705                         decoder->guts->frame_header.blocksize = 192;
706                         break;
707                 case 2:
708                 case 3:
709                 case 4:
710                 case 5:
711                         decoder->guts->frame_header.blocksize = 576 << (x-2);
712                         break;
713                 case 6:
714                 case 7:
715                         blocksize_hint = x;
716                         break;
717                 default:
718                         assert(0);
719                         break;
720         }
721
722         switch(x = raw_header[1] & 0x0f) {
723                 case 0:
724                         if(decoder->guts->has_stream_header)
725                                 decoder->guts->frame_header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
726                         else
727                                 is_unparseable = true;
728                         break;
729                 case 1:
730                 case 2:
731                 case 3:
732                         is_unparseable = true;
733                         break;
734                 case 4:
735                         decoder->guts->frame_header.sample_rate = 8000;
736                         break;
737                 case 5:
738                         decoder->guts->frame_header.sample_rate = 16000;
739                         break;
740                 case 6:
741                         decoder->guts->frame_header.sample_rate = 22050;
742                         break;
743                 case 7:
744                         decoder->guts->frame_header.sample_rate = 24000;
745                         break;
746                 case 8:
747                         decoder->guts->frame_header.sample_rate = 32000;
748                         break;
749                 case 9:
750                         decoder->guts->frame_header.sample_rate = 44100;
751                         break;
752                 case 10:
753                         decoder->guts->frame_header.sample_rate = 48000;
754                         break;
755                 case 11:
756                         decoder->guts->frame_header.sample_rate = 96000;
757                         break;
758                 case 12:
759                 case 13:
760                 case 14:
761                         sample_rate_hint = x;
762                         break;
763                 case 15:
764                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
765                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
766                         return true;
767                 default:
768                         assert(0);
769         }
770
771         x = (unsigned)(raw_header[2] >> 4);
772         if(x & 8) {
773                 decoder->guts->frame_header.channels = 2;
774                 switch(x & 7) {
775                         case 0:
776                                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
777                                 break;
778                         case 1:
779                                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
780                                 break;
781                         case 2:
782                                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
783                                 break;
784                         default:
785                                 is_unparseable = true;
786                                 break;
787                 }
788         }
789         else {
790                 decoder->guts->frame_header.channels = (unsigned)x + 1;
791                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
792         }
793
794         switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
795                 case 0:
796                         if(decoder->guts->has_stream_header)
797                                 decoder->guts->frame_header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
798                         else
799                                 is_unparseable = true;
800                         break;
801                 case 1:
802                         decoder->guts->frame_header.bits_per_sample = 8;
803                         break;
804                 case 2:
805                         decoder->guts->frame_header.bits_per_sample = 12;
806                         break;
807                 case 4:
808                         decoder->guts->frame_header.bits_per_sample = 16;
809                         break;
810                 case 5:
811                         decoder->guts->frame_header.bits_per_sample = 20;
812                         break;
813                 case 6:
814                         decoder->guts->frame_header.bits_per_sample = 24;
815                         break;
816                 case 3:
817                 case 7:
818                         is_unparseable = true;
819                         break;
820                 default:
821                         assert(0);
822                         break;
823         }
824
825         if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
826                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
827                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
828                 return true;
829         }
830
831         if(blocksize_hint) {
832                 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
833                         return false; /* the read_callback_ sets the state for us */
834                 if(xx == 0xffffffffffffffff) {
835                         if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
836                                 uint32 y;
837                                 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
838                                         return false; /* the read_callback_ sets the state for us */
839                                 if(!y) { /* MAGIC NUMBER for the last sync bit */
840                                         decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
841                                         return true;
842                                 }
843                                 else {
844                                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
845                                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
846                                         return true;
847                                 }
848                         }
849                         else {
850                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
851                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
852                                 return true;
853                         }
854                 }
855                 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 */
856                         decoder->guts->frame_header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.encoding.min_blocksize + xx;
857                 else
858                         decoder->guts->frame_header.number.sample_number = xx;
859         }
860         else {
861                 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
862                         return false; /* the read_callback_ sets the state for us */
863                 if(x == 0xffffffff) {
864                         if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
865                                 uint32 y;
866                                 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
867                                         return false; /* the read_callback_ sets the state for us */
868                                 if(!y) { /* MAGIC NUMBER for the last sync bit */
869                                         decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
870                                         return true;
871                                 }
872                                 else {
873                                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
874                                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
875                                         return true;
876                                 }
877                         }
878                         else {
879                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
880                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
881                                 return true;
882                         }
883                 }
884                 decoder->guts->last_frame_number = x;
885                 if(decoder->guts->has_stream_header) {
886                         decoder->guts->frame_header.number.sample_number = (int64)decoder->guts->stream_header.data.encoding.min_blocksize * (int64)x;
887                 }
888                 else {
889                         is_unparseable = true;
890                 }
891         }
892
893         if(blocksize_hint) {
894                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
895                         return false; /* the read_callback_ sets the state for us */
896                 raw_header[raw_header_len++] = (byte)x;
897                 if(blocksize_hint == 7) {
898                         uint32 _x;
899                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
900                                 return false; /* the read_callback_ sets the state for us */
901                         raw_header[raw_header_len++] = (byte)_x;
902                         x = (x << 8) | _x;
903                 }
904                 decoder->guts->frame_header.blocksize = x+1;
905         }
906
907         if(sample_rate_hint) {
908                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
909                         return false; /* the read_callback_ sets the state for us */
910                 raw_header[raw_header_len++] = (byte)x;
911                 if(sample_rate_hint != 12) {
912                         uint32 _x;
913                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
914                                 return false; /* the read_callback_ sets the state for us */
915                         raw_header[raw_header_len++] = (byte)_x;
916                         x = (x << 8) | _x;
917                 }
918                 if(sample_rate_hint == 12)
919                         decoder->guts->frame_header.sample_rate = x*1000;
920                 else if(sample_rate_hint == 13)
921                         decoder->guts->frame_header.sample_rate = x;
922                 else
923                         decoder->guts->frame_header.sample_rate = x*10;
924         }
925
926         /* read the crc byte */
927         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
928                 return false; /* the read_callback_ sets the state for us */
929         crc = (byte)x;
930
931         if(FLAC__crc8(raw_header, raw_header_len) != crc) {
932                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
933                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
934                 return true;
935         }
936
937         if(is_unparseable) {
938                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
939                 return false;
940         }
941
942         return true;
943 }
944
945 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel)
946 {
947         uint32 x;
948
949         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_HEADER_TYPE_LEN, read_callback_, decoder))
950                 return false; /* the read_callback_ sets the state for us */
951         if(x & 0x01 || x & 0x80) {
952                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
953                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
954                 return true;
955         }
956         else if(x == 0) {
957                 return stream_decoder_read_subframe_constant_(decoder, channel);
958         }
959         else if(x == 2) {
960                 return stream_decoder_read_subframe_verbatim_(decoder, channel);
961         }
962         else if(x < 16) {
963                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
964                 return false;
965         }
966         else if(x <= 24) {
967                 return stream_decoder_read_subframe_fixed_(decoder, channel, (x>>1)&7);
968         }
969         else if(x < 64) {
970                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
971                 return false;
972         }
973         else {
974                 return stream_decoder_read_subframe_lpc_(decoder, channel, ((x>>1)&31)+1);
975         }
976 }
977
978 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel)
979 {
980         int32 x;
981         unsigned i;
982
983         if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
984                 return false; /* the read_callback_ sets the state for us */
985
986         for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
987                 decoder->guts->output[channel][i] = x;
988
989         return true;
990 }
991
992 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
993 {
994         FLAC__SubframeHeader_Fixed subframe_header;
995         int32 i32;
996         uint32 u32;
997         unsigned u;
998
999         subframe_header.order = order;
1000
1001         /* read warm-up samples */
1002         for(u = 0; u < order; u++) {
1003                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1004                         return false; /* the read_callback_ sets the state for us */
1005                 decoder->guts->output[channel][u] = i32;
1006         }
1007
1008         /* read entropy coding method info */
1009         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1010                 return false; /* the read_callback_ sets the state for us */
1011         subframe_header.entropy_coding_method.type = u32;
1012         switch(subframe_header.entropy_coding_method.type) {
1013                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1014                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1015                                 return false; /* the read_callback_ sets the state for us */
1016                         subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1017                         break;
1018                 default:
1019                         decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1020                         return false;
1021         }
1022
1023         /* read residual */
1024         switch(subframe_header.entropy_coding_method.type) {
1025                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1026                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1027                                 return false;
1028                         break;
1029                 default:
1030                         assert(0);
1031         }
1032
1033         /* decode the subframe */
1034         FLAC__fixed_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, order, decoder->guts->output[channel]+order);
1035
1036         return true;
1037 }
1038
1039 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
1040 {
1041         FLAC__SubframeHeader_LPC subframe_header;
1042         int32 i32;
1043         uint32 u32;
1044         unsigned u;
1045
1046         subframe_header.order = order;
1047
1048         /* read warm-up samples */
1049         for(u = 0; u < order; u++) {
1050                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1051                         return false; /* the read_callback_ sets the state for us */
1052                 decoder->guts->output[channel][u] = i32;
1053         }
1054
1055         /* read qlp coeff precision */
1056         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1057                 return false; /* the read_callback_ sets the state for us */
1058         if(u32 == 15) {
1059                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1060                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1061                 return true;
1062         }
1063         subframe_header.qlp_coeff_precision = u32+1;
1064
1065         /* read qlp shift */
1066         if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1067                 return false; /* the read_callback_ sets the state for us */
1068         subframe_header.quantization_level = i32;
1069
1070         /* read quantized lp coefficiencts */
1071         for(u = 0; u < order; u++) {
1072                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe_header.qlp_coeff_precision, read_callback_, decoder))
1073                         return false; /* the read_callback_ sets the state for us */
1074                 subframe_header.qlp_coeff[u] = i32;
1075         }
1076
1077         /* read entropy coding method info */
1078         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1079                 return false; /* the read_callback_ sets the state for us */
1080         subframe_header.entropy_coding_method.type = u32;
1081         switch(subframe_header.entropy_coding_method.type) {
1082                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1083                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1084                                 return false; /* the read_callback_ sets the state for us */
1085                         subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1086                         break;
1087                 default:
1088                         decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1089                         return false;
1090         }
1091
1092         /* read residual */
1093         switch(subframe_header.entropy_coding_method.type) {
1094                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1095                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1096                                 return false;
1097                         break;
1098                 default:
1099                         assert(0);
1100         }
1101
1102         /* decode the subframe */
1103         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);
1104
1105         return true;
1106 }
1107
1108 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel)
1109 {
1110         int32 x;
1111         unsigned i;
1112
1113         for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
1114                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1115                         return false; /* the read_callback_ sets the state for us */
1116                 decoder->guts->output[channel][i] = x;
1117         }
1118
1119         return true;
1120 }
1121
1122 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order)
1123 {
1124         uint32 rice_parameter;
1125         int i;
1126         unsigned partition, sample, u;
1127         const unsigned partitions = 1u << partition_order;
1128         const unsigned partition_samples = partition_order > 0? decoder->guts->frame_header.blocksize >> partition_order : decoder->guts->frame_header.blocksize - predictor_order;
1129
1130         sample = 0;
1131         for(partition = 0; partition < partitions; partition++) {
1132                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1133                         return false; /* the read_callback_ sets the state for us */
1134                 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1135                         if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1136                                 return false; /* the read_callback_ sets the state for us */
1137                         decoder->guts->residual[sample] = i;
1138                 }
1139         }
1140
1141         return true;
1142 }
1143
1144 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1145 {
1146         if(decoder->guts->input.consumed_bits != 0) {
1147                 uint32 zero;
1148                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1149                         return false; /* the read_callback_ sets the state for us */
1150                 if(zero != 0) {
1151                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1152                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1153                 }
1154         }
1155         return true;
1156 }
1157
1158 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1159 {
1160         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1161         FLAC__StreamDecoderReadStatus status;
1162         status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1163         if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1164                 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1165         else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1166                 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1167         return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1168 }