add checking based on number_type in the frame header
[platform/upstream/flac.git] / src / libFLAC / stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memset/memcpy() */
23 #include "FLAC/assert.h"
24 #include "protected/stream_decoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/cpu.h"
27 #include "private/crc.h"
28 #include "private/fixed.h"
29 #include "private/lpc.h"
30
31 /***********************************************************************
32  *
33  * Private static data
34  *
35  ***********************************************************************/
36
37 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
38
39 /***********************************************************************
40  *
41  * Private class method prototypes
42  *
43  ***********************************************************************/
44
45 static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
46 static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
47 static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
48 static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
49 static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
50 static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
51 static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
52 static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
53 static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
54 static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
55 static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
56 static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
57 static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
58 static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
59 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
60
61 /***********************************************************************
62  *
63  * Private class data
64  *
65  ***********************************************************************/
66
67 typedef struct FLAC__StreamDecoderPrivate {
68         FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
69         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
70         void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
71         void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
72         void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
73         void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
74         void *client_data;
75         FLAC__BitBuffer input;
76         FLAC__int32 *output[FLAC__MAX_CHANNELS];
77         FLAC__int32 *residual[FLAC__MAX_CHANNELS];
78         unsigned output_capacity, output_channels;
79         FLAC__uint32 last_frame_number;
80         FLAC__uint64 samples_decoded;
81         FLAC__bool has_stream_info, has_seek_table;
82         FLAC__StreamMetaData stream_info;
83         FLAC__StreamMetaData seek_table;
84         FLAC__Frame frame;
85         FLAC__bool cached; /* true if there is a byte in lookahead */
86         FLAC__CPUInfo cpuinfo;
87         FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
88         FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
89 } FLAC__StreamDecoderPrivate;
90
91 /***********************************************************************
92  *
93  * Public static class data
94  *
95  ***********************************************************************/
96
97 const char *FLAC__StreamDecoderStateString[] = {
98         "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
99         "FLAC__STREAM_DECODER_READ_METADATA",
100         "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
101         "FLAC__STREAM_DECODER_READ_FRAME",
102         "FLAC__STREAM_DECODER_END_OF_STREAM",
103         "FLAC__STREAM_DECODER_ABORTED",
104         "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
105         "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
106         "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
107         "FLAC__STREAM_DECODER_INVALID_CALLBACK",
108         "FLAC__STREAM_DECODER_UNINITIALIZED"
109 };
110
111 const char *FLAC__StreamDecoderReadStatusString[] = {
112         "FLAC__STREAM_DECODER_READ_CONTINUE",
113         "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
114         "FLAC__STREAM_DECODER_READ_ABORT"
115 };
116
117 const char *FLAC__StreamDecoderWriteStatusString[] = {
118         "FLAC__STREAM_DECODER_WRITE_CONTINUE",
119         "FLAC__STREAM_DECODER_WRITE_ABORT"
120 };
121
122 const char *FLAC__StreamDecoderErrorStatusString[] = {
123         "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
124         "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
125         "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
126 };
127
128 /***********************************************************************
129  *
130  * Class constructor/destructor
131  *
132  ***********************************************************************/
133 FLAC__StreamDecoder *FLAC__stream_decoder_new()
134 {
135         FLAC__StreamDecoder *decoder;
136
137         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
138
139         decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
140         if(decoder == 0) {
141                 return 0;
142         }
143         decoder->protected = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
144         if(decoder->protected == 0) {
145                 free(decoder);
146                 return 0;
147         }
148         decoder->private = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
149         if(decoder->private == 0) {
150                 free(decoder->protected);
151                 free(decoder);
152                 return 0;
153         }
154
155         decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;
156
157         decoder->private->read_callback = 0;
158         decoder->private->write_callback = 0;
159         decoder->private->metadata_callback = 0;
160         decoder->private->error_callback = 0;
161         decoder->private->client_data = 0;
162
163         return decoder;
164 }
165
166 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
167 {
168         FLAC__ASSERT(decoder != 0);
169         FLAC__ASSERT(decoder->protected != 0);
170         FLAC__ASSERT(decoder->private != 0);
171
172         free(decoder->private);
173         free(decoder->protected);
174         free(decoder);
175 }
176
177 /***********************************************************************
178  *
179  * Public class methods
180  *
181  ***********************************************************************/
182
183 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
184 {
185         unsigned i;
186
187         FLAC__ASSERT(decoder != 0);
188
189         if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
190                 return decoder->protected->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
191
192         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
193
194         if(0 == decoder->private->read_callback || 0 == decoder->private->write_callback || 0 == decoder->private->metadata_callback || 0 == decoder->private->error_callback)
195                 return decoder->protected->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
196
197         FLAC__bitbuffer_init(&decoder->private->input);
198
199         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
200                 decoder->private->output[i] = 0;
201                 decoder->private->residual[i] = 0;
202         }
203
204         decoder->private->output_capacity = 0;
205         decoder->private->output_channels = 0;
206         decoder->private->last_frame_number = 0;
207         decoder->private->samples_decoded = 0;
208         decoder->private->has_stream_info = false;
209         decoder->private->has_seek_table = false;
210         decoder->private->cached = false;
211
212         /*
213          * get the CPU info and set the function pointers
214          */
215         FLAC__cpu_info(&decoder->private->cpuinfo);
216         /* first default to the non-asm routines */
217         decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal;
218         decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
219         /* now override with asm where appropriate */
220 #ifndef FLAC__NO_ASM
221         FLAC__ASSERT(decoder->private->cpuinfo.use_asm);
222 #ifdef FLAC__CPU_IA32
223         FLAC__ASSERT(decoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
224 #ifdef FLAC__HAS_NASM
225         if(decoder->private->cpuinfo.data.ia32.mmx) {
226                 decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
227                 decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
228         }
229         else {
230                 decoder->private->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
231                 decoder->private->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
232         }
233 #endif
234 #endif
235 #endif
236
237         return decoder->protected->state;
238 }
239
240 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
241 {
242         unsigned i;
243         FLAC__ASSERT(decoder != 0);
244         if(decoder->protected->state == FLAC__STREAM_DECODER_UNINITIALIZED)
245                 return;
246         if(decoder->private->has_seek_table) {
247                 free(decoder->private->seek_table.data.seek_table.points);
248                 decoder->private->seek_table.data.seek_table.points = 0;
249         }
250         FLAC__bitbuffer_free(&decoder->private->input);
251         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
252                 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
253                 if(decoder->private->output[i] != 0) {
254                         free(decoder->private->output[i]-4);
255                         decoder->private->output[i] = 0;
256                 }
257                 if(decoder->private->residual[i] != 0) {
258                         free(decoder->private->residual[i]);
259                         decoder->private->residual[i] = 0;
260                 }
261         }
262         decoder->private->output_capacity = 0;
263         decoder->private->output_channels = 0;
264         decoder->protected->state = FLAC__STREAM_DECODER_UNINITIALIZED;
265 }
266
267 FLAC__bool FLAC__stream_decoder_set_read_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
268 {
269         if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
270                 return false;
271         decoder->private->read_callback = value;
272         return true;
273 }
274
275 FLAC__bool FLAC__stream_decoder_set_write_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
276 {
277         if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
278                 return false;
279         decoder->private->write_callback = value;
280         return true;
281 }
282
283 FLAC__bool FLAC__stream_decoder_set_metadata_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
284 {
285         if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
286                 return false;
287         decoder->private->metadata_callback = value;
288         return true;
289 }
290
291 FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
292 {
293         if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
294                 return false;
295         decoder->private->error_callback = value;
296         return true;
297 }
298
299 FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
300 {
301         if(decoder->protected->state != FLAC__STREAM_DECODER_UNINITIALIZED)
302                 return false;
303         decoder->private->client_data = value;
304         return true;
305 }
306
307 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
308 {
309         return decoder->protected->state;
310 }
311
312 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
313 {
314         return decoder->protected->channels;
315 }
316
317 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
318 {
319         return decoder->protected->channel_assignment;
320 }
321
322 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
323 {
324         return decoder->protected->bits_per_sample;
325 }
326
327 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
328 {
329         return decoder->protected->sample_rate;
330 }
331
332 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
333 {
334         return decoder->protected->blocksize;
335 }
336
337 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
338 {
339         FLAC__ASSERT(decoder != 0);
340
341         if(!FLAC__bitbuffer_clear(&decoder->private->input)) {
342                 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
343                 return false;
344         }
345
346         return true;
347 }
348
349 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
350 {
351         FLAC__ASSERT(decoder != 0);
352
353         if(!FLAC__stream_decoder_flush(decoder)) {
354                 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
355                 return false;
356         }
357         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
358
359         decoder->private->samples_decoded = 0;
360
361         return true;
362 }
363
364 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
365 {
366         FLAC__bool dummy;
367         FLAC__ASSERT(decoder != 0);
368
369         if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
370                 return true;
371
372         FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
373
374         if(!FLAC__stream_decoder_reset(decoder)) {
375                 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
376                 return false;
377         }
378
379         while(1) {
380                 switch(decoder->protected->state) {
381                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
382                                 if(!stream_decoder_find_metadata_(decoder))
383                                         return false; /* above function sets the status for us */
384                                 break;
385                         case FLAC__STREAM_DECODER_READ_METADATA:
386                                 if(!stream_decoder_read_metadata_(decoder))
387                                         return false; /* above function sets the status for us */
388                                 break;
389                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
390                                 if(!stream_decoder_frame_sync_(decoder))
391                                         return true; /* above function sets the status for us */
392                                 break;
393                         case FLAC__STREAM_DECODER_READ_FRAME:
394                                 if(!stream_decoder_read_frame_(decoder, &dummy))
395                                         return false; /* above function sets the status for us */
396                                 break;
397                         case FLAC__STREAM_DECODER_END_OF_STREAM:
398                                 return true;
399                         default:
400                                 FLAC__ASSERT(0);
401                 }
402         }
403 }
404
405 FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
406 {
407         FLAC__ASSERT(decoder != 0);
408
409         if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
410                 return true;
411
412         FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
413
414         if(!FLAC__stream_decoder_reset(decoder)) {
415                 decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
416                 return false;
417         }
418
419         while(1) {
420                 switch(decoder->protected->state) {
421                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
422                                 if(!stream_decoder_find_metadata_(decoder))
423                                         return false; /* above function sets the status for us */
424                                 break;
425                         case FLAC__STREAM_DECODER_READ_METADATA:
426                                 if(!stream_decoder_read_metadata_(decoder))
427                                         return false; /* above function sets the status for us */
428                                 break;
429                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
430                                 return true;
431                                 break;
432                         case FLAC__STREAM_DECODER_END_OF_STREAM:
433                                 return true;
434                         default:
435                                 FLAC__ASSERT(0);
436                 }
437         }
438 }
439
440 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
441 {
442         FLAC__bool got_a_frame;
443         FLAC__ASSERT(decoder != 0);
444
445         if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
446                 return true;
447
448         FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
449
450         while(1) {
451                 switch(decoder->protected->state) {
452                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
453                                 if(!stream_decoder_frame_sync_(decoder))
454                                         return true; /* above function sets the status for us */
455                                 break;
456                         case FLAC__STREAM_DECODER_READ_FRAME:
457                                 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
458                                         return false; /* above function sets the status for us */
459                                 if(got_a_frame)
460                                         return true; /* above function sets the status for us */
461                                 break;
462                         case FLAC__STREAM_DECODER_END_OF_STREAM:
463                                 return true;
464                         default:
465                                 FLAC__ASSERT(0);
466                 }
467         }
468 }
469
470 FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
471 {
472         FLAC__bool dummy;
473         FLAC__ASSERT(decoder != 0);
474
475         if(decoder->protected->state == FLAC__STREAM_DECODER_END_OF_STREAM)
476                 return true;
477
478         FLAC__ASSERT(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
479
480         while(1) {
481                 switch(decoder->protected->state) {
482                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
483                                 if(!stream_decoder_frame_sync_(decoder))
484                                         return true; /* above function sets the status for us */
485                                 break;
486                         case FLAC__STREAM_DECODER_READ_FRAME:
487                                 if(!stream_decoder_read_frame_(decoder, &dummy))
488                                         return false; /* above function sets the status for us */
489                                 break;
490                         case FLAC__STREAM_DECODER_END_OF_STREAM:
491                                 return true;
492                         default:
493                                 FLAC__ASSERT(0);
494                 }
495         }
496 }
497
498 /***********************************************************************
499  *
500  * Protected class methods
501  *
502  ***********************************************************************/
503
504 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
505 {
506         FLAC__ASSERT(decoder != 0);
507         return decoder->private->input.bytes - decoder->private->input.consumed_bytes;
508 }
509
510 /***********************************************************************
511  *
512  * Private class methods
513  *
514  ***********************************************************************/
515
516 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
517 {
518         unsigned i;
519         FLAC__int32 *tmp;
520
521         if(size <= decoder->private->output_capacity && channels <= decoder->private->output_channels)
522                 return true;
523
524         /* @@@ should change to use realloc() */
525
526         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
527                 if(decoder->private->output[i] != 0) {
528                         free(decoder->private->output[i]);
529                         decoder->private->output[i] = 0;
530                 }
531                 if(decoder->private->residual[i] != 0) {
532                         free(decoder->private->residual[i]);
533                         decoder->private->residual[i] = 0;
534                 }
535         }
536
537         for(i = 0; i < channels; i++) {
538                 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
539                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
540                 if(tmp == 0) {
541                         decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
542                         return false;
543                 }
544                 memset(tmp, 0, sizeof(FLAC__int32)*4);
545                 decoder->private->output[i] = tmp + 4;
546
547                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
548                 if(tmp == 0) {
549                         decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
550                         return false;
551                 }
552                 decoder->private->residual[i] = tmp;
553         }
554
555         decoder->private->output_capacity = size;
556         decoder->private->output_channels = channels;
557
558         return true;
559 }
560
561 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
562 {
563         FLAC__uint32 x;
564         unsigned i, id;
565         FLAC__bool first = true;
566
567         FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
568
569         for(i = id = 0; i < 4; ) {
570                 if(decoder->private->cached) {
571                         x = (FLAC__uint32)decoder->private->lookahead;
572                         decoder->private->cached = false;
573                 }
574                 else {
575                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
576                                 return false; /* the read_callback_ sets the state for us */
577                 }
578                 if(x == FLAC__STREAM_SYNC_STRING[i]) {
579                         first = true;
580                         i++;
581                         id = 0;
582                         continue;
583                 }
584                 if(x == ID3V2_TAG_[id]) {
585                         id++;
586                         i = 0;
587                         if(id == 3) {
588                                 if(!stream_decoder_skip_id3v2_tag_(decoder))
589                                         return false; /* the read_callback_ sets the state for us */
590                         }
591                         continue;
592                 }
593                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
594                         decoder->private->header_warmup[0] = (FLAC__byte)x;
595                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
596                                 return false; /* the read_callback_ sets the state for us */
597
598                         /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
599                         /* else we have to check if the second byte is the end of a sync code */
600                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
601                                 decoder->private->lookahead = (FLAC__byte)x;
602                                 decoder->private->cached = true;
603                         }
604                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
605                                 decoder->private->header_warmup[1] = (FLAC__byte)x;
606                                 decoder->protected->state = FLAC__STREAM_DECODER_READ_FRAME;
607                                 return true;
608                         }
609                 }
610                 i = 0;
611                 if(first) {
612                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
613                         first = false;
614                 }
615         }
616
617         decoder->protected->state = FLAC__STREAM_DECODER_READ_METADATA;
618         return true;
619 }
620
621 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
622 {
623         FLAC__uint32 i, x, last_block, type, length;
624         FLAC__uint64 xx;
625
626         FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
627
628         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
629                 return false; /* the read_callback_ sets the state for us */
630         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
631                 return false; /* the read_callback_ sets the state for us */
632         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
633                 return false; /* the read_callback_ sets the state for us */
634         if(type == FLAC__METADATA_TYPE_STREAMINFO) {
635                 unsigned used_bits = 0;
636                 decoder->private->stream_info.type = type;
637                 decoder->private->stream_info.is_last = last_block;
638                 decoder->private->stream_info.length = length;
639
640                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
641                         return false; /* the read_callback_ sets the state for us */
642                 decoder->private->stream_info.data.stream_info.min_blocksize = x;
643                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
644
645                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
646                         return false; /* the read_callback_ sets the state for us */
647                 decoder->private->stream_info.data.stream_info.max_blocksize = x;
648                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
649
650                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
651                         return false; /* the read_callback_ sets the state for us */
652                 decoder->private->stream_info.data.stream_info.min_framesize = x;
653                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
654
655                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
656                         return false; /* the read_callback_ sets the state for us */
657                 decoder->private->stream_info.data.stream_info.max_framesize = x;
658                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
659
660                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
661                         return false; /* the read_callback_ sets the state for us */
662                 decoder->private->stream_info.data.stream_info.sample_rate = x;
663                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
664
665                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
666                         return false; /* the read_callback_ sets the state for us */
667                 decoder->private->stream_info.data.stream_info.channels = x+1;
668                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
669
670                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
671                         return false; /* the read_callback_ sets the state for us */
672                 decoder->private->stream_info.data.stream_info.bits_per_sample = x+1;
673                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
674
675                 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &decoder->private->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
676                         return false; /* the read_callback_ sets the state for us */
677                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
678
679                 for(i = 0; i < 16; i++) {
680                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
681                                 return false; /* the read_callback_ sets the state for us */
682                         decoder->private->stream_info.data.stream_info.md5sum[i] = (FLAC__byte)x;
683                 }
684                 used_bits += i*8;
685
686                 /* skip the rest of the block */
687                 FLAC__ASSERT(used_bits % 8 == 0);
688                 length -= (used_bits / 8);
689                 for(i = 0; i < length; i++) {
690                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
691                                 return false; /* the read_callback_ sets the state for us */
692                 }
693
694                 decoder->private->has_stream_info = true;
695                 decoder->private->metadata_callback(decoder, &decoder->private->stream_info, decoder->private->client_data);
696         }
697         else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
698                 unsigned real_points;
699
700                 decoder->private->seek_table.type = type;
701                 decoder->private->seek_table.is_last = last_block;
702                 decoder->private->seek_table.length = length;
703
704                 decoder->private->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
705
706                 if(0 == (decoder->private->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->private->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
707                         decoder->protected->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
708                         return false;
709                 }
710                 for(i = real_points = 0; i < decoder->private->seek_table.data.seek_table.num_points; i++) {
711                         if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
712                                 return false; /* the read_callback_ sets the state for us */
713                         decoder->private->seek_table.data.seek_table.points[real_points].sample_number = xx;
714
715                         if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
716                                 return false; /* the read_callback_ sets the state for us */
717                         decoder->private->seek_table.data.seek_table.points[real_points].stream_offset = xx;
718
719                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
720                                 return false; /* the read_callback_ sets the state for us */
721                         decoder->private->seek_table.data.seek_table.points[real_points].frame_samples = x;
722
723                         if(decoder->private->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
724                                 real_points++;
725                 }
726                 decoder->private->seek_table.data.seek_table.num_points = real_points;
727
728                 decoder->private->has_seek_table = true;
729                 decoder->private->metadata_callback(decoder, &decoder->private->seek_table, decoder->private->client_data);
730         }
731         else {
732                 /* skip other metadata blocks */
733                 for(i = 0; i < length; i++) {
734                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
735                                 return false; /* the read_callback_ sets the state for us */
736                 }
737         }
738
739         if(last_block)
740                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
741
742         return true;
743 }
744
745 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
746 {
747         FLAC__uint32 x;
748         unsigned i, skip;
749
750         /* skip the version and flags bytes */
751         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 24, read_callback_, decoder))
752                 return false; /* the read_callback_ sets the state for us */
753         /* get the size (in bytes) to skip */
754         skip = 0;
755         for(i = 0; i < 4; i++) {
756                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
757                         return false; /* the read_callback_ sets the state for us */
758                 skip <<= 7;
759                 skip |= (x & 0x7f);
760         }
761         /* skip the rest of the tag */
762         for(i = 0; i < skip; i++) {
763                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
764                         return false; /* the read_callback_ sets the state for us */
765         }
766         return true;
767 }
768
769 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
770 {
771         FLAC__uint32 x;
772         FLAC__bool first = true;
773
774         /* If we know the total number of samples in the stream, stop if we've read that many. */
775         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
776         if(decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.total_samples) {
777                 if(decoder->private->samples_decoded >= decoder->private->stream_info.data.stream_info.total_samples) {
778                         decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
779                         return true;
780                 }
781         }
782
783         /* make sure we're byte aligned */
784         if(decoder->private->input.consumed_bits != 0) {
785                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
786                         return false; /* the read_callback_ sets the state for us */
787         }
788
789         while(1) {
790                 if(decoder->private->cached) {
791                         x = (FLAC__uint32)decoder->private->lookahead;
792                         decoder->private->cached = false;
793                 }
794                 else {
795                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
796                                 return false; /* the read_callback_ sets the state for us */
797                 }
798                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
799                         decoder->private->header_warmup[0] = (FLAC__byte)x;
800                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
801                                 return false; /* the read_callback_ sets the state for us */
802
803                         /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
804                         /* else we have to check if the second byte is the end of a sync code */
805                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
806                                 decoder->private->lookahead = (FLAC__byte)x;
807                                 decoder->private->cached = true;
808                         }
809                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
810                                 decoder->private->header_warmup[1] = (FLAC__byte)x;
811                                 decoder->protected->state = FLAC__STREAM_DECODER_READ_FRAME;
812                                 return true;
813                         }
814                 }
815                 if(first) {
816                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
817                         first = 0;
818                 }
819         }
820
821         return true;
822 }
823
824 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
825 {
826         unsigned channel;
827         unsigned i;
828         FLAC__int32 mid, side, left, right;
829         FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
830         FLAC__uint32 x;
831
832         *got_a_frame = false;
833
834         /* init the CRC */
835         frame_crc = 0;
836         FLAC__CRC16_UPDATE(decoder->private->header_warmup[0], frame_crc);
837         FLAC__CRC16_UPDATE(decoder->private->header_warmup[1], frame_crc);
838         FLAC__bitbuffer_init_read_crc16(&decoder->private->input, frame_crc);
839
840         if(!stream_decoder_read_frame_header_(decoder))
841                 return false;
842         if(decoder->protected->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
843                 return true;
844         if(!stream_decoder_allocate_output_(decoder, decoder->private->frame.header.blocksize, decoder->private->frame.header.channels))
845                 return false;
846         for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
847                 /*
848                  * first figure the correct bits-per-sample of the subframe
849                  */
850                 unsigned bps = decoder->private->frame.header.bits_per_sample;
851                 switch(decoder->private->frame.header.channel_assignment) {
852                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
853                                 /* no adjustment needed */
854                                 break;
855                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
856                                 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
857                                 if(channel == 1)
858                                         bps++;
859                                 break;
860                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
861                                 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
862                                 if(channel == 0)
863                                         bps++;
864                                 break;
865                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
866                                 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
867                                 if(channel == 1)
868                                         bps++;
869                                 break;
870                         default:
871                                 FLAC__ASSERT(0);
872                 }
873                 /*
874                  * now read it
875                  */
876                 if(!stream_decoder_read_subframe_(decoder, channel, bps))
877                         return false;
878                 if(decoder->protected->state != FLAC__STREAM_DECODER_READ_FRAME) {
879                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
880                         return true;
881                 }
882         }
883         if(!stream_decoder_read_zero_padding_(decoder))
884                 return false;
885
886         /*
887          * Read the frame CRC-16 from the footer and check
888          */
889         frame_crc = decoder->private->input.read_crc16;
890         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
891                 return false; /* the read_callback_ sets the state for us */
892         if(frame_crc == (FLAC__uint16)x) {
893                 /* Undo any special channel coding */
894                 switch(decoder->private->frame.header.channel_assignment) {
895                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
896                                 /* do nothing */
897                                 break;
898                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
899                                 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
900                                 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
901                                         decoder->private->output[1][i] = decoder->private->output[0][i] - decoder->private->output[1][i];
902                                 break;
903                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
904                                 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
905                                 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
906                                         decoder->private->output[0][i] += decoder->private->output[1][i];
907                                 break;
908                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
909                                 FLAC__ASSERT(decoder->private->frame.header.channels == 2);
910                                 for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
911                                         mid = decoder->private->output[0][i];
912                                         side = decoder->private->output[1][i];
913                                         mid <<= 1;
914                                         if(side & 1) /* i.e. if 'side' is odd... */
915                                                 mid++;
916                                         left = mid + side;
917                                         right = mid - side;
918                                         decoder->private->output[0][i] = left >> 1;
919                                         decoder->private->output[1][i] = right >> 1;
920                                 }
921                                 break;
922                         default:
923                                 FLAC__ASSERT(0);
924                                 break;
925                 }
926         }
927         else {
928                 /* Bad frame, emit error and zero the output signal */
929                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private->client_data);
930                 for(channel = 0; channel < decoder->private->frame.header.channels; channel++) {
931                         memset(decoder->private->output[channel], 0, sizeof(FLAC__int32) * decoder->private->frame.header.blocksize);
932                 }
933         }
934
935         *got_a_frame = true;
936
937         /* put the latest values into the public section of the decoder instance */
938         decoder->protected->channels = decoder->private->frame.header.channels;
939         decoder->protected->channel_assignment = decoder->private->frame.header.channel_assignment;
940         decoder->protected->bits_per_sample = decoder->private->frame.header.bits_per_sample;
941         decoder->protected->sample_rate = decoder->private->frame.header.sample_rate;
942         decoder->protected->blocksize = decoder->private->frame.header.blocksize;
943
944         FLAC__ASSERT(decoder->private->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
945         decoder->private->samples_decoded = decoder->private->frame.header.number.sample_number + decoder->private->frame.header.blocksize;
946
947         /* write it */
948         /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
949         if(decoder->private->write_callback(decoder, &decoder->private->frame, decoder->private->output, decoder->private->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
950                 return false;
951
952         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
953         return true;
954 }
955
956 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
957 {
958         FLAC__uint32 x;
959         FLAC__uint64 xx;
960         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
961         FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
962         unsigned raw_header_len;
963         FLAC__bool is_unparseable = false;
964         const FLAC__bool is_known_variable_blocksize_stream = (decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.min_blocksize != decoder->private->stream_info.data.stream_info.max_blocksize);
965         const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private->has_stream_info && decoder->private->stream_info.data.stream_info.min_blocksize == decoder->private->stream_info.data.stream_info.max_blocksize);
966
967         FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
968
969         /* init the raw header with the saved bits from synchronization */
970         raw_header[0] = decoder->private->header_warmup[0];
971         raw_header[1] = decoder->private->header_warmup[1];
972         raw_header_len = 2;
973
974         /*
975          * check to make sure that the reserved bits are 0
976          */
977         if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
978                 is_unparseable = true;
979         }
980
981         /*
982          * Note that along the way as we read the header, we look for a sync
983          * code inside.  If we find one it would indicate that our original
984          * sync was bad since there cannot be a sync code in a valid header.
985          */
986
987         /*
988          * read in the raw header as bytes so we can CRC it, and parse it on the way
989          */
990         for(i = 0; i < 2; i++) {
991                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
992                         return false; /* the read_callback_ sets the state for us */
993                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
994                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
995                         decoder->private->lookahead = (FLAC__byte)x;
996                         decoder->private->cached = true;
997                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
998                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
999                         return true;
1000                 }
1001                 raw_header[raw_header_len++] = (FLAC__byte)x;
1002         }
1003
1004         switch(x = raw_header[2] >> 4) {
1005                 case 0:
1006                         if(is_known_fixed_blocksize_stream)
1007                                 decoder->private->frame.header.blocksize = decoder->private->stream_info.data.stream_info.min_blocksize;
1008                         else
1009                                 is_unparseable = true;
1010                         break;
1011                 case 1:
1012                         decoder->private->frame.header.blocksize = 192;
1013                         break;
1014                 case 2:
1015                 case 3:
1016                 case 4:
1017                 case 5:
1018                         decoder->private->frame.header.blocksize = 576 << (x-2);
1019                         break;
1020                 case 6:
1021                 case 7:
1022                         blocksize_hint = x;
1023                         break;
1024                 case 8:
1025                 case 9:
1026                 case 10:
1027                 case 11:
1028                 case 12:
1029                 case 13:
1030                 case 14:
1031                 case 15:
1032                         decoder->private->frame.header.blocksize = 256 << (x-8);
1033                         break;
1034                 default:
1035                         FLAC__ASSERT(0);
1036                         break;
1037         }
1038
1039         switch(x = raw_header[2] & 0x0f) {
1040                 case 0:
1041                         if(decoder->private->has_stream_info)
1042                                 decoder->private->frame.header.sample_rate = decoder->private->stream_info.data.stream_info.sample_rate;
1043                         else
1044                                 is_unparseable = true;
1045                         break;
1046                 case 1:
1047                 case 2:
1048                 case 3:
1049                         is_unparseable = true;
1050                         break;
1051                 case 4:
1052                         decoder->private->frame.header.sample_rate = 8000;
1053                         break;
1054                 case 5:
1055                         decoder->private->frame.header.sample_rate = 16000;
1056                         break;
1057                 case 6:
1058                         decoder->private->frame.header.sample_rate = 22050;
1059                         break;
1060                 case 7:
1061                         decoder->private->frame.header.sample_rate = 24000;
1062                         break;
1063                 case 8:
1064                         decoder->private->frame.header.sample_rate = 32000;
1065                         break;
1066                 case 9:
1067                         decoder->private->frame.header.sample_rate = 44100;
1068                         break;
1069                 case 10:
1070                         decoder->private->frame.header.sample_rate = 48000;
1071                         break;
1072                 case 11:
1073                         decoder->private->frame.header.sample_rate = 96000;
1074                         break;
1075                 case 12:
1076                 case 13:
1077                 case 14:
1078                         sample_rate_hint = x;
1079                         break;
1080                 case 15:
1081                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1082                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1083                         return true;
1084                 default:
1085                         FLAC__ASSERT(0);
1086         }
1087
1088         x = (unsigned)(raw_header[3] >> 4);
1089         if(x & 8) {
1090                 decoder->private->frame.header.channels = 2;
1091                 switch(x & 7) {
1092                         case 0:
1093                                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1094                                 break;
1095                         case 1:
1096                                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1097                                 break;
1098                         case 2:
1099                                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1100                                 break;
1101                         default:
1102                                 is_unparseable = true;
1103                                 break;
1104                 }
1105         }
1106         else {
1107                 decoder->private->frame.header.channels = (unsigned)x + 1;
1108                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1109         }
1110
1111         switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1112                 case 0:
1113                         if(decoder->private->has_stream_info)
1114                                 decoder->private->frame.header.bits_per_sample = decoder->private->stream_info.data.stream_info.bits_per_sample;
1115                         else
1116                                 is_unparseable = true;
1117                         break;
1118                 case 1:
1119                         decoder->private->frame.header.bits_per_sample = 8;
1120                         break;
1121                 case 2:
1122                         decoder->private->frame.header.bits_per_sample = 12;
1123                         break;
1124                 case 4:
1125                         decoder->private->frame.header.bits_per_sample = 16;
1126                         break;
1127                 case 5:
1128                         decoder->private->frame.header.bits_per_sample = 20;
1129                         break;
1130                 case 6:
1131                         decoder->private->frame.header.bits_per_sample = 24;
1132                         break;
1133                 case 3:
1134                 case 7:
1135                         is_unparseable = true;
1136                         break;
1137                 default:
1138                         FLAC__ASSERT(0);
1139                         break;
1140         }
1141
1142         if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1143                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1144                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1145                 return true;
1146         }
1147
1148         if(blocksize_hint && is_known_variable_blocksize_stream) {
1149                 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->private->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1150                         return false; /* the read_callback_ sets the state for us */
1151                 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1152                         decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1153                         decoder->private->cached = true;
1154                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1155                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1156                         return true;
1157                 }
1158                 decoder->private->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1159                 decoder->private->frame.header.number.sample_number = xx;
1160         }
1161         else {
1162                 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->private->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1163                         return false; /* the read_callback_ sets the state for us */
1164                 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1165                         decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1166                         decoder->private->cached = true;
1167                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1168                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1169                         return true;
1170                 }
1171                 decoder->private->last_frame_number = x;
1172                 if(decoder->private->has_stream_info) {
1173                         decoder->private->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1174                         decoder->private->frame.header.number.sample_number = (FLAC__int64)decoder->private->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1175                 }
1176                 else {
1177                         is_unparseable = true;
1178                 }
1179         }
1180
1181         if(blocksize_hint) {
1182                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1183                         return false; /* the read_callback_ sets the state for us */
1184                 raw_header[raw_header_len++] = (FLAC__byte)x;
1185                 if(blocksize_hint == 7) {
1186                         FLAC__uint32 _x;
1187                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
1188                                 return false; /* the read_callback_ sets the state for us */
1189                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1190                         x = (x << 8) | _x;
1191                 }
1192                 decoder->private->frame.header.blocksize = x+1;
1193         }
1194
1195         if(sample_rate_hint) {
1196                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1197                         return false; /* the read_callback_ sets the state for us */
1198                 raw_header[raw_header_len++] = (FLAC__byte)x;
1199                 if(sample_rate_hint != 12) {
1200                         FLAC__uint32 _x;
1201                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
1202                                 return false; /* the read_callback_ sets the state for us */
1203                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1204                         x = (x << 8) | _x;
1205                 }
1206                 if(sample_rate_hint == 12)
1207                         decoder->private->frame.header.sample_rate = x*1000;
1208                 else if(sample_rate_hint == 13)
1209                         decoder->private->frame.header.sample_rate = x;
1210                 else
1211                         decoder->private->frame.header.sample_rate = x*10;
1212         }
1213
1214         /* read the CRC-8 byte */
1215         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1216                 return false; /* the read_callback_ sets the state for us */
1217         crc8 = (FLAC__byte)x;
1218
1219         if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1220                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1221                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1222                 return true;
1223         }
1224
1225         if(is_unparseable) {
1226                 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1227                 return false;
1228         }
1229
1230         return true;
1231 }
1232
1233 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1234 {
1235         FLAC__uint32 x;
1236         FLAC__bool wasted_bits;
1237
1238         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1239                 return false; /* the read_callback_ sets the state for us */
1240
1241         wasted_bits = (x & 1);
1242         x &= 0xfe;
1243
1244         if(wasted_bits) {
1245                 unsigned u;
1246                 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->private->input, &u, read_callback_, decoder))
1247                         return false; /* the read_callback_ sets the state for us */
1248                 decoder->private->frame.subframes[channel].wasted_bits = u+1;
1249                 bps -= decoder->private->frame.subframes[channel].wasted_bits;
1250         }
1251         else
1252                 decoder->private->frame.subframes[channel].wasted_bits = 0;
1253
1254         /*
1255          * Lots of magic numbers here
1256          */
1257         if(x & 0x80) {
1258                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1259                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1260                 return true;
1261         }
1262         else if(x == 0) {
1263                 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1264                         return false;
1265         }
1266         else if(x == 2) {
1267                 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1268                         return false;
1269         }
1270         else if(x < 16) {
1271                 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1272                 return false;
1273         }
1274         else if(x <= 24) {
1275                 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1276                         return false;
1277         }
1278         else if(x < 64) {
1279                 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1280                 return false;
1281         }
1282         else {
1283                 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1284                         return false;
1285         }
1286
1287         if(wasted_bits) {
1288                 unsigned i;
1289                 x = decoder->private->frame.subframes[channel].wasted_bits;
1290                 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
1291                         decoder->private->output[channel][i] <<= x;
1292         }
1293
1294         return true;
1295 }
1296
1297 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1298 {
1299         FLAC__Subframe_Constant *subframe = &decoder->private->frame.subframes[channel].data.constant;
1300         FLAC__int32 x;
1301         unsigned i;
1302         FLAC__int32 *output = decoder->private->output[channel];
1303
1304         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1305
1306         if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
1307                 return false; /* the read_callback_ sets the state for us */
1308
1309         subframe->value = x;
1310
1311         /* decode the subframe */
1312         for(i = 0; i < decoder->private->frame.header.blocksize; i++)
1313                 output[i] = x;
1314
1315         return true;
1316 }
1317
1318 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1319 {
1320         FLAC__Subframe_Fixed *subframe = &decoder->private->frame.subframes[channel].data.fixed;
1321         FLAC__int32 i32;
1322         FLAC__uint32 u32;
1323         unsigned u;
1324
1325         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1326
1327         subframe->residual = decoder->private->residual[channel];
1328         subframe->order = order;
1329
1330         /* read warm-up samples */
1331         for(u = 0; u < order; u++) {
1332                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
1333                         return false; /* the read_callback_ sets the state for us */
1334                 subframe->warmup[u] = i32;
1335         }
1336
1337         /* read entropy coding method info */
1338         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1339                 return false; /* the read_callback_ sets the state for us */
1340         subframe->entropy_coding_method.type = u32;
1341         switch(subframe->entropy_coding_method.type) {
1342                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1343                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1344                                 return false; /* the read_callback_ sets the state for us */
1345                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1346                         break;
1347                 default:
1348                         decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1349                         return false;
1350         }
1351
1352         /* read residual */
1353         switch(subframe->entropy_coding_method.type) {
1354                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1355                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private->residual[channel]))
1356                                 return false;
1357                         break;
1358                 default:
1359                         FLAC__ASSERT(0);
1360         }
1361
1362         /* decode the subframe */
1363         memcpy(decoder->private->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1364         FLAC__fixed_restore_signal(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, order, decoder->private->output[channel]+order);
1365
1366         return true;
1367 }
1368
1369 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1370 {
1371         FLAC__Subframe_LPC *subframe = &decoder->private->frame.subframes[channel].data.lpc;
1372         FLAC__int32 i32;
1373         FLAC__uint32 u32;
1374         unsigned u;
1375
1376         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1377
1378         subframe->residual = decoder->private->residual[channel];
1379         subframe->order = order;
1380
1381         /* read warm-up samples */
1382         for(u = 0; u < order; u++) {
1383                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
1384                         return false; /* the read_callback_ sets the state for us */
1385                 subframe->warmup[u] = i32;
1386         }
1387
1388         /* read qlp coeff precision */
1389         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1390                 return false; /* the read_callback_ sets the state for us */
1391         if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1392                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1393                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1394                 return true;
1395         }
1396         subframe->qlp_coeff_precision = u32+1;
1397
1398         /* read qlp shift */
1399         if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1400                 return false; /* the read_callback_ sets the state for us */
1401         subframe->quantization_level = i32;
1402
1403         /* read quantized lp coefficiencts */
1404         for(u = 0; u < order; u++) {
1405                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1406                         return false; /* the read_callback_ sets the state for us */
1407                 subframe->qlp_coeff[u] = i32;
1408         }
1409
1410         /* read entropy coding method info */
1411         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1412                 return false; /* the read_callback_ sets the state for us */
1413         subframe->entropy_coding_method.type = u32;
1414         switch(subframe->entropy_coding_method.type) {
1415                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1416                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1417                                 return false; /* the read_callback_ sets the state for us */
1418                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1419                         break;
1420                 default:
1421                         decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1422                         return false;
1423         }
1424
1425         /* read residual */
1426         switch(subframe->entropy_coding_method.type) {
1427                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1428                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private->residual[channel]))
1429                                 return false;
1430                         break;
1431                 default:
1432                         FLAC__ASSERT(0);
1433         }
1434
1435         /* decode the subframe */
1436         memcpy(decoder->private->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1437         if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1438                 decoder->private->local_lpc_restore_signal_16bit(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private->output[channel]+order);
1439         else
1440                 decoder->private->local_lpc_restore_signal(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private->output[channel]+order);
1441
1442         return true;
1443 }
1444
1445 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1446 {
1447         FLAC__Subframe_Verbatim *subframe = &decoder->private->frame.subframes[channel].data.verbatim;
1448         FLAC__int32 x, *residual = decoder->private->residual[channel];
1449         unsigned i;
1450
1451         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1452
1453         subframe->data = residual;
1454
1455         for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
1456                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
1457                         return false; /* the read_callback_ sets the state for us */
1458                 residual[i] = x;
1459         }
1460
1461         /* decode the subframe */
1462         memcpy(decoder->private->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private->frame.header.blocksize);
1463
1464         return true;
1465 }
1466
1467 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1468 {
1469         FLAC__uint32 rice_parameter;
1470         int i;
1471         unsigned partition, sample, u;
1472         const unsigned partition_order = partitioned_rice->order;
1473         const unsigned partitions = 1u << partition_order;
1474         const unsigned partition_samples = partition_order > 0? decoder->private->frame.header.blocksize >> partition_order : decoder->private->frame.header.blocksize - predictor_order;
1475
1476         sample = 0;
1477         for(partition = 0; partition < partitions; partition++) {
1478                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1479                         return false; /* the read_callback_ sets the state for us */
1480                 partitioned_rice->parameters[partition] = rice_parameter;
1481                 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1482                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1483 #ifdef FLAC__SYMMETRIC_RICE
1484                                 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1485                                         return false; /* the read_callback_ sets the state for us */
1486 #else
1487                                 if(!FLAC__bitbuffer_read_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1488                                         return false; /* the read_callback_ sets the state for us */
1489 #endif
1490                                 residual[sample] = i;
1491                         }
1492                 }
1493                 else {
1494                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1495                                 return false; /* the read_callback_ sets the state for us */
1496                         partitioned_rice->raw_bits[partition] = rice_parameter;
1497                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1498                                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1499                                         return false; /* the read_callback_ sets the state for us */
1500                                 residual[sample] = i;
1501                         }
1502                 }
1503         }
1504
1505         return true;
1506 }
1507
1508 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1509 {
1510         if(decoder->private->input.consumed_bits != 0) {
1511                 FLAC__uint32 zero = 0;
1512                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &zero, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
1513                         return false; /* the read_callback_ sets the state for us */
1514                 if(zero != 0) {
1515                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1516                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1517                 }
1518         }
1519         return true;
1520 }
1521
1522 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1523 {
1524         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1525         FLAC__StreamDecoderReadStatus status;
1526         status = decoder->private->read_callback(decoder, buffer, bytes, decoder->private->client_data);
1527         if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1528                 decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1529         else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1530                 decoder->protected->state = FLAC__STREAM_DECODER_ABORTED;
1531         return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1532 }