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