comparison src/flac/libflac/stream_decoder.c @ 722:454ad11020ec trunk

[svn] * Delete flac112 * Rename flac113 -> flac * Change configure.ac
author js
date Sat, 24 Feb 2007 16:17:26 -0800
parents src/flac113/libflac/stream_decoder.c@574de61036a3
children 31ad9abf9606
comparison
equal deleted inserted replaced
721:574de61036a3 722:454ad11020ec
1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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 #define USE_VFS 1
33 #include <audacious/vfs.h>
34
35 #if HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #if defined _MSC_VER || defined __MINGW32__
40 #include <io.h> /* for _setmode() */
41 #include <fcntl.h> /* for _O_BINARY */
42 #endif
43 #if defined __CYGWIN__ || defined __EMX__
44 #include <io.h> /* for setmode(), O_BINARY */
45 #include <fcntl.h> /* for _O_BINARY */
46 #endif
47 #include <stdio.h>
48 #include <stdlib.h> /* for malloc() */
49 #include <string.h> /* for memset/memcpy() */
50 #include <sys/stat.h> /* for stat() */
51 #include <sys/types.h> /* for off_t */
52 #if defined USE_VFS || defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
53 #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
54 #define fseeko fseek
55 #define ftello ftell
56 #endif
57 #endif
58 #include "FLAC/assert.h"
59 #include "protected/stream_decoder.h"
60 #include "private/bitreader.h"
61 #include "private/bitmath.h"
62 #include "private/cpu.h"
63 #include "private/crc.h"
64 #include "private/fixed.h"
65 #include "private/format.h"
66 #include "private/lpc.h"
67 #include "private/md5.h"
68 #include "private/memory.h"
69
70 #ifdef max
71 #undef max
72 #endif
73 #define max(a,b) ((a)>(b)?(a):(b))
74
75 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
76 #ifdef _MSC_VER
77 #define FLAC__U64L(x) x
78 #else
79 #define FLAC__U64L(x) x##LLU
80 #endif
81
82
83 /* technically this should be in an "export.c" but this is convenient enough */
84 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
85 #if FLAC__HAS_OGG
86 1
87 #else
88 0
89 #endif
90 ;
91
92
93 /***********************************************************************
94 *
95 * Private static data
96 *
97 ***********************************************************************/
98
99 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
100
101 /***********************************************************************
102 *
103 * Private class method prototypes
104 *
105 ***********************************************************************/
106
107 static void set_defaults_(FLAC__StreamDecoder *decoder);
108 #ifndef USE_VFS
109 static FILE *get_binary_stdin_(void);
110 #endif
111 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
112 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
113 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
114 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
115 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
116 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
117 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
118 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
119 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
120 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
121 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
122 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
123 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
124 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
125 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
126 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
127 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
128 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
129 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
130 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
131 static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
132 #if FLAC__HAS_OGG
133 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
134 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
135 #endif
136 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
137 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
138 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
139 #if FLAC__HAS_OGG
140 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
141 #endif
142 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
143 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
144 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
145 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
146 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
147
148 /***********************************************************************
149 *
150 * Private class data
151 *
152 ***********************************************************************/
153
154 typedef struct FLAC__StreamDecoderPrivate {
155 #if FLAC__HAS_OGG
156 FLAC__bool is_ogg;
157 #endif
158 FLAC__StreamDecoderReadCallback read_callback;
159 FLAC__StreamDecoderSeekCallback seek_callback;
160 FLAC__StreamDecoderTellCallback tell_callback;
161 FLAC__StreamDecoderLengthCallback length_callback;
162 FLAC__StreamDecoderEofCallback eof_callback;
163 FLAC__StreamDecoderWriteCallback write_callback;
164 FLAC__StreamDecoderMetadataCallback metadata_callback;
165 FLAC__StreamDecoderErrorCallback error_callback;
166 /* generic 32-bit datapath: */
167 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[]);
168 /* generic 64-bit datapath: */
169 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[]);
170 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
171 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[]);
172 /* 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: */
173 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[]);
174 void *client_data;
175 VFSFile *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
176 FLAC__BitReader *input;
177 FLAC__int32 *output[FLAC__MAX_CHANNELS];
178 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
179 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
180 unsigned output_capacity, output_channels;
181 FLAC__uint32 last_frame_number;
182 FLAC__uint32 last_block_size;
183 FLAC__uint64 samples_decoded;
184 FLAC__bool has_stream_info, has_seek_table;
185 FLAC__StreamMetadata stream_info;
186 FLAC__StreamMetadata seek_table;
187 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
188 FLAC__byte *metadata_filter_ids;
189 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
190 FLAC__Frame frame;
191 FLAC__bool cached; /* true if there is a byte in lookahead */
192 FLAC__CPUInfo cpuinfo;
193 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
194 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
195 /* unaligned (original) pointers to allocated data */
196 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
197 FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
198 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
199 FLAC__bool is_seeking;
200 struct FLAC__MD5Context md5context;
201 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
202 /* (the rest of these are only used for seeking) */
203 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
204 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
205 FLAC__uint64 target_sample;
206 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
207 #if FLAC__HAS_OGG
208 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
209 #endif
210 } FLAC__StreamDecoderPrivate;
211
212 /***********************************************************************
213 *
214 * Public static class data
215 *
216 ***********************************************************************/
217
218 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
219 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
220 "FLAC__STREAM_DECODER_READ_METADATA",
221 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
222 "FLAC__STREAM_DECODER_READ_FRAME",
223 "FLAC__STREAM_DECODER_END_OF_STREAM",
224 "FLAC__STREAM_DECODER_OGG_ERROR",
225 "FLAC__STREAM_DECODER_SEEK_ERROR",
226 "FLAC__STREAM_DECODER_ABORTED",
227 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
228 "FLAC__STREAM_DECODER_UNINITIALIZED"
229 };
230
231 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
232 "FLAC__STREAM_DECODER_INIT_STATUS_OK",
233 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
234 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
235 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
236 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
237 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
238 };
239
240 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
241 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
242 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
243 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
244 };
245
246 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
247 "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
248 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
249 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
250 };
251
252 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
253 "FLAC__STREAM_DECODER_TELL_STATUS_OK",
254 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
255 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
256 };
257
258 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
259 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
260 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
261 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
262 };
263
264 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
265 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
266 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
267 };
268
269 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
270 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
271 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
272 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
273 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
274 };
275
276 /***********************************************************************
277 *
278 * Class constructor/destructor
279 *
280 ***********************************************************************/
281 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
282 {
283 FLAC__StreamDecoder *decoder;
284 unsigned i;
285
286 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
287
288 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
289 if(decoder == 0) {
290 return 0;
291 }
292
293 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
294 if(decoder->protected_ == 0) {
295 free(decoder);
296 return 0;
297 }
298
299 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
300 if(decoder->private_ == 0) {
301 free(decoder->protected_);
302 free(decoder);
303 return 0;
304 }
305
306 decoder->private_->input = FLAC__bitreader_new();
307 if(decoder->private_->input == 0) {
308 free(decoder->private_);
309 free(decoder->protected_);
310 free(decoder);
311 return 0;
312 }
313
314 decoder->private_->metadata_filter_ids_capacity = 16;
315 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
316 FLAC__bitreader_delete(decoder->private_->input);
317 free(decoder->private_);
318 free(decoder->protected_);
319 free(decoder);
320 return 0;
321 }
322
323 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
324 decoder->private_->output[i] = 0;
325 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
326 }
327
328 decoder->private_->output_capacity = 0;
329 decoder->private_->output_channels = 0;
330 decoder->private_->has_seek_table = false;
331
332 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
333 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
334
335 decoder->private_->file = 0;
336
337 set_defaults_(decoder);
338
339 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
340
341 return decoder;
342 }
343
344 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
345 {
346 unsigned i;
347
348 FLAC__ASSERT(0 != decoder);
349 FLAC__ASSERT(0 != decoder->protected_);
350 FLAC__ASSERT(0 != decoder->private_);
351 FLAC__ASSERT(0 != decoder->private_->input);
352
353 (void)FLAC__stream_decoder_finish(decoder);
354
355 if(0 != decoder->private_->metadata_filter_ids)
356 free(decoder->private_->metadata_filter_ids);
357
358 FLAC__bitreader_delete(decoder->private_->input);
359
360 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
361 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
362
363 free(decoder->private_);
364 free(decoder->protected_);
365 free(decoder);
366 }
367
368 /***********************************************************************
369 *
370 * Public class methods
371 *
372 ***********************************************************************/
373
374 static FLAC__StreamDecoderInitStatus init_stream_internal_(
375 FLAC__StreamDecoder *decoder,
376 FLAC__StreamDecoderReadCallback read_callback,
377 FLAC__StreamDecoderSeekCallback seek_callback,
378 FLAC__StreamDecoderTellCallback tell_callback,
379 FLAC__StreamDecoderLengthCallback length_callback,
380 FLAC__StreamDecoderEofCallback eof_callback,
381 FLAC__StreamDecoderWriteCallback write_callback,
382 FLAC__StreamDecoderMetadataCallback metadata_callback,
383 FLAC__StreamDecoderErrorCallback error_callback,
384 void *client_data,
385 FLAC__bool is_ogg
386 )
387 {
388 FLAC__ASSERT(0 != decoder);
389
390 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
391 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
392
393 #if !FLAC__HAS_OGG
394 if(is_ogg)
395 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
396 #endif
397
398 if(
399 0 == read_callback ||
400 0 == write_callback ||
401 0 == error_callback ||
402 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
403 )
404 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
405
406 #if FLAC__HAS_OGG
407 decoder->private_->is_ogg = is_ogg;
408 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
409 return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
410 #endif
411
412 /*
413 * get the CPU info and set the function pointers
414 */
415 FLAC__cpu_info(&decoder->private_->cpuinfo);
416 /* first default to the non-asm routines */
417 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
418 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
419 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
420 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
421 /* now override with asm where appropriate */
422 #ifndef FLAC__NO_ASM
423 if(decoder->private_->cpuinfo.use_asm) {
424 #ifdef FLAC__CPU_IA32
425 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
426 #ifdef FLAC__HAS_NASM
427 if(decoder->private_->cpuinfo.data.ia32.mmx) {
428 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
429 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
430 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
431 }
432 else {
433 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
434 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
435 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
436 }
437 #endif
438 #elif defined FLAC__CPU_PPC
439 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
440 if(decoder->private_->cpuinfo.data.ppc.altivec) {
441 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
442 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
443 }
444 #endif
445 }
446 #endif
447
448 /* from here on, errors are fatal */
449
450 if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
451 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
452 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
453 }
454
455 decoder->private_->read_callback = read_callback;
456 decoder->private_->seek_callback = seek_callback;
457 decoder->private_->tell_callback = tell_callback;
458 decoder->private_->length_callback = length_callback;
459 decoder->private_->eof_callback = eof_callback;
460 decoder->private_->write_callback = write_callback;
461 decoder->private_->metadata_callback = metadata_callback;
462 decoder->private_->error_callback = error_callback;
463 decoder->private_->client_data = client_data;
464 decoder->private_->last_frame_number = 0;
465 decoder->private_->last_block_size = 0;
466 decoder->private_->samples_decoded = 0;
467 decoder->private_->has_stream_info = false;
468 decoder->private_->cached = false;
469
470 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
471 decoder->private_->is_seeking = false;
472
473 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
474 if(!FLAC__stream_decoder_reset(decoder)) {
475 /* above call sets the state for us */
476 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
477 }
478
479 return FLAC__STREAM_DECODER_INIT_STATUS_OK;
480 }
481
482 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
483 FLAC__StreamDecoder *decoder,
484 FLAC__StreamDecoderReadCallback read_callback,
485 FLAC__StreamDecoderSeekCallback seek_callback,
486 FLAC__StreamDecoderTellCallback tell_callback,
487 FLAC__StreamDecoderLengthCallback length_callback,
488 FLAC__StreamDecoderEofCallback eof_callback,
489 FLAC__StreamDecoderWriteCallback write_callback,
490 FLAC__StreamDecoderMetadataCallback metadata_callback,
491 FLAC__StreamDecoderErrorCallback error_callback,
492 void *client_data
493 )
494 {
495 return init_stream_internal_(
496 decoder,
497 read_callback,
498 seek_callback,
499 tell_callback,
500 length_callback,
501 eof_callback,
502 write_callback,
503 metadata_callback,
504 error_callback,
505 client_data,
506 /*is_ogg=*/false
507 );
508 }
509
510 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
511 FLAC__StreamDecoder *decoder,
512 FLAC__StreamDecoderReadCallback read_callback,
513 FLAC__StreamDecoderSeekCallback seek_callback,
514 FLAC__StreamDecoderTellCallback tell_callback,
515 FLAC__StreamDecoderLengthCallback length_callback,
516 FLAC__StreamDecoderEofCallback eof_callback,
517 FLAC__StreamDecoderWriteCallback write_callback,
518 FLAC__StreamDecoderMetadataCallback metadata_callback,
519 FLAC__StreamDecoderErrorCallback error_callback,
520 void *client_data
521 )
522 {
523 return init_stream_internal_(
524 decoder,
525 read_callback,
526 seek_callback,
527 tell_callback,
528 length_callback,
529 eof_callback,
530 write_callback,
531 metadata_callback,
532 error_callback,
533 client_data,
534 /*is_ogg=*/true
535 );
536 }
537
538 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
539 FLAC__StreamDecoder *decoder,
540 VFSFile *file,
541 FLAC__StreamDecoderWriteCallback write_callback,
542 FLAC__StreamDecoderMetadataCallback metadata_callback,
543 FLAC__StreamDecoderErrorCallback error_callback,
544 void *client_data,
545 FLAC__bool is_ogg
546 )
547 {
548 FLAC__ASSERT(0 != decoder);
549 FLAC__ASSERT(0 != file);
550
551 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
552 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
553
554 if(0 == write_callback || 0 == error_callback)
555 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
556
557 /*
558 * To make sure that our file does not go unclosed after an error, we
559 * must assign the FILE pointer before any further error can occur in
560 * this routine.
561 */
562 #ifndef USE_VFS
563 if(file == stdin)
564 file = get_binary_stdin_(); /* just to be safe */
565 #endif
566
567 decoder->private_->file = file;
568
569 return init_stream_internal_(
570 decoder,
571 file_read_callback_,
572 #ifdef USE_VFS
573 file_seek_callback_,
574 file_tell_callback_,
575 file_length_callback_,
576 #else
577 decoder->private_->file == stdin? 0: file_seek_callback_,
578 decoder->private_->file == stdin? 0: file_tell_callback_,
579 decoder->private_->file == stdin? 0: file_length_callback_,
580 #endif
581 file_eof_callback_,
582 write_callback,
583 metadata_callback,
584 error_callback,
585 client_data,
586 is_ogg
587 );
588 }
589
590 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
591 FLAC__StreamDecoder *decoder,
592 VFSFile *file,
593 FLAC__StreamDecoderWriteCallback write_callback,
594 FLAC__StreamDecoderMetadataCallback metadata_callback,
595 FLAC__StreamDecoderErrorCallback error_callback,
596 void *client_data
597 )
598 {
599 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
600 }
601
602 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
603 FLAC__StreamDecoder *decoder,
604 VFSFile *file,
605 FLAC__StreamDecoderWriteCallback write_callback,
606 FLAC__StreamDecoderMetadataCallback metadata_callback,
607 FLAC__StreamDecoderErrorCallback error_callback,
608 void *client_data
609 )
610 {
611 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
612 }
613
614 static FLAC__StreamDecoderInitStatus init_file_internal_(
615 FLAC__StreamDecoder *decoder,
616 const char *filename,
617 FLAC__StreamDecoderWriteCallback write_callback,
618 FLAC__StreamDecoderMetadataCallback metadata_callback,
619 FLAC__StreamDecoderErrorCallback error_callback,
620 void *client_data,
621 FLAC__bool is_ogg
622 )
623 {
624 VFSFile *file;
625
626 FLAC__ASSERT(0 != decoder);
627
628 /*
629 * To make sure that our file does not go unclosed after an error, we
630 * have to do the same entrance checks here that are later performed
631 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
632 */
633 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
634 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
635
636 if(0 == write_callback || 0 == error_callback)
637 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
638
639 #ifdef USE_VFS
640 file = vfs_fopen(filename, "rb");
641 #else
642 file = filename? fopen(filename, "rb") : stdin;
643 #endif
644
645 if(0 == file)
646 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
647
648 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
649 }
650
651 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
652 FLAC__StreamDecoder *decoder,
653 const char *filename,
654 FLAC__StreamDecoderWriteCallback write_callback,
655 FLAC__StreamDecoderMetadataCallback metadata_callback,
656 FLAC__StreamDecoderErrorCallback error_callback,
657 void *client_data
658 )
659 {
660 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
661 }
662
663 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
664 FLAC__StreamDecoder *decoder,
665 const char *filename,
666 FLAC__StreamDecoderWriteCallback write_callback,
667 FLAC__StreamDecoderMetadataCallback metadata_callback,
668 FLAC__StreamDecoderErrorCallback error_callback,
669 void *client_data
670 )
671 {
672 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
673 }
674
675 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
676 {
677 FLAC__bool md5_failed = false;
678 unsigned i;
679
680 FLAC__ASSERT(0 != decoder);
681 FLAC__ASSERT(0 != decoder->private_);
682 FLAC__ASSERT(0 != decoder->protected_);
683
684 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
685 return true;
686
687 /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
688 * always call FLAC__MD5Final()
689 */
690 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
691
692 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
693 free(decoder->private_->seek_table.data.seek_table.points);
694 decoder->private_->seek_table.data.seek_table.points = 0;
695 decoder->private_->has_seek_table = false;
696 }
697 FLAC__bitreader_free(decoder->private_->input);
698 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
699 /* WATCHOUT:
700 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
701 * output arrays have a buffer of up to 3 zeroes in front
702 * (at negative indices) for alignment purposes; we use 4
703 * to keep the data well-aligned.
704 */
705 if(0 != decoder->private_->output[i]) {
706 free(decoder->private_->output[i]-4);
707 decoder->private_->output[i] = 0;
708 }
709 if(0 != decoder->private_->residual_unaligned[i]) {
710 free(decoder->private_->residual_unaligned[i]);
711 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
712 }
713 }
714 decoder->private_->output_capacity = 0;
715 decoder->private_->output_channels = 0;
716
717 #if FLAC__HAS_OGG
718 if(decoder->private_->is_ogg)
719 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
720 #endif
721
722 if(0 != decoder->private_->file) {
723 #ifndef USE_VFS
724 if(decoder->private_->file != stdin)
725 fclose(decoder->private_->file);
726 #endif
727 decoder->private_->file = 0;
728 }
729
730 if(decoder->private_->do_md5_checking) {
731 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
732 md5_failed = true;
733 }
734 decoder->private_->is_seeking = false;
735
736 set_defaults_(decoder);
737
738 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
739
740 return !md5_failed;
741 }
742
743 FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
744 {
745 FLAC__ASSERT(0 != decoder);
746 FLAC__ASSERT(0 != decoder->private_);
747 FLAC__ASSERT(0 != decoder->protected_);
748 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
749 return false;
750 #if FLAC__HAS_OGG
751 /* can't check decoder->private_->is_ogg since that's not set until init time */
752 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
753 return true;
754 #else
755 (void)value;
756 return false;
757 #endif
758 }
759
760 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
761 {
762 FLAC__ASSERT(0 != decoder);
763 FLAC__ASSERT(0 != decoder->protected_);
764 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
765 return false;
766 decoder->protected_->md5_checking = value;
767 return true;
768 }
769
770 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
771 {
772 FLAC__ASSERT(0 != decoder);
773 FLAC__ASSERT(0 != decoder->private_);
774 FLAC__ASSERT(0 != decoder->protected_);
775 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
776 /* double protection */
777 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
778 return false;
779 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
780 return false;
781 decoder->private_->metadata_filter[type] = true;
782 if(type == FLAC__METADATA_TYPE_APPLICATION)
783 decoder->private_->metadata_filter_ids_count = 0;
784 return true;
785 }
786
787 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
788 {
789 FLAC__ASSERT(0 != decoder);
790 FLAC__ASSERT(0 != decoder->private_);
791 FLAC__ASSERT(0 != decoder->protected_);
792 FLAC__ASSERT(0 != id);
793 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
794 return false;
795
796 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
797 return true;
798
799 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
800
801 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
802 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
803 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
804 return false;
805 }
806 decoder->private_->metadata_filter_ids_capacity *= 2;
807 }
808
809 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));
810 decoder->private_->metadata_filter_ids_count++;
811
812 return true;
813 }
814
815 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
816 {
817 unsigned i;
818 FLAC__ASSERT(0 != decoder);
819 FLAC__ASSERT(0 != decoder->private_);
820 FLAC__ASSERT(0 != decoder->protected_);
821 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
822 return false;
823 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
824 decoder->private_->metadata_filter[i] = true;
825 decoder->private_->metadata_filter_ids_count = 0;
826 return true;
827 }
828
829 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
830 {
831 FLAC__ASSERT(0 != decoder);
832 FLAC__ASSERT(0 != decoder->private_);
833 FLAC__ASSERT(0 != decoder->protected_);
834 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
835 /* double protection */
836 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
837 return false;
838 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
839 return false;
840 decoder->private_->metadata_filter[type] = false;
841 if(type == FLAC__METADATA_TYPE_APPLICATION)
842 decoder->private_->metadata_filter_ids_count = 0;
843 return true;
844 }
845
846 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
847 {
848 FLAC__ASSERT(0 != decoder);
849 FLAC__ASSERT(0 != decoder->private_);
850 FLAC__ASSERT(0 != decoder->protected_);
851 FLAC__ASSERT(0 != id);
852 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
853 return false;
854
855 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
856 return true;
857
858 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
859
860 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
861 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
862 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
863 return false;
864 }
865 decoder->private_->metadata_filter_ids_capacity *= 2;
866 }
867
868 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));
869 decoder->private_->metadata_filter_ids_count++;
870
871 return true;
872 }
873
874 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
875 {
876 FLAC__ASSERT(0 != decoder);
877 FLAC__ASSERT(0 != decoder->private_);
878 FLAC__ASSERT(0 != decoder->protected_);
879 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
880 return false;
881 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
882 decoder->private_->metadata_filter_ids_count = 0;
883 return true;
884 }
885
886 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
887 {
888 FLAC__ASSERT(0 != decoder);
889 FLAC__ASSERT(0 != decoder->protected_);
890 return decoder->protected_->state;
891 }
892
893 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
894 {
895 return FLAC__StreamDecoderStateString[decoder->protected_->state];
896 }
897
898 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
899 {
900 FLAC__ASSERT(0 != decoder);
901 FLAC__ASSERT(0 != decoder->protected_);
902 return decoder->protected_->md5_checking;
903 }
904
905 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
906 {
907 FLAC__ASSERT(0 != decoder);
908 FLAC__ASSERT(0 != decoder->protected_);
909 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
910 }
911
912 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
913 {
914 FLAC__ASSERT(0 != decoder);
915 FLAC__ASSERT(0 != decoder->protected_);
916 return decoder->protected_->channels;
917 }
918
919 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
920 {
921 FLAC__ASSERT(0 != decoder);
922 FLAC__ASSERT(0 != decoder->protected_);
923 return decoder->protected_->channel_assignment;
924 }
925
926 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
927 {
928 FLAC__ASSERT(0 != decoder);
929 FLAC__ASSERT(0 != decoder->protected_);
930 return decoder->protected_->bits_per_sample;
931 }
932
933 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
934 {
935 FLAC__ASSERT(0 != decoder);
936 FLAC__ASSERT(0 != decoder->protected_);
937 return decoder->protected_->sample_rate;
938 }
939
940 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
941 {
942 FLAC__ASSERT(0 != decoder);
943 FLAC__ASSERT(0 != decoder->protected_);
944 return decoder->protected_->blocksize;
945 }
946
947 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
948 {
949 FLAC__ASSERT(0 != decoder);
950 FLAC__ASSERT(0 != decoder->private_);
951 FLAC__ASSERT(0 != position);
952
953 #if FLAC__HAS_OGG
954 if(decoder->private_->is_ogg)
955 return false;
956 #endif
957 if(0 == decoder->private_->tell_callback)
958 return false;
959 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
960 return false;
961 /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
962 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
963 return false;
964 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
965 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
966 return true;
967 }
968
969 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
970 {
971 FLAC__ASSERT(0 != decoder);
972 FLAC__ASSERT(0 != decoder->private_);
973 FLAC__ASSERT(0 != decoder->protected_);
974
975 decoder->private_->samples_decoded = 0;
976 decoder->private_->do_md5_checking = false;
977
978 #if FLAC__HAS_OGG
979 if(decoder->private_->is_ogg)
980 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
981 #endif
982
983 if(!FLAC__bitreader_clear(decoder->private_->input)) {
984 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
985 return false;
986 }
987 decoder->private_->last_frame_number = 0;
988 decoder->private_->last_block_size = 0;
989 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
990
991 return true;
992 }
993
994 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
995 {
996 FLAC__ASSERT(0 != decoder);
997 FLAC__ASSERT(0 != decoder->private_);
998 FLAC__ASSERT(0 != decoder->protected_);
999
1000 if(!FLAC__stream_decoder_flush(decoder)) {
1001 /* above call sets the state for us */
1002 return false;
1003 }
1004
1005 #if FLAC__HAS_OGG
1006 /*@@@ could go in !internal_reset_hack block below */
1007 if(decoder->private_->is_ogg)
1008 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
1009 #endif
1010
1011 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
1012 * (internal_reset_hack) don't try to rewind since we are already at
1013 * the beginning of the stream and don't want to fail if the input is
1014 * not seekable.
1015 */
1016 if(!decoder->private_->internal_reset_hack) {
1017 #ifndef USE_VFS
1018 if(decoder->private_->file == stdin)
1019 return false; /* can't rewind stdin, reset fails */
1020 #endif
1021 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
1022 return false; /* seekable and seek fails, reset fails */
1023 }
1024 else
1025 decoder->private_->internal_reset_hack = false;
1026
1027 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
1028
1029 decoder->private_->has_stream_info = false;
1030 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
1031 free(decoder->private_->seek_table.data.seek_table.points);
1032 decoder->private_->seek_table.data.seek_table.points = 0;
1033 decoder->private_->has_seek_table = false;
1034 }
1035 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
1036
1037 /* We initialize the FLAC__MD5Context even though we may never use it. This
1038 * is because md5 checking may be turned on to start and then turned off if
1039 * a seek occurs. So we init the context here and finalize it in
1040 * FLAC__stream_decoder_finish() to make sure things are always cleaned up
1041 * properly.
1042 */
1043 FLAC__MD5Init(&decoder->private_->md5context);
1044
1045 decoder->private_->first_frame_offset = 0;
1046 decoder->private_->unparseable_frame_count = 0;
1047
1048 return true;
1049 }
1050
1051 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1052 {
1053 FLAC__bool got_a_frame;
1054 FLAC__ASSERT(0 != decoder);
1055 FLAC__ASSERT(0 != decoder->protected_);
1056
1057 while(1) {
1058 switch(decoder->protected_->state) {
1059 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1060 if(!find_metadata_(decoder))
1061 return false; /* above function sets the status for us */
1062 break;
1063 case FLAC__STREAM_DECODER_READ_METADATA:
1064 if(!read_metadata_(decoder))
1065 return false; /* above function sets the status for us */
1066 else
1067 return true;
1068 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1069 if(!frame_sync_(decoder))
1070 return true; /* above function sets the status for us */
1071 break;
1072 case FLAC__STREAM_DECODER_READ_FRAME:
1073 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1074 return false; /* above function sets the status for us */
1075 if(got_a_frame)
1076 return true; /* above function sets the status for us */
1077 break;
1078 case FLAC__STREAM_DECODER_END_OF_STREAM:
1079 case FLAC__STREAM_DECODER_ABORTED:
1080 return true;
1081 default:
1082 FLAC__ASSERT(0);
1083 return false;
1084 }
1085 }
1086 }
1087
1088 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1089 {
1090 FLAC__ASSERT(0 != decoder);
1091 FLAC__ASSERT(0 != decoder->protected_);
1092
1093 while(1) {
1094 switch(decoder->protected_->state) {
1095 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1096 if(!find_metadata_(decoder))
1097 return false; /* above function sets the status for us */
1098 break;
1099 case FLAC__STREAM_DECODER_READ_METADATA:
1100 if(!read_metadata_(decoder))
1101 return false; /* above function sets the status for us */
1102 break;
1103 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1104 case FLAC__STREAM_DECODER_READ_FRAME:
1105 case FLAC__STREAM_DECODER_END_OF_STREAM:
1106 case FLAC__STREAM_DECODER_ABORTED:
1107 return true;
1108 default:
1109 FLAC__ASSERT(0);
1110 return false;
1111 }
1112 }
1113 }
1114
1115 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1116 {
1117 FLAC__bool dummy;
1118 FLAC__ASSERT(0 != decoder);
1119 FLAC__ASSERT(0 != decoder->protected_);
1120
1121 while(1) {
1122 switch(decoder->protected_->state) {
1123 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1124 if(!find_metadata_(decoder))
1125 return false; /* above function sets the status for us */
1126 break;
1127 case FLAC__STREAM_DECODER_READ_METADATA:
1128 if(!read_metadata_(decoder))
1129 return false; /* above function sets the status for us */
1130 break;
1131 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1132 if(!frame_sync_(decoder))
1133 return true; /* above function sets the status for us */
1134 break;
1135 case FLAC__STREAM_DECODER_READ_FRAME:
1136 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1137 return false; /* above function sets the status for us */
1138 break;
1139 case FLAC__STREAM_DECODER_END_OF_STREAM:
1140 case FLAC__STREAM_DECODER_ABORTED:
1141 return true;
1142 default:
1143 FLAC__ASSERT(0);
1144 return false;
1145 }
1146 }
1147 }
1148
1149 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1150 {
1151 FLAC__bool got_a_frame;
1152 FLAC__ASSERT(0 != decoder);
1153 FLAC__ASSERT(0 != decoder->protected_);
1154
1155 while(1) {
1156 switch(decoder->protected_->state) {
1157 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1158 case FLAC__STREAM_DECODER_READ_METADATA:
1159 return false; /* above function sets the status for us */
1160 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1161 if(!frame_sync_(decoder))
1162 return true; /* above function sets the status for us */
1163 break;
1164 case FLAC__STREAM_DECODER_READ_FRAME:
1165 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1166 return false; /* above function sets the status for us */
1167 if(got_a_frame)
1168 return true; /* above function sets the status for us */
1169 break;
1170 case FLAC__STREAM_DECODER_END_OF_STREAM:
1171 case FLAC__STREAM_DECODER_ABORTED:
1172 return true;
1173 default:
1174 FLAC__ASSERT(0);
1175 return false;
1176 }
1177 }
1178 }
1179
1180 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1181 {
1182 FLAC__uint64 length;
1183
1184 FLAC__ASSERT(0 != decoder);
1185
1186 if(
1187 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1188 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1189 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1190 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1191 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1192 )
1193 return false;
1194
1195 if(0 == decoder->private_->seek_callback)
1196 return false;
1197
1198 FLAC__ASSERT(decoder->private_->seek_callback);
1199 FLAC__ASSERT(decoder->private_->tell_callback);
1200 FLAC__ASSERT(decoder->private_->length_callback);
1201 FLAC__ASSERT(decoder->private_->eof_callback);
1202
1203 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1204 return false;
1205
1206 decoder->private_->is_seeking = true;
1207
1208 /* turn off md5 checking if a seek is attempted */
1209 decoder->private_->do_md5_checking = false;
1210
1211 /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */
1212 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1213 decoder->private_->is_seeking = false;
1214 return false;
1215 }
1216
1217 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
1218 if(
1219 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1220 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1221 ) {
1222 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1223 /* above call sets the state for us */
1224 decoder->private_->is_seeking = false;
1225 return false;
1226 }
1227 /* check this again in case we didn't know total_samples the first time */
1228 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1229 decoder->private_->is_seeking = false;
1230 return false;
1231 }
1232 }
1233
1234 {
1235 const FLAC__bool ok =
1236 #if FLAC__HAS_OGG
1237 decoder->private_->is_ogg?
1238 seek_to_absolute_sample_ogg_(decoder, length, sample) :
1239 #endif
1240 seek_to_absolute_sample_(decoder, length, sample)
1241 ;
1242 decoder->private_->is_seeking = false;
1243 return ok;
1244 }
1245 }
1246
1247 /***********************************************************************
1248 *
1249 * Protected class methods
1250 *
1251 ***********************************************************************/
1252
1253 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1254 {
1255 FLAC__ASSERT(0 != decoder);
1256 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1257 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
1258 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
1259 }
1260
1261 /***********************************************************************
1262 *
1263 * Private class methods
1264 *
1265 ***********************************************************************/
1266
1267 void set_defaults_(FLAC__StreamDecoder *decoder)
1268 {
1269 #if FLAC__HAS_OGG
1270 decoder->private_->is_ogg = false;
1271 #endif
1272 decoder->private_->read_callback = 0;
1273 decoder->private_->seek_callback = 0;
1274 decoder->private_->tell_callback = 0;
1275 decoder->private_->length_callback = 0;
1276 decoder->private_->eof_callback = 0;
1277 decoder->private_->write_callback = 0;
1278 decoder->private_->metadata_callback = 0;
1279 decoder->private_->error_callback = 0;
1280 decoder->private_->client_data = 0;
1281
1282 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1283 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1284 decoder->private_->metadata_filter_ids_count = 0;
1285
1286 decoder->protected_->md5_checking = false;
1287
1288 #if FLAC__HAS_OGG
1289 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1290 #endif
1291 }
1292
1293 /*
1294 * This will forcibly set stdin to binary mode (for OSes that require it)
1295 */
1296 #ifndef USE_VFS
1297 FILE *get_binary_stdin_(void)
1298 {
1299 /* if something breaks here it is probably due to the presence or
1300 * absence of an underscore before the identifiers 'setmode',
1301 * 'fileno', and/or 'O_BINARY'; check your system header files.
1302 */
1303 #if defined _MSC_VER || defined __MINGW32__
1304 _setmode(_fileno(stdin), _O_BINARY);
1305 #elif defined __CYGWIN__ || defined __EMX__
1306 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
1307 setmode(_fileno(stdin), _O_BINARY);
1308 #endif
1309
1310 return stdin;
1311 }
1312 #endif
1313
1314 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
1315 {
1316 unsigned i;
1317 FLAC__int32 *tmp;
1318
1319 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
1320 return true;
1321
1322 /* simply using realloc() is not practical because the number of channels may change mid-stream */
1323
1324 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1325 if(0 != decoder->private_->output[i]) {
1326 free(decoder->private_->output[i]-4);
1327 decoder->private_->output[i] = 0;
1328 }
1329 if(0 != decoder->private_->residual_unaligned[i]) {
1330 free(decoder->private_->residual_unaligned[i]);
1331 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1332 }
1333 }
1334
1335 for(i = 0; i < channels; i++) {
1336 /* WATCHOUT:
1337 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
1338 * output arrays have a buffer of up to 3 zeroes in front
1339 * (at negative indices) for alignment purposes; we use 4
1340 * to keep the data well-aligned.
1341 */
1342 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
1343 if(tmp == 0) {
1344 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1345 return false;
1346 }
1347 memset(tmp, 0, sizeof(FLAC__int32)*4);
1348 decoder->private_->output[i] = tmp + 4;
1349
1350 /* WATCHOUT:
1351 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
1352 */
1353 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
1354 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1355 return false;
1356 }
1357 }
1358
1359 decoder->private_->output_capacity = size;
1360 decoder->private_->output_channels = channels;
1361
1362 return true;
1363 }
1364
1365 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1366 {
1367 unsigned i;
1368
1369 FLAC__ASSERT(0 != decoder);
1370 FLAC__ASSERT(0 != decoder->private_);
1371
1372 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1373 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
1374 return true;
1375
1376 return false;
1377 }
1378
1379 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1380 {
1381 FLAC__uint32 x;
1382 unsigned i, id;
1383 FLAC__bool first = true;
1384
1385 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1386
1387 for(i = id = 0; i < 4; ) {
1388 if(decoder->private_->cached) {
1389 x = (FLAC__uint32)decoder->private_->lookahead;
1390 decoder->private_->cached = false;
1391 }
1392 else {
1393 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1394 return false; /* read_callback_ sets the state for us */
1395 }
1396 if(x == FLAC__STREAM_SYNC_STRING[i]) {
1397 first = true;
1398 i++;
1399 id = 0;
1400 continue;
1401 }
1402 if(x == ID3V2_TAG_[id]) {
1403 id++;
1404 i = 0;
1405 if(id == 3) {
1406 if(!skip_id3v2_tag_(decoder))
1407 return false; /* skip_id3v2_tag_ sets the state for us */
1408 }
1409 continue;
1410 }
1411 id = 0;
1412 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1413 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1414 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1415 return false; /* read_callback_ sets the state for us */
1416
1417 /* 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 */
1418 /* else we have to check if the second byte is the end of a sync code */
1419 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1420 decoder->private_->lookahead = (FLAC__byte)x;
1421 decoder->private_->cached = true;
1422 }
1423 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1424 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1425 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1426 return true;
1427 }
1428 }
1429 i = 0;
1430 if(first) {
1431 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1432 first = false;
1433 }
1434 }
1435
1436 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1437 return true;
1438 }
1439
1440 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1441 {
1442 FLAC__bool is_last;
1443 FLAC__uint32 i, x, type, length;
1444
1445 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1446
1447 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1448 return false; /* read_callback_ sets the state for us */
1449 is_last = x? true : false;
1450
1451 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
1452 return false; /* read_callback_ sets the state for us */
1453
1454 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1455 return false; /* read_callback_ sets the state for us */
1456
1457 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1458 if(!read_metadata_streaminfo_(decoder, is_last, length))
1459 return false;
1460
1461 decoder->private_->has_stream_info = true;
1462 if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
1463 decoder->private_->do_md5_checking = false;
1464 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1465 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1466 }
1467 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1468 if(!read_metadata_seektable_(decoder, is_last, length))
1469 return false;
1470
1471 decoder->private_->has_seek_table = true;
1472 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1473 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1474 }
1475 else {
1476 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1477 unsigned real_length = length;
1478 FLAC__StreamMetadata block;
1479
1480 block.is_last = is_last;
1481 block.type = (FLAC__MetadataType)type;
1482 block.length = length;
1483
1484 if(type == FLAC__METADATA_TYPE_APPLICATION) {
1485 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1486 return false; /* read_callback_ sets the state for us */
1487
1488 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1489
1490 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1491 skip_it = !skip_it;
1492 }
1493
1494 if(skip_it) {
1495 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1496 return false; /* read_callback_ sets the state for us */
1497 }
1498 else {
1499 switch(type) {
1500 case FLAC__METADATA_TYPE_PADDING:
1501 /* skip the padding bytes */
1502 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1503 return false; /* read_callback_ sets the state for us */
1504 break;
1505 case FLAC__METADATA_TYPE_APPLICATION:
1506 /* remember, we read the ID already */
1507 if(real_length > 0) {
1508 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
1509 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1510 return false;
1511 }
1512 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1513 return false; /* read_callback_ sets the state for us */
1514 }
1515 else
1516 block.data.application.data = 0;
1517 break;
1518 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1519 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
1520 return false;
1521 break;
1522 case FLAC__METADATA_TYPE_CUESHEET:
1523 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1524 return false;
1525 break;
1526 case FLAC__METADATA_TYPE_PICTURE:
1527 if(!read_metadata_picture_(decoder, &block.data.picture))
1528 return false;
1529 break;
1530 case FLAC__METADATA_TYPE_STREAMINFO:
1531 case FLAC__METADATA_TYPE_SEEKTABLE:
1532 FLAC__ASSERT(0);
1533 break;
1534 default:
1535 if(real_length > 0) {
1536 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
1537 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1538 return false;
1539 }
1540 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1541 return false; /* read_callback_ sets the state for us */
1542 }
1543 else
1544 block.data.unknown.data = 0;
1545 break;
1546 }
1547 if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
1548 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1549
1550 /* now we have to free any malloc'ed data in the block */
1551 switch(type) {
1552 case FLAC__METADATA_TYPE_PADDING:
1553 break;
1554 case FLAC__METADATA_TYPE_APPLICATION:
1555 if(0 != block.data.application.data)
1556 free(block.data.application.data);
1557 break;
1558 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1559 if(0 != block.data.vorbis_comment.vendor_string.entry)
1560 free(block.data.vorbis_comment.vendor_string.entry);
1561 if(block.data.vorbis_comment.num_comments > 0)
1562 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1563 if(0 != block.data.vorbis_comment.comments[i].entry)
1564 free(block.data.vorbis_comment.comments[i].entry);
1565 if(0 != block.data.vorbis_comment.comments)
1566 free(block.data.vorbis_comment.comments);
1567 break;
1568 case FLAC__METADATA_TYPE_CUESHEET:
1569 if(block.data.cue_sheet.num_tracks > 0)
1570 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1571 if(0 != block.data.cue_sheet.tracks[i].indices)
1572 free(block.data.cue_sheet.tracks[i].indices);
1573 if(0 != block.data.cue_sheet.tracks)
1574 free(block.data.cue_sheet.tracks);
1575 break;
1576 case FLAC__METADATA_TYPE_PICTURE:
1577 if(0 != block.data.picture.mime_type)
1578 free(block.data.picture.mime_type);
1579 if(0 != block.data.picture.description)
1580 free(block.data.picture.description);
1581 if(0 != block.data.picture.data)
1582 free(block.data.picture.data);
1583 break;
1584 case FLAC__METADATA_TYPE_STREAMINFO:
1585 case FLAC__METADATA_TYPE_SEEKTABLE:
1586 FLAC__ASSERT(0);
1587 default:
1588 if(0 != block.data.unknown.data)
1589 free(block.data.unknown.data);
1590 break;
1591 }
1592 }
1593 }
1594
1595 if(is_last) {
1596 /* if this fails, it's OK, it's just a hint for the seek routine */
1597 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1598 decoder->private_->first_frame_offset = 0;
1599 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1600 }
1601
1602 return true;
1603 }
1604
1605 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1606 {
1607 FLAC__uint32 x;
1608 unsigned bits, used_bits = 0;
1609
1610 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1611
1612 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1613 decoder->private_->stream_info.is_last = is_last;
1614 decoder->private_->stream_info.length = length;
1615
1616 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1617 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1618 return false; /* read_callback_ sets the state for us */
1619 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1620 used_bits += bits;
1621
1622 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1623 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1624 return false; /* read_callback_ sets the state for us */
1625 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1626 used_bits += bits;
1627
1628 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1629 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1630 return false; /* read_callback_ sets the state for us */
1631 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1632 used_bits += bits;
1633
1634 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1635 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1636 return false; /* read_callback_ sets the state for us */
1637 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1638 used_bits += bits;
1639
1640 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1641 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1642 return false; /* read_callback_ sets the state for us */
1643 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1644 used_bits += bits;
1645
1646 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1647 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1648 return false; /* read_callback_ sets the state for us */
1649 decoder->private_->stream_info.data.stream_info.channels = x+1;
1650 used_bits += bits;
1651
1652 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1653 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1654 return false; /* read_callback_ sets the state for us */
1655 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1656 used_bits += bits;
1657
1658 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1659 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
1660 return false; /* read_callback_ sets the state for us */
1661 used_bits += bits;
1662
1663 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1664 return false; /* read_callback_ sets the state for us */
1665 used_bits += 16*8;
1666
1667 /* skip the rest of the block */
1668 FLAC__ASSERT(used_bits % 8 == 0);
1669 length -= (used_bits / 8);
1670 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1671 return false; /* read_callback_ sets the state for us */
1672
1673 return true;
1674 }
1675
1676 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1677 {
1678 FLAC__uint32 i, x;
1679 FLAC__uint64 xx;
1680
1681 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1682
1683 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1684 decoder->private_->seek_table.is_last = is_last;
1685 decoder->private_->seek_table.length = length;
1686
1687 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1688
1689 /* use realloc since we may pass through here several times (e.g. after seeking) */
1690 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)))) {
1691 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1692 return false;
1693 }
1694 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1695 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
1696 return false; /* read_callback_ sets the state for us */
1697 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1698
1699 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
1700 return false; /* read_callback_ sets the state for us */
1701 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1702
1703 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
1704 return false; /* read_callback_ sets the state for us */
1705 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1706 }
1707 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1708 /* if there is a partial point left, skip over it */
1709 if(length > 0) {
1710 /*@@@ do a send_error_to_client_() here? there's an argument for either way */
1711 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1712 return false; /* read_callback_ sets the state for us */
1713 }
1714
1715 return true;
1716 }
1717
1718 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1719 {
1720 FLAC__uint32 i;
1721
1722 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1723
1724 /* read vendor string */
1725 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1726 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
1727 return false; /* read_callback_ sets the state for us */
1728 if(obj->vendor_string.length > 0) {
1729 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
1730 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1731 return false;
1732 }
1733 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
1734 return false; /* read_callback_ sets the state for us */
1735 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
1736 }
1737 else
1738 obj->vendor_string.entry = 0;
1739
1740 /* read num comments */
1741 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1742 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
1743 return false; /* read_callback_ sets the state for us */
1744
1745 /* read comments */
1746 if(obj->num_comments > 0) {
1747 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1748 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1749 return false;
1750 }
1751 for(i = 0; i < obj->num_comments; i++) {
1752 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1753 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
1754 return false; /* read_callback_ sets the state for us */
1755 if(obj->comments[i].length > 0) {
1756 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
1757 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1758 return false;
1759 }
1760 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
1761 return false; /* read_callback_ sets the state for us */
1762 obj->comments[i].entry[obj->comments[i].length] = '\0';
1763 }
1764 else
1765 obj->comments[i].entry = 0;
1766 }
1767 }
1768 else {
1769 obj->comments = 0;
1770 }
1771
1772 return true;
1773 }
1774
1775 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1776 {
1777 FLAC__uint32 i, j, x;
1778
1779 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1780
1781 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1782
1783 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1784 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
1785 return false; /* read_callback_ sets the state for us */
1786
1787 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
1788 return false; /* read_callback_ sets the state for us */
1789
1790 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
1791 return false; /* read_callback_ sets the state for us */
1792 obj->is_cd = x? true : false;
1793
1794 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
1795 return false; /* read_callback_ sets the state for us */
1796
1797 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
1798 return false; /* read_callback_ sets the state for us */
1799 obj->num_tracks = x;
1800
1801 if(obj->num_tracks > 0) {
1802 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1803 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1804 return false;
1805 }
1806 for(i = 0; i < obj->num_tracks; i++) {
1807 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1808 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
1809 return false; /* read_callback_ sets the state for us */
1810
1811 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
1812 return false; /* read_callback_ sets the state for us */
1813 track->number = (FLAC__byte)x;
1814
1815 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1816 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
1817 return false; /* read_callback_ sets the state for us */
1818
1819 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
1820 return false; /* read_callback_ sets the state for us */
1821 track->type = x;
1822
1823 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
1824 return false; /* read_callback_ sets the state for us */
1825 track->pre_emphasis = x;
1826
1827 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
1828 return false; /* read_callback_ sets the state for us */
1829
1830 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
1831 return false; /* read_callback_ sets the state for us */
1832 track->num_indices = (FLAC__byte)x;
1833
1834 if(track->num_indices > 0) {
1835 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1836 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1837 return false;
1838 }
1839 for(j = 0; j < track->num_indices; j++) {
1840 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1841 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
1842 return false; /* read_callback_ sets the state for us */
1843
1844 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
1845 return false; /* read_callback_ sets the state for us */
1846 index->number = (FLAC__byte)x;
1847
1848 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
1849 return false; /* read_callback_ sets the state for us */
1850 }
1851 }
1852 }
1853 }
1854
1855 return true;
1856 }
1857
1858 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
1859 {
1860 FLAC__uint32 len;
1861
1862 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1863
1864 /* read type */
1865 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
1866 return false; /* read_callback_ sets the state for us */
1867
1868 /* read MIME type */
1869 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
1870 return false; /* read_callback_ sets the state for us */
1871 if(0 == (obj->mime_type = (char*)malloc(len+1))) {
1872 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1873 return false;
1874 }
1875 if(len > 0) {
1876 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len))
1877 return false; /* read_callback_ sets the state for us */
1878 }
1879 obj->mime_type[len] = '\0';
1880
1881 /* read description */
1882 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
1883 return false; /* read_callback_ sets the state for us */
1884 if(0 == (obj->description = (FLAC__byte*)malloc(len+1))) {
1885 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1886 return false;
1887 }
1888 if(len > 0) {
1889 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len))
1890 return false; /* read_callback_ sets the state for us */
1891 }
1892 obj->description[len] = '\0';
1893
1894 /* read width */
1895 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
1896 return false; /* read_callback_ sets the state for us */
1897
1898 /* read height */
1899 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
1900 return false; /* read_callback_ sets the state for us */
1901
1902 /* read depth */
1903 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
1904 return false; /* read_callback_ sets the state for us */
1905
1906 /* read colors */
1907 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
1908 return false; /* read_callback_ sets the state for us */
1909
1910 /* read data */
1911 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
1912 return false; /* read_callback_ sets the state for us */
1913 if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
1914 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1915 return false;
1916 }
1917 if(obj->data_length > 0) {
1918 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
1919 return false; /* read_callback_ sets the state for us */
1920 }
1921
1922 return true;
1923 }
1924
1925 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1926 {
1927 FLAC__uint32 x;
1928 unsigned i, skip;
1929
1930 /* skip the version and flags bytes */
1931 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
1932 return false; /* read_callback_ sets the state for us */
1933 /* get the size (in bytes) to skip */
1934 skip = 0;
1935 for(i = 0; i < 4; i++) {
1936 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1937 return false; /* read_callback_ sets the state for us */
1938 skip <<= 7;
1939 skip |= (x & 0x7f);
1940 }
1941 /* skip the rest of the tag */
1942 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
1943 return false; /* read_callback_ sets the state for us */
1944 return true;
1945 }
1946
1947 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
1948 {
1949 FLAC__uint32 x;
1950 FLAC__bool first = true;
1951
1952 /* If we know the total number of samples in the stream, stop if we've read that many. */
1953 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1954 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
1955 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1956 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1957 return true;
1958 }
1959 }
1960
1961 /* make sure we're byte aligned */
1962 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
1963 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
1964 return false; /* read_callback_ sets the state for us */
1965 }
1966
1967 while(1) {
1968 if(decoder->private_->cached) {
1969 x = (FLAC__uint32)decoder->private_->lookahead;
1970 decoder->private_->cached = false;
1971 }
1972 else {
1973 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1974 return false; /* read_callback_ sets the state for us */
1975 }
1976 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1977 decoder->private_->header_warmup[0] = (FLAC__byte)x;
1978 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1979 return false; /* read_callback_ sets the state for us */
1980
1981 /* 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 */
1982 /* else we have to check if the second byte is the end of a sync code */
1983 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1984 decoder->private_->lookahead = (FLAC__byte)x;
1985 decoder->private_->cached = true;
1986 }
1987 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
1988 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1989 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1990 return true;
1991 }
1992 }
1993 if(first) {
1994 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1995 first = false;
1996 }
1997 }
1998
1999 return true;
2000 }
2001
2002 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
2003 {
2004 unsigned channel;
2005 unsigned i;
2006 FLAC__int32 mid, side, left, right;
2007 unsigned frame_crc; /* the one we calculate from the input stream */
2008 FLAC__uint32 x;
2009
2010 *got_a_frame = false;
2011
2012 /* init the CRC */
2013 frame_crc = 0;
2014 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
2015 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
2016 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
2017
2018 if(!read_frame_header_(decoder))
2019 return false;
2020 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
2021 return true;
2022 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
2023 return false;
2024 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2025 /*
2026 * first figure the correct bits-per-sample of the subframe
2027 */
2028 unsigned bps = decoder->private_->frame.header.bits_per_sample;
2029 switch(decoder->private_->frame.header.channel_assignment) {
2030 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2031 /* no adjustment needed */
2032 break;
2033 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2034 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2035 if(channel == 1)
2036 bps++;
2037 break;
2038 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2039 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2040 if(channel == 0)
2041 bps++;
2042 break;
2043 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2044 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2045 if(channel == 1)
2046 bps++;
2047 break;
2048 default:
2049 FLAC__ASSERT(0);
2050 }
2051 /*
2052 * now read it
2053 */
2054 if(!read_subframe_(decoder, channel, bps, do_full_decode))
2055 return false;
2056 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2057 return true;
2058 }
2059 if(!read_zero_padding_(decoder))
2060 return false;
2061
2062 /*
2063 * Read the frame CRC-16 from the footer and check
2064 */
2065 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2066 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
2067 return false; /* read_callback_ sets the state for us */
2068 if(frame_crc == x) {
2069 if(do_full_decode) {
2070 /* Undo any special channel coding */
2071 switch(decoder->private_->frame.header.channel_assignment) {
2072 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2073 /* do nothing */
2074 break;
2075 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2076 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2077 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2078 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
2079 break;
2080 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2081 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2082 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2083 decoder->private_->output[0][i] += decoder->private_->output[1][i];
2084 break;
2085 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2086 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2087 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2088 mid = decoder->private_->output[0][i];
2089 side = decoder->private_->output[1][i];
2090 mid <<= 1;
2091 if(side & 1) /* i.e. if 'side' is odd... */
2092 mid++;
2093 left = mid + side;
2094 right = mid - side;
2095 decoder->private_->output[0][i] = left >> 1;
2096 decoder->private_->output[1][i] = right >> 1;
2097 }
2098 break;
2099 default:
2100 FLAC__ASSERT(0);
2101 break;
2102 }
2103 }
2104 }
2105 else {
2106 /* Bad frame, emit error and zero the output signal */
2107 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2108 if(do_full_decode) {
2109 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2110 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2111 }
2112 }
2113 }
2114
2115 *got_a_frame = true;
2116
2117 /* put the latest values into the public section of the decoder instance */
2118 decoder->protected_->channels = decoder->private_->frame.header.channels;
2119 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2120 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2121 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2122 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2123
2124 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2125 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2126
2127 /* write it */
2128 if(do_full_decode) {
2129 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
2130 return false;
2131 }
2132
2133 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2134 return true;
2135 }
2136
2137 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2138 {
2139 FLAC__uint32 x;
2140 FLAC__uint64 xx;
2141 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
2142 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2143 unsigned raw_header_len;
2144 FLAC__bool is_unparseable = false;
2145 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);
2146 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);
2147
2148 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2149
2150 /* init the raw header with the saved bits from synchronization */
2151 raw_header[0] = decoder->private_->header_warmup[0];
2152 raw_header[1] = decoder->private_->header_warmup[1];
2153 raw_header_len = 2;
2154
2155 /*
2156 * check to make sure that the reserved bits are 0
2157 */
2158 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
2159 is_unparseable = true;
2160 }
2161
2162 /*
2163 * Note that along the way as we read the header, we look for a sync
2164 * code inside. If we find one it would indicate that our original
2165 * sync was bad since there cannot be a sync code in a valid header.
2166 */
2167
2168 /*
2169 * read in the raw header as bytes so we can CRC it, and parse it on the way
2170 */
2171 for(i = 0; i < 2; i++) {
2172 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2173 return false; /* read_callback_ sets the state for us */
2174 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2175 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
2176 decoder->private_->lookahead = (FLAC__byte)x;
2177 decoder->private_->cached = true;
2178 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2179 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2180 return true;
2181 }
2182 raw_header[raw_header_len++] = (FLAC__byte)x;
2183 }
2184
2185 switch(x = raw_header[2] >> 4) {
2186 case 0:
2187 if(is_known_fixed_blocksize_stream)
2188 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2189 else
2190 is_unparseable = true;
2191 break;
2192 case 1:
2193 decoder->private_->frame.header.blocksize = 192;
2194 break;
2195 case 2:
2196 case 3:
2197 case 4:
2198 case 5:
2199 decoder->private_->frame.header.blocksize = 576 << (x-2);
2200 break;
2201 case 6:
2202 case 7:
2203 blocksize_hint = x;
2204 break;
2205 case 8:
2206 case 9:
2207 case 10:
2208 case 11:
2209 case 12:
2210 case 13:
2211 case 14:
2212 case 15:
2213 decoder->private_->frame.header.blocksize = 256 << (x-8);
2214 break;
2215 default:
2216 FLAC__ASSERT(0);
2217 break;
2218 }
2219
2220 switch(x = raw_header[2] & 0x0f) {
2221 case 0:
2222 if(decoder->private_->has_stream_info)
2223 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2224 else
2225 is_unparseable = true;
2226 break;
2227 case 1:
2228 case 2:
2229 case 3:
2230 is_unparseable = true;
2231 break;
2232 case 4:
2233 decoder->private_->frame.header.sample_rate = 8000;
2234 break;
2235 case 5:
2236 decoder->private_->frame.header.sample_rate = 16000;
2237 break;
2238 case 6:
2239 decoder->private_->frame.header.sample_rate = 22050;
2240 break;
2241 case 7:
2242 decoder->private_->frame.header.sample_rate = 24000;
2243 break;
2244 case 8:
2245 decoder->private_->frame.header.sample_rate = 32000;
2246 break;
2247 case 9:
2248 decoder->private_->frame.header.sample_rate = 44100;
2249 break;
2250 case 10:
2251 decoder->private_->frame.header.sample_rate = 48000;
2252 break;
2253 case 11:
2254 decoder->private_->frame.header.sample_rate = 96000;
2255 break;
2256 case 12:
2257 case 13:
2258 case 14:
2259 sample_rate_hint = x;
2260 break;
2261 case 15:
2262 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2263 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2264 return true;
2265 default:
2266 FLAC__ASSERT(0);
2267 }
2268
2269 x = (unsigned)(raw_header[3] >> 4);
2270 if(x & 8) {
2271 decoder->private_->frame.header.channels = 2;
2272 switch(x & 7) {
2273 case 0:
2274 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2275 break;
2276 case 1:
2277 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2278 break;
2279 case 2:
2280 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2281 break;
2282 default:
2283 is_unparseable = true;
2284 break;
2285 }
2286 }
2287 else {
2288 decoder->private_->frame.header.channels = (unsigned)x + 1;
2289 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2290 }
2291
2292 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
2293 case 0:
2294 if(decoder->private_->has_stream_info)
2295 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2296 else
2297 is_unparseable = true;
2298 break;
2299 case 1:
2300 decoder->private_->frame.header.bits_per_sample = 8;
2301 break;
2302 case 2:
2303 decoder->private_->frame.header.bits_per_sample = 12;
2304 break;
2305 case 4:
2306 decoder->private_->frame.header.bits_per_sample = 16;
2307 break;
2308 case 5:
2309 decoder->private_->frame.header.bits_per_sample = 20;
2310 break;
2311 case 6:
2312 decoder->private_->frame.header.bits_per_sample = 24;
2313 break;
2314 case 3:
2315 case 7:
2316 is_unparseable = true;
2317 break;
2318 default:
2319 FLAC__ASSERT(0);
2320 break;
2321 }
2322
2323 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
2324 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2325 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2326 return true;
2327 }
2328
2329 /*
2330 * Now we get to the regrettable consequences of not knowing for sure
2331 * whether we got a frame number or a sample number. There are no
2332 * encoders that do variable-blocksize encoding so unless we know from
2333 * the STREAMINFO that it is variable-blocksize we will assume it is
2334 * fixed-blocksize. The trouble comes when we have no STREAMINFO; again
2335 * we will guess that is fixed-blocksize. Where this can go wrong: 1) a
2336 * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize
2337 * stream that was edited such that one or more frames before or
2338 * including this one do not have the same number of samples as the
2339 * STREAMINFO's min and max blocksize.
2340 */
2341 if(is_known_variable_blocksize_stream) {
2342 if(blocksize_hint) {
2343 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2344 return false; /* read_callback_ sets the state for us */
2345 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2346 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2347 decoder->private_->cached = true;
2348 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2349 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2350 return true;
2351 }
2352 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2353 decoder->private_->frame.header.number.sample_number = xx;
2354 }
2355 else
2356 is_unparseable = true;
2357 }
2358 else {
2359 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2360 return false; /* read_callback_ sets the state for us */
2361 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2362 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2363 decoder->private_->cached = true;
2364 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2365 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2366 return true;
2367 }
2368 decoder->private_->last_frame_number = x;
2369 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2370 if(decoder->private_->has_stream_info) {
2371 FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
2372 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2373 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
2374 }
2375 else if(blocksize_hint) {
2376 if(decoder->private_->last_block_size)
2377 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x;
2378 else
2379 is_unparseable = true;
2380 }
2381 else {
2382 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2383 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
2384 }
2385 }
2386
2387 if(blocksize_hint) {
2388 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2389 return false; /* read_callback_ sets the state for us */
2390 raw_header[raw_header_len++] = (FLAC__byte)x;
2391 if(blocksize_hint == 7) {
2392 FLAC__uint32 _x;
2393 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2394 return false; /* read_callback_ sets the state for us */
2395 raw_header[raw_header_len++] = (FLAC__byte)_x;
2396 x = (x << 8) | _x;
2397 }
2398 decoder->private_->frame.header.blocksize = x+1;
2399 }
2400
2401 if(sample_rate_hint) {
2402 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2403 return false; /* read_callback_ sets the state for us */
2404 raw_header[raw_header_len++] = (FLAC__byte)x;
2405 if(sample_rate_hint != 12) {
2406 FLAC__uint32 _x;
2407 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2408 return false; /* read_callback_ sets the state for us */
2409 raw_header[raw_header_len++] = (FLAC__byte)_x;
2410 x = (x << 8) | _x;
2411 }
2412 if(sample_rate_hint == 12)
2413 decoder->private_->frame.header.sample_rate = x*1000;
2414 else if(sample_rate_hint == 13)
2415 decoder->private_->frame.header.sample_rate = x;
2416 else
2417 decoder->private_->frame.header.sample_rate = x*10;
2418 }
2419
2420 /* read the CRC-8 byte */
2421 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2422 return false; /* read_callback_ sets the state for us */
2423 crc8 = (FLAC__byte)x;
2424
2425 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
2426 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2427 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2428 return true;
2429 }
2430
2431 if(is_unparseable) {
2432 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2433 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2434 return true;
2435 }
2436
2437 return true;
2438 }
2439
2440 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2441 {
2442 FLAC__uint32 x;
2443 FLAC__bool wasted_bits;
2444 unsigned i;
2445
2446 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2447 return false; /* read_callback_ sets the state for us */
2448
2449 wasted_bits = (x & 1);
2450 x &= 0xfe;
2451
2452 if(wasted_bits) {
2453 unsigned u;
2454 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2455 return false; /* read_callback_ sets the state for us */
2456 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2457 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2458 }
2459 else
2460 decoder->private_->frame.subframes[channel].wasted_bits = 0;
2461
2462 /*
2463 * Lots of magic numbers here
2464 */
2465 if(x & 0x80) {
2466 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2467 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2468 return true;
2469 }
2470 else if(x == 0) {
2471 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2472 return false;
2473 }
2474 else if(x == 2) {
2475 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2476 return false;
2477 }
2478 else if(x < 16) {
2479 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2480 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2481 return true;
2482 }
2483 else if(x <= 24) {
2484 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
2485 return false;
2486 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2487 return true;
2488 }
2489 else if(x < 64) {
2490 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2491 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2492 return true;
2493 }
2494 else {
2495 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
2496 return false;
2497 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
2498 return true;
2499 }
2500
2501 if(wasted_bits && do_full_decode) {
2502 x = decoder->private_->frame.subframes[channel].wasted_bits;
2503 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2504 decoder->private_->output[channel][i] <<= x;
2505 }
2506
2507 return true;
2508 }
2509
2510 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2511 {
2512 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
2513 FLAC__int32 x;
2514 unsigned i;
2515 FLAC__int32 *output = decoder->private_->output[channel];
2516
2517 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2518
2519 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2520 return false; /* read_callback_ sets the state for us */
2521
2522 subframe->value = x;
2523
2524 /* decode the subframe */
2525 if(do_full_decode) {
2526 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
2527 output[i] = x;
2528 }
2529
2530 return true;
2531 }
2532
2533 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2534 {
2535 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
2536 FLAC__int32 i32;
2537 FLAC__uint32 u32;
2538 unsigned u;
2539
2540 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
2541
2542 subframe->residual = decoder->private_->residual[channel];
2543 subframe->order = order;
2544
2545 /* read warm-up samples */
2546 for(u = 0; u < order; u++) {
2547 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2548 return false; /* read_callback_ sets the state for us */
2549 subframe->warmup[u] = i32;
2550 }
2551
2552 /* read entropy coding method info */
2553 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2554 return false; /* read_callback_ sets the state for us */
2555 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2556 switch(subframe->entropy_coding_method.type) {
2557 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2558 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2559 return false; /* read_callback_ sets the state for us */
2560 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2561 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2562 break;
2563 default:
2564 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2565 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2566 return true;
2567 }
2568
2569 /* read residual */
2570 switch(subframe->entropy_coding_method.type) {
2571 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2572 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]))
2573 return false;
2574 break;
2575 default:
2576 FLAC__ASSERT(0);
2577 }
2578
2579 /* decode the subframe */
2580 if(do_full_decode) {
2581 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2582 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
2583 }
2584
2585 return true;
2586 }
2587
2588 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
2589 {
2590 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
2591 FLAC__int32 i32;
2592 FLAC__uint32 u32;
2593 unsigned u;
2594
2595 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
2596
2597 subframe->residual = decoder->private_->residual[channel];
2598 subframe->order = order;
2599
2600 /* read warm-up samples */
2601 for(u = 0; u < order; u++) {
2602 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
2603 return false; /* read_callback_ sets the state for us */
2604 subframe->warmup[u] = i32;
2605 }
2606
2607 /* read qlp coeff precision */
2608 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
2609 return false; /* read_callback_ sets the state for us */
2610 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
2611 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2612 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2613 return true;
2614 }
2615 subframe->qlp_coeff_precision = u32+1;
2616
2617 /* read qlp shift */
2618 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
2619 return false; /* read_callback_ sets the state for us */
2620 subframe->quantization_level = i32;
2621
2622 /* read quantized lp coefficiencts */
2623 for(u = 0; u < order; u++) {
2624 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
2625 return false; /* read_callback_ sets the state for us */
2626 subframe->qlp_coeff[u] = i32;
2627 }
2628
2629 /* read entropy coding method info */
2630 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
2631 return false; /* read_callback_ sets the state for us */
2632 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2633 switch(subframe->entropy_coding_method.type) {
2634 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2635 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
2636 return false; /* read_callback_ sets the state for us */
2637 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2638 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2639 break;
2640 default:
2641 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2642 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2643 return true;
2644 }
2645
2646 /* read residual */
2647 switch(subframe->entropy_coding_method.type) {
2648 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2649 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]))
2650 return false;
2651 break;
2652 default:
2653 FLAC__ASSERT(0);
2654 }
2655
2656 /* decode the subframe */
2657 if(do_full_decode) {
2658 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2659 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
2660 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
2661 if(order <= 8)
2662 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);
2663 else
2664 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);
2665 }
2666 else
2667 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);
2668 else
2669 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);
2670 }
2671
2672 return true;
2673 }
2674
2675 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
2676 {
2677 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2678 FLAC__int32 x, *residual = decoder->private_->residual[channel];
2679 unsigned i;
2680
2681 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
2682
2683 subframe->data = residual;
2684
2685 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2686 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
2687 return false; /* read_callback_ sets the state for us */
2688 residual[i] = x;
2689 }
2690
2691 /* decode the subframe */
2692 if(do_full_decode)
2693 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
2694
2695 return true;
2696 }
2697
2698 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
2699 {
2700 FLAC__uint32 rice_parameter;
2701 int i;
2702 unsigned partition, sample, u;
2703 const unsigned partitions = 1u << partition_order;
2704 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
2705
2706 /* sanity checks */
2707 if(partition_order == 0) {
2708 if(decoder->private_->frame.header.blocksize < predictor_order) {
2709 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2710 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2711 return true;
2712 }
2713 }
2714 else {
2715 if(partition_samples < predictor_order) {
2716 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2717 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2718 return true;
2719 }
2720 }
2721
2722 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
2723 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2724 return false;
2725 }
2726
2727 sample = 0;
2728 for(partition = 0; partition < partitions; partition++) {
2729 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
2730 return false; /* read_callback_ sets the state for us */
2731 partitioned_rice_contents->parameters[partition] = rice_parameter;
2732 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2733 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2734 if(!FLAC__bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
2735 return false; /* read_callback_ sets the state for us */
2736 sample += u;
2737 }
2738 else {
2739 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
2740 return false; /* read_callback_ sets the state for us */
2741 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2742 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2743 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
2744 return false; /* read_callback_ sets the state for us */
2745 residual[sample] = i;
2746 }
2747 }
2748 }
2749
2750 return true;
2751 }
2752
2753 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2754 {
2755 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2756 FLAC__uint32 zero = 0;
2757 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2758 return false; /* read_callback_ sets the state for us */
2759 if(zero != 0) {
2760 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2761 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2762 }
2763 }
2764 return true;
2765 }
2766
2767 FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
2768 {
2769 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2770
2771 if(
2772 #if FLAC__HAS_OGG
2773 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2774 !decoder->private_->is_ogg &&
2775 #endif
2776 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2777 ) {
2778 *bytes = 0;
2779 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2780 return false;
2781 }
2782 else if(*bytes > 0) {
2783 /* While seeking, it is possible for our seek to land in the
2784 * middle of audio data that looks exactly like a frame header
2785 * from a future version of an encoder. When that happens, our
2786 * error callback will get an
2787 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
2788 * unparseable_frame_count. But there is a remote possibility
2789 * that it is properly synced at such a "future-codec frame",
2790 * so to make sure, we wait to see many "unparseable" errors in
2791 * a row before bailing out.
2792 */
2793 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
2794 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2795 return false;
2796 }
2797 else {
2798 const FLAC__StreamDecoderReadStatus status =
2799 #if FLAC__HAS_OGG
2800 decoder->private_->is_ogg?
2801 read_callback_ogg_aspect_(decoder, buffer, bytes) :
2802 #endif
2803 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
2804 ;
2805 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
2806 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2807 return false;
2808 }
2809 else if(*bytes == 0) {
2810 if(
2811 status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
2812 (
2813 #if FLAC__HAS_OGG
2814 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
2815 !decoder->private_->is_ogg &&
2816 #endif
2817 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
2818 )
2819 ) {
2820 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2821 return false;
2822 }
2823 else
2824 return true;
2825 }
2826 else
2827 return true;
2828 }
2829 }
2830 else {
2831 /* abort to avoid a deadlock */
2832 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2833 return false;
2834 }
2835 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
2836 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
2837 * and at the same time hit the end of the stream (for example, seeking
2838 * to a point that is after the beginning of the last Ogg page). There
2839 * is no way to report an Ogg sync loss through the callbacks (see note
2840 * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
2841 * So to keep the decoder from stopping at this point we gate the call
2842 * to the eof_callback and let the Ogg decoder aspect set the
2843 * end-of-stream state when it is needed.
2844 */
2845 }
2846
2847 #if FLAC__HAS_OGG
2848 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
2849 {
2850 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
2851 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
2852 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2853 /* we don't really have a way to handle lost sync via read
2854 * callback so we'll let it pass and let the underlying
2855 * FLAC decoder catch the error
2856 */
2857 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
2858 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
2859 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
2860 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
2861 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
2862 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
2863 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
2864 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
2865 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
2866 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2867 default:
2868 FLAC__ASSERT(0);
2869 /* double protection */
2870 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
2871 }
2872 }
2873
2874 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
2875 {
2876 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
2877
2878 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
2879 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
2880 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
2881 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
2882 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
2883 case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
2884 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2885 default:
2886 /* double protection: */
2887 FLAC__ASSERT(0);
2888 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
2889 }
2890 }
2891 #endif
2892
2893 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
2894 {
2895 if(decoder->private_->is_seeking) {
2896 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
2897 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
2898 FLAC__uint64 target_sample = decoder->private_->target_sample;
2899
2900 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2901
2902 #if FLAC__HAS_OGG
2903 decoder->private_->got_a_frame = true;
2904 #endif
2905 decoder->private_->last_frame = *frame; /* save the frame */
2906 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
2907 unsigned delta = (unsigned)(target_sample - this_frame_sample);
2908 /* kick out of seek mode */
2909 decoder->private_->is_seeking = false;
2910 /* shift out the samples before target_sample */
2911 if(delta > 0) {
2912 unsigned channel;
2913 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
2914 for(channel = 0; channel < frame->header.channels; channel++)
2915 newbuffer[channel] = buffer[channel] + delta;
2916 decoder->private_->last_frame.header.blocksize -= delta;
2917 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
2918 /* write the relevant samples */
2919 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
2920 }
2921 else {
2922 /* write the relevant samples */
2923 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2924 }
2925 }
2926 else {
2927 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
2928 }
2929 }
2930 else {
2931 /*
2932 * If we never got STREAMINFO, turn off MD5 checking to save
2933 * cycles since we don't have a sum to compare to anyway
2934 */
2935 if(!decoder->private_->has_stream_info)
2936 decoder->private_->do_md5_checking = false;
2937 if(decoder->private_->do_md5_checking) {
2938 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
2939 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
2940 }
2941 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
2942 }
2943 }
2944
2945 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
2946 {
2947 if(!decoder->private_->is_seeking)
2948 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
2949 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
2950 decoder->private_->unparseable_frame_count++;
2951 }
2952
2953 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
2954 {
2955 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
2956 FLAC__int64 pos = -1;
2957 int i;
2958 unsigned approx_bytes_per_frame;
2959 FLAC__bool first_seek = true;
2960 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
2961 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
2962 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
2963 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
2964 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
2965 /* take these from the current frame in case they've changed mid-stream */
2966 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
2967 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
2968 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
2969
2970 /* use values from stream info if we didn't decode a frame */
2971 if(channels == 0)
2972 channels = decoder->private_->stream_info.data.stream_info.channels;
2973 if(bps == 0)
2974 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2975
2976 /* we are just guessing here */
2977 if(max_framesize > 0)
2978 approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
2979
2980 /*
2981 * Check if it's a known fixed-blocksize stream. Note that though
2982 * the spec doesn't allow zeroes in the STREAMINFO block, we may
2983 * never get a STREAMINFO block when decoding so the value of
2984 * min_blocksize might be zero.
2985 */
2986 else if(min_blocksize == max_blocksize && min_blocksize > 0) {
2987 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
2988 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
2989 }
2990 else
2991 approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
2992
2993 /*
2994 * First, we set an upper and lower bound on where in the
2995 * stream we will search. For now we assume the worst case
2996 * scenario, which is our best guess at the beginning of
2997 * the first frame and end of the stream.
2998 */
2999 lower_bound = first_frame_offset;
3000 lower_bound_sample = 0;
3001 upper_bound = stream_length;
3002 upper_bound_sample = total_samples > 0 ? total_samples : target_sample;
3003 if(upper_bound_sample == 0)
3004 upper_bound_sample = 1;
3005
3006 /*
3007 * Now we refine the bounds if we have a seektable with
3008 * suitable points. Note that according to the spec they
3009 * must be ordered by ascending sample number.
3010 */
3011 if(seek_table) {
3012 /* find the closest seek point <= target_sample, if it exists */
3013 for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
3014 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number <= target_sample)
3015 break;
3016 }
3017 if(i >= 0) { /* i.e. we found a suitable seek point... */
3018 lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
3019 lower_bound_sample = seek_table->points[i].sample_number;
3020 }
3021
3022 /* find the closest seek point > target_sample, if it exists */
3023 for(i = 0; i < (int)seek_table->num_points; i++) {
3024 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number > target_sample)
3025 break;
3026 }
3027 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3028 upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3029 upper_bound_sample = seek_table->points[i].sample_number;
3030 }
3031 }
3032
3033 decoder->private_->target_sample = target_sample;
3034 while(1) {
3035 /* check if the bounds are still ok */
3036 if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
3037 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3038 return false;
3039 }
3040 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3041 #if defined _MSC_VER || defined __MINGW32__
3042 /* with VC++ you have to spoon feed it the casting */
3043 pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame;
3044 #else
3045 pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
3046 #endif
3047 #else
3048 /* a little less accurate: */
3049 if(upper_bound - lower_bound < 0xffffffff)
3050 pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
3051 else /* @@@ WATCHOUT, ~2TB limit */
3052 pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame;
3053 #endif
3054 if(pos >= (FLAC__int64)upper_bound)
3055 pos = (FLAC__int64)upper_bound - 1;
3056 if(pos < (FLAC__int64)lower_bound)
3057 pos = (FLAC__int64)lower_bound;
3058 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3059 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3060 return false;
3061 }
3062 if(!FLAC__stream_decoder_flush(decoder)) {
3063 /* above call sets the state for us */
3064 return false;
3065 }
3066 /* Now we need to get a frame. First we need to reset our
3067 * unparseable_frame_count; if we get too many unparseable
3068 * frames in a row, the read callback will return
3069 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3070 * FLAC__stream_decoder_process_single() to return false.
3071 */
3072 decoder->private_->unparseable_frame_count = 0;
3073 if(!FLAC__stream_decoder_process_single(decoder)) {
3074 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3075 return false;
3076 }
3077 /* our write callback will change the state when it gets to the target frame */
3078 /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */
3079 #if 0
3080 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
3081 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
3082 break;
3083 #endif
3084 if(!decoder->private_->is_seeking) {
3085 break;
3086 }
3087 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3088
3089 if (!decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
3090 if (pos == (FLAC__int64)lower_bound) {
3091 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3092 return false;
3093 }
3094 /* our last move backwards wasn't big enough, try again */
3095 approx_bytes_per_frame *= 2;
3096 continue;
3097 }
3098 /* allow one seek over upper bound, required for streams with unknown total_samples */
3099 first_seek = false;
3100
3101 /* make sure we are not seeking in corrupted stream */
3102 if (this_frame_sample < lower_bound_sample) {
3103 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3104 return false;
3105 }
3106
3107 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3108
3109 /* we need to narrow the search */
3110 if(target_sample < this_frame_sample) {
3111 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3112 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3113 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3114 return false;
3115 }
3116 approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
3117 }
3118 else {
3119 /* target_sample >= this_frame_sample + this frame's blocksize */
3120
3121 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3122 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3123 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3124 return false;
3125 }
3126 approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
3127 }
3128 }
3129
3130 return true;
3131 }
3132
3133 #if FLAC__HAS_OGG
3134 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3135 {
3136 FLAC__uint64 left_pos = 0, right_pos = stream_length;
3137 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3138 FLAC__uint64 this_frame_sample = 0; /* only initialized to avoid compiler warning */
3139 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3140 FLAC__bool did_a_seek;
3141 unsigned iteration = 0;
3142
3143 /* In the first iterations, we will calculate the target byte position
3144 * by the distance from the target sample to left_sample and
3145 * right_sample (let's call it "proportional search"). After that, we
3146 * will switch to binary search.
3147 */
3148 unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
3149
3150 /* We will switch to a linear search once our current sample is less
3151 * than this number of samples ahead of the target sample
3152 */
3153 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3154
3155 /* If the total number of samples is unknown, use a large value, and
3156 * force binary search immediately.
3157 */
3158 if(right_sample == 0) {
3159 right_sample = (FLAC__uint64)(-1);
3160 BINARY_SEARCH_AFTER_ITERATION = 0;
3161 }
3162
3163 decoder->private_->target_sample = target_sample;
3164 for( ; ; iteration++) {
3165 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3166 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3167 pos = (right_pos + left_pos) / 2;
3168 }
3169 else {
3170 #ifndef FLAC__INTEGER_ONLY_LIBRARY
3171 #if defined _MSC_VER || defined __MINGW32__
3172 /* with MSVC you have to spoon feed it the casting */
3173 pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos));
3174 #else
3175 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
3176 #endif
3177 #else
3178 /* a little less accurate: */
3179 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
3180 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
3181 else /* @@@ WATCHOUT, ~2TB limit */
3182 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
3183 #endif
3184 /* @@@ TODO: might want to limit pos to some distance
3185 * before EOF, to make sure we land before the last frame,
3186 * thereby getting a this_frame_sample and so having a better
3187 * estimate. @@@@@@DELETE:this would also mostly (or totally if we could
3188 * be sure to land before the last frame) avoid the
3189 * end-of-stream case we have to check later.
3190 */
3191 }
3192
3193 /* physical seek */
3194 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3195 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3196 return false;
3197 }
3198 if(!FLAC__stream_decoder_flush(decoder)) {
3199 /* above call sets the state for us */
3200 return false;
3201 }
3202 did_a_seek = true;
3203 }
3204 else
3205 did_a_seek = false;
3206
3207 decoder->private_->got_a_frame = false;
3208 if(!FLAC__stream_decoder_process_single(decoder)) {
3209 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3210 return false;
3211 }
3212 if(!decoder->private_->got_a_frame) {
3213 if(did_a_seek) {
3214 /* this can happen if we seek to a point after the last frame; we drop
3215 * to binary search right away in this case to avoid any wasted
3216 * iterations of proportional search.
3217 */
3218 right_pos = pos;
3219 BINARY_SEARCH_AFTER_ITERATION = 0;
3220 }
3221 else {
3222 /* this can probably only happen if total_samples is unknown and the
3223 * target_sample is past the end of the stream
3224 */
3225 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3226 return false;
3227 }
3228 }
3229 /* our write callback will change the state when it gets to the target frame */
3230 else if(!decoder->private_->is_seeking/*@@@@@@ && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM*/) {
3231 break;
3232 }
3233 else {
3234 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3235 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3236
3237 if (did_a_seek) {
3238 if (this_frame_sample <= target_sample) {
3239 /* The 'equal' case should not happen, since
3240 * FLAC__stream_decoder_process_single()
3241 * should recognize that it has hit the
3242 * target sample and we would exit through
3243 * the 'break' above.
3244 */
3245 FLAC__ASSERT(this_frame_sample != target_sample);
3246
3247 left_sample = this_frame_sample;
3248 /* sanity check to avoid infinite loop */
3249 if (left_pos == pos) {
3250 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3251 return false;
3252 }
3253 left_pos = pos;
3254 }
3255 else if(this_frame_sample > target_sample) {
3256 right_sample = this_frame_sample;
3257 /* sanity check to avoid infinite loop */
3258 if (right_pos == pos) {
3259 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3260 return false;
3261 }
3262 right_pos = pos;
3263 }
3264 }
3265 }
3266 }
3267
3268 return true;
3269 }
3270 #endif
3271
3272 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3273 {
3274 (void)client_data;
3275
3276 if(*bytes > 0) {
3277 *bytes = vfs_fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
3278 #ifndef USE_VFS
3279 if(ferror(decoder->private_->file))
3280 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3281 else
3282 #endif
3283 if(*bytes == 0)
3284 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3285 else
3286 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3287 }
3288 else
3289 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
3290 }
3291
3292 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
3293 {
3294 (void)client_data;
3295
3296 #ifndef USE_VFS
3297 if(decoder->private_->file == stdin)
3298 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
3299 else
3300 #endif
3301 if(vfs_fseek(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
3302 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
3303 else
3304 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
3305 }
3306
3307 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
3308 {
3309 off_t pos;
3310 (void)client_data;
3311
3312 #ifndef USE_VFS
3313 if(decoder->private_->file == stdin)
3314 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
3315 else
3316 #endif
3317 if((pos = vfs_ftell(decoder->private_->file)) < 0)
3318 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
3319 else {
3320 *absolute_byte_offset = (FLAC__uint64)pos;
3321 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
3322 }
3323 }
3324
3325 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
3326 {
3327 struct stat filestats;
3328 (void)client_data;
3329
3330 #ifndef USE_VFS
3331 if(decoder->private_->file == stdin)
3332 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
3333 else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
3334 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
3335 else {
3336 #endif
3337 *stream_length = (FLAC__uint64)filestats.st_size;
3338 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
3339 #ifndef USE_VFS
3340 }
3341 #endif
3342 }
3343
3344 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
3345 {
3346 (void)client_data;
3347
3348 return vfs_feof(decoder->private_->file)? true : false;
3349 }