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