fix bug with implicit assumption in ia32 mmx routines, add more complete filling...
[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         decoder->private->samples_decoded = decoder->private->frame.header.number.sample_number + decoder->private->frame.header.blocksize;
945
946         /* write it */
947         /* 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: */
948         if(decoder->private->write_callback(decoder, &decoder->private->frame, decoder->private->output, decoder->private->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
949                 return false;
950
951         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
952         return true;
953 }
954
955 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
956 {
957         FLAC__uint32 x;
958         FLAC__uint64 xx;
959         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
960         FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
961         unsigned raw_header_len;
962         FLAC__bool is_unparseable = false;
963         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);
964         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);
965
966         FLAC__ASSERT(decoder->private->input.consumed_bits == 0); /* make sure we're byte aligned */
967
968         /* init the raw header with the saved bits from synchronization */
969         raw_header[0] = decoder->private->header_warmup[0];
970         raw_header[1] = decoder->private->header_warmup[1];
971         raw_header_len = 2;
972
973         /*
974          * check to make sure that the reserved bits are 0
975          */
976         if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
977                 is_unparseable = true;
978         }
979
980         /*
981          * Note that along the way as we read the header, we look for a sync
982          * code inside.  If we find one it would indicate that our original
983          * sync was bad since there cannot be a sync code in a valid header.
984          */
985
986         /*
987          * read in the raw header as bytes so we can CRC it, and parse it on the way
988          */
989         for(i = 0; i < 2; i++) {
990                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
991                         return false; /* the read_callback_ sets the state for us */
992                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
993                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
994                         decoder->private->lookahead = (FLAC__byte)x;
995                         decoder->private->cached = true;
996                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
997                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
998                         return true;
999                 }
1000                 raw_header[raw_header_len++] = (FLAC__byte)x;
1001         }
1002
1003         switch(x = raw_header[2] >> 4) {
1004                 case 0:
1005                         if(is_known_fixed_blocksize_stream)
1006                                 decoder->private->frame.header.blocksize = decoder->private->stream_info.data.stream_info.min_blocksize;
1007                         else
1008                                 is_unparseable = true;
1009                         break;
1010                 case 1:
1011                         decoder->private->frame.header.blocksize = 192;
1012                         break;
1013                 case 2:
1014                 case 3:
1015                 case 4:
1016                 case 5:
1017                         decoder->private->frame.header.blocksize = 576 << (x-2);
1018                         break;
1019                 case 6:
1020                 case 7:
1021                         blocksize_hint = x;
1022                         break;
1023                 case 8:
1024                 case 9:
1025                 case 10:
1026                 case 11:
1027                 case 12:
1028                 case 13:
1029                 case 14:
1030                 case 15:
1031                         decoder->private->frame.header.blocksize = 256 << (x-8);
1032                         break;
1033                 default:
1034                         FLAC__ASSERT(0);
1035                         break;
1036         }
1037
1038         switch(x = raw_header[2] & 0x0f) {
1039                 case 0:
1040                         if(decoder->private->has_stream_info)
1041                                 decoder->private->frame.header.sample_rate = decoder->private->stream_info.data.stream_info.sample_rate;
1042                         else
1043                                 is_unparseable = true;
1044                         break;
1045                 case 1:
1046                 case 2:
1047                 case 3:
1048                         is_unparseable = true;
1049                         break;
1050                 case 4:
1051                         decoder->private->frame.header.sample_rate = 8000;
1052                         break;
1053                 case 5:
1054                         decoder->private->frame.header.sample_rate = 16000;
1055                         break;
1056                 case 6:
1057                         decoder->private->frame.header.sample_rate = 22050;
1058                         break;
1059                 case 7:
1060                         decoder->private->frame.header.sample_rate = 24000;
1061                         break;
1062                 case 8:
1063                         decoder->private->frame.header.sample_rate = 32000;
1064                         break;
1065                 case 9:
1066                         decoder->private->frame.header.sample_rate = 44100;
1067                         break;
1068                 case 10:
1069                         decoder->private->frame.header.sample_rate = 48000;
1070                         break;
1071                 case 11:
1072                         decoder->private->frame.header.sample_rate = 96000;
1073                         break;
1074                 case 12:
1075                 case 13:
1076                 case 14:
1077                         sample_rate_hint = x;
1078                         break;
1079                 case 15:
1080                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1081                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1082                         return true;
1083                 default:
1084                         FLAC__ASSERT(0);
1085         }
1086
1087         x = (unsigned)(raw_header[3] >> 4);
1088         if(x & 8) {
1089                 decoder->private->frame.header.channels = 2;
1090                 switch(x & 7) {
1091                         case 0:
1092                                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1093                                 break;
1094                         case 1:
1095                                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1096                                 break;
1097                         case 2:
1098                                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1099                                 break;
1100                         default:
1101                                 is_unparseable = true;
1102                                 break;
1103                 }
1104         }
1105         else {
1106                 decoder->private->frame.header.channels = (unsigned)x + 1;
1107                 decoder->private->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1108         }
1109
1110         switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1111                 case 0:
1112                         if(decoder->private->has_stream_info)
1113                                 decoder->private->frame.header.bits_per_sample = decoder->private->stream_info.data.stream_info.bits_per_sample;
1114                         else
1115                                 is_unparseable = true;
1116                         break;
1117                 case 1:
1118                         decoder->private->frame.header.bits_per_sample = 8;
1119                         break;
1120                 case 2:
1121                         decoder->private->frame.header.bits_per_sample = 12;
1122                         break;
1123                 case 4:
1124                         decoder->private->frame.header.bits_per_sample = 16;
1125                         break;
1126                 case 5:
1127                         decoder->private->frame.header.bits_per_sample = 20;
1128                         break;
1129                 case 6:
1130                         decoder->private->frame.header.bits_per_sample = 24;
1131                         break;
1132                 case 3:
1133                 case 7:
1134                         is_unparseable = true;
1135                         break;
1136                 default:
1137                         FLAC__ASSERT(0);
1138                         break;
1139         }
1140
1141         if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1142                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1143                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1144                 return true;
1145         }
1146
1147         if(blocksize_hint && is_known_variable_blocksize_stream) {
1148                 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->private->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1149                         return false; /* the read_callback_ sets the state for us */
1150                 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1151                         decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1152                         decoder->private->cached = true;
1153                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1154                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1155                         return true;
1156                 }
1157                 decoder->private->frame.header.number.sample_number = xx;
1158         }
1159         else {
1160                 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->private->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1161                         return false; /* the read_callback_ sets the state for us */
1162                 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1163                         decoder->private->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1164                         decoder->private->cached = true;
1165                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1166                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1167                         return true;
1168                 }
1169                 decoder->private->last_frame_number = x;
1170                 if(decoder->private->has_stream_info) {
1171                         decoder->private->frame.header.number.sample_number = (FLAC__int64)decoder->private->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1172                 }
1173                 else {
1174                         is_unparseable = true;
1175                 }
1176         }
1177
1178         if(blocksize_hint) {
1179                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1180                         return false; /* the read_callback_ sets the state for us */
1181                 raw_header[raw_header_len++] = (FLAC__byte)x;
1182                 if(blocksize_hint == 7) {
1183                         FLAC__uint32 _x;
1184                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
1185                                 return false; /* the read_callback_ sets the state for us */
1186                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1187                         x = (x << 8) | _x;
1188                 }
1189                 decoder->private->frame.header.blocksize = x+1;
1190         }
1191
1192         if(sample_rate_hint) {
1193                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1194                         return false; /* the read_callback_ sets the state for us */
1195                 raw_header[raw_header_len++] = (FLAC__byte)x;
1196                 if(sample_rate_hint != 12) {
1197                         FLAC__uint32 _x;
1198                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &_x, 8, read_callback_, decoder))
1199                                 return false; /* the read_callback_ sets the state for us */
1200                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1201                         x = (x << 8) | _x;
1202                 }
1203                 if(sample_rate_hint == 12)
1204                         decoder->private->frame.header.sample_rate = x*1000;
1205                 else if(sample_rate_hint == 13)
1206                         decoder->private->frame.header.sample_rate = x;
1207                 else
1208                         decoder->private->frame.header.sample_rate = x*10;
1209         }
1210
1211         /* read the CRC-8 byte */
1212         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder))
1213                 return false; /* the read_callback_ sets the state for us */
1214         crc8 = (FLAC__byte)x;
1215
1216         if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1217                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private->client_data);
1218                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1219                 return true;
1220         }
1221
1222         if(is_unparseable) {
1223                 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1224                 return false;
1225         }
1226
1227         return true;
1228 }
1229
1230 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1231 {
1232         FLAC__uint32 x;
1233         FLAC__bool wasted_bits;
1234
1235         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1236                 return false; /* the read_callback_ sets the state for us */
1237
1238         wasted_bits = (x & 1);
1239         x &= 0xfe;
1240
1241         if(wasted_bits) {
1242                 unsigned u;
1243                 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->private->input, &u, read_callback_, decoder))
1244                         return false; /* the read_callback_ sets the state for us */
1245                 decoder->private->frame.subframes[channel].wasted_bits = u+1;
1246                 bps -= decoder->private->frame.subframes[channel].wasted_bits;
1247         }
1248         else
1249                 decoder->private->frame.subframes[channel].wasted_bits = 0;
1250
1251         /*
1252          * Lots of magic numbers here
1253          */
1254         if(x & 0x80) {
1255                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1256                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1257                 return true;
1258         }
1259         else if(x == 0) {
1260                 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1261                         return false;
1262         }
1263         else if(x == 2) {
1264                 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1265                         return false;
1266         }
1267         else if(x < 16) {
1268                 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1269                 return false;
1270         }
1271         else if(x <= 24) {
1272                 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1273                         return false;
1274         }
1275         else if(x < 64) {
1276                 decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1277                 return false;
1278         }
1279         else {
1280                 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1281                         return false;
1282         }
1283
1284         if(wasted_bits) {
1285                 unsigned i;
1286                 x = decoder->private->frame.subframes[channel].wasted_bits;
1287                 for(i = 0; i < decoder->private->frame.header.blocksize; i++)
1288                         decoder->private->output[channel][i] <<= x;
1289         }
1290
1291         return true;
1292 }
1293
1294 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1295 {
1296         FLAC__Subframe_Constant *subframe = &decoder->private->frame.subframes[channel].data.constant;
1297         FLAC__int32 x;
1298         unsigned i;
1299         FLAC__int32 *output = decoder->private->output[channel];
1300
1301         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1302
1303         if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
1304                 return false; /* the read_callback_ sets the state for us */
1305
1306         subframe->value = x;
1307
1308         /* decode the subframe */
1309         for(i = 0; i < decoder->private->frame.header.blocksize; i++)
1310                 output[i] = x;
1311
1312         return true;
1313 }
1314
1315 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1316 {
1317         FLAC__Subframe_Fixed *subframe = &decoder->private->frame.subframes[channel].data.fixed;
1318         FLAC__int32 i32;
1319         FLAC__uint32 u32;
1320         unsigned u;
1321
1322         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1323
1324         subframe->residual = decoder->private->residual[channel];
1325         subframe->order = order;
1326
1327         /* read warm-up samples */
1328         for(u = 0; u < order; u++) {
1329                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
1330                         return false; /* the read_callback_ sets the state for us */
1331                 subframe->warmup[u] = i32;
1332         }
1333
1334         /* read entropy coding method info */
1335         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1336                 return false; /* the read_callback_ sets the state for us */
1337         subframe->entropy_coding_method.type = u32;
1338         switch(subframe->entropy_coding_method.type) {
1339                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1340                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1341                                 return false; /* the read_callback_ sets the state for us */
1342                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1343                         break;
1344                 default:
1345                         decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1346                         return false;
1347         }
1348
1349         /* read residual */
1350         switch(subframe->entropy_coding_method.type) {
1351                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1352                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private->residual[channel]))
1353                                 return false;
1354                         break;
1355                 default:
1356                         FLAC__ASSERT(0);
1357         }
1358
1359         /* decode the subframe */
1360         memcpy(decoder->private->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1361         FLAC__fixed_restore_signal(decoder->private->residual[channel], decoder->private->frame.header.blocksize-order, order, decoder->private->output[channel]+order);
1362
1363         return true;
1364 }
1365
1366 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1367 {
1368         FLAC__Subframe_LPC *subframe = &decoder->private->frame.subframes[channel].data.lpc;
1369         FLAC__int32 i32;
1370         FLAC__uint32 u32;
1371         unsigned u;
1372
1373         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1374
1375         subframe->residual = decoder->private->residual[channel];
1376         subframe->order = order;
1377
1378         /* read warm-up samples */
1379         for(u = 0; u < order; u++) {
1380                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, bps, read_callback_, decoder))
1381                         return false; /* the read_callback_ sets the state for us */
1382                 subframe->warmup[u] = i32;
1383         }
1384
1385         /* read qlp coeff precision */
1386         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1387                 return false; /* the read_callback_ sets the state for us */
1388         if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1389                 decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1390                 decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1391                 return true;
1392         }
1393         subframe->qlp_coeff_precision = u32+1;
1394
1395         /* read qlp shift */
1396         if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1397                 return false; /* the read_callback_ sets the state for us */
1398         subframe->quantization_level = i32;
1399
1400         /* read quantized lp coefficiencts */
1401         for(u = 0; u < order; u++) {
1402                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1403                         return false; /* the read_callback_ sets the state for us */
1404                 subframe->qlp_coeff[u] = i32;
1405         }
1406
1407         /* read entropy coding method info */
1408         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1409                 return false; /* the read_callback_ sets the state for us */
1410         subframe->entropy_coding_method.type = u32;
1411         switch(subframe->entropy_coding_method.type) {
1412                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1413                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1414                                 return false; /* the read_callback_ sets the state for us */
1415                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1416                         break;
1417                 default:
1418                         decoder->protected->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1419                         return false;
1420         }
1421
1422         /* read residual */
1423         switch(subframe->entropy_coding_method.type) {
1424                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1425                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private->residual[channel]))
1426                                 return false;
1427                         break;
1428                 default:
1429                         FLAC__ASSERT(0);
1430         }
1431
1432         /* decode the subframe */
1433         memcpy(decoder->private->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1434         if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1435                 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);
1436         else
1437                 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);
1438
1439         return true;
1440 }
1441
1442 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1443 {
1444         FLAC__Subframe_Verbatim *subframe = &decoder->private->frame.subframes[channel].data.verbatim;
1445         FLAC__int32 x, *residual = decoder->private->residual[channel];
1446         unsigned i;
1447
1448         decoder->private->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1449
1450         subframe->data = residual;
1451
1452         for(i = 0; i < decoder->private->frame.header.blocksize; i++) {
1453                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &x, bps, read_callback_, decoder))
1454                         return false; /* the read_callback_ sets the state for us */
1455                 residual[i] = x;
1456         }
1457
1458         /* decode the subframe */
1459         memcpy(decoder->private->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private->frame.header.blocksize);
1460
1461         return true;
1462 }
1463
1464 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1465 {
1466         FLAC__uint32 rice_parameter;
1467         int i;
1468         unsigned partition, sample, u;
1469         const unsigned partition_order = partitioned_rice->order;
1470         const unsigned partitions = 1u << partition_order;
1471         const unsigned partition_samples = partition_order > 0? decoder->private->frame.header.blocksize >> partition_order : decoder->private->frame.header.blocksize - predictor_order;
1472
1473         sample = 0;
1474         for(partition = 0; partition < partitions; partition++) {
1475                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1476                         return false; /* the read_callback_ sets the state for us */
1477                 partitioned_rice->parameters[partition] = rice_parameter;
1478                 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1479                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1480 #ifdef FLAC__SYMMETRIC_RICE
1481                                 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1482                                         return false; /* the read_callback_ sets the state for us */
1483 #else
1484                                 if(!FLAC__bitbuffer_read_rice_signed(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1485                                         return false; /* the read_callback_ sets the state for us */
1486 #endif
1487                                 residual[sample] = i;
1488                         }
1489                 }
1490                 else {
1491                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1492                                 return false; /* the read_callback_ sets the state for us */
1493                         partitioned_rice->raw_bits[partition] = rice_parameter;
1494                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1495                                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private->input, &i, rice_parameter, read_callback_, decoder))
1496                                         return false; /* the read_callback_ sets the state for us */
1497                                 residual[sample] = i;
1498                         }
1499                 }
1500         }
1501
1502         return true;
1503 }
1504
1505 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1506 {
1507         if(decoder->private->input.consumed_bits != 0) {
1508                 FLAC__uint32 zero = 0;
1509                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private->input, &zero, 8-decoder->private->input.consumed_bits, read_callback_, decoder))
1510                         return false; /* the read_callback_ sets the state for us */
1511                 if(zero != 0) {
1512                         decoder->private->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private->client_data);
1513                         decoder->protected->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1514                 }
1515         }
1516         return true;
1517 }
1518
1519 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1520 {
1521         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1522         FLAC__StreamDecoderReadStatus status;
1523         status = decoder->private->read_callback(decoder, buffer, bytes, decoder->private->client_data);
1524         if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1525                 decoder->protected->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1526         else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1527                 decoder->protected->state = FLAC__STREAM_DECODER_ABORTED;
1528         return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1529 }