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