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