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