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