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