#include config.h
[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_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1136                 return false; /* the read_callback_ sets the state for us */
1137
1138         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1139                 return false; /* the read_callback_ sets the state for us */
1140         obj->num_tracks = x;
1141
1142         if(obj->num_tracks > 0) {
1143                 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1144                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1145                         return false;
1146                 }
1147                 for(i = 0; i < obj->num_tracks; i++) {
1148                         FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1149                         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1150                                 return false; /* the read_callback_ sets the state for us */
1151
1152                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1153                                 return false; /* the read_callback_ sets the state for us */
1154                         track->number = (FLAC__byte)x;
1155
1156                         FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1157                         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))
1158                                 return false; /* the read_callback_ sets the state for us */
1159
1160                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1161                                 return false; /* the read_callback_ sets the state for us */
1162                         track->type = x;
1163
1164                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1165                                 return false; /* the read_callback_ sets the state for us */
1166                         track->pre_emphasis = x;
1167
1168                         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1169                                 return false; /* the read_callback_ sets the state for us */
1170
1171                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1172                                 return false; /* the read_callback_ sets the state for us */
1173                         track->num_indices = (FLAC__byte)x;
1174
1175                         if(track->num_indices > 0) {
1176                                 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1177                                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1178                                         return false;
1179                                 }
1180                                 for(j = 0; j < track->num_indices; j++) {
1181                                         FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1182                                         if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1183                                                 return false; /* the read_callback_ sets the state for us */
1184
1185                                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1186                                                 return false; /* the read_callback_ sets the state for us */
1187                                         index->number = (FLAC__byte)x;
1188
1189                                         if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1190                                                 return false; /* the read_callback_ sets the state for us */
1191                                 }
1192                         }
1193                 }
1194         }
1195
1196         return true;
1197 }
1198
1199 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1200 {
1201         FLAC__uint32 x;
1202         unsigned i, skip;
1203
1204         /* skip the version and flags bytes */
1205         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1206                 return false; /* the read_callback_ sets the state for us */
1207         /* get the size (in bytes) to skip */
1208         skip = 0;
1209         for(i = 0; i < 4; i++) {
1210                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1211                         return false; /* the read_callback_ sets the state for us */
1212                 skip <<= 7;
1213                 skip |= (x & 0x7f);
1214         }
1215         /* skip the rest of the tag */
1216         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1217                 return false; /* the read_callback_ sets the state for us */
1218         return true;
1219 }
1220
1221 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1222 {
1223         FLAC__uint32 x;
1224         FLAC__bool first = true;
1225
1226         /* If we know the total number of samples in the stream, stop if we've read that many. */
1227         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1228         if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1229                 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1230                         decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1231                         return true;
1232                 }
1233         }
1234
1235         /* make sure we're byte aligned */
1236         if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1237                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1238                         return false; /* the read_callback_ sets the state for us */
1239         }
1240
1241         while(1) {
1242                 if(decoder->private_->cached) {
1243                         x = (FLAC__uint32)decoder->private_->lookahead;
1244                         decoder->private_->cached = false;
1245                 }
1246                 else {
1247                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1248                                 return false; /* the read_callback_ sets the state for us */
1249                 }
1250                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1251                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
1252                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1253                                 return false; /* the read_callback_ sets the state for us */
1254
1255                         /* 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 */
1256                         /* else we have to check if the second byte is the end of a sync code */
1257                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1258                                 decoder->private_->lookahead = (FLAC__byte)x;
1259                                 decoder->private_->cached = true;
1260                         }
1261                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1262                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1263                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1264                                 return true;
1265                         }
1266                 }
1267                 if(first) {
1268                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1269                         first = false;
1270                 }
1271         }
1272
1273         return true;
1274 }
1275
1276 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
1277 {
1278         unsigned channel;
1279         unsigned i;
1280         FLAC__int32 mid, side, left, right;
1281         FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1282         FLAC__uint32 x;
1283
1284         *got_a_frame = false;
1285
1286         /* init the CRC */
1287         frame_crc = 0;
1288         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1289         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1290         FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
1291
1292         if(!read_frame_header_(decoder))
1293                 return false;
1294         if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
1295                 return true;
1296         if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1297                 return false;
1298         for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1299                 /*
1300                  * first figure the correct bits-per-sample of the subframe
1301                  */
1302                 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1303                 switch(decoder->private_->frame.header.channel_assignment) {
1304                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1305                                 /* no adjustment needed */
1306                                 break;
1307                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1308                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1309                                 if(channel == 1)
1310                                         bps++;
1311                                 break;
1312                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1313                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1314                                 if(channel == 0)
1315                                         bps++;
1316                                 break;
1317                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1318                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1319                                 if(channel == 1)
1320                                         bps++;
1321                                 break;
1322                         default:
1323                                 FLAC__ASSERT(0);
1324                 }
1325                 /*
1326                  * now read it
1327                  */
1328                 if(!read_subframe_(decoder, channel, bps))
1329                         return false;
1330                 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1331                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1332                         return true;
1333                 }
1334         }
1335         if(!read_zero_padding_(decoder))
1336                 return false;
1337
1338         /*
1339          * Read the frame CRC-16 from the footer and check
1340          */
1341         frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1342         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1343                 return false; /* the read_callback_ sets the state for us */
1344         if(frame_crc == (FLAC__uint16)x) {
1345                 /* Undo any special channel coding */
1346                 switch(decoder->private_->frame.header.channel_assignment) {
1347                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1348                                 /* do nothing */
1349                                 break;
1350                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1351                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1352                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1353                                         decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1354                                 break;
1355                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1356                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1357                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1358                                         decoder->private_->output[0][i] += decoder->private_->output[1][i];
1359                                 break;
1360                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1361                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1362                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1363                                         mid = decoder->private_->output[0][i];
1364                                         side = decoder->private_->output[1][i];
1365                                         mid <<= 1;
1366                                         if(side & 1) /* i.e. if 'side' is odd... */
1367                                                 mid++;
1368                                         left = mid + side;
1369                                         right = mid - side;
1370                                         decoder->private_->output[0][i] = left >> 1;
1371                                         decoder->private_->output[1][i] = right >> 1;
1372                                 }
1373                                 break;
1374                         default:
1375                                 FLAC__ASSERT(0);
1376                                 break;
1377                 }
1378         }
1379         else {
1380                 /* Bad frame, emit error and zero the output signal */
1381                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
1382                 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1383                         memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1384                 }
1385         }
1386
1387         *got_a_frame = true;
1388
1389         /* put the latest values into the public section of the decoder instance */
1390         decoder->protected_->channels = decoder->private_->frame.header.channels;
1391         decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1392         decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1393         decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1394         decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
1395
1396         FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1397         decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
1398
1399         /* write it */
1400         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)
1401                 return false;
1402
1403         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1404         return true;
1405 }
1406
1407 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
1408 {
1409         FLAC__uint32 x;
1410         FLAC__uint64 xx;
1411         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
1412         FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
1413         unsigned raw_header_len;
1414         FLAC__bool is_unparseable = false;
1415         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);
1416         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);
1417
1418         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1419
1420         /* init the raw header with the saved bits from synchronization */
1421         raw_header[0] = decoder->private_->header_warmup[0];
1422         raw_header[1] = decoder->private_->header_warmup[1];
1423         raw_header_len = 2;
1424
1425         /*
1426          * check to make sure that the reserved bits are 0
1427          */
1428         if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1429                 is_unparseable = true;
1430         }
1431
1432         /*
1433          * Note that along the way as we read the header, we look for a sync
1434          * code inside.  If we find one it would indicate that our original
1435          * sync was bad since there cannot be a sync code in a valid header.
1436          */
1437
1438         /*
1439          * read in the raw header as bytes so we can CRC it, and parse it on the way
1440          */
1441         for(i = 0; i < 2; i++) {
1442                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1443                         return false; /* the read_callback_ sets the state for us */
1444                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1445                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1446                         decoder->private_->lookahead = (FLAC__byte)x;
1447                         decoder->private_->cached = true;
1448                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1449                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1450                         return true;
1451                 }
1452                 raw_header[raw_header_len++] = (FLAC__byte)x;
1453         }
1454
1455         switch(x = raw_header[2] >> 4) {
1456                 case 0:
1457                         if(is_known_fixed_blocksize_stream)
1458                                 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1459                         else
1460                                 is_unparseable = true;
1461                         break;
1462                 case 1:
1463                         decoder->private_->frame.header.blocksize = 192;
1464                         break;
1465                 case 2:
1466                 case 3:
1467                 case 4:
1468                 case 5:
1469                         decoder->private_->frame.header.blocksize = 576 << (x-2);
1470                         break;
1471                 case 6:
1472                 case 7:
1473                         blocksize_hint = x;
1474                         break;
1475                 case 8:
1476                 case 9:
1477                 case 10:
1478                 case 11:
1479                 case 12:
1480                 case 13:
1481                 case 14:
1482                 case 15:
1483                         decoder->private_->frame.header.blocksize = 256 << (x-8);
1484                         break;
1485                 default:
1486                         FLAC__ASSERT(0);
1487                         break;
1488         }
1489
1490         switch(x = raw_header[2] & 0x0f) {
1491                 case 0:
1492                         if(decoder->private_->has_stream_info)
1493                                 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1494                         else
1495                                 is_unparseable = true;
1496                         break;
1497                 case 1:
1498                 case 2:
1499                 case 3:
1500                         is_unparseable = true;
1501                         break;
1502                 case 4:
1503                         decoder->private_->frame.header.sample_rate = 8000;
1504                         break;
1505                 case 5:
1506                         decoder->private_->frame.header.sample_rate = 16000;
1507                         break;
1508                 case 6:
1509                         decoder->private_->frame.header.sample_rate = 22050;
1510                         break;
1511                 case 7:
1512                         decoder->private_->frame.header.sample_rate = 24000;
1513                         break;
1514                 case 8:
1515                         decoder->private_->frame.header.sample_rate = 32000;
1516                         break;
1517                 case 9:
1518                         decoder->private_->frame.header.sample_rate = 44100;
1519                         break;
1520                 case 10:
1521                         decoder->private_->frame.header.sample_rate = 48000;
1522                         break;
1523                 case 11:
1524                         decoder->private_->frame.header.sample_rate = 96000;
1525                         break;
1526                 case 12:
1527                 case 13:
1528                 case 14:
1529                         sample_rate_hint = x;
1530                         break;
1531                 case 15:
1532                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1533                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1534                         return true;
1535                 default:
1536                         FLAC__ASSERT(0);
1537         }
1538
1539         x = (unsigned)(raw_header[3] >> 4);
1540         if(x & 8) {
1541                 decoder->private_->frame.header.channels = 2;
1542                 switch(x & 7) {
1543                         case 0:
1544                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1545                                 break;
1546                         case 1:
1547                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1548                                 break;
1549                         case 2:
1550                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1551                                 break;
1552                         default:
1553                                 is_unparseable = true;
1554                                 break;
1555                 }
1556         }
1557         else {
1558                 decoder->private_->frame.header.channels = (unsigned)x + 1;
1559                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1560         }
1561
1562         switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1563                 case 0:
1564                         if(decoder->private_->has_stream_info)
1565                                 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1566                         else
1567                                 is_unparseable = true;
1568                         break;
1569                 case 1:
1570                         decoder->private_->frame.header.bits_per_sample = 8;
1571                         break;
1572                 case 2:
1573                         decoder->private_->frame.header.bits_per_sample = 12;
1574                         break;
1575                 case 4:
1576                         decoder->private_->frame.header.bits_per_sample = 16;
1577                         break;
1578                 case 5:
1579                         decoder->private_->frame.header.bits_per_sample = 20;
1580                         break;
1581                 case 6:
1582                         decoder->private_->frame.header.bits_per_sample = 24;
1583                         break;
1584                 case 3:
1585                 case 7:
1586                         is_unparseable = true;
1587                         break;
1588                 default:
1589                         FLAC__ASSERT(0);
1590                         break;
1591         }
1592
1593         if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1594                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1595                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1596                 return true;
1597         }
1598
1599         if(blocksize_hint && is_known_variable_blocksize_stream) {
1600                 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1601                         return false; /* the read_callback_ sets the state for us */
1602                 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1603                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1604                         decoder->private_->cached = true;
1605                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1606                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1607                         return true;
1608                 }
1609                 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1610                 decoder->private_->frame.header.number.sample_number = xx;
1611         }
1612         else {
1613                 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1614                         return false; /* the read_callback_ sets the state for us */
1615                 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1616                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1617                         decoder->private_->cached = true;
1618                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1619                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1620                         return true;
1621                 }
1622                 decoder->private_->last_frame_number = x;
1623                 if(decoder->private_->has_stream_info) {
1624                         decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1625                         decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1626                 }
1627                 else {
1628                         is_unparseable = true;
1629                 }
1630         }
1631
1632         if(blocksize_hint) {
1633                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1634                         return false; /* the read_callback_ sets the state for us */
1635                 raw_header[raw_header_len++] = (FLAC__byte)x;
1636                 if(blocksize_hint == 7) {
1637                         FLAC__uint32 _x;
1638                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1639                                 return false; /* the read_callback_ sets the state for us */
1640                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1641                         x = (x << 8) | _x;
1642                 }
1643                 decoder->private_->frame.header.blocksize = x+1;
1644         }
1645
1646         if(sample_rate_hint) {
1647                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1648                         return false; /* the read_callback_ sets the state for us */
1649                 raw_header[raw_header_len++] = (FLAC__byte)x;
1650                 if(sample_rate_hint != 12) {
1651                         FLAC__uint32 _x;
1652                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1653                                 return false; /* the read_callback_ sets the state for us */
1654                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1655                         x = (x << 8) | _x;
1656                 }
1657                 if(sample_rate_hint == 12)
1658                         decoder->private_->frame.header.sample_rate = x*1000;
1659                 else if(sample_rate_hint == 13)
1660                         decoder->private_->frame.header.sample_rate = x;
1661                 else
1662                         decoder->private_->frame.header.sample_rate = x*10;
1663         }
1664
1665         /* read the CRC-8 byte */
1666         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1667                 return false; /* the read_callback_ sets the state for us */
1668         crc8 = (FLAC__byte)x;
1669
1670         if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1671                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1672                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1673                 return true;
1674         }
1675
1676         if(is_unparseable) {
1677                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1678                 return false;
1679         }
1680
1681         return true;
1682 }
1683
1684 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1685 {
1686         FLAC__uint32 x;
1687         FLAC__bool wasted_bits;
1688
1689         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1690                 return false; /* the read_callback_ sets the state for us */
1691
1692         wasted_bits = (x & 1);
1693         x &= 0xfe;
1694
1695         if(wasted_bits) {
1696                 unsigned u;
1697                 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1698                         return false; /* the read_callback_ sets the state for us */
1699                 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1700                 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1701         }
1702         else
1703                 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1704
1705         /*
1706          * Lots of magic numbers here
1707          */
1708         if(x & 0x80) {
1709                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1710                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1711                 return true;
1712         }
1713         else if(x == 0) {
1714                 if(!read_subframe_constant_(decoder, channel, bps))
1715                         return false;
1716         }
1717         else if(x == 2) {
1718                 if(!read_subframe_verbatim_(decoder, channel, bps))
1719                         return false;
1720         }
1721         else if(x < 16) {
1722                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1723                 return false;
1724         }
1725         else if(x <= 24) {
1726                 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1727                         return false;
1728         }
1729         else if(x < 64) {
1730                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1731                 return false;
1732         }
1733         else {
1734                 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1735                         return false;
1736         }
1737
1738         if(wasted_bits) {
1739                 unsigned i;
1740                 x = decoder->private_->frame.subframes[channel].wasted_bits;
1741                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1742                         decoder->private_->output[channel][i] <<= x;
1743         }
1744
1745         return true;
1746 }
1747
1748 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1749 {
1750         FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1751         FLAC__int32 x;
1752         unsigned i;
1753         FLAC__int32 *output = decoder->private_->output[channel];
1754
1755         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1756
1757         if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1758                 return false; /* the read_callback_ sets the state for us */
1759
1760         subframe->value = x;
1761
1762         /* decode the subframe */
1763         for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1764                 output[i] = x;
1765
1766         return true;
1767 }
1768
1769 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1770 {
1771         FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1772         FLAC__int32 i32;
1773         FLAC__uint32 u32;
1774         unsigned u;
1775
1776         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1777
1778         subframe->residual = decoder->private_->residual[channel];
1779         subframe->order = order;
1780
1781         /* read warm-up samples */
1782         for(u = 0; u < order; u++) {
1783                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1784                         return false; /* the read_callback_ sets the state for us */
1785                 subframe->warmup[u] = i32;
1786         }
1787
1788         /* read entropy coding method info */
1789         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1790                 return false; /* the read_callback_ sets the state for us */
1791         subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1792         switch(subframe->entropy_coding_method.type) {
1793                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1794                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1795                                 return false; /* the read_callback_ sets the state for us */
1796                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1797                         subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1798                         break;
1799                 default:
1800                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1801                         return false;
1802         }
1803
1804         /* read residual */
1805         switch(subframe->entropy_coding_method.type) {
1806                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1807                         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]))
1808                                 return false;
1809                         break;
1810                 default:
1811                         FLAC__ASSERT(0);
1812         }
1813
1814         /* decode the subframe */
1815         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1816         FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1817
1818         return true;
1819 }
1820
1821 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1822 {
1823         FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1824         FLAC__int32 i32;
1825         FLAC__uint32 u32;
1826         unsigned u;
1827
1828         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1829
1830         subframe->residual = decoder->private_->residual[channel];
1831         subframe->order = order;
1832
1833         /* read warm-up samples */
1834         for(u = 0; u < order; u++) {
1835                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1836                         return false; /* the read_callback_ sets the state for us */
1837                 subframe->warmup[u] = i32;
1838         }
1839
1840         /* read qlp coeff precision */
1841         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1842                 return false; /* the read_callback_ sets the state for us */
1843         if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1844                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1845                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1846                 return true;
1847         }
1848         subframe->qlp_coeff_precision = u32+1;
1849
1850         /* read qlp shift */
1851         if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1852                 return false; /* the read_callback_ sets the state for us */
1853         subframe->quantization_level = i32;
1854
1855         /* read quantized lp coefficiencts */
1856         for(u = 0; u < order; u++) {
1857                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1858                         return false; /* the read_callback_ sets the state for us */
1859                 subframe->qlp_coeff[u] = i32;
1860         }
1861
1862         /* read entropy coding method info */
1863         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1864                 return false; /* the read_callback_ sets the state for us */
1865         subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1866         switch(subframe->entropy_coding_method.type) {
1867                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1868                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1869                                 return false; /* the read_callback_ sets the state for us */
1870                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1871                         subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1872                         break;
1873                 default:
1874                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1875                         return false;
1876         }
1877
1878         /* read residual */
1879         switch(subframe->entropy_coding_method.type) {
1880                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1881                         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]))
1882                                 return false;
1883                         break;
1884                 default:
1885                         FLAC__ASSERT(0);
1886         }
1887
1888         /* decode the subframe */
1889         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1890         if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
1891                 if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1892                         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);
1893                 else
1894                         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);
1895         else
1896                 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);
1897
1898         return true;
1899 }
1900
1901 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1902 {
1903         FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1904         FLAC__int32 x, *residual = decoder->private_->residual[channel];
1905         unsigned i;
1906
1907         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1908
1909         subframe->data = residual;
1910
1911         for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1912                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1913                         return false; /* the read_callback_ sets the state for us */
1914                 residual[i] = x;
1915         }
1916
1917         /* decode the subframe */
1918         memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1919
1920         return true;
1921 }
1922
1923 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
1924 {
1925         FLAC__uint32 rice_parameter;
1926         int i;
1927         unsigned partition, sample, u;
1928         const unsigned partitions = 1u << partition_order;
1929         const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1930
1931         if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
1932                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1933                 return false;
1934         }
1935
1936         sample = 0;
1937         for(partition = 0; partition < partitions; partition++) {
1938                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1939                         return false; /* the read_callback_ sets the state for us */
1940                 partitioned_rice_contents->parameters[partition] = rice_parameter;
1941                 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1942 #ifdef FLAC__SYMMETRIC_RICE
1943                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1944                                 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1945                                         return false; /* the read_callback_ sets the state for us */
1946                                 residual[sample] = i;
1947                         }
1948 #else
1949                         u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
1950                         if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
1951                                 return false; /* the read_callback_ sets the state for us */
1952                         sample += u;
1953 #endif
1954                 }
1955                 else {
1956                         if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1957                                 return false; /* the read_callback_ sets the state for us */
1958                         partitioned_rice_contents->raw_bits[partition] = rice_parameter;
1959                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1960                                 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1961                                         return false; /* the read_callback_ sets the state for us */
1962                                 residual[sample] = i;
1963                         }
1964                 }
1965         }
1966
1967         return true;
1968 }
1969
1970 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
1971 {
1972         if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1973                 FLAC__uint32 zero = 0;
1974                 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1975                         return false; /* the read_callback_ sets the state for us */
1976                 if(zero != 0) {
1977                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
1978                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1979                 }
1980         }
1981         return true;
1982 }
1983
1984 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1985 {
1986         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1987         FLAC__StreamDecoderReadStatus status;
1988
1989         status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1990         if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
1991                 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1992         else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
1993                 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1994         return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1995 }