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