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