add "is_cd" flag to CUESHEET everywhere
[platform/upstream/flac.git] / src / libFLAC / stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memset/memcpy() */
23 #include "FLAC/assert.h"
24 #include "protected/stream_decoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/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[FLAC__METADATA_TYPE_UNDEFINED];
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 < FLAC__METADATA_TYPE_UNDEFINED);
394         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
395                 return false;
396         decoder->private_->metadata_filter[type] = true;
397         if(type == FLAC__METADATA_TYPE_APPLICATION)
398                 decoder->private_->metadata_filter_ids_count = 0;
399         return true;
400 }
401
402 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
403 {
404         FLAC__ASSERT(0 != decoder);
405         FLAC__ASSERT(0 != decoder->private_);
406         FLAC__ASSERT(0 != decoder->protected_);
407         FLAC__ASSERT(0 != id);
408         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
409                 return false;
410
411         if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
412                 return true;
413
414         FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
415
416         if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
417                 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
418                         return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
419                 decoder->private_->metadata_filter_ids_capacity *= 2;
420         }
421
422         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));
423         decoder->private_->metadata_filter_ids_count++;
424
425         return true;
426 }
427
428 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
429 {
430         unsigned i;
431         FLAC__ASSERT(0 != decoder);
432         FLAC__ASSERT(0 != decoder->private_);
433         FLAC__ASSERT(0 != decoder->protected_);
434         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
435                 return false;
436         for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
437                 decoder->private_->metadata_filter[i] = true;
438         decoder->private_->metadata_filter_ids_count = 0;
439         return true;
440 }
441
442 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
443 {
444         FLAC__ASSERT(0 != decoder);
445         FLAC__ASSERT(0 != decoder->private_);
446         FLAC__ASSERT(0 != decoder->protected_);
447         FLAC__ASSERT(type < FLAC__METADATA_TYPE_UNDEFINED);
448         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
449                 return false;
450         decoder->private_->metadata_filter[type] = false;
451         if(type == FLAC__METADATA_TYPE_APPLICATION)
452                 decoder->private_->metadata_filter_ids_count = 0;
453         return true;
454 }
455
456 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
457 {
458         FLAC__ASSERT(0 != decoder);
459         FLAC__ASSERT(0 != decoder->private_);
460         FLAC__ASSERT(0 != decoder->protected_);
461         FLAC__ASSERT(0 != id);
462         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
463                 return false;
464
465         if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
466                 return true;
467
468         FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
469
470         if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
471                 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
472                         return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
473                 decoder->private_->metadata_filter_ids_capacity *= 2;
474         }
475
476         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));
477         decoder->private_->metadata_filter_ids_count++;
478
479         return true;
480 }
481
482 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
483 {
484         FLAC__ASSERT(0 != decoder);
485         FLAC__ASSERT(0 != decoder->private_);
486         FLAC__ASSERT(0 != decoder->protected_);
487         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
488                 return false;
489         memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
490         decoder->private_->metadata_filter_ids_count = 0;
491         return true;
492 }
493
494 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
495 {
496         FLAC__ASSERT(0 != decoder);
497         FLAC__ASSERT(0 != decoder->protected_);
498         return decoder->protected_->state;
499 }
500
501 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
502 {
503         FLAC__ASSERT(0 != decoder);
504         FLAC__ASSERT(0 != decoder->protected_);
505         return decoder->protected_->channels;
506 }
507
508 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
509 {
510         FLAC__ASSERT(0 != decoder);
511         FLAC__ASSERT(0 != decoder->protected_);
512         return decoder->protected_->channel_assignment;
513 }
514
515 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
516 {
517         FLAC__ASSERT(0 != decoder);
518         FLAC__ASSERT(0 != decoder->protected_);
519         return decoder->protected_->bits_per_sample;
520 }
521
522 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
523 {
524         FLAC__ASSERT(0 != decoder);
525         FLAC__ASSERT(0 != decoder->protected_);
526         return decoder->protected_->sample_rate;
527 }
528
529 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
530 {
531         FLAC__ASSERT(0 != decoder);
532         FLAC__ASSERT(0 != decoder->protected_);
533         return decoder->protected_->blocksize;
534 }
535
536 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
537 {
538         FLAC__ASSERT(0 != decoder);
539         FLAC__ASSERT(0 != decoder->private_);
540         FLAC__ASSERT(0 != decoder->protected_);
541
542         if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
543                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
544                 return false;
545         }
546         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
547
548         return true;
549 }
550
551 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
552 {
553         FLAC__ASSERT(0 != decoder);
554         FLAC__ASSERT(0 != decoder->private_);
555         FLAC__ASSERT(0 != decoder->protected_);
556
557         if(!FLAC__stream_decoder_flush(decoder)) {
558                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
559                 return false;
560         }
561         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
562
563         decoder->private_->samples_decoded = 0;
564
565         return true;
566 }
567
568 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
569 {
570         FLAC__bool got_a_frame;
571         FLAC__ASSERT(0 != decoder);
572         FLAC__ASSERT(0 != decoder->protected_);
573
574         while(1) {
575                 switch(decoder->protected_->state) {
576                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
577                                 if(!find_metadata_(decoder))
578                                         return false; /* above function sets the status for us */
579                                 break;
580                         case FLAC__STREAM_DECODER_READ_METADATA:
581                                 if(!read_metadata_(decoder))
582                                         return false; /* above function sets the status for us */
583                                 else
584                                         return true;
585                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
586                                 if(!frame_sync_(decoder))
587                                         return true; /* above function sets the status for us */
588                                 break;
589                         case FLAC__STREAM_DECODER_READ_FRAME:
590                                 if(!read_frame_(decoder, &got_a_frame))
591                                         return false; /* above function sets the status for us */
592                                 if(got_a_frame)
593                                         return true; /* above function sets the status for us */
594                                 break;
595                         case FLAC__STREAM_DECODER_END_OF_STREAM:
596                         case FLAC__STREAM_DECODER_ABORTED:
597                                 return true;
598                         default:
599                                 FLAC__ASSERT(0);
600                                 return false;
601                 }
602         }
603 }
604
605 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
606 {
607         FLAC__ASSERT(0 != decoder);
608         FLAC__ASSERT(0 != decoder->protected_);
609
610         while(1) {
611                 switch(decoder->protected_->state) {
612                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
613                                 if(!find_metadata_(decoder))
614                                         return false; /* above function sets the status for us */
615                                 break;
616                         case FLAC__STREAM_DECODER_READ_METADATA:
617                                 if(!read_metadata_(decoder))
618                                         return false; /* above function sets the status for us */
619                                 break;
620                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
621                         case FLAC__STREAM_DECODER_READ_FRAME:
622                         case FLAC__STREAM_DECODER_END_OF_STREAM:
623                         case FLAC__STREAM_DECODER_ABORTED:
624                                 return true;
625                         default:
626                                 FLAC__ASSERT(0);
627                                 return false;
628                 }
629         }
630 }
631
632 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
633 {
634         FLAC__bool dummy;
635         FLAC__ASSERT(0 != decoder);
636         FLAC__ASSERT(0 != decoder->protected_);
637
638         while(1) {
639                 switch(decoder->protected_->state) {
640                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
641                                 if(!find_metadata_(decoder))
642                                         return false; /* above function sets the status for us */
643                                 break;
644                         case FLAC__STREAM_DECODER_READ_METADATA:
645                                 if(!read_metadata_(decoder))
646                                         return false; /* above function sets the status for us */
647                                 break;
648                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
649                                 if(!frame_sync_(decoder))
650                                         return true; /* above function sets the status for us */
651                                 break;
652                         case FLAC__STREAM_DECODER_READ_FRAME:
653                                 if(!read_frame_(decoder, &dummy))
654                                         return false; /* above function sets the status for us */
655                                 break;
656                         case FLAC__STREAM_DECODER_END_OF_STREAM:
657                         case FLAC__STREAM_DECODER_ABORTED:
658                                 return true;
659                         default:
660                                 FLAC__ASSERT(0);
661                                 return false;
662                 }
663         }
664 }
665
666 /***********************************************************************
667  *
668  * Protected class methods
669  *
670  ***********************************************************************/
671
672 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
673 {
674         FLAC__ASSERT(0 != decoder);
675         return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
676 }
677
678 /***********************************************************************
679  *
680  * Private class methods
681  *
682  ***********************************************************************/
683
684 void set_defaults_(FLAC__StreamDecoder *decoder)
685 {
686         decoder->private_->read_callback = 0;
687         decoder->private_->write_callback = 0;
688         decoder->private_->metadata_callback = 0;
689         decoder->private_->error_callback = 0;
690         decoder->private_->client_data = 0;
691
692         memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
693         decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
694         decoder->private_->metadata_filter_ids_count = 0;
695 }
696
697 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
698 {
699         unsigned i;
700         FLAC__int32 *tmp;
701
702         if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
703                 return true;
704
705         /* simply using realloc() is not practical because the number of channels may change mid-stream */
706
707         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
708                 if(0 != decoder->private_->output[i]) {
709                         free(decoder->private_->output[i]-4);
710                         decoder->private_->output[i] = 0;
711                 }
712                 if(0 != decoder->private_->residual[i]) {
713                         free(decoder->private_->residual[i]);
714                         decoder->private_->residual[i] = 0;
715                 }
716         }
717
718         for(i = 0; i < channels; i++) {
719                 /* WATCHOUT:
720                  * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
721                  * output arrays have a buffer of up to 3 zeroes in front
722                  * (at negative indices) for alignment purposes; we use 4
723                  * to keep the data well-aligned.
724                  */
725                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
726                 if(tmp == 0) {
727                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
728                         return false;
729                 }
730                 memset(tmp, 0, sizeof(FLAC__int32)*4);
731                 decoder->private_->output[i] = tmp + 4;
732
733                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
734                 if(tmp == 0) {
735                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
736                         return false;
737                 }
738                 decoder->private_->residual[i] = tmp;
739         }
740
741         decoder->private_->output_capacity = size;
742         decoder->private_->output_channels = channels;
743
744         return true;
745 }
746
747 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
748 {
749         unsigned i;
750
751         FLAC__ASSERT(0 != decoder);
752         FLAC__ASSERT(0 != decoder->private_);
753
754         for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
755                 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
756                         return true;
757
758         return false;
759 }
760
761 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
762 {
763         FLAC__uint32 x;
764         unsigned i, id;
765         FLAC__bool first = true;
766
767         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
768
769         for(i = id = 0; i < 4; ) {
770                 if(decoder->private_->cached) {
771                         x = (FLAC__uint32)decoder->private_->lookahead;
772                         decoder->private_->cached = false;
773                 }
774                 else {
775                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
776                                 return false; /* the read_callback_ sets the state for us */
777                 }
778                 if(x == FLAC__STREAM_SYNC_STRING[i]) {
779                         first = true;
780                         i++;
781                         id = 0;
782                         continue;
783                 }
784                 if(x == ID3V2_TAG_[id]) {
785                         id++;
786                         i = 0;
787                         if(id == 3) {
788                                 if(!skip_id3v2_tag_(decoder))
789                                         return false; /* the read_callback_ sets the state for us */
790                         }
791                         continue;
792                 }
793                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
794                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
795                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
796                                 return false; /* the read_callback_ sets the state for us */
797
798                         /* 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 */
799                         /* else we have to check if the second byte is the end of a sync code */
800                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
801                                 decoder->private_->lookahead = (FLAC__byte)x;
802                                 decoder->private_->cached = true;
803                         }
804                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
805                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
806                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
807                                 return true;
808                         }
809                 }
810                 i = 0;
811                 if(first) {
812                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
813                         first = false;
814                 }
815         }
816
817         decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
818         return true;
819 }
820
821 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
822 {
823         FLAC__bool is_last;
824         FLAC__uint32 i, x, type, length;
825
826         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
827
828         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
829                 return false; /* the read_callback_ sets the state for us */
830         is_last = x? true : false;
831
832         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
833                 return false; /* the read_callback_ sets the state for us */
834
835         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
836                 return false; /* the read_callback_ sets the state for us */
837
838         if(type == FLAC__METADATA_TYPE_STREAMINFO) {
839                 if(!read_metadata_streaminfo_(decoder, is_last, length))
840                         return false;
841
842                 decoder->private_->has_stream_info = true;
843                 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
844                         decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
845         }
846         else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
847                 if(!read_metadata_seektable_(decoder, is_last, length))
848                         return false;
849
850                 decoder->private_->has_seek_table = true;
851                 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
852                         decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
853         }
854         else {
855                 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
856                 unsigned real_length = length;
857                 FLAC__StreamMetadata block;
858
859                 block.is_last = is_last;
860                 block.type = (FLAC__MetadataType)type;
861                 block.length = length;
862
863                 if(type == FLAC__METADATA_TYPE_APPLICATION) {
864                         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))
865                                 return false; /* the read_callback_ sets the state for us */
866
867                         real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
868
869                         if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
870                                 skip_it = !skip_it;
871                 }
872
873                 if(skip_it) {
874                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
875                                 return false; /* the read_callback_ sets the state for us */
876                 }
877                 else {
878                         switch(type) {
879                                 case FLAC__METADATA_TYPE_PADDING:
880                                         /* skip the padding bytes */
881                                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
882                                                 return false; /* the read_callback_ sets the state for us */
883                                         break;
884                                 case FLAC__METADATA_TYPE_APPLICATION:
885                                         /* remember, we read the ID already */
886                                         if(real_length > 0) {
887                                                 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
888                                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
889                                                         return false;
890                                                 }
891                                                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
892                                                         return false; /* the read_callback_ sets the state for us */
893                                         }
894                                         else
895                                                 block.data.application.data = 0;
896                                         break;
897                                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
898                                         if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
899                                                 return false;
900                                         break;
901                                 case FLAC__METADATA_TYPE_CUESHEET:
902                                         if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
903                                                 return false;
904                                         break;
905                                 case FLAC__METADATA_TYPE_STREAMINFO:
906                                 case FLAC__METADATA_TYPE_SEEKTABLE:
907                                 default:
908                                         FLAC__ASSERT(0);
909                         }
910                         decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
911
912                         /* now we have to free any malloc'ed data in the block */
913                         switch(type) {
914                                 case FLAC__METADATA_TYPE_PADDING:
915                                         break;
916                                 case FLAC__METADATA_TYPE_APPLICATION:
917                                         if(0 != block.data.application.data)
918                                                 free(block.data.application.data);
919                                         break;
920                                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
921                                         if(0 != block.data.vorbis_comment.vendor_string.entry)
922                                                 free(block.data.vorbis_comment.vendor_string.entry);
923                                         if(block.data.vorbis_comment.num_comments > 0)
924                                                 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
925                                                         if(0 != block.data.vorbis_comment.comments[i].entry)
926                                                                 free(block.data.vorbis_comment.comments[i].entry);
927                                         if(0 != block.data.vorbis_comment.comments)
928                                                 free(block.data.vorbis_comment.comments);
929                                         break;
930                                 case FLAC__METADATA_TYPE_CUESHEET:
931                                         if(block.data.cue_sheet.num_tracks > 0)
932                                                 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
933                                                         if(0 != block.data.cue_sheet.tracks[i].indices)
934                                                                 free(block.data.cue_sheet.tracks[i].indices);
935                                         if(0 != block.data.cue_sheet.tracks)
936                                                 free(block.data.cue_sheet.tracks);
937                                         break;
938                                 case FLAC__METADATA_TYPE_STREAMINFO:
939                                 case FLAC__METADATA_TYPE_SEEKTABLE:
940                                 default:
941                                         FLAC__ASSERT(0);
942                         }
943                 }
944         }
945
946         if(is_last)
947                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
948
949         return true;
950 }
951
952 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
953 {
954         FLAC__uint32 x;
955         unsigned bits, used_bits = 0;
956
957         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
958
959         decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
960         decoder->private_->stream_info.is_last = is_last;
961         decoder->private_->stream_info.length = length;
962
963         bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
964         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
965                 return false; /* the read_callback_ sets the state for us */
966         decoder->private_->stream_info.data.stream_info.min_blocksize = x;
967         used_bits += bits;
968
969         bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
970         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
971                 return false; /* the read_callback_ sets the state for us */
972         decoder->private_->stream_info.data.stream_info.max_blocksize = x;
973         used_bits += bits;
974
975         bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
976         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
977                 return false; /* the read_callback_ sets the state for us */
978         decoder->private_->stream_info.data.stream_info.min_framesize = x;
979         used_bits += bits;
980
981         bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
982         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
983                 return false; /* the read_callback_ sets the state for us */
984         decoder->private_->stream_info.data.stream_info.max_framesize = x;
985         used_bits += bits;
986
987         bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
988         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
989                 return false; /* the read_callback_ sets the state for us */
990         decoder->private_->stream_info.data.stream_info.sample_rate = x;
991         used_bits += bits;
992
993         bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
994         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
995                 return false; /* the read_callback_ sets the state for us */
996         decoder->private_->stream_info.data.stream_info.channels = x+1;
997         used_bits += bits;
998
999         bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1000         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1001                 return false; /* the read_callback_ sets the state for us */
1002         decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1003         used_bits += bits;
1004
1005         bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1006         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))
1007                 return false; /* the read_callback_ sets the state for us */
1008         used_bits += bits;
1009
1010         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1011                 return false; /* the read_callback_ sets the state for us */
1012         used_bits += 16*8;
1013
1014         /* skip the rest of the block */
1015         FLAC__ASSERT(used_bits % 8 == 0);
1016         length -= (used_bits / 8);
1017         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1018                 return false; /* the read_callback_ sets the state for us */
1019
1020         return true;
1021 }
1022
1023 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1024 {
1025         FLAC__uint32 i, x;
1026         FLAC__uint64 xx;
1027
1028         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1029
1030         decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1031         decoder->private_->seek_table.is_last = is_last;
1032         decoder->private_->seek_table.length = length;
1033
1034         decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1035
1036         /* use realloc since we may pass through here several times (e.g. after seeking) */
1037         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)))) {
1038                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1039                 return false;
1040         }
1041         for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1042                 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1043                         return false; /* the read_callback_ sets the state for us */
1044                 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1045
1046                 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1047                         return false; /* the read_callback_ sets the state for us */
1048                 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1049
1050                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1051                         return false; /* the read_callback_ sets the state for us */
1052                 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1053         }
1054         length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1055         /* if there is a partial point left, skip over it */
1056         if(length > 0) {
1057                 /*@@@ do an error_callback() here?  there's an argument for either way */
1058                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1059                         return false; /* the read_callback_ sets the state for us */
1060         }
1061
1062         return true;
1063 }
1064
1065 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1066 {
1067         FLAC__uint32 i;
1068
1069         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1070
1071         /* read vendor string */
1072         FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1073         if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1074                 return false; /* the read_callback_ sets the state for us */
1075         if(obj->vendor_string.length > 0) {
1076                 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length))) {
1077                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1078                         return false;
1079                 }
1080                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1081                         return false; /* the read_callback_ sets the state for us */
1082         }
1083         else
1084                 obj->vendor_string.entry = 0;
1085
1086         /* read num comments */
1087         FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1088         if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1089                 return false; /* the read_callback_ sets the state for us */
1090
1091         /* read comments */
1092         if(obj->num_comments > 0) {
1093                 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1094                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1095                         return false;
1096                 }
1097                 for(i = 0; i < obj->num_comments; i++) {
1098                         FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1099                         if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1100                                 return false; /* the read_callback_ sets the state for us */
1101                         if(obj->comments[i].length > 0) {
1102                                 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length))) {
1103                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1104                                         return false;
1105                                 }
1106                                 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1107                                         return false; /* the read_callback_ sets the state for us */
1108                         }
1109                         else
1110                                 obj->comments[i].entry = 0;
1111                 }
1112         }
1113         else {
1114                 obj->comments = 0;
1115         }
1116
1117         return true;
1118 }
1119
1120 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1121 {
1122         FLAC__uint32 i, j, x;
1123
1124         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1125
1126         memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1127
1128         FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1129         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))
1130                 return false; /* the read_callback_ sets the state for us */
1131
1132         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1133                 return false; /* the read_callback_ sets the state for us */
1134
1135         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1136                 return false; /* the read_callback_ sets the state for us */
1137         obj->is_cd = x? true : false;
1138
1139         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1140                 return false; /* the read_callback_ sets the state for us */
1141
1142         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1143                 return false; /* the read_callback_ sets the state for us */
1144         obj->num_tracks = x;
1145
1146         if(obj->num_tracks > 0) {
1147                 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1148                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1149                         return false;
1150                 }
1151                 for(i = 0; i < obj->num_tracks; i++) {
1152                         FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1153                         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_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_TRACK_NUMBER_LEN, read_callback_, decoder))
1157                                 return false; /* the read_callback_ sets the state for us */
1158                         track->number = (FLAC__byte)x;
1159
1160                         FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1161                         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))
1162                                 return false; /* the read_callback_ sets the state for us */
1163
1164                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1165                                 return false; /* the read_callback_ sets the state for us */
1166                         track->type = x;
1167
1168                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1169                                 return false; /* the read_callback_ sets the state for us */
1170                         track->pre_emphasis = x;
1171
1172                         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1173                                 return false; /* the read_callback_ sets the state for us */
1174
1175                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1176                                 return false; /* the read_callback_ sets the state for us */
1177                         track->num_indices = (FLAC__byte)x;
1178
1179                         if(track->num_indices > 0) {
1180                                 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1181                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1182                                         return false;
1183                                 }
1184                                 for(j = 0; j < track->num_indices; j++) {
1185                                         FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1186                                         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1187                                                 return false; /* the read_callback_ sets the state for us */
1188
1189                                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1190                                                 return false; /* the read_callback_ sets the state for us */
1191                                         index->number = (FLAC__byte)x;
1192
1193                                         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1194                                                 return false; /* the read_callback_ sets the state for us */
1195                                 }
1196                         }
1197                 }
1198         }
1199
1200         return true;
1201 }
1202
1203 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1204 {
1205         FLAC__uint32 x;
1206         unsigned i, skip;
1207
1208         /* skip the version and flags bytes */
1209         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1210                 return false; /* the read_callback_ sets the state for us */
1211         /* get the size (in bytes) to skip */
1212         skip = 0;
1213         for(i = 0; i < 4; i++) {
1214                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1215                         return false; /* the read_callback_ sets the state for us */
1216                 skip <<= 7;
1217                 skip |= (x & 0x7f);
1218         }
1219         /* skip the rest of the tag */
1220         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1221                 return false; /* the read_callback_ sets the state for us */
1222         return true;
1223 }
1224
1225 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1226 {
1227         FLAC__uint32 x;
1228         FLAC__bool first = true;
1229
1230         /* If we know the total number of samples in the stream, stop if we've read that many. */
1231         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1232         if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1233                 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1234                         decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1235                         return true;
1236                 }
1237         }
1238
1239         /* make sure we're byte aligned */
1240         if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1241                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1242                         return false; /* the read_callback_ sets the state for us */
1243         }
1244
1245         while(1) {
1246                 if(decoder->private_->cached) {
1247                         x = (FLAC__uint32)decoder->private_->lookahead;
1248                         decoder->private_->cached = false;
1249                 }
1250                 else {
1251                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1252                                 return false; /* the read_callback_ sets the state for us */
1253                 }
1254                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1255                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
1256                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1257                                 return false; /* the read_callback_ sets the state for us */
1258
1259                         /* 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 */
1260                         /* else we have to check if the second byte is the end of a sync code */
1261                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1262                                 decoder->private_->lookahead = (FLAC__byte)x;
1263                                 decoder->private_->cached = true;
1264                         }
1265                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1266                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1267                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1268                                 return true;
1269                         }
1270                 }
1271                 if(first) {
1272                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1273                         first = false;
1274                 }
1275         }
1276
1277         return true;
1278 }
1279
1280 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1281 {
1282         unsigned channel;
1283         unsigned i;
1284         FLAC__int32 mid, side, left, right;
1285         FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1286         FLAC__uint32 x;
1287
1288         *got_a_frame = false;
1289
1290         /* init the CRC */
1291         frame_crc = 0;
1292         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1293         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1294         FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1295
1296         if(!read_frame_header_(decoder))
1297                 return false;
1298         if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1299                 return true;
1300         if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1301                 return false;
1302         for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1303                 /*
1304                  * first figure the correct bits-per-sample of the subframe
1305                  */
1306                 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1307                 switch(decoder->private_->frame.header.channel_assignment) {
1308                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1309                                 /* no adjustment needed */
1310                                 break;
1311                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1312                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1313                                 if(channel == 1)
1314                                         bps++;
1315                                 break;
1316                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1317                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1318                                 if(channel == 0)
1319                                         bps++;
1320                                 break;
1321                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1322                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1323                                 if(channel == 1)
1324                                         bps++;
1325                                 break;
1326                         default:
1327                                 FLAC__ASSERT(0);
1328                 }
1329                 /*
1330                  * now read it
1331                  */
1332                 if(!read_subframe_(decoder, channel, bps))
1333                         return false;
1334                 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1335                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1336                         return true;
1337                 }
1338         }
1339         if(!read_zero_padding_(decoder))
1340                 return false;
1341
1342         /*
1343          * Read the frame CRC-16 from the footer and check
1344          */
1345         frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1346         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1347                 return false; /* the read_callback_ sets the state for us */
1348         if(frame_crc == (FLAC__uint16)x) {
1349                 /* Undo any special channel coding */
1350                 switch(decoder->private_->frame.header.channel_assignment) {
1351                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1352                                 /* do nothing */
1353                                 break;
1354                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1355                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1356                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1357                                         decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1358                                 break;
1359                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1360                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1361                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1362                                         decoder->private_->output[0][i] += decoder->private_->output[1][i];
1363                                 break;
1364                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1365                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1366                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1367                                         mid = decoder->private_->output[0][i];
1368                                         side = decoder->private_->output[1][i];
1369                                         mid <<= 1;
1370                                         if(side & 1) /* i.e. if 'side' is odd... */
1371                                                 mid++;
1372                                         left = mid + side;
1373                                         right = mid - side;
1374                                         decoder->private_->output[0][i] = left >> 1;
1375                                         decoder->private_->output[1][i] = right >> 1;
1376                                 }
1377                                 break;
1378                         default:
1379                                 FLAC__ASSERT(0);
1380                                 break;
1381                 }
1382         }
1383         else {
1384                 /* Bad frame, emit error and zero the output signal */
1385                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1386                 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1387                         memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1388                 }
1389         }
1390
1391         *got_a_frame = true;
1392
1393         /* put the latest values into the public section of the decoder instance */
1394         decoder->protected_->channels = decoder->private_->frame.header.channels;
1395         decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1396         decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1397         decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1398         decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1399
1400         FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1401         decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1402
1403         /* write it */
1404         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)
1405                 return false;
1406
1407         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1408         return true;
1409 }
1410
1411 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1412 {
1413         FLAC__uint32 x;
1414         FLAC__uint64 xx;
1415         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1416         FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1417         unsigned raw_header_len;
1418         FLAC__bool is_unparseable = false;
1419         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);
1420         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);
1421
1422         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1423
1424         /* init the raw header with the saved bits from synchronization */
1425         raw_header[0] = decoder->private_->header_warmup[0];
1426         raw_header[1] = decoder->private_->header_warmup[1];
1427         raw_header_len = 2;
1428
1429         /*
1430          * check to make sure that the reserved bits are 0
1431          */
1432         if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1433                 is_unparseable = true;
1434         }
1435
1436         /*
1437          * Note that along the way as we read the header, we look for a sync
1438          * code inside.  If we find one it would indicate that our original
1439          * sync was bad since there cannot be a sync code in a valid header.
1440          */
1441
1442         /*
1443          * read in the raw header as bytes so we can CRC it, and parse it on the way
1444          */
1445         for(i = 0; i < 2; i++) {
1446                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1447                         return false; /* the read_callback_ sets the state for us */
1448                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1449                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1450                         decoder->private_->lookahead = (FLAC__byte)x;
1451                         decoder->private_->cached = true;
1452                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1453                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1454                         return true;
1455                 }
1456                 raw_header[raw_header_len++] = (FLAC__byte)x;
1457         }
1458
1459         switch(x = raw_header[2] >> 4) {
1460                 case 0:
1461                         if(is_known_fixed_blocksize_stream)
1462                                 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1463                         else
1464                                 is_unparseable = true;
1465                         break;
1466                 case 1:
1467                         decoder->private_->frame.header.blocksize = 192;
1468                         break;
1469                 case 2:
1470                 case 3:
1471                 case 4:
1472                 case 5:
1473                         decoder->private_->frame.header.blocksize = 576 << (x-2);
1474                         break;
1475                 case 6:
1476                 case 7:
1477                         blocksize_hint = x;
1478                         break;
1479                 case 8:
1480                 case 9:
1481                 case 10:
1482                 case 11:
1483                 case 12:
1484                 case 13:
1485                 case 14:
1486                 case 15:
1487                         decoder->private_->frame.header.blocksize = 256 << (x-8);
1488                         break;
1489                 default:
1490                         FLAC__ASSERT(0);
1491                         break;
1492         }
1493
1494         switch(x = raw_header[2] & 0x0f) {
1495                 case 0:
1496                         if(decoder->private_->has_stream_info)
1497                                 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1498                         else
1499                                 is_unparseable = true;
1500                         break;
1501                 case 1:
1502                 case 2:
1503                 case 3:
1504                         is_unparseable = true;
1505                         break;
1506                 case 4:
1507                         decoder->private_->frame.header.sample_rate = 8000;
1508                         break;
1509                 case 5:
1510                         decoder->private_->frame.header.sample_rate = 16000;
1511                         break;
1512                 case 6:
1513                         decoder->private_->frame.header.sample_rate = 22050;
1514                         break;
1515                 case 7:
1516                         decoder->private_->frame.header.sample_rate = 24000;
1517                         break;
1518                 case 8:
1519                         decoder->private_->frame.header.sample_rate = 32000;
1520                         break;
1521                 case 9:
1522                         decoder->private_->frame.header.sample_rate = 44100;
1523                         break;
1524                 case 10:
1525                         decoder->private_->frame.header.sample_rate = 48000;
1526                         break;
1527                 case 11:
1528                         decoder->private_->frame.header.sample_rate = 96000;
1529                         break;
1530                 case 12:
1531                 case 13:
1532                 case 14:
1533                         sample_rate_hint = x;
1534                         break;
1535                 case 15:
1536                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1537                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1538                         return true;
1539                 default:
1540                         FLAC__ASSERT(0);
1541         }
1542
1543         x = (unsigned)(raw_header[3] >> 4);
1544         if(x & 8) {
1545                 decoder->private_->frame.header.channels = 2;
1546                 switch(x & 7) {
1547                         case 0:
1548                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1549                                 break;
1550                         case 1:
1551                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1552                                 break;
1553                         case 2:
1554                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1555                                 break;
1556                         default:
1557                                 is_unparseable = true;
1558                                 break;
1559                 }
1560         }
1561         else {
1562                 decoder->private_->frame.header.channels = (unsigned)x + 1;
1563                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1564         }
1565
1566         switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1567                 case 0:
1568                         if(decoder->private_->has_stream_info)
1569                                 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1570                         else
1571                                 is_unparseable = true;
1572                         break;
1573                 case 1:
1574                         decoder->private_->frame.header.bits_per_sample = 8;
1575                         break;
1576                 case 2:
1577                         decoder->private_->frame.header.bits_per_sample = 12;
1578                         break;
1579                 case 4:
1580                         decoder->private_->frame.header.bits_per_sample = 16;
1581                         break;
1582                 case 5:
1583                         decoder->private_->frame.header.bits_per_sample = 20;
1584                         break;
1585                 case 6:
1586                         decoder->private_->frame.header.bits_per_sample = 24;
1587                         break;
1588                 case 3:
1589                 case 7:
1590                         is_unparseable = true;
1591                         break;
1592                 default:
1593                         FLAC__ASSERT(0);
1594                         break;
1595         }
1596
1597         if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1598                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1599                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1600                 return true;
1601         }
1602
1603         if(blocksize_hint && is_known_variable_blocksize_stream) {
1604                 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1605                         return false; /* the read_callback_ sets the state for us */
1606                 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1607                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1608                         decoder->private_->cached = true;
1609                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1610                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1611                         return true;
1612                 }
1613                 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1614                 decoder->private_->frame.header.number.sample_number = xx;
1615         }
1616         else {
1617                 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1618                         return false; /* the read_callback_ sets the state for us */
1619                 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1620                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1621                         decoder->private_->cached = true;
1622                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1623                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1624                         return true;
1625                 }
1626                 decoder->private_->last_frame_number = x;
1627                 if(decoder->private_->has_stream_info) {
1628                         decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1629                         decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1630                 }
1631                 else {
1632                         is_unparseable = true;
1633                 }
1634         }
1635
1636         if(blocksize_hint) {
1637                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1638                         return false; /* the read_callback_ sets the state for us */
1639                 raw_header[raw_header_len++] = (FLAC__byte)x;
1640                 if(blocksize_hint == 7) {
1641                         FLAC__uint32 _x;
1642                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1643                                 return false; /* the read_callback_ sets the state for us */
1644                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1645                         x = (x << 8) | _x;
1646                 }
1647                 decoder->private_->frame.header.blocksize = x+1;
1648         }
1649
1650         if(sample_rate_hint) {
1651                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1652                         return false; /* the read_callback_ sets the state for us */
1653                 raw_header[raw_header_len++] = (FLAC__byte)x;
1654                 if(sample_rate_hint != 12) {
1655                         FLAC__uint32 _x;
1656                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1657                                 return false; /* the read_callback_ sets the state for us */
1658                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1659                         x = (x << 8) | _x;
1660                 }
1661                 if(sample_rate_hint == 12)
1662                         decoder->private_->frame.header.sample_rate = x*1000;
1663                 else if(sample_rate_hint == 13)
1664                         decoder->private_->frame.header.sample_rate = x;
1665                 else
1666                         decoder->private_->frame.header.sample_rate = x*10;
1667         }
1668
1669         /* read the CRC-8 byte */
1670         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1671                 return false; /* the read_callback_ sets the state for us */
1672         crc8 = (FLAC__byte)x;
1673
1674         if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1675                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1676                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1677                 return true;
1678         }
1679
1680         if(is_unparseable) {
1681                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1682                 return false;
1683         }
1684
1685         return true;
1686 }
1687
1688 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1689 {
1690         FLAC__uint32 x;
1691         FLAC__bool wasted_bits;
1692
1693         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1694                 return false; /* the read_callback_ sets the state for us */
1695
1696         wasted_bits = (x & 1);
1697         x &= 0xfe;
1698
1699         if(wasted_bits) {
1700                 unsigned u;
1701                 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1702                         return false; /* the read_callback_ sets the state for us */
1703                 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1704                 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1705         }
1706         else
1707                 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1708
1709         /*
1710          * Lots of magic numbers here
1711          */
1712         if(x & 0x80) {
1713                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1714                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1715                 return true;
1716         }
1717         else if(x == 0) {
1718                 if(!read_subframe_constant_(decoder, channel, bps))
1719                         return false;
1720         }
1721         else if(x == 2) {
1722                 if(!read_subframe_verbatim_(decoder, channel, bps))
1723                         return false;
1724         }
1725         else if(x < 16) {
1726                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1727                 return false;
1728         }
1729         else if(x <= 24) {
1730                 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1731                         return false;
1732         }
1733         else if(x < 64) {
1734                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1735                 return false;
1736         }
1737         else {
1738                 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1739                         return false;
1740         }
1741
1742         if(wasted_bits) {
1743                 unsigned i;
1744                 x = decoder->private_->frame.subframes[channel].wasted_bits;
1745                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1746                         decoder->private_->output[channel][i] <<= x;
1747         }
1748
1749         return true;
1750 }
1751
1752 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1753 {
1754         FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1755         FLAC__int32 x;
1756         unsigned i;
1757         FLAC__int32 *output = decoder->private_->output[channel];
1758
1759         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1760
1761         if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1762                 return false; /* the read_callback_ sets the state for us */
1763
1764         subframe->value = x;
1765
1766         /* decode the subframe */
1767         for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1768                 output[i] = x;
1769
1770         return true;
1771 }
1772
1773 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1774 {
1775         FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1776         FLAC__int32 i32;
1777         FLAC__uint32 u32;
1778         unsigned u;
1779
1780         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1781
1782         subframe->residual = decoder->private_->residual[channel];
1783         subframe->order = order;
1784
1785         /* read warm-up samples */
1786         for(u = 0; u < order; u++) {
1787                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1788                         return false; /* the read_callback_ sets the state for us */
1789                 subframe->warmup[u] = i32;
1790         }
1791
1792         /* read entropy coding method info */
1793         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1794                 return false; /* the read_callback_ sets the state for us */
1795         subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1796         switch(subframe->entropy_coding_method.type) {
1797                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1798                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1799                                 return false; /* the read_callback_ sets the state for us */
1800                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1801                         subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1802                         break;
1803                 default:
1804                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1805                         return false;
1806         }
1807
1808         /* read residual */
1809         switch(subframe->entropy_coding_method.type) {
1810                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1811                         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]))
1812                                 return false;
1813                         break;
1814                 default:
1815                         FLAC__ASSERT(0);
1816         }
1817
1818         /* decode the subframe */
1819         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1820         FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1821
1822         return true;
1823 }
1824
1825 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1826 {
1827         FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1828         FLAC__int32 i32;
1829         FLAC__uint32 u32;
1830         unsigned u;
1831
1832         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1833
1834         subframe->residual = decoder->private_->residual[channel];
1835         subframe->order = order;
1836
1837         /* read warm-up samples */
1838         for(u = 0; u < order; u++) {
1839                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1840                         return false; /* the read_callback_ sets the state for us */
1841                 subframe->warmup[u] = i32;
1842         }
1843
1844         /* read qlp coeff precision */
1845         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1846                 return false; /* the read_callback_ sets the state for us */
1847         if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1848                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1849                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1850                 return true;
1851         }
1852         subframe->qlp_coeff_precision = u32+1;
1853
1854         /* read qlp shift */
1855         if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1856                 return false; /* the read_callback_ sets the state for us */
1857         subframe->quantization_level = i32;
1858
1859         /* read quantized lp coefficiencts */
1860         for(u = 0; u < order; u++) {
1861                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1862                         return false; /* the read_callback_ sets the state for us */
1863                 subframe->qlp_coeff[u] = i32;
1864         }
1865
1866         /* read entropy coding method info */
1867         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1868                 return false; /* the read_callback_ sets the state for us */
1869         subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1870         switch(subframe->entropy_coding_method.type) {
1871                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1872                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1873                                 return false; /* the read_callback_ sets the state for us */
1874                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1875                         subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1876                         break;
1877                 default:
1878                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1879                         return false;
1880         }
1881
1882         /* read residual */
1883         switch(subframe->entropy_coding_method.type) {
1884                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1885                         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]))
1886                                 return false;
1887                         break;
1888                 default:
1889                         FLAC__ASSERT(0);
1890         }
1891
1892         /* decode the subframe */
1893         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1894         if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1895                 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1896                         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);
1897                 else
1898                         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);
1899         else
1900                 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);
1901
1902         return true;
1903 }
1904
1905 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1906 {
1907         FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1908         FLAC__int32 x, *residual = decoder->private_->residual[channel];
1909         unsigned i;
1910
1911         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1912
1913         subframe->data = residual;
1914
1915         for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1916                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1917                         return false; /* the read_callback_ sets the state for us */
1918                 residual[i] = x;
1919         }
1920
1921         /* decode the subframe */
1922         memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1923
1924         return true;
1925 }
1926
1927 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1928 {
1929         FLAC__uint32 rice_parameter;
1930         int i;
1931         unsigned partition, sample, u;
1932         const unsigned partitions = 1u << partition_order;
1933         const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1934
1935         if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1936                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1937                 return false;
1938         }
1939
1940         sample = 0;
1941         for(partition = 0; partition < partitions; partition++) {
1942                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1943                         return false; /* the read_callback_ sets the state for us */
1944                 partitioned_rice_contents->parameters[partition] = rice_parameter;
1945                 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1946 #ifdef FLAC__SYMMETRIC_RICE
1947                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1948                                 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1949                                         return false; /* the read_callback_ sets the state for us */
1950                                 residual[sample] = i;
1951                         }
1952 #else
1953                         u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1954                         if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1955                                 return false; /* the read_callback_ sets the state for us */
1956                         sample += u;
1957 #endif
1958                 }
1959                 else {
1960                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1961                                 return false; /* the read_callback_ sets the state for us */
1962                         partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1963                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1964                                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1965                                         return false; /* the read_callback_ sets the state for us */
1966                                 residual[sample] = i;
1967                         }
1968                 }
1969         }
1970
1971         return true;
1972 }
1973
1974 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1975 {
1976         if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1977                 FLAC__uint32 zero = 0;
1978                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1979                         return false; /* the read_callback_ sets the state for us */
1980                 if(zero != 0) {
1981                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1982                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1983                 }
1984         }
1985         return true;
1986 }
1987
1988 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1989 {
1990         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1991         FLAC__StreamDecoderReadStatus status;
1992
1993         status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1994         if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1995                 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1996         else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1997                 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1998         return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1999 }