minor syntax
[platform/upstream/flac.git] / src / libFLAC / stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003  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/bitmath.h"
27 #include "private/cpu.h"
28 #include "private/crc.h"
29 #include "private/fixed.h"
30 #include "private/format.h"
31 #include "private/lpc.h"
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #ifdef max
38 #undef max
39 #endif
40 #define max(a,b) ((a)>(b)?(a):(b))
41
42 /***********************************************************************
43  *
44  * Private static data
45  *
46  ***********************************************************************/
47
48 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
49
50 /***********************************************************************
51  *
52  * Private class method prototypes
53  *
54  ***********************************************************************/
55
56 static void set_defaults_(FLAC__StreamDecoder *decoder);
57 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
58 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
59 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
60 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
61 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
62 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
63 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
64 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
65 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
66 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
67 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
68 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
69 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
70 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
71 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
72 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
73 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
74 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
75 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
76 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
77
78 /***********************************************************************
79  *
80  * Private class data
81  *
82  ***********************************************************************/
83
84 typedef struct FLAC__StreamDecoderPrivate {
85         FLAC__StreamDecoderReadCallback read_callback;
86         FLAC__StreamDecoderWriteCallback write_callback;
87         FLAC__StreamDecoderMetadataCallback metadata_callback;
88         FLAC__StreamDecoderErrorCallback error_callback;
89         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[]);
90         void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
91         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[]);
92         void *client_data;
93         FLAC__BitBuffer *input;
94         FLAC__int32 *output[FLAC__MAX_CHANNELS];
95         FLAC__int32 *residual[FLAC__MAX_CHANNELS];
96         FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
97         unsigned output_capacity, output_channels;
98         FLAC__uint32 last_frame_number;
99         FLAC__uint64 samples_decoded;
100         FLAC__bool has_stream_info, has_seek_table;
101         FLAC__StreamMetadata stream_info;
102         FLAC__StreamMetadata seek_table;
103         FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
104         FLAC__byte *metadata_filter_ids;
105         unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
106         FLAC__Frame frame;
107         FLAC__bool cached; /* true if there is a byte in lookahead */
108         FLAC__CPUInfo cpuinfo;
109         FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
110         FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
111 } FLAC__StreamDecoderPrivate;
112
113 /***********************************************************************
114  *
115  * Public static class data
116  *
117  ***********************************************************************/
118
119 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
120         "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
121         "FLAC__STREAM_DECODER_READ_METADATA",
122         "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
123         "FLAC__STREAM_DECODER_READ_FRAME",
124         "FLAC__STREAM_DECODER_END_OF_STREAM",
125         "FLAC__STREAM_DECODER_ABORTED",
126         "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
127         "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
128         "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
129         "FLAC__STREAM_DECODER_INVALID_CALLBACK",
130         "FLAC__STREAM_DECODER_UNINITIALIZED"
131 };
132
133 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
134         "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
135         "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
136         "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
137 };
138
139 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
140         "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
141         "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
142 };
143
144 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
145         "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
146         "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
147         "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
148 };
149
150 /***********************************************************************
151  *
152  * Class constructor/destructor
153  *
154  ***********************************************************************/
155 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
156 {
157         FLAC__StreamDecoder *decoder;
158         unsigned i;
159
160         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
161
162         decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
163         if(decoder == 0) {
164                 return 0;
165         }
166
167         decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
168         if(decoder->protected_ == 0) {
169                 free(decoder);
170                 return 0;
171         }
172
173         decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
174         if(decoder->private_ == 0) {
175                 free(decoder->protected_);
176                 free(decoder);
177                 return 0;
178         }
179
180         decoder->private_->input = FLAC__bitbuffer_new();
181         if(decoder->private_->input == 0) {
182                 free(decoder->private_);
183                 free(decoder->protected_);
184                 free(decoder);
185                 return 0;
186         }
187
188         decoder->private_->metadata_filter_ids_capacity = 16;
189         if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
190                 FLAC__bitbuffer_delete(decoder->private_->input);
191                 free(decoder->private_);
192                 free(decoder->protected_);
193                 free(decoder);
194                 return 0;
195         }
196
197         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
198                 decoder->private_->output[i] = 0;
199                 decoder->private_->residual[i] = 0;
200         }
201
202         decoder->private_->output_capacity = 0;
203         decoder->private_->output_channels = 0;
204         decoder->private_->has_seek_table = false;
205
206         for(i = 0; i < FLAC__MAX_CHANNELS; i++)
207                 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
208
209         set_defaults_(decoder);
210
211         decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
212
213         return decoder;
214 }
215
216 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
217 {
218         unsigned i;
219
220         FLAC__ASSERT(0 != decoder);
221         FLAC__ASSERT(0 != decoder->protected_);
222         FLAC__ASSERT(0 != decoder->private_);
223         FLAC__ASSERT(0 != decoder->private_->input);
224
225         FLAC__stream_decoder_finish(decoder);
226
227         if(0 != decoder->private_->metadata_filter_ids)
228                 free(decoder->private_->metadata_filter_ids);
229
230         FLAC__bitbuffer_delete(decoder->private_->input);
231
232         for(i = 0; i < FLAC__MAX_CHANNELS; i++)
233                 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
234
235         free(decoder->private_);
236         free(decoder->protected_);
237         free(decoder);
238 }
239
240 /***********************************************************************
241  *
242  * Public class methods
243  *
244  ***********************************************************************/
245
246 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
247 {
248         FLAC__ASSERT(0 != decoder);
249
250         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
251                 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
252
253         if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
254                 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
255
256         if(!FLAC__bitbuffer_init(decoder->private_->input))
257                 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
258
259         decoder->private_->last_frame_number = 0;
260         decoder->private_->samples_decoded = 0;
261         decoder->private_->has_stream_info = false;
262         decoder->private_->cached = false;
263
264         /*
265          * get the CPU info and set the function pointers
266          */
267         FLAC__cpu_info(&decoder->private_->cpuinfo);
268         /* first default to the non-asm routines */
269         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
270         decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
271         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
272         /* now override with asm where appropriate */
273 #ifndef FLAC__NO_ASM
274         if(decoder->private_->cpuinfo.use_asm) {
275 #ifdef FLAC__CPU_IA32
276                 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
277 #ifdef FLAC__HAS_NASM
278                 if(decoder->private_->cpuinfo.data.ia32.mmx) {
279                         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
280                         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
281                 }
282                 else {
283                         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
284                         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
285                 }
286 #endif
287 #endif
288         }
289 #endif
290
291         if(!FLAC__stream_decoder_reset(decoder))
292                 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
293
294         return decoder->protected_->state;
295 }
296
297 FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
298 {
299         unsigned i;
300         FLAC__ASSERT(0 != decoder);
301         if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
302                 return;
303         if(0 != decoder->private_->seek_table.data.seek_table.points) {
304                 free(decoder->private_->seek_table.data.seek_table.points);
305                 decoder->private_->seek_table.data.seek_table.points = 0;
306                 decoder->private_->has_seek_table = false;
307         }
308         FLAC__bitbuffer_free(decoder->private_->input);
309         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
310                 /* WATCHOUT:
311                  * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
312                  * output arrays have a buffer of up to 3 zeroes in front
313                  * (at negative indices) for alignment purposes; we use 4
314                  * to keep the data well-aligned.
315                  */
316                 if(0 != decoder->private_->output[i]) {
317                         free(decoder->private_->output[i]-4);
318                         decoder->private_->output[i] = 0;
319                 }
320                 if(0 != decoder->private_->residual[i]) {
321                         free(decoder->private_->residual[i]);
322                         decoder->private_->residual[i] = 0;
323                 }
324         }
325         decoder->private_->output_capacity = 0;
326         decoder->private_->output_channels = 0;
327
328         set_defaults_(decoder);
329
330         decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
331 }
332
333 FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
334 {
335         FLAC__ASSERT(0 != decoder);
336         FLAC__ASSERT(0 != decoder->private_);
337         FLAC__ASSERT(0 != decoder->protected_);
338         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
339                 return false;
340         decoder->private_->read_callback = value;
341         return true;
342 }
343
344 FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
345 {
346         FLAC__ASSERT(0 != decoder);
347         FLAC__ASSERT(0 != decoder->private_);
348         FLAC__ASSERT(0 != decoder->protected_);
349         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
350                 return false;
351         decoder->private_->write_callback = value;
352         return true;
353 }
354
355 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
356 {
357         FLAC__ASSERT(0 != decoder);
358         FLAC__ASSERT(0 != decoder->private_);
359         FLAC__ASSERT(0 != decoder->protected_);
360         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
361                 return false;
362         decoder->private_->metadata_callback = value;
363         return true;
364 }
365
366 FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
367 {
368         FLAC__ASSERT(0 != decoder);
369         FLAC__ASSERT(0 != decoder->private_);
370         FLAC__ASSERT(0 != decoder->protected_);
371         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
372                 return false;
373         decoder->private_->error_callback = value;
374         return true;
375 }
376
377 FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
378 {
379         FLAC__ASSERT(0 != decoder);
380         FLAC__ASSERT(0 != decoder->private_);
381         FLAC__ASSERT(0 != decoder->protected_);
382         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
383                 return false;
384         decoder->private_->client_data = value;
385         return true;
386 }
387
388 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
389 {
390         FLAC__ASSERT(0 != decoder);
391         FLAC__ASSERT(0 != decoder->private_);
392         FLAC__ASSERT(0 != decoder->protected_);
393         FLAC__ASSERT(type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
394         /* double protection */
395         if(type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
396                 return false;
397         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
398                 return false;
399         decoder->private_->metadata_filter[type] = true;
400         if(type == FLAC__METADATA_TYPE_APPLICATION)
401                 decoder->private_->metadata_filter_ids_count = 0;
402         return true;
403 }
404
405 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
406 {
407         FLAC__ASSERT(0 != decoder);
408         FLAC__ASSERT(0 != decoder->private_);
409         FLAC__ASSERT(0 != decoder->protected_);
410         FLAC__ASSERT(0 != id);
411         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
412                 return false;
413
414         if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
415                 return true;
416
417         FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
418
419         if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
420                 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
421                         return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
422                 decoder->private_->metadata_filter_ids_capacity *= 2;
423         }
424
425         memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
426         decoder->private_->metadata_filter_ids_count++;
427
428         return true;
429 }
430
431 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
432 {
433         unsigned i;
434         FLAC__ASSERT(0 != decoder);
435         FLAC__ASSERT(0 != decoder->private_);
436         FLAC__ASSERT(0 != decoder->protected_);
437         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
438                 return false;
439         for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
440                 decoder->private_->metadata_filter[i] = true;
441         decoder->private_->metadata_filter_ids_count = 0;
442         return true;
443 }
444
445 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
446 {
447         FLAC__ASSERT(0 != decoder);
448         FLAC__ASSERT(0 != decoder->private_);
449         FLAC__ASSERT(0 != decoder->protected_);
450         FLAC__ASSERT(type < (1u << FLAC__STREAM_METADATA_TYPE_LEN));
451         /* double protection */
452         if(type >= (1u << FLAC__STREAM_METADATA_TYPE_LEN))
453                 return false;
454         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
455                 return false;
456         decoder->private_->metadata_filter[type] = false;
457         if(type == FLAC__METADATA_TYPE_APPLICATION)
458                 decoder->private_->metadata_filter_ids_count = 0;
459         return true;
460 }
461
462 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
463 {
464         FLAC__ASSERT(0 != decoder);
465         FLAC__ASSERT(0 != decoder->private_);
466         FLAC__ASSERT(0 != decoder->protected_);
467         FLAC__ASSERT(0 != id);
468         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
469                 return false;
470
471         if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
472                 return true;
473
474         FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
475
476         if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
477                 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
478                         return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
479                 decoder->private_->metadata_filter_ids_capacity *= 2;
480         }
481
482         memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
483         decoder->private_->metadata_filter_ids_count++;
484
485         return true;
486 }
487
488 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
489 {
490         FLAC__ASSERT(0 != decoder);
491         FLAC__ASSERT(0 != decoder->private_);
492         FLAC__ASSERT(0 != decoder->protected_);
493         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
494                 return false;
495         memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
496         decoder->private_->metadata_filter_ids_count = 0;
497         return true;
498 }
499
500 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
501 {
502         FLAC__ASSERT(0 != decoder);
503         FLAC__ASSERT(0 != decoder->protected_);
504         return decoder->protected_->state;
505 }
506
507 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
508 {
509         FLAC__ASSERT(0 != decoder);
510         FLAC__ASSERT(0 != decoder->protected_);
511         return decoder->protected_->channels;
512 }
513
514 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
515 {
516         FLAC__ASSERT(0 != decoder);
517         FLAC__ASSERT(0 != decoder->protected_);
518         return decoder->protected_->channel_assignment;
519 }
520
521 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
522 {
523         FLAC__ASSERT(0 != decoder);
524         FLAC__ASSERT(0 != decoder->protected_);
525         return decoder->protected_->bits_per_sample;
526 }
527
528 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
529 {
530         FLAC__ASSERT(0 != decoder);
531         FLAC__ASSERT(0 != decoder->protected_);
532         return decoder->protected_->sample_rate;
533 }
534
535 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
536 {
537         FLAC__ASSERT(0 != decoder);
538         FLAC__ASSERT(0 != decoder->protected_);
539         return decoder->protected_->blocksize;
540 }
541
542 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
543 {
544         FLAC__ASSERT(0 != decoder);
545         FLAC__ASSERT(0 != decoder->private_);
546         FLAC__ASSERT(0 != decoder->protected_);
547
548         if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
549                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
550                 return false;
551         }
552         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
553
554         return true;
555 }
556
557 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
558 {
559         FLAC__ASSERT(0 != decoder);
560         FLAC__ASSERT(0 != decoder->private_);
561         FLAC__ASSERT(0 != decoder->protected_);
562
563         if(!FLAC__stream_decoder_flush(decoder)) {
564                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
565                 return false;
566         }
567         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
568
569         decoder->private_->samples_decoded = 0;
570
571         return true;
572 }
573
574 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
575 {
576         FLAC__bool got_a_frame;
577         FLAC__ASSERT(0 != decoder);
578         FLAC__ASSERT(0 != decoder->protected_);
579
580         while(1) {
581                 switch(decoder->protected_->state) {
582                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
583                                 if(!find_metadata_(decoder))
584                                         return false; /* above function sets the status for us */
585                                 break;
586                         case FLAC__STREAM_DECODER_READ_METADATA:
587                                 if(!read_metadata_(decoder))
588                                         return false; /* above function sets the status for us */
589                                 else
590                                         return true;
591                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
592                                 if(!frame_sync_(decoder))
593                                         return true; /* above function sets the status for us */
594                                 break;
595                         case FLAC__STREAM_DECODER_READ_FRAME:
596                                 if(!read_frame_(decoder, &got_a_frame))
597                                         return false; /* above function sets the status for us */
598                                 if(got_a_frame)
599                                         return true; /* above function sets the status for us */
600                                 break;
601                         case FLAC__STREAM_DECODER_END_OF_STREAM:
602                         case FLAC__STREAM_DECODER_ABORTED:
603                                 return true;
604                         default:
605                                 FLAC__ASSERT(0);
606                                 return false;
607                 }
608         }
609 }
610
611 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
612 {
613         FLAC__ASSERT(0 != decoder);
614         FLAC__ASSERT(0 != decoder->protected_);
615
616         while(1) {
617                 switch(decoder->protected_->state) {
618                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
619                                 if(!find_metadata_(decoder))
620                                         return false; /* above function sets the status for us */
621                                 break;
622                         case FLAC__STREAM_DECODER_READ_METADATA:
623                                 if(!read_metadata_(decoder))
624                                         return false; /* above function sets the status for us */
625                                 break;
626                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
627                         case FLAC__STREAM_DECODER_READ_FRAME:
628                         case FLAC__STREAM_DECODER_END_OF_STREAM:
629                         case FLAC__STREAM_DECODER_ABORTED:
630                                 return true;
631                         default:
632                                 FLAC__ASSERT(0);
633                                 return false;
634                 }
635         }
636 }
637
638 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
639 {
640         FLAC__bool dummy;
641         FLAC__ASSERT(0 != decoder);
642         FLAC__ASSERT(0 != decoder->protected_);
643
644         while(1) {
645                 switch(decoder->protected_->state) {
646                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
647                                 if(!find_metadata_(decoder))
648                                         return false; /* above function sets the status for us */
649                                 break;
650                         case FLAC__STREAM_DECODER_READ_METADATA:
651                                 if(!read_metadata_(decoder))
652                                         return false; /* above function sets the status for us */
653                                 break;
654                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
655                                 if(!frame_sync_(decoder))
656                                         return true; /* above function sets the status for us */
657                                 break;
658                         case FLAC__STREAM_DECODER_READ_FRAME:
659                                 if(!read_frame_(decoder, &dummy))
660                                         return false; /* above function sets the status for us */
661                                 break;
662                         case FLAC__STREAM_DECODER_END_OF_STREAM:
663                         case FLAC__STREAM_DECODER_ABORTED:
664                                 return true;
665                         default:
666                                 FLAC__ASSERT(0);
667                                 return false;
668                 }
669         }
670 }
671
672 /***********************************************************************
673  *
674  * Protected class methods
675  *
676  ***********************************************************************/
677
678 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
679 {
680         FLAC__ASSERT(0 != decoder);
681         return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
682 }
683
684 /***********************************************************************
685  *
686  * Private class methods
687  *
688  ***********************************************************************/
689
690 void set_defaults_(FLAC__StreamDecoder *decoder)
691 {
692         decoder->private_->read_callback = 0;
693         decoder->private_->write_callback = 0;
694         decoder->private_->metadata_callback = 0;
695         decoder->private_->error_callback = 0;
696         decoder->private_->client_data = 0;
697
698         memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
699         decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
700         decoder->private_->metadata_filter_ids_count = 0;
701 }
702
703 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
704 {
705         unsigned i;
706         FLAC__int32 *tmp;
707
708         if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
709                 return true;
710
711         /* simply using realloc() is not practical because the number of channels may change mid-stream */
712
713         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
714                 if(0 != decoder->private_->output[i]) {
715                         free(decoder->private_->output[i]-4);
716                         decoder->private_->output[i] = 0;
717                 }
718                 if(0 != decoder->private_->residual[i]) {
719                         free(decoder->private_->residual[i]);
720                         decoder->private_->residual[i] = 0;
721                 }
722         }
723
724         for(i = 0; i < channels; i++) {
725                 /* WATCHOUT:
726                  * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
727                  * output arrays have a buffer of up to 3 zeroes in front
728                  * (at negative indices) for alignment purposes; we use 4
729                  * to keep the data well-aligned.
730                  */
731                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
732                 if(tmp == 0) {
733                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
734                         return false;
735                 }
736                 memset(tmp, 0, sizeof(FLAC__int32)*4);
737                 decoder->private_->output[i] = tmp + 4;
738
739                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
740                 if(tmp == 0) {
741                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
742                         return false;
743                 }
744                 decoder->private_->residual[i] = tmp;
745         }
746
747         decoder->private_->output_capacity = size;
748         decoder->private_->output_channels = channels;
749
750         return true;
751 }
752
753 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
754 {
755         unsigned i;
756
757         FLAC__ASSERT(0 != decoder);
758         FLAC__ASSERT(0 != decoder->private_);
759
760         for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
761                 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
762                         return true;
763
764         return false;
765 }
766
767 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
768 {
769         FLAC__uint32 x;
770         unsigned i, id;
771         FLAC__bool first = true;
772
773         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
774
775         for(i = id = 0; i < 4; ) {
776                 if(decoder->private_->cached) {
777                         x = (FLAC__uint32)decoder->private_->lookahead;
778                         decoder->private_->cached = false;
779                 }
780                 else {
781                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
782                                 return false; /* the read_callback_ sets the state for us */
783                 }
784                 if(x == FLAC__STREAM_SYNC_STRING[i]) {
785                         first = true;
786                         i++;
787                         id = 0;
788                         continue;
789                 }
790                 if(x == ID3V2_TAG_[id]) {
791                         id++;
792                         i = 0;
793                         if(id == 3) {
794                                 if(!skip_id3v2_tag_(decoder))
795                                         return false; /* the read_callback_ sets the state for us */
796                         }
797                         continue;
798                 }
799                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
800                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
801                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
802                                 return false; /* the read_callback_ sets the state for us */
803
804                         /* 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 */
805                         /* else we have to check if the second byte is the end of a sync code */
806                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
807                                 decoder->private_->lookahead = (FLAC__byte)x;
808                                 decoder->private_->cached = true;
809                         }
810                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
811                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
812                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
813                                 return true;
814                         }
815                 }
816                 i = 0;
817                 if(first) {
818                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
819                         first = false;
820                 }
821         }
822
823         decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
824         return true;
825 }
826
827 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
828 {
829         FLAC__bool is_last;
830         FLAC__uint32 i, x, type, length;
831
832         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
833
834         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
835                 return false; /* the read_callback_ sets the state for us */
836         is_last = x? true : false;
837
838         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
839                 return false; /* the read_callback_ sets the state for us */
840
841         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
842                 return false; /* the read_callback_ sets the state for us */
843
844         if(type == FLAC__METADATA_TYPE_STREAMINFO) {
845                 if(!read_metadata_streaminfo_(decoder, is_last, length))
846                         return false;
847
848                 decoder->private_->has_stream_info = true;
849                 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
850                         decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
851         }
852         else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
853                 if(!read_metadata_seektable_(decoder, is_last, length))
854                         return false;
855
856                 decoder->private_->has_seek_table = true;
857                 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
858                         decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
859         }
860         else {
861                 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
862                 unsigned real_length = length;
863                 FLAC__StreamMetadata block;
864
865                 block.is_last = is_last;
866                 block.type = (FLAC__MetadataType)type;
867                 block.length = length;
868
869                 if(type == FLAC__METADATA_TYPE_APPLICATION) {
870                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
871                                 return false; /* the read_callback_ sets the state for us */
872
873                         real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
874
875                         if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
876                                 skip_it = !skip_it;
877                 }
878
879                 if(skip_it) {
880                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
881                                 return false; /* the read_callback_ sets the state for us */
882                 }
883                 else {
884                         switch(type) {
885                                 case FLAC__METADATA_TYPE_PADDING:
886                                         /* skip the padding bytes */
887                                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
888                                                 return false; /* the read_callback_ sets the state for us */
889                                         break;
890                                 case FLAC__METADATA_TYPE_APPLICATION:
891                                         /* remember, we read the ID already */
892                                         if(real_length > 0) {
893                                                 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
894                                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
895                                                         return false;
896                                                 }
897                                                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
898                                                         return false; /* the read_callback_ sets the state for us */
899                                         }
900                                         else
901                                                 block.data.application.data = 0;
902                                         break;
903                                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
904                                         if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
905                                                 return false;
906                                         break;
907                                 case FLAC__METADATA_TYPE_CUESHEET:
908                                         if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
909                                                 return false;
910                                         break;
911                                 case FLAC__METADATA_TYPE_STREAMINFO:
912                                 case FLAC__METADATA_TYPE_SEEKTABLE:
913                                         FLAC__ASSERT(0);
914                                         break;
915                                 default:
916                                         if(real_length > 0) {
917                                                 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
918                                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
919                                                         return false;
920                                                 }
921                                                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
922                                                         return false; /* the read_callback_ sets the state for us */
923                                         }
924                                         else
925                                                 block.data.unknown.data = 0;
926                                         break;
927                         }
928                         decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
929
930                         /* now we have to free any malloc'ed data in the block */
931                         switch(type) {
932                                 case FLAC__METADATA_TYPE_PADDING:
933                                         break;
934                                 case FLAC__METADATA_TYPE_APPLICATION:
935                                         if(0 != block.data.application.data)
936                                                 free(block.data.application.data);
937                                         break;
938                                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
939                                         if(0 != block.data.vorbis_comment.vendor_string.entry)
940                                                 free(block.data.vorbis_comment.vendor_string.entry);
941                                         if(block.data.vorbis_comment.num_comments > 0)
942                                                 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
943                                                         if(0 != block.data.vorbis_comment.comments[i].entry)
944                                                                 free(block.data.vorbis_comment.comments[i].entry);
945                                         if(0 != block.data.vorbis_comment.comments)
946                                                 free(block.data.vorbis_comment.comments);
947                                         break;
948                                 case FLAC__METADATA_TYPE_CUESHEET:
949                                         if(block.data.cue_sheet.num_tracks > 0)
950                                                 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
951                                                         if(0 != block.data.cue_sheet.tracks[i].indices)
952                                                                 free(block.data.cue_sheet.tracks[i].indices);
953                                         if(0 != block.data.cue_sheet.tracks)
954                                                 free(block.data.cue_sheet.tracks);
955                                         break;
956                                 case FLAC__METADATA_TYPE_STREAMINFO:
957                                 case FLAC__METADATA_TYPE_SEEKTABLE:
958                                         FLAC__ASSERT(0);
959                                 default:
960                                         if(0 != block.data.unknown.data)
961                                                 free(block.data.unknown.data);
962                                         break;
963                         }
964                 }
965         }
966
967         if(is_last)
968                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
969
970         return true;
971 }
972
973 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
974 {
975         FLAC__uint32 x;
976         unsigned bits, used_bits = 0;
977
978         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
979
980         decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
981         decoder->private_->stream_info.is_last = is_last;
982         decoder->private_->stream_info.length = length;
983
984         bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
985         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
986                 return false; /* the read_callback_ sets the state for us */
987         decoder->private_->stream_info.data.stream_info.min_blocksize = x;
988         used_bits += bits;
989
990         bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
991         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
992                 return false; /* the read_callback_ sets the state for us */
993         decoder->private_->stream_info.data.stream_info.max_blocksize = x;
994         used_bits += bits;
995
996         bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
997         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
998                 return false; /* the read_callback_ sets the state for us */
999         decoder->private_->stream_info.data.stream_info.min_framesize = x;
1000         used_bits += bits;
1001
1002         bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1003         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1004                 return false; /* the read_callback_ sets the state for us */
1005         decoder->private_->stream_info.data.stream_info.max_framesize = x;
1006         used_bits += bits;
1007
1008         bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1009         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1010                 return false; /* the read_callback_ sets the state for us */
1011         decoder->private_->stream_info.data.stream_info.sample_rate = x;
1012         used_bits += bits;
1013
1014         bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1015         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1016                 return false; /* the read_callback_ sets the state for us */
1017         decoder->private_->stream_info.data.stream_info.channels = x+1;
1018         used_bits += bits;
1019
1020         bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1021         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1022                 return false; /* the read_callback_ sets the state for us */
1023         decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1024         used_bits += bits;
1025
1026         bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1027         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))
1028                 return false; /* the read_callback_ sets the state for us */
1029         used_bits += bits;
1030
1031         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1032                 return false; /* the read_callback_ sets the state for us */
1033         used_bits += 16*8;
1034
1035         /* skip the rest of the block */
1036         FLAC__ASSERT(used_bits % 8 == 0);
1037         length -= (used_bits / 8);
1038         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1039                 return false; /* the read_callback_ sets the state for us */
1040
1041         return true;
1042 }
1043
1044 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1045 {
1046         FLAC__uint32 i, x;
1047         FLAC__uint64 xx;
1048
1049         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1050
1051         decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1052         decoder->private_->seek_table.is_last = is_last;
1053         decoder->private_->seek_table.length = length;
1054
1055         decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1056
1057         /* use realloc since we may pass through here several times (e.g. after seeking) */
1058         if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
1059                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1060                 return false;
1061         }
1062         for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1063                 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1064                         return false; /* the read_callback_ sets the state for us */
1065                 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1066
1067                 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1068                         return false; /* the read_callback_ sets the state for us */
1069                 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1070
1071                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1072                         return false; /* the read_callback_ sets the state for us */
1073                 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1074         }
1075         length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1076         /* if there is a partial point left, skip over it */
1077         if(length > 0) {
1078                 /*@@@ do an error_callback() here?  there's an argument for either way */
1079                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1080                         return false; /* the read_callback_ sets the state for us */
1081         }
1082
1083         return true;
1084 }
1085
1086 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1087 {
1088         FLAC__uint32 i;
1089
1090         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1091
1092         /* read vendor string */
1093         FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1094         if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1095                 return false; /* the read_callback_ sets the state for us */
1096         if(obj->vendor_string.length > 0) {
1097                 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length))) {
1098                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1099                         return false;
1100                 }
1101                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1102                         return false; /* the read_callback_ sets the state for us */
1103         }
1104         else
1105                 obj->vendor_string.entry = 0;
1106
1107         /* read num comments */
1108         FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1109         if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1110                 return false; /* the read_callback_ sets the state for us */
1111
1112         /* read comments */
1113         if(obj->num_comments > 0) {
1114                 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1115                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1116                         return false;
1117                 }
1118                 for(i = 0; i < obj->num_comments; i++) {
1119                         FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1120                         if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1121                                 return false; /* the read_callback_ sets the state for us */
1122                         if(obj->comments[i].length > 0) {
1123                                 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length))) {
1124                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1125                                         return false;
1126                                 }
1127                                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1128                                         return false; /* the read_callback_ sets the state for us */
1129                         }
1130                         else
1131                                 obj->comments[i].entry = 0;
1132                 }
1133         }
1134         else {
1135                 obj->comments = 0;
1136         }
1137
1138         return true;
1139 }
1140
1141 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1142 {
1143         FLAC__uint32 i, j, x;
1144
1145         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1146
1147         memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1148
1149         FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1150         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
1151                 return false; /* the read_callback_ sets the state for us */
1152
1153         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1154                 return false; /* the read_callback_ sets the state for us */
1155
1156         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1157                 return false; /* the read_callback_ sets the state for us */
1158         obj->is_cd = x? true : false;
1159
1160         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1161                 return false; /* the read_callback_ sets the state for us */
1162
1163         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1164                 return false; /* the read_callback_ sets the state for us */
1165         obj->num_tracks = x;
1166
1167         if(obj->num_tracks > 0) {
1168                 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1169                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1170                         return false;
1171                 }
1172                 for(i = 0; i < obj->num_tracks; i++) {
1173                         FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1174                         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1175                                 return false; /* the read_callback_ sets the state for us */
1176
1177                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1178                                 return false; /* the read_callback_ sets the state for us */
1179                         track->number = (FLAC__byte)x;
1180
1181                         FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1182                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
1183                                 return false; /* the read_callback_ sets the state for us */
1184
1185                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1186                                 return false; /* the read_callback_ sets the state for us */
1187                         track->type = x;
1188
1189                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1190                                 return false; /* the read_callback_ sets the state for us */
1191                         track->pre_emphasis = x;
1192
1193                         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1194                                 return false; /* the read_callback_ sets the state for us */
1195
1196                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1197                                 return false; /* the read_callback_ sets the state for us */
1198                         track->num_indices = (FLAC__byte)x;
1199
1200                         if(track->num_indices > 0) {
1201                                 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1202                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1203                                         return false;
1204                                 }
1205                                 for(j = 0; j < track->num_indices; j++) {
1206                                         FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1207                                         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1208                                                 return false; /* the read_callback_ sets the state for us */
1209
1210                                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1211                                                 return false; /* the read_callback_ sets the state for us */
1212                                         index->number = (FLAC__byte)x;
1213
1214                                         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1215                                                 return false; /* the read_callback_ sets the state for us */
1216                                 }
1217                         }
1218                 }
1219         }
1220
1221         return true;
1222 }
1223
1224 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1225 {
1226         FLAC__uint32 x;
1227         unsigned i, skip;
1228
1229         /* skip the version and flags bytes */
1230         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1231                 return false; /* the read_callback_ sets the state for us */
1232         /* get the size (in bytes) to skip */
1233         skip = 0;
1234         for(i = 0; i < 4; i++) {
1235                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1236                         return false; /* the read_callback_ sets the state for us */
1237                 skip <<= 7;
1238                 skip |= (x & 0x7f);
1239         }
1240         /* skip the rest of the tag */
1241         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1242                 return false; /* the read_callback_ sets the state for us */
1243         return true;
1244 }
1245
1246 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1247 {
1248         FLAC__uint32 x;
1249         FLAC__bool first = true;
1250
1251         /* If we know the total number of samples in the stream, stop if we've read that many. */
1252         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1253         if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1254                 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1255                         decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1256                         return true;
1257                 }
1258         }
1259
1260         /* make sure we're byte aligned */
1261         if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1262                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1263                         return false; /* the read_callback_ sets the state for us */
1264         }
1265
1266         while(1) {
1267                 if(decoder->private_->cached) {
1268                         x = (FLAC__uint32)decoder->private_->lookahead;
1269                         decoder->private_->cached = false;
1270                 }
1271                 else {
1272                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1273                                 return false; /* the read_callback_ sets the state for us */
1274                 }
1275                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1276                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
1277                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1278                                 return false; /* the read_callback_ sets the state for us */
1279
1280                         /* 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 */
1281                         /* else we have to check if the second byte is the end of a sync code */
1282                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1283                                 decoder->private_->lookahead = (FLAC__byte)x;
1284                                 decoder->private_->cached = true;
1285                         }
1286                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1287                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1288                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1289                                 return true;
1290                         }
1291                 }
1292                 if(first) {
1293                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1294                         first = false;
1295                 }
1296         }
1297
1298         return true;
1299 }
1300
1301 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1302 {
1303         unsigned channel;
1304         unsigned i;
1305         FLAC__int32 mid, side, left, right;
1306         FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1307         FLAC__uint32 x;
1308
1309         *got_a_frame = false;
1310
1311         /* init the CRC */
1312         frame_crc = 0;
1313         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1314         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1315         FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1316
1317         if(!read_frame_header_(decoder))
1318                 return false;
1319         if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1320                 return true;
1321         if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1322                 return false;
1323         for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1324                 /*
1325                  * first figure the correct bits-per-sample of the subframe
1326                  */
1327                 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1328                 switch(decoder->private_->frame.header.channel_assignment) {
1329                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1330                                 /* no adjustment needed */
1331                                 break;
1332                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1333                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1334                                 if(channel == 1)
1335                                         bps++;
1336                                 break;
1337                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1338                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1339                                 if(channel == 0)
1340                                         bps++;
1341                                 break;
1342                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1343                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1344                                 if(channel == 1)
1345                                         bps++;
1346                                 break;
1347                         default:
1348                                 FLAC__ASSERT(0);
1349                 }
1350                 /*
1351                  * now read it
1352                  */
1353                 if(!read_subframe_(decoder, channel, bps))
1354                         return false;
1355                 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1356                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1357                         return true;
1358                 }
1359         }
1360         if(!read_zero_padding_(decoder))
1361                 return false;
1362
1363         /*
1364          * Read the frame CRC-16 from the footer and check
1365          */
1366         frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1367         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1368                 return false; /* the read_callback_ sets the state for us */
1369         if(frame_crc == (FLAC__uint16)x) {
1370                 /* Undo any special channel coding */
1371                 switch(decoder->private_->frame.header.channel_assignment) {
1372                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1373                                 /* do nothing */
1374                                 break;
1375                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1376                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1377                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1378                                         decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1379                                 break;
1380                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1381                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1382                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1383                                         decoder->private_->output[0][i] += decoder->private_->output[1][i];
1384                                 break;
1385                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1386                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1387                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1388                                         mid = decoder->private_->output[0][i];
1389                                         side = decoder->private_->output[1][i];
1390                                         mid <<= 1;
1391                                         if(side & 1) /* i.e. if 'side' is odd... */
1392                                                 mid++;
1393                                         left = mid + side;
1394                                         right = mid - side;
1395                                         decoder->private_->output[0][i] = left >> 1;
1396                                         decoder->private_->output[1][i] = right >> 1;
1397                                 }
1398                                 break;
1399                         default:
1400                                 FLAC__ASSERT(0);
1401                                 break;
1402                 }
1403         }
1404         else {
1405                 /* Bad frame, emit error and zero the output signal */
1406                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1407                 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1408                         memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1409                 }
1410         }
1411
1412         *got_a_frame = true;
1413
1414         /* put the latest values into the public section of the decoder instance */
1415         decoder->protected_->channels = decoder->private_->frame.header.channels;
1416         decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1417         decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1418         decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1419         decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1420
1421         FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1422         decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1423
1424         /* write it */
1425         if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
1426                 return false;
1427
1428         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1429         return true;
1430 }
1431
1432 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1433 {
1434         FLAC__uint32 x;
1435         FLAC__uint64 xx;
1436         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1437         FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1438         unsigned raw_header_len;
1439         FLAC__bool is_unparseable = false;
1440         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);
1441         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);
1442
1443         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1444
1445         /* init the raw header with the saved bits from synchronization */
1446         raw_header[0] = decoder->private_->header_warmup[0];
1447         raw_header[1] = decoder->private_->header_warmup[1];
1448         raw_header_len = 2;
1449
1450         /*
1451          * check to make sure that the reserved bits are 0
1452          */
1453         if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1454                 is_unparseable = true;
1455         }
1456
1457         /*
1458          * Note that along the way as we read the header, we look for a sync
1459          * code inside.  If we find one it would indicate that our original
1460          * sync was bad since there cannot be a sync code in a valid header.
1461          */
1462
1463         /*
1464          * read in the raw header as bytes so we can CRC it, and parse it on the way
1465          */
1466         for(i = 0; i < 2; i++) {
1467                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1468                         return false; /* the read_callback_ sets the state for us */
1469                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1470                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1471                         decoder->private_->lookahead = (FLAC__byte)x;
1472                         decoder->private_->cached = true;
1473                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1474                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1475                         return true;
1476                 }
1477                 raw_header[raw_header_len++] = (FLAC__byte)x;
1478         }
1479
1480         switch(x = raw_header[2] >> 4) {
1481                 case 0:
1482                         if(is_known_fixed_blocksize_stream)
1483                                 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1484                         else
1485                                 is_unparseable = true;
1486                         break;
1487                 case 1:
1488                         decoder->private_->frame.header.blocksize = 192;
1489                         break;
1490                 case 2:
1491                 case 3:
1492                 case 4:
1493                 case 5:
1494                         decoder->private_->frame.header.blocksize = 576 << (x-2);
1495                         break;
1496                 case 6:
1497                 case 7:
1498                         blocksize_hint = x;
1499                         break;
1500                 case 8:
1501                 case 9:
1502                 case 10:
1503                 case 11:
1504                 case 12:
1505                 case 13:
1506                 case 14:
1507                 case 15:
1508                         decoder->private_->frame.header.blocksize = 256 << (x-8);
1509                         break;
1510                 default:
1511                         FLAC__ASSERT(0);
1512                         break;
1513         }
1514
1515         switch(x = raw_header[2] & 0x0f) {
1516                 case 0:
1517                         if(decoder->private_->has_stream_info)
1518                                 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1519                         else
1520                                 is_unparseable = true;
1521                         break;
1522                 case 1:
1523                 case 2:
1524                 case 3:
1525                         is_unparseable = true;
1526                         break;
1527                 case 4:
1528                         decoder->private_->frame.header.sample_rate = 8000;
1529                         break;
1530                 case 5:
1531                         decoder->private_->frame.header.sample_rate = 16000;
1532                         break;
1533                 case 6:
1534                         decoder->private_->frame.header.sample_rate = 22050;
1535                         break;
1536                 case 7:
1537                         decoder->private_->frame.header.sample_rate = 24000;
1538                         break;
1539                 case 8:
1540                         decoder->private_->frame.header.sample_rate = 32000;
1541                         break;
1542                 case 9:
1543                         decoder->private_->frame.header.sample_rate = 44100;
1544                         break;
1545                 case 10:
1546                         decoder->private_->frame.header.sample_rate = 48000;
1547                         break;
1548                 case 11:
1549                         decoder->private_->frame.header.sample_rate = 96000;
1550                         break;
1551                 case 12:
1552                 case 13:
1553                 case 14:
1554                         sample_rate_hint = x;
1555                         break;
1556                 case 15:
1557                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1558                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1559                         return true;
1560                 default:
1561                         FLAC__ASSERT(0);
1562         }
1563
1564         x = (unsigned)(raw_header[3] >> 4);
1565         if(x & 8) {
1566                 decoder->private_->frame.header.channels = 2;
1567                 switch(x & 7) {
1568                         case 0:
1569                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1570                                 break;
1571                         case 1:
1572                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1573                                 break;
1574                         case 2:
1575                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1576                                 break;
1577                         default:
1578                                 is_unparseable = true;
1579                                 break;
1580                 }
1581         }
1582         else {
1583                 decoder->private_->frame.header.channels = (unsigned)x + 1;
1584                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1585         }
1586
1587         switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1588                 case 0:
1589                         if(decoder->private_->has_stream_info)
1590                                 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1591                         else
1592                                 is_unparseable = true;
1593                         break;
1594                 case 1:
1595                         decoder->private_->frame.header.bits_per_sample = 8;
1596                         break;
1597                 case 2:
1598                         decoder->private_->frame.header.bits_per_sample = 12;
1599                         break;
1600                 case 4:
1601                         decoder->private_->frame.header.bits_per_sample = 16;
1602                         break;
1603                 case 5:
1604                         decoder->private_->frame.header.bits_per_sample = 20;
1605                         break;
1606                 case 6:
1607                         decoder->private_->frame.header.bits_per_sample = 24;
1608                         break;
1609                 case 3:
1610                 case 7:
1611                         is_unparseable = true;
1612                         break;
1613                 default:
1614                         FLAC__ASSERT(0);
1615                         break;
1616         }
1617
1618         if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1619                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1620                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1621                 return true;
1622         }
1623
1624         if(blocksize_hint && is_known_variable_blocksize_stream) {
1625                 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1626                         return false; /* the read_callback_ sets the state for us */
1627                 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1628                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1629                         decoder->private_->cached = true;
1630                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1631                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1632                         return true;
1633                 }
1634                 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1635                 decoder->private_->frame.header.number.sample_number = xx;
1636         }
1637         else {
1638                 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1639                         return false; /* the read_callback_ sets the state for us */
1640                 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1641                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1642                         decoder->private_->cached = true;
1643                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1644                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1645                         return true;
1646                 }
1647                 decoder->private_->last_frame_number = x;
1648                 if(decoder->private_->has_stream_info) {
1649                         decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1650                         decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1651                 }
1652                 else {
1653                         is_unparseable = true;
1654                 }
1655         }
1656
1657         if(blocksize_hint) {
1658                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1659                         return false; /* the read_callback_ sets the state for us */
1660                 raw_header[raw_header_len++] = (FLAC__byte)x;
1661                 if(blocksize_hint == 7) {
1662                         FLAC__uint32 _x;
1663                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1664                                 return false; /* the read_callback_ sets the state for us */
1665                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1666                         x = (x << 8) | _x;
1667                 }
1668                 decoder->private_->frame.header.blocksize = x+1;
1669         }
1670
1671         if(sample_rate_hint) {
1672                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1673                         return false; /* the read_callback_ sets the state for us */
1674                 raw_header[raw_header_len++] = (FLAC__byte)x;
1675                 if(sample_rate_hint != 12) {
1676                         FLAC__uint32 _x;
1677                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1678                                 return false; /* the read_callback_ sets the state for us */
1679                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1680                         x = (x << 8) | _x;
1681                 }
1682                 if(sample_rate_hint == 12)
1683                         decoder->private_->frame.header.sample_rate = x*1000;
1684                 else if(sample_rate_hint == 13)
1685                         decoder->private_->frame.header.sample_rate = x;
1686                 else
1687                         decoder->private_->frame.header.sample_rate = x*10;
1688         }
1689
1690         /* read the CRC-8 byte */
1691         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1692                 return false; /* the read_callback_ sets the state for us */
1693         crc8 = (FLAC__byte)x;
1694
1695         if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1696                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1697                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1698                 return true;
1699         }
1700
1701         if(is_unparseable) {
1702                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1703                 return false;
1704         }
1705
1706         return true;
1707 }
1708
1709 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1710 {
1711         FLAC__uint32 x;
1712         FLAC__bool wasted_bits;
1713
1714         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1715                 return false; /* the read_callback_ sets the state for us */
1716
1717         wasted_bits = (x & 1);
1718         x &= 0xfe;
1719
1720         if(wasted_bits) {
1721                 unsigned u;
1722                 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1723                         return false; /* the read_callback_ sets the state for us */
1724                 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1725                 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1726         }
1727         else
1728                 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1729
1730         /*
1731          * Lots of magic numbers here
1732          */
1733         if(x & 0x80) {
1734                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1735                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1736                 return true;
1737         }
1738         else if(x == 0) {
1739                 if(!read_subframe_constant_(decoder, channel, bps))
1740                         return false;
1741         }
1742         else if(x == 2) {
1743                 if(!read_subframe_verbatim_(decoder, channel, bps))
1744                         return false;
1745         }
1746         else if(x < 16) {
1747                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1748                 return false;
1749         }
1750         else if(x <= 24) {
1751                 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1752                         return false;
1753         }
1754         else if(x < 64) {
1755                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1756                 return false;
1757         }
1758         else {
1759                 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1760                         return false;
1761         }
1762
1763         if(wasted_bits) {
1764                 unsigned i;
1765                 x = decoder->private_->frame.subframes[channel].wasted_bits;
1766                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1767                         decoder->private_->output[channel][i] <<= x;
1768         }
1769
1770         return true;
1771 }
1772
1773 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1774 {
1775         FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1776         FLAC__int32 x;
1777         unsigned i;
1778         FLAC__int32 *output = decoder->private_->output[channel];
1779
1780         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1781
1782         if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1783                 return false; /* the read_callback_ sets the state for us */
1784
1785         subframe->value = x;
1786
1787         /* decode the subframe */
1788         for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1789                 output[i] = x;
1790
1791         return true;
1792 }
1793
1794 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1795 {
1796         FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1797         FLAC__int32 i32;
1798         FLAC__uint32 u32;
1799         unsigned u;
1800
1801         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1802
1803         subframe->residual = decoder->private_->residual[channel];
1804         subframe->order = order;
1805
1806         /* read warm-up samples */
1807         for(u = 0; u < order; u++) {
1808                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1809                         return false; /* the read_callback_ sets the state for us */
1810                 subframe->warmup[u] = i32;
1811         }
1812
1813         /* read entropy coding method info */
1814         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1815                 return false; /* the read_callback_ sets the state for us */
1816         subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1817         switch(subframe->entropy_coding_method.type) {
1818                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1819                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1820                                 return false; /* the read_callback_ sets the state for us */
1821                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1822                         subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1823                         break;
1824                 default:
1825                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1826                         return false;
1827         }
1828
1829         /* read residual */
1830         switch(subframe->entropy_coding_method.type) {
1831                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1832                         if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
1833                                 return false;
1834                         break;
1835                 default:
1836                         FLAC__ASSERT(0);
1837         }
1838
1839         /* decode the subframe */
1840         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1841         FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1842
1843         return true;
1844 }
1845
1846 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1847 {
1848         FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1849         FLAC__int32 i32;
1850         FLAC__uint32 u32;
1851         unsigned u;
1852
1853         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1854
1855         subframe->residual = decoder->private_->residual[channel];
1856         subframe->order = order;
1857
1858         /* read warm-up samples */
1859         for(u = 0; u < order; u++) {
1860                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1861                         return false; /* the read_callback_ sets the state for us */
1862                 subframe->warmup[u] = i32;
1863         }
1864
1865         /* read qlp coeff precision */
1866         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1867                 return false; /* the read_callback_ sets the state for us */
1868         if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1869                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1870                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1871                 return true;
1872         }
1873         subframe->qlp_coeff_precision = u32+1;
1874
1875         /* read qlp shift */
1876         if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1877                 return false; /* the read_callback_ sets the state for us */
1878         subframe->quantization_level = i32;
1879
1880         /* read quantized lp coefficiencts */
1881         for(u = 0; u < order; u++) {
1882                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1883                         return false; /* the read_callback_ sets the state for us */
1884                 subframe->qlp_coeff[u] = i32;
1885         }
1886
1887         /* read entropy coding method info */
1888         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1889                 return false; /* the read_callback_ sets the state for us */
1890         subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1891         switch(subframe->entropy_coding_method.type) {
1892                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1893                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1894                                 return false; /* the read_callback_ sets the state for us */
1895                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1896                         subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1897                         break;
1898                 default:
1899                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1900                         return false;
1901         }
1902
1903         /* read residual */
1904         switch(subframe->entropy_coding_method.type) {
1905                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1906                         if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
1907                                 return false;
1908                         break;
1909                 default:
1910                         FLAC__ASSERT(0);
1911         }
1912
1913         /* decode the subframe */
1914         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1915         if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1916                 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1917                         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);
1918                 else
1919                         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);
1920         else
1921                 decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
1922
1923         return true;
1924 }
1925
1926 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1927 {
1928         FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1929         FLAC__int32 x, *residual = decoder->private_->residual[channel];
1930         unsigned i;
1931
1932         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1933
1934         subframe->data = residual;
1935
1936         for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1937                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1938                         return false; /* the read_callback_ sets the state for us */
1939                 residual[i] = x;
1940         }
1941
1942         /* decode the subframe */
1943         memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1944
1945         return true;
1946 }
1947
1948 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1949 {
1950         FLAC__uint32 rice_parameter;
1951         int i;
1952         unsigned partition, sample, u;
1953         const unsigned partitions = 1u << partition_order;
1954         const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1955
1956         if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1957                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1958                 return false;
1959         }
1960
1961         sample = 0;
1962         for(partition = 0; partition < partitions; partition++) {
1963                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1964                         return false; /* the read_callback_ sets the state for us */
1965                 partitioned_rice_contents->parameters[partition] = rice_parameter;
1966                 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1967 #ifdef FLAC__SYMMETRIC_RICE
1968                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1969                                 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1970                                         return false; /* the read_callback_ sets the state for us */
1971                                 residual[sample] = i;
1972                         }
1973 #else
1974                         u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1975                         if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1976                                 return false; /* the read_callback_ sets the state for us */
1977                         sample += u;
1978 #endif
1979                 }
1980                 else {
1981                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1982                                 return false; /* the read_callback_ sets the state for us */
1983                         partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1984                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1985                                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1986                                         return false; /* the read_callback_ sets the state for us */
1987                                 residual[sample] = i;
1988                         }
1989                 }
1990         }
1991
1992         return true;
1993 }
1994
1995 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1996 {
1997         if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1998                 FLAC__uint32 zero = 0;
1999                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
2000                         return false; /* the read_callback_ sets the state for us */
2001                 if(zero != 0) {
2002                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
2003                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2004                 }
2005         }
2006         return true;
2007 }
2008
2009 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
2010 {
2011         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2012         FLAC__StreamDecoderReadStatus status;
2013
2014         status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
2015         if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
2016                 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2017         else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
2018                 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2019         return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2020 }