The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.
Dependencies: GR-PEACH_video R_BSP TLV320_RBSP USBHost_custom
Fork of GR-PEACH_Audio_Playback_Sample by
stream_decoder.c
00001 /* libFLAC - Free Lossless Audio Codec library 00002 * Copyright (C) 2000-2009 Josh Coalson 00003 * Copyright (C) 2011-2014 Xiph.Org Foundation 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 00012 * - Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * - Neither the name of the Xiph.org Foundation nor the names of its 00017 * contributors may be used to endorse or promote products derived from 00018 * this software without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00023 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 00024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifdef HAVE_CONFIG_H 00034 # include <config.h> 00035 #endif 00036 00037 #include <stdio.h> 00038 #include <stdlib.h> /* for malloc() */ 00039 #include <string.h> /* for memset/memcpy() */ 00040 #if(1) /* mbed */ 00041 #else /* not mbed */ 00042 #include <sys/stat.h> /* for stat() */ 00043 #include <sys/types.h> /* for off_t */ 00044 #endif /* end mbed */ 00045 #include "share/compat.h" 00046 #include "FLAC/assert.h" 00047 #include "share/alloc.h" 00048 #include "protected/stream_decoder.h" 00049 #include "private/bitreader.h" 00050 #include "private/bitmath.h" 00051 #include "private/cpu.h" 00052 #include "private/crc.h" 00053 #include "private/fixed.h" 00054 #include "private/format.h" 00055 #include "private/lpc.h" 00056 #include "private/md5.h" 00057 #include "private/memory.h" 00058 #include "private/macros.h" 00059 00060 00061 /* technically this should be in an "export.c" but this is convenient enough */ 00062 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = 00063 #if FLAC__HAS_OGG 00064 1 00065 #else 00066 0 00067 #endif 00068 ; 00069 00070 00071 /*********************************************************************** 00072 * 00073 * Private static data 00074 * 00075 ***********************************************************************/ 00076 00077 static const FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' }; 00078 00079 /*********************************************************************** 00080 * 00081 * Private class method prototypes 00082 * 00083 ***********************************************************************/ 00084 00085 static void set_defaults_(FLAC__StreamDecoder *decoder); 00086 static FILE *get_binary_stdin_(void); 00087 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); 00088 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); 00089 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); 00090 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); 00091 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); 00092 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); 00093 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, unsigned length); 00094 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj); 00095 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj); 00096 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); 00097 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); 00098 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode); 00099 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); 00100 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); 00101 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); 00102 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); 00103 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); 00104 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); 00105 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); 00106 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); 00107 static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data); 00108 #if FLAC__HAS_OGG 00109 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes); 00110 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); 00111 #endif 00112 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]); 00113 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status); 00114 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); 00115 #if FLAC__HAS_OGG 00116 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); 00117 #endif 00118 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); 00119 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); 00120 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); 00121 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); 00122 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data); 00123 00124 /*********************************************************************** 00125 * 00126 * Private class data 00127 * 00128 ***********************************************************************/ 00129 00130 typedef struct FLAC__StreamDecoderPrivate { 00131 #if FLAC__HAS_OGG 00132 FLAC__bool is_ogg; 00133 #endif 00134 FLAC__StreamDecoderReadCallback read_callback; 00135 FLAC__StreamDecoderSeekCallback seek_callback; 00136 FLAC__StreamDecoderTellCallback tell_callback; 00137 FLAC__StreamDecoderLengthCallback length_callback; 00138 FLAC__StreamDecoderEofCallback eof_callback; 00139 FLAC__StreamDecoderWriteCallback write_callback; 00140 FLAC__StreamDecoderMetadataCallback metadata_callback; 00141 FLAC__StreamDecoderErrorCallback error_callback; 00142 /* generic 32-bit datapath: */ 00143 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[]); 00144 /* generic 64-bit datapath: */ 00145 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[]); 00146 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */ 00147 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[]); 00148 void *client_data; 00149 FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */ 00150 FLAC__BitReader *input; 00151 FLAC__int32 *output[FLAC__MAX_CHANNELS]; 00152 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ 00153 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; 00154 unsigned output_capacity, output_channels; 00155 FLAC__uint32 fixed_block_size, next_fixed_block_size; 00156 FLAC__uint64 samples_decoded; 00157 FLAC__bool has_stream_info, has_seek_table; 00158 FLAC__StreamMetadata stream_info; 00159 FLAC__StreamMetadata seek_table; 00160 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */ 00161 FLAC__byte *metadata_filter_ids; 00162 size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */ 00163 FLAC__Frame frame; 00164 FLAC__bool cached; /* true if there is a byte in lookahead */ 00165 FLAC__CPUInfo cpuinfo; 00166 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ 00167 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ 00168 /* unaligned (original) pointers to allocated data */ 00169 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS]; 00170 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 */ 00171 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */ 00172 FLAC__bool is_seeking; 00173 FLAC__MD5Context md5context; 00174 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */ 00175 /* (the rest of these are only used for seeking) */ 00176 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */ 00177 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */ 00178 FLAC__uint64 target_sample; 00179 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */ 00180 #if FLAC__HAS_OGG 00181 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */ 00182 #endif 00183 } FLAC__StreamDecoderPrivate; 00184 00185 /*********************************************************************** 00186 * 00187 * Public static class data 00188 * 00189 ***********************************************************************/ 00190 00191 FLAC_API const char * const FLAC__StreamDecoderStateString[] = { 00192 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", 00193 "FLAC__STREAM_DECODER_READ_METADATA", 00194 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", 00195 "FLAC__STREAM_DECODER_READ_FRAME", 00196 "FLAC__STREAM_DECODER_END_OF_STREAM", 00197 "FLAC__STREAM_DECODER_OGG_ERROR", 00198 "FLAC__STREAM_DECODER_SEEK_ERROR", 00199 "FLAC__STREAM_DECODER_ABORTED", 00200 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", 00201 "FLAC__STREAM_DECODER_UNINITIALIZED" 00202 }; 00203 00204 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = { 00205 "FLAC__STREAM_DECODER_INIT_STATUS_OK", 00206 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER", 00207 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS", 00208 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR", 00209 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE", 00210 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED" 00211 }; 00212 00213 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { 00214 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", 00215 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", 00216 "FLAC__STREAM_DECODER_READ_STATUS_ABORT" 00217 }; 00218 00219 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = { 00220 "FLAC__STREAM_DECODER_SEEK_STATUS_OK", 00221 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR", 00222 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED" 00223 }; 00224 00225 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = { 00226 "FLAC__STREAM_DECODER_TELL_STATUS_OK", 00227 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR", 00228 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED" 00229 }; 00230 00231 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = { 00232 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK", 00233 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR", 00234 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED" 00235 }; 00236 00237 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { 00238 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", 00239 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" 00240 }; 00241 00242 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { 00243 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", 00244 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", 00245 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH", 00246 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM" 00247 }; 00248 00249 /*********************************************************************** 00250 * 00251 * Class constructor/destructor 00252 * 00253 ***********************************************************************/ 00254 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void) 00255 { 00256 FLAC__StreamDecoder *decoder; 00257 unsigned i; 00258 00259 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ 00260 00261 decoder = calloc(1, sizeof(FLAC__StreamDecoder)); 00262 if(decoder == 0) { 00263 return 0; 00264 } 00265 00266 decoder->protected_ = calloc(1, sizeof(FLAC__StreamDecoderProtected)); 00267 if(decoder->protected_ == 0) { 00268 free(decoder); 00269 return 0; 00270 } 00271 00272 decoder->private_ = calloc(1, sizeof(FLAC__StreamDecoderPrivate)); 00273 if(decoder->private_ == 0) { 00274 free(decoder->protected_); 00275 free(decoder); 00276 return 0; 00277 } 00278 00279 decoder->private_->input = FLAC__bitreader_new(); 00280 if(decoder->private_->input == 0) { 00281 free(decoder->private_); 00282 free(decoder->protected_); 00283 free(decoder); 00284 return 0; 00285 } 00286 00287 decoder->private_->metadata_filter_ids_capacity = 16; 00288 if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { 00289 FLAC__bitreader_delete(decoder->private_->input); 00290 free(decoder->private_); 00291 free(decoder->protected_); 00292 free(decoder); 00293 return 0; 00294 } 00295 00296 for(i = 0; i < FLAC__MAX_CHANNELS; i++) { 00297 decoder->private_->output[i] = 0; 00298 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; 00299 } 00300 00301 decoder->private_->output_capacity = 0; 00302 decoder->private_->output_channels = 0; 00303 decoder->private_->has_seek_table = false; 00304 00305 for(i = 0; i < FLAC__MAX_CHANNELS; i++) 00306 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]); 00307 00308 decoder->private_->file = 0; 00309 00310 set_defaults_(decoder); 00311 00312 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; 00313 00314 return decoder; 00315 } 00316 00317 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) 00318 { 00319 unsigned i; 00320 00321 if (decoder == NULL) 00322 return ; 00323 00324 FLAC__ASSERT(0 != decoder->protected_); 00325 FLAC__ASSERT(0 != decoder->private_); 00326 FLAC__ASSERT(0 != decoder->private_->input); 00327 00328 (void)FLAC__stream_decoder_finish(decoder); 00329 00330 if(0 != decoder->private_->metadata_filter_ids) 00331 free(decoder->private_->metadata_filter_ids); 00332 00333 FLAC__bitreader_delete(decoder->private_->input); 00334 00335 for(i = 0; i < FLAC__MAX_CHANNELS; i++) 00336 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]); 00337 00338 free(decoder->private_); 00339 free(decoder->protected_); 00340 free(decoder); 00341 } 00342 00343 /*********************************************************************** 00344 * 00345 * Public class methods 00346 * 00347 ***********************************************************************/ 00348 00349 static FLAC__StreamDecoderInitStatus init_stream_internal_( 00350 FLAC__StreamDecoder *decoder, 00351 FLAC__StreamDecoderReadCallback read_callback, 00352 FLAC__StreamDecoderSeekCallback seek_callback, 00353 FLAC__StreamDecoderTellCallback tell_callback, 00354 FLAC__StreamDecoderLengthCallback length_callback, 00355 FLAC__StreamDecoderEofCallback eof_callback, 00356 FLAC__StreamDecoderWriteCallback write_callback, 00357 FLAC__StreamDecoderMetadataCallback metadata_callback, 00358 FLAC__StreamDecoderErrorCallback error_callback, 00359 void *client_data, 00360 FLAC__bool is_ogg 00361 ) 00362 { 00363 FLAC__ASSERT(0 != decoder); 00364 00365 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00366 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; 00367 00368 #if !FLAC__HAS_OGG 00369 if(is_ogg) 00370 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER; 00371 #endif 00372 00373 if( 00374 0 == read_callback || 00375 0 == write_callback || 00376 0 == error_callback || 00377 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback)) 00378 ) 00379 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; 00380 00381 #if FLAC__HAS_OGG 00382 decoder->private_->is_ogg = is_ogg; 00383 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect)) 00384 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; 00385 #endif 00386 00387 /* 00388 * get the CPU info and set the function pointers 00389 */ 00390 FLAC__cpu_info(&decoder->private_->cpuinfo); 00391 /* first default to the non-asm routines */ 00392 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal; 00393 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide; 00394 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal; 00395 /* now override with asm where appropriate */ 00396 #ifndef FLAC__NO_ASM 00397 if(decoder->private_->cpuinfo.use_asm) { 00398 #ifdef FLAC__CPU_IA32 00399 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); 00400 #ifdef FLAC__HAS_NASM 00401 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide_asm_ia32; /* OPT_IA32: was really necessary for GCC < 4.9 */ 00402 if(decoder->private_->cpuinfo.ia32.mmx) { 00403 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; 00404 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx; 00405 } 00406 else { 00407 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; 00408 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32; 00409 } 00410 #endif 00411 #ifdef FLAC__HAS_X86INTRIN 00412 # if defined FLAC__SSE2_SUPPORTED && !defined FLAC__HAS_NASM /* OPT_SSE: not better than MMX asm */ 00413 if(decoder->private_->cpuinfo.ia32.sse2) { 00414 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_16_intrin_sse2; 00415 } 00416 # endif 00417 # if defined FLAC__SSE4_1_SUPPORTED 00418 if(decoder->private_->cpuinfo.ia32.sse41) { 00419 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide_intrin_sse41; 00420 } 00421 # endif 00422 #endif 00423 #elif defined FLAC__CPU_X86_64 00424 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_X86_64); 00425 /* No useful SSE optimizations yet */ 00426 #endif 00427 } 00428 #endif 00429 00430 /* from here on, errors are fatal */ 00431 00432 if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) { 00433 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 00434 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; 00435 } 00436 00437 decoder->private_->read_callback = read_callback; 00438 decoder->private_->seek_callback = seek_callback; 00439 decoder->private_->tell_callback = tell_callback; 00440 decoder->private_->length_callback = length_callback; 00441 decoder->private_->eof_callback = eof_callback; 00442 decoder->private_->write_callback = write_callback; 00443 decoder->private_->metadata_callback = metadata_callback; 00444 decoder->private_->error_callback = error_callback; 00445 decoder->private_->client_data = client_data; 00446 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; 00447 decoder->private_->samples_decoded = 0; 00448 decoder->private_->has_stream_info = false; 00449 decoder->private_->cached = false; 00450 00451 decoder->private_->do_md5_checking = decoder->protected_->md5_checking; 00452 decoder->private_->is_seeking = false; 00453 00454 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */ 00455 if(!FLAC__stream_decoder_reset(decoder)) { 00456 /* above call sets the state for us */ 00457 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; 00458 } 00459 00460 return FLAC__STREAM_DECODER_INIT_STATUS_OK; 00461 } 00462 00463 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream( 00464 FLAC__StreamDecoder *decoder, 00465 FLAC__StreamDecoderReadCallback read_callback, 00466 FLAC__StreamDecoderSeekCallback seek_callback, 00467 FLAC__StreamDecoderTellCallback tell_callback, 00468 FLAC__StreamDecoderLengthCallback length_callback, 00469 FLAC__StreamDecoderEofCallback eof_callback, 00470 FLAC__StreamDecoderWriteCallback write_callback, 00471 FLAC__StreamDecoderMetadataCallback metadata_callback, 00472 FLAC__StreamDecoderErrorCallback error_callback, 00473 void *client_data 00474 ) 00475 { 00476 return init_stream_internal_( 00477 decoder, 00478 read_callback, 00479 seek_callback, 00480 tell_callback, 00481 length_callback, 00482 eof_callback, 00483 write_callback, 00484 metadata_callback, 00485 error_callback, 00486 client_data, 00487 /*is_ogg=*/false 00488 ); 00489 } 00490 00491 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream( 00492 FLAC__StreamDecoder *decoder, 00493 FLAC__StreamDecoderReadCallback read_callback, 00494 FLAC__StreamDecoderSeekCallback seek_callback, 00495 FLAC__StreamDecoderTellCallback tell_callback, 00496 FLAC__StreamDecoderLengthCallback length_callback, 00497 FLAC__StreamDecoderEofCallback eof_callback, 00498 FLAC__StreamDecoderWriteCallback write_callback, 00499 FLAC__StreamDecoderMetadataCallback metadata_callback, 00500 FLAC__StreamDecoderErrorCallback error_callback, 00501 void *client_data 00502 ) 00503 { 00504 return init_stream_internal_( 00505 decoder, 00506 read_callback, 00507 seek_callback, 00508 tell_callback, 00509 length_callback, 00510 eof_callback, 00511 write_callback, 00512 metadata_callback, 00513 error_callback, 00514 client_data, 00515 /*is_ogg=*/true 00516 ); 00517 } 00518 00519 static FLAC__StreamDecoderInitStatus init_FILE_internal_( 00520 FLAC__StreamDecoder *decoder, 00521 FILE *file, 00522 FLAC__StreamDecoderWriteCallback write_callback, 00523 FLAC__StreamDecoderMetadataCallback metadata_callback, 00524 FLAC__StreamDecoderErrorCallback error_callback, 00525 void *client_data, 00526 FLAC__bool is_ogg 00527 ) 00528 { 00529 FLAC__ASSERT(0 != decoder); 00530 FLAC__ASSERT(0 != file); 00531 00532 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00533 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; 00534 00535 if(0 == write_callback || 0 == error_callback) 00536 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; 00537 00538 /* 00539 * To make sure that our file does not go unclosed after an error, we 00540 * must assign the FILE pointer before any further error can occur in 00541 * this routine. 00542 */ 00543 if(file == stdin) 00544 file = get_binary_stdin_(); /* just to be safe */ 00545 00546 decoder->private_->file = file; 00547 00548 return init_stream_internal_( 00549 decoder, 00550 file_read_callback_, 00551 decoder->private_->file == stdin? 0: file_seek_callback_, 00552 decoder->private_->file == stdin? 0: file_tell_callback_, 00553 decoder->private_->file == stdin? 0: file_length_callback_, 00554 file_eof_callback_, 00555 write_callback, 00556 metadata_callback, 00557 error_callback, 00558 client_data, 00559 is_ogg 00560 ); 00561 } 00562 00563 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( 00564 FLAC__StreamDecoder *decoder, 00565 FILE *file, 00566 FLAC__StreamDecoderWriteCallback write_callback, 00567 FLAC__StreamDecoderMetadataCallback metadata_callback, 00568 FLAC__StreamDecoderErrorCallback error_callback, 00569 void *client_data 00570 ) 00571 { 00572 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); 00573 } 00574 00575 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( 00576 FLAC__StreamDecoder *decoder, 00577 FILE *file, 00578 FLAC__StreamDecoderWriteCallback write_callback, 00579 FLAC__StreamDecoderMetadataCallback metadata_callback, 00580 FLAC__StreamDecoderErrorCallback error_callback, 00581 void *client_data 00582 ) 00583 { 00584 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); 00585 } 00586 00587 static FLAC__StreamDecoderInitStatus init_file_internal_( 00588 FLAC__StreamDecoder *decoder, 00589 const char *filename, 00590 FLAC__StreamDecoderWriteCallback write_callback, 00591 FLAC__StreamDecoderMetadataCallback metadata_callback, 00592 FLAC__StreamDecoderErrorCallback error_callback, 00593 void *client_data, 00594 FLAC__bool is_ogg 00595 ) 00596 { 00597 FILE *file; 00598 00599 FLAC__ASSERT(0 != decoder); 00600 00601 /* 00602 * To make sure that our file does not go unclosed after an error, we 00603 * have to do the same entrance checks here that are later performed 00604 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned. 00605 */ 00606 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00607 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; 00608 00609 if(0 == write_callback || 0 == error_callback) 00610 return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; 00611 00612 file = filename? flac_fopen(filename, "rb") : stdin; 00613 00614 if(0 == file) 00615 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; 00616 00617 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg); 00618 } 00619 00620 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file( 00621 FLAC__StreamDecoder *decoder, 00622 const char *filename, 00623 FLAC__StreamDecoderWriteCallback write_callback, 00624 FLAC__StreamDecoderMetadataCallback metadata_callback, 00625 FLAC__StreamDecoderErrorCallback error_callback, 00626 void *client_data 00627 ) 00628 { 00629 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); 00630 } 00631 00632 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file( 00633 FLAC__StreamDecoder *decoder, 00634 const char *filename, 00635 FLAC__StreamDecoderWriteCallback write_callback, 00636 FLAC__StreamDecoderMetadataCallback metadata_callback, 00637 FLAC__StreamDecoderErrorCallback error_callback, 00638 void *client_data 00639 ) 00640 { 00641 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); 00642 } 00643 00644 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) 00645 { 00646 FLAC__bool md5_failed = false; 00647 unsigned i; 00648 00649 FLAC__ASSERT(0 != decoder); 00650 FLAC__ASSERT(0 != decoder->private_); 00651 FLAC__ASSERT(0 != decoder->protected_); 00652 00653 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) 00654 return true; 00655 00656 /* see the comment in FLAC__stream_decoder_reset() as to why we 00657 * always call FLAC__MD5Final() 00658 */ 00659 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); 00660 00661 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) { 00662 free(decoder->private_->seek_table.data.seek_table.points); 00663 decoder->private_->seek_table.data.seek_table.points = 0; 00664 decoder->private_->has_seek_table = false; 00665 } 00666 FLAC__bitreader_free(decoder->private_->input); 00667 for(i = 0; i < FLAC__MAX_CHANNELS; i++) { 00668 /* WATCHOUT: 00669 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the 00670 * output arrays have a buffer of up to 3 zeroes in front 00671 * (at negative indices) for alignment purposes; we use 4 00672 * to keep the data well-aligned. 00673 */ 00674 if(0 != decoder->private_->output[i]) { 00675 free(decoder->private_->output[i]-4); 00676 decoder->private_->output[i] = 0; 00677 } 00678 if(0 != decoder->private_->residual_unaligned[i]) { 00679 free(decoder->private_->residual_unaligned[i]); 00680 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; 00681 } 00682 } 00683 decoder->private_->output_capacity = 0; 00684 decoder->private_->output_channels = 0; 00685 00686 #if FLAC__HAS_OGG 00687 if(decoder->private_->is_ogg) 00688 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect); 00689 #endif 00690 00691 if(0 != decoder->private_->file) { 00692 if(decoder->private_->file != stdin) 00693 fclose(decoder->private_->file); 00694 decoder->private_->file = 0; 00695 } 00696 00697 if(decoder->private_->do_md5_checking) { 00698 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16)) 00699 md5_failed = true; 00700 } 00701 decoder->private_->is_seeking = false; 00702 00703 set_defaults_(decoder); 00704 00705 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; 00706 00707 return !md5_failed; 00708 } 00709 00710 FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value) 00711 { 00712 FLAC__ASSERT(0 != decoder); 00713 FLAC__ASSERT(0 != decoder->private_); 00714 FLAC__ASSERT(0 != decoder->protected_); 00715 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00716 return false; 00717 #if FLAC__HAS_OGG 00718 /* can't check decoder->private_->is_ogg since that's not set until init time */ 00719 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value); 00720 return true; 00721 #else 00722 (void)value; 00723 return false; 00724 #endif 00725 } 00726 00727 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value) 00728 { 00729 FLAC__ASSERT(0 != decoder); 00730 FLAC__ASSERT(0 != decoder->protected_); 00731 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00732 return false; 00733 decoder->protected_->md5_checking = value; 00734 return true; 00735 } 00736 00737 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) 00738 { 00739 FLAC__ASSERT(0 != decoder); 00740 FLAC__ASSERT(0 != decoder->private_); 00741 FLAC__ASSERT(0 != decoder->protected_); 00742 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); 00743 /* double protection */ 00744 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) 00745 return false; 00746 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00747 return false; 00748 decoder->private_->metadata_filter[type] = true; 00749 if(type == FLAC__METADATA_TYPE_APPLICATION) 00750 decoder->private_->metadata_filter_ids_count = 0; 00751 return true; 00752 } 00753 00754 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) 00755 { 00756 FLAC__ASSERT(0 != decoder); 00757 FLAC__ASSERT(0 != decoder->private_); 00758 FLAC__ASSERT(0 != decoder->protected_); 00759 FLAC__ASSERT(0 != id); 00760 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00761 return false; 00762 00763 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) 00764 return true; 00765 00766 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); 00767 00768 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { 00769 if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) { 00770 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 00771 return false; 00772 } 00773 decoder->private_->metadata_filter_ids_capacity *= 2; 00774 } 00775 00776 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)); 00777 decoder->private_->metadata_filter_ids_count++; 00778 00779 return true; 00780 } 00781 00782 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder) 00783 { 00784 unsigned i; 00785 FLAC__ASSERT(0 != decoder); 00786 FLAC__ASSERT(0 != decoder->private_); 00787 FLAC__ASSERT(0 != decoder->protected_); 00788 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00789 return false; 00790 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) 00791 decoder->private_->metadata_filter[i] = true; 00792 decoder->private_->metadata_filter_ids_count = 0; 00793 return true; 00794 } 00795 00796 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) 00797 { 00798 FLAC__ASSERT(0 != decoder); 00799 FLAC__ASSERT(0 != decoder->private_); 00800 FLAC__ASSERT(0 != decoder->protected_); 00801 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); 00802 /* double protection */ 00803 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) 00804 return false; 00805 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00806 return false; 00807 decoder->private_->metadata_filter[type] = false; 00808 if(type == FLAC__METADATA_TYPE_APPLICATION) 00809 decoder->private_->metadata_filter_ids_count = 0; 00810 return true; 00811 } 00812 00813 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) 00814 { 00815 FLAC__ASSERT(0 != decoder); 00816 FLAC__ASSERT(0 != decoder->private_); 00817 FLAC__ASSERT(0 != decoder->protected_); 00818 FLAC__ASSERT(0 != id); 00819 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00820 return false; 00821 00822 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) 00823 return true; 00824 00825 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); 00826 00827 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { 00828 if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) { 00829 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 00830 return false; 00831 } 00832 decoder->private_->metadata_filter_ids_capacity *= 2; 00833 } 00834 00835 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)); 00836 decoder->private_->metadata_filter_ids_count++; 00837 00838 return true; 00839 } 00840 00841 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder) 00842 { 00843 FLAC__ASSERT(0 != decoder); 00844 FLAC__ASSERT(0 != decoder->private_); 00845 FLAC__ASSERT(0 != decoder->protected_); 00846 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 00847 return false; 00848 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); 00849 decoder->private_->metadata_filter_ids_count = 0; 00850 return true; 00851 } 00852 00853 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder) 00854 { 00855 FLAC__ASSERT(0 != decoder); 00856 FLAC__ASSERT(0 != decoder->protected_); 00857 return decoder->protected_->state; 00858 } 00859 00860 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder) 00861 { 00862 return FLAC__StreamDecoderStateString[decoder->protected_->state]; 00863 } 00864 00865 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder) 00866 { 00867 FLAC__ASSERT(0 != decoder); 00868 FLAC__ASSERT(0 != decoder->protected_); 00869 return decoder->protected_->md5_checking; 00870 } 00871 00872 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder) 00873 { 00874 FLAC__ASSERT(0 != decoder); 00875 FLAC__ASSERT(0 != decoder->protected_); 00876 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0; 00877 } 00878 00879 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) 00880 { 00881 FLAC__ASSERT(0 != decoder); 00882 FLAC__ASSERT(0 != decoder->protected_); 00883 return decoder->protected_->channels; 00884 } 00885 00886 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder) 00887 { 00888 FLAC__ASSERT(0 != decoder); 00889 FLAC__ASSERT(0 != decoder->protected_); 00890 return decoder->protected_->channel_assignment; 00891 } 00892 00893 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder) 00894 { 00895 FLAC__ASSERT(0 != decoder); 00896 FLAC__ASSERT(0 != decoder->protected_); 00897 return decoder->protected_->bits_per_sample; 00898 } 00899 00900 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder) 00901 { 00902 FLAC__ASSERT(0 != decoder); 00903 FLAC__ASSERT(0 != decoder->protected_); 00904 return decoder->protected_->sample_rate; 00905 } 00906 00907 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) 00908 { 00909 FLAC__ASSERT(0 != decoder); 00910 FLAC__ASSERT(0 != decoder->protected_); 00911 return decoder->protected_->blocksize; 00912 } 00913 00914 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position) 00915 { 00916 FLAC__ASSERT(0 != decoder); 00917 FLAC__ASSERT(0 != decoder->private_); 00918 FLAC__ASSERT(0 != position); 00919 00920 #if FLAC__HAS_OGG 00921 if(decoder->private_->is_ogg) 00922 return false; 00923 #endif 00924 if(0 == decoder->private_->tell_callback) 00925 return false; 00926 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) 00927 return false; 00928 /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */ 00929 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) 00930 return false; 00931 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder)); 00932 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder); 00933 return true; 00934 } 00935 00936 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) 00937 { 00938 FLAC__ASSERT(0 != decoder); 00939 FLAC__ASSERT(0 != decoder->private_); 00940 FLAC__ASSERT(0 != decoder->protected_); 00941 00942 decoder->private_->samples_decoded = 0; 00943 decoder->private_->do_md5_checking = false; 00944 00945 #if FLAC__HAS_OGG 00946 if(decoder->private_->is_ogg) 00947 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect); 00948 #endif 00949 00950 if(!FLAC__bitreader_clear(decoder->private_->input)) { 00951 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 00952 return false; 00953 } 00954 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 00955 00956 return true; 00957 } 00958 00959 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) 00960 { 00961 FLAC__ASSERT(0 != decoder); 00962 FLAC__ASSERT(0 != decoder->private_); 00963 FLAC__ASSERT(0 != decoder->protected_); 00964 00965 if(!FLAC__stream_decoder_flush(decoder)) { 00966 /* above call sets the state for us */ 00967 return false; 00968 } 00969 00970 #if FLAC__HAS_OGG 00971 /*@@@ could go in !internal_reset_hack block below */ 00972 if(decoder->private_->is_ogg) 00973 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect); 00974 #endif 00975 00976 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us, 00977 * (internal_reset_hack) don't try to rewind since we are already at 00978 * the beginning of the stream and don't want to fail if the input is 00979 * not seekable. 00980 */ 00981 if(!decoder->private_->internal_reset_hack) { 00982 if(decoder->private_->file == stdin) 00983 return false; /* can't rewind stdin, reset fails */ 00984 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) 00985 return false; /* seekable and seek fails, reset fails */ 00986 } 00987 else 00988 decoder->private_->internal_reset_hack = false; 00989 00990 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; 00991 00992 decoder->private_->has_stream_info = false; 00993 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) { 00994 free(decoder->private_->seek_table.data.seek_table.points); 00995 decoder->private_->seek_table.data.seek_table.points = 0; 00996 decoder->private_->has_seek_table = false; 00997 } 00998 decoder->private_->do_md5_checking = decoder->protected_->md5_checking; 00999 /* 01000 * This goes in reset() and not flush() because according to the spec, a 01001 * fixed-blocksize stream must stay that way through the whole stream. 01002 */ 01003 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; 01004 01005 /* We initialize the FLAC__MD5Context even though we may never use it. This 01006 * is because md5 checking may be turned on to start and then turned off if 01007 * a seek occurs. So we init the context here and finalize it in 01008 * FLAC__stream_decoder_finish() to make sure things are always cleaned up 01009 * properly. 01010 */ 01011 FLAC__MD5Init(&decoder->private_->md5context); 01012 01013 decoder->private_->first_frame_offset = 0; 01014 decoder->private_->unparseable_frame_count = 0; 01015 01016 return true; 01017 } 01018 01019 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) 01020 { 01021 FLAC__bool got_a_frame; 01022 FLAC__ASSERT(0 != decoder); 01023 FLAC__ASSERT(0 != decoder->protected_); 01024 01025 while(1) { 01026 switch(decoder->protected_->state) { 01027 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 01028 if(!find_metadata_(decoder)) 01029 return false; /* above function sets the status for us */ 01030 break; 01031 case FLAC__STREAM_DECODER_READ_METADATA: 01032 if(!read_metadata_(decoder)) 01033 return false; /* above function sets the status for us */ 01034 else 01035 return true; 01036 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 01037 if(!frame_sync_(decoder)) 01038 return true; /* above function sets the status for us */ 01039 break; 01040 case FLAC__STREAM_DECODER_READ_FRAME: 01041 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true)) 01042 return false; /* above function sets the status for us */ 01043 if(got_a_frame) 01044 return true; /* above function sets the status for us */ 01045 break; 01046 case FLAC__STREAM_DECODER_END_OF_STREAM: 01047 case FLAC__STREAM_DECODER_ABORTED: 01048 return true; 01049 default: 01050 FLAC__ASSERT(0); 01051 return false; 01052 } 01053 } 01054 } 01055 01056 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder) 01057 { 01058 FLAC__ASSERT(0 != decoder); 01059 FLAC__ASSERT(0 != decoder->protected_); 01060 01061 while(1) { 01062 switch(decoder->protected_->state) { 01063 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 01064 if(!find_metadata_(decoder)) 01065 return false; /* above function sets the status for us */ 01066 break; 01067 case FLAC__STREAM_DECODER_READ_METADATA: 01068 if(!read_metadata_(decoder)) 01069 return false; /* above function sets the status for us */ 01070 break; 01071 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 01072 case FLAC__STREAM_DECODER_READ_FRAME: 01073 case FLAC__STREAM_DECODER_END_OF_STREAM: 01074 case FLAC__STREAM_DECODER_ABORTED: 01075 return true; 01076 default: 01077 FLAC__ASSERT(0); 01078 return false; 01079 } 01080 } 01081 } 01082 01083 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder) 01084 { 01085 FLAC__bool dummy; 01086 FLAC__ASSERT(0 != decoder); 01087 FLAC__ASSERT(0 != decoder->protected_); 01088 01089 while(1) { 01090 switch(decoder->protected_->state) { 01091 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 01092 if(!find_metadata_(decoder)) 01093 return false; /* above function sets the status for us */ 01094 break; 01095 case FLAC__STREAM_DECODER_READ_METADATA: 01096 if(!read_metadata_(decoder)) 01097 return false; /* above function sets the status for us */ 01098 break; 01099 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 01100 if(!frame_sync_(decoder)) 01101 return true; /* above function sets the status for us */ 01102 break; 01103 case FLAC__STREAM_DECODER_READ_FRAME: 01104 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) 01105 return false; /* above function sets the status for us */ 01106 break; 01107 case FLAC__STREAM_DECODER_END_OF_STREAM: 01108 case FLAC__STREAM_DECODER_ABORTED: 01109 return true; 01110 default: 01111 FLAC__ASSERT(0); 01112 return false; 01113 } 01114 } 01115 } 01116 01117 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder) 01118 { 01119 FLAC__bool got_a_frame; 01120 FLAC__ASSERT(0 != decoder); 01121 FLAC__ASSERT(0 != decoder->protected_); 01122 01123 while(1) { 01124 switch(decoder->protected_->state) { 01125 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 01126 case FLAC__STREAM_DECODER_READ_METADATA: 01127 return false; /* above function sets the status for us */ 01128 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 01129 if(!frame_sync_(decoder)) 01130 return true; /* above function sets the status for us */ 01131 break; 01132 case FLAC__STREAM_DECODER_READ_FRAME: 01133 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false)) 01134 return false; /* above function sets the status for us */ 01135 if(got_a_frame) 01136 return true; /* above function sets the status for us */ 01137 break; 01138 case FLAC__STREAM_DECODER_END_OF_STREAM: 01139 case FLAC__STREAM_DECODER_ABORTED: 01140 return true; 01141 default: 01142 FLAC__ASSERT(0); 01143 return false; 01144 } 01145 } 01146 } 01147 01148 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample) 01149 { 01150 FLAC__uint64 length; 01151 01152 FLAC__ASSERT(0 != decoder); 01153 01154 if( 01155 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && 01156 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA && 01157 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && 01158 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME && 01159 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM 01160 ) 01161 return false; 01162 01163 if(0 == decoder->private_->seek_callback) 01164 return false; 01165 01166 FLAC__ASSERT(decoder->private_->seek_callback); 01167 FLAC__ASSERT(decoder->private_->tell_callback); 01168 FLAC__ASSERT(decoder->private_->length_callback); 01169 FLAC__ASSERT(decoder->private_->eof_callback); 01170 01171 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) 01172 return false; 01173 01174 decoder->private_->is_seeking = true; 01175 01176 /* turn off md5 checking if a seek is attempted */ 01177 decoder->private_->do_md5_checking = false; 01178 01179 /* 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) */ 01180 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) { 01181 decoder->private_->is_seeking = false; 01182 return false; 01183 } 01184 01185 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */ 01186 if( 01187 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA || 01188 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA 01189 ) { 01190 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) { 01191 /* above call sets the state for us */ 01192 decoder->private_->is_seeking = false; 01193 return false; 01194 } 01195 /* check this again in case we didn't know total_samples the first time */ 01196 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) { 01197 decoder->private_->is_seeking = false; 01198 return false; 01199 } 01200 } 01201 01202 { 01203 const FLAC__bool ok = 01204 #if FLAC__HAS_OGG 01205 decoder->private_->is_ogg? 01206 seek_to_absolute_sample_ogg_(decoder, length, sample) : 01207 #endif 01208 seek_to_absolute_sample_(decoder, length, sample) 01209 ; 01210 decoder->private_->is_seeking = false; 01211 return ok; 01212 } 01213 } 01214 01215 /*********************************************************************** 01216 * 01217 * Protected class methods 01218 * 01219 ***********************************************************************/ 01220 01221 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) 01222 { 01223 FLAC__ASSERT(0 != decoder); 01224 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01225 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7)); 01226 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8; 01227 } 01228 01229 /*********************************************************************** 01230 * 01231 * Private class methods 01232 * 01233 ***********************************************************************/ 01234 01235 void set_defaults_(FLAC__StreamDecoder *decoder) 01236 { 01237 #if FLAC__HAS_OGG 01238 decoder->private_->is_ogg = false; 01239 #endif 01240 decoder->private_->read_callback = 0; 01241 decoder->private_->seek_callback = 0; 01242 decoder->private_->tell_callback = 0; 01243 decoder->private_->length_callback = 0; 01244 decoder->private_->eof_callback = 0; 01245 decoder->private_->write_callback = 0; 01246 decoder->private_->metadata_callback = 0; 01247 decoder->private_->error_callback = 0; 01248 decoder->private_->client_data = 0; 01249 01250 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); 01251 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; 01252 decoder->private_->metadata_filter_ids_count = 0; 01253 01254 decoder->protected_->md5_checking = false; 01255 01256 #if FLAC__HAS_OGG 01257 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect); 01258 #endif 01259 } 01260 01261 /* 01262 * This will forcibly set stdin to binary mode (for OSes that require it) 01263 */ 01264 FILE *get_binary_stdin_(void) 01265 { 01266 /* if something breaks here it is probably due to the presence or 01267 * absence of an underscore before the identifiers 'setmode', 01268 * 'fileno', and/or 'O_BINARY'; check your system header files. 01269 */ 01270 #if defined _MSC_VER || defined __MINGW32__ 01271 _setmode(_fileno(stdin), _O_BINARY); 01272 #elif defined __CYGWIN__ 01273 /* almost certainly not needed for any modern Cygwin, but let's be safe... */ 01274 setmode(_fileno(stdin), _O_BINARY); 01275 #elif defined __EMX__ 01276 setmode(fileno(stdin), O_BINARY); 01277 #endif 01278 01279 return stdin; 01280 } 01281 01282 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) 01283 { 01284 unsigned i; 01285 FLAC__int32 *tmp; 01286 01287 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels) 01288 return true; 01289 01290 /* simply using realloc() is not practical because the number of channels may change mid-stream */ 01291 01292 for(i = 0; i < FLAC__MAX_CHANNELS; i++) { 01293 if(0 != decoder->private_->output[i]) { 01294 free(decoder->private_->output[i]-4); 01295 decoder->private_->output[i] = 0; 01296 } 01297 if(0 != decoder->private_->residual_unaligned[i]) { 01298 free(decoder->private_->residual_unaligned[i]); 01299 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; 01300 } 01301 } 01302 01303 for(i = 0; i < channels; i++) { 01304 /* WATCHOUT: 01305 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the 01306 * output arrays have a buffer of up to 3 zeroes in front 01307 * (at negative indices) for alignment purposes; we use 4 01308 * to keep the data well-aligned. 01309 */ 01310 tmp = safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/); 01311 if(tmp == 0) { 01312 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01313 return false; 01314 } 01315 memset(tmp, 0, sizeof(FLAC__int32)*4); 01316 decoder->private_->output[i] = tmp + 4; 01317 01318 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) { 01319 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01320 return false; 01321 } 01322 } 01323 01324 decoder->private_->output_capacity = size; 01325 decoder->private_->output_channels = channels; 01326 01327 return true; 01328 } 01329 01330 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) 01331 { 01332 size_t i; 01333 01334 FLAC__ASSERT(0 != decoder); 01335 FLAC__ASSERT(0 != decoder->private_); 01336 01337 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) 01338 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) 01339 return true; 01340 01341 return false; 01342 } 01343 01344 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) 01345 { 01346 FLAC__uint32 x; 01347 unsigned i, id; 01348 FLAC__bool first = true; 01349 01350 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01351 01352 for(i = id = 0; i < 4; ) { 01353 if(decoder->private_->cached) { 01354 x = (FLAC__uint32)decoder->private_->lookahead; 01355 decoder->private_->cached = false; 01356 } 01357 else { 01358 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 01359 return false; /* read_callback_ sets the state for us */ 01360 } 01361 if(x == FLAC__STREAM_SYNC_STRING[i]) { 01362 first = true; 01363 i++; 01364 id = 0; 01365 continue; 01366 } 01367 01368 if(id >= 3) 01369 return false; 01370 01371 if(x == ID3V2_TAG_[id]) { 01372 id++; 01373 i = 0; 01374 if(id == 3) { 01375 if(!skip_id3v2_tag_(decoder)) 01376 return false; /* skip_id3v2_tag_ sets the state for us */ 01377 } 01378 continue; 01379 } 01380 id = 0; 01381 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 01382 decoder->private_->header_warmup[0] = (FLAC__byte)x; 01383 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 01384 return false; /* read_callback_ sets the state for us */ 01385 01386 /* 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 */ 01387 /* else we have to check if the second byte is the end of a sync code */ 01388 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 01389 decoder->private_->lookahead = (FLAC__byte)x; 01390 decoder->private_->cached = true; 01391 } 01392 else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */ 01393 decoder->private_->header_warmup[1] = (FLAC__byte)x; 01394 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; 01395 return true; 01396 } 01397 } 01398 i = 0; 01399 if(first) { 01400 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 01401 first = false; 01402 } 01403 } 01404 01405 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; 01406 return true; 01407 } 01408 01409 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) 01410 { 01411 FLAC__bool is_last; 01412 FLAC__uint32 i, x, type, length; 01413 01414 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01415 01416 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN)) 01417 return false; /* read_callback_ sets the state for us */ 01418 is_last = x? true : false; 01419 01420 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN)) 01421 return false; /* read_callback_ sets the state for us */ 01422 01423 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN)) 01424 return false; /* read_callback_ sets the state for us */ 01425 01426 if(type == FLAC__METADATA_TYPE_STREAMINFO) { 01427 if(!read_metadata_streaminfo_(decoder, is_last, length)) 01428 return false; 01429 01430 decoder->private_->has_stream_info = true; 01431 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)) 01432 decoder->private_->do_md5_checking = false; 01433 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback) 01434 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data); 01435 } 01436 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { 01437 if(!read_metadata_seektable_(decoder, is_last, length)) 01438 return false; 01439 01440 decoder->private_->has_seek_table = true; 01441 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback) 01442 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data); 01443 } 01444 else { 01445 FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; 01446 unsigned real_length = length; 01447 FLAC__StreamMetadata block; 01448 01449 memset(&block, 0, sizeof(block)); 01450 block.is_last = is_last; 01451 block.type = (FLAC__MetadataType)type; 01452 block.length = length; 01453 01454 if(type == FLAC__METADATA_TYPE_APPLICATION) { 01455 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)) 01456 return false; /* read_callback_ sets the state for us */ 01457 01458 if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */ 01459 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/ 01460 return false; 01461 } 01462 01463 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; 01464 01465 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id)) 01466 skip_it = !skip_it; 01467 } 01468 01469 if(skip_it) { 01470 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) 01471 return false; /* read_callback_ sets the state for us */ 01472 } 01473 else { 01474 FLAC__bool ok = true; 01475 switch(type) { 01476 case FLAC__METADATA_TYPE_PADDING: 01477 /* skip the padding bytes */ 01478 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) 01479 ok = false; /* read_callback_ sets the state for us */ 01480 break; 01481 case FLAC__METADATA_TYPE_APPLICATION: 01482 /* remember, we read the ID already */ 01483 if(real_length > 0) { 01484 if(0 == (block.data.application.data = malloc(real_length))) { 01485 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01486 ok = false; 01487 } 01488 else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length)) 01489 ok = false; /* read_callback_ sets the state for us */ 01490 } 01491 else 01492 block.data.application.data = 0; 01493 break; 01494 case FLAC__METADATA_TYPE_VORBIS_COMMENT: 01495 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment, real_length)) 01496 ok = false; 01497 break; 01498 case FLAC__METADATA_TYPE_CUESHEET: 01499 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet)) 01500 ok = false; 01501 break; 01502 case FLAC__METADATA_TYPE_PICTURE: 01503 if(!read_metadata_picture_(decoder, &block.data.picture)) 01504 ok = false; 01505 break; 01506 case FLAC__METADATA_TYPE_STREAMINFO: 01507 case FLAC__METADATA_TYPE_SEEKTABLE: 01508 FLAC__ASSERT(0); 01509 break; 01510 default: 01511 if(real_length > 0) { 01512 if(0 == (block.data.unknown.data = malloc(real_length))) { 01513 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01514 ok = false; 01515 } 01516 else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length)) 01517 ok = false; /* read_callback_ sets the state for us */ 01518 } 01519 else 01520 block.data.unknown.data = 0; 01521 break; 01522 } 01523 if(ok && !decoder->private_->is_seeking && decoder->private_->metadata_callback) 01524 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); 01525 01526 /* now we have to free any malloc()ed data in the block */ 01527 switch(type) { 01528 case FLAC__METADATA_TYPE_PADDING: 01529 break; 01530 case FLAC__METADATA_TYPE_APPLICATION: 01531 if(0 != block.data.application.data) 01532 free(block.data.application.data); 01533 break; 01534 case FLAC__METADATA_TYPE_VORBIS_COMMENT: 01535 if(0 != block.data.vorbis_comment.vendor_string.entry) 01536 free(block.data.vorbis_comment.vendor_string.entry); 01537 if(block.data.vorbis_comment.num_comments > 0) 01538 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) 01539 if(0 != block.data.vorbis_comment.comments[i].entry) 01540 free(block.data.vorbis_comment.comments[i].entry); 01541 if(0 != block.data.vorbis_comment.comments) 01542 free(block.data.vorbis_comment.comments); 01543 break; 01544 case FLAC__METADATA_TYPE_CUESHEET: 01545 if(block.data.cue_sheet.num_tracks > 0) 01546 for(i = 0; i < block.data.cue_sheet.num_tracks; i++) 01547 if(0 != block.data.cue_sheet.tracks[i].indices) 01548 free(block.data.cue_sheet.tracks[i].indices); 01549 if(0 != block.data.cue_sheet.tracks) 01550 free(block.data.cue_sheet.tracks); 01551 break; 01552 case FLAC__METADATA_TYPE_PICTURE: 01553 if(0 != block.data.picture.mime_type) 01554 free(block.data.picture.mime_type); 01555 if(0 != block.data.picture.description) 01556 free(block.data.picture.description); 01557 if(0 != block.data.picture.data) 01558 free(block.data.picture.data); 01559 break; 01560 case FLAC__METADATA_TYPE_STREAMINFO: 01561 case FLAC__METADATA_TYPE_SEEKTABLE: 01562 FLAC__ASSERT(0); 01563 default: 01564 if(0 != block.data.unknown.data) 01565 free(block.data.unknown.data); 01566 break; 01567 } 01568 01569 if(!ok) /* anything that unsets "ok" should also make sure decoder->protected_->state is updated */ 01570 return false; 01571 } 01572 } 01573 01574 if(is_last) { 01575 /* if this fails, it's OK, it's just a hint for the seek routine */ 01576 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset)) 01577 decoder->private_->first_frame_offset = 0; 01578 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 01579 } 01580 01581 return true; 01582 } 01583 01584 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) 01585 { 01586 FLAC__uint32 x; 01587 unsigned bits, used_bits = 0; 01588 01589 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01590 01591 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO; 01592 decoder->private_->stream_info.is_last = is_last; 01593 decoder->private_->stream_info.length = length; 01594 01595 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; 01596 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits)) 01597 return false; /* read_callback_ sets the state for us */ 01598 decoder->private_->stream_info.data.stream_info.min_blocksize = x; 01599 used_bits += bits; 01600 01601 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; 01602 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN)) 01603 return false; /* read_callback_ sets the state for us */ 01604 decoder->private_->stream_info.data.stream_info.max_blocksize = x; 01605 used_bits += bits; 01606 01607 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; 01608 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) 01609 return false; /* read_callback_ sets the state for us */ 01610 decoder->private_->stream_info.data.stream_info.min_framesize = x; 01611 used_bits += bits; 01612 01613 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; 01614 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) 01615 return false; /* read_callback_ sets the state for us */ 01616 decoder->private_->stream_info.data.stream_info.max_framesize = x; 01617 used_bits += bits; 01618 01619 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; 01620 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN)) 01621 return false; /* read_callback_ sets the state for us */ 01622 decoder->private_->stream_info.data.stream_info.sample_rate = x; 01623 used_bits += bits; 01624 01625 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; 01626 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN)) 01627 return false; /* read_callback_ sets the state for us */ 01628 decoder->private_->stream_info.data.stream_info.channels = x+1; 01629 used_bits += bits; 01630 01631 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; 01632 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN)) 01633 return false; /* read_callback_ sets the state for us */ 01634 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; 01635 used_bits += bits; 01636 01637 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; 01638 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)) 01639 return false; /* read_callback_ sets the state for us */ 01640 used_bits += bits; 01641 01642 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16)) 01643 return false; /* read_callback_ sets the state for us */ 01644 used_bits += 16*8; 01645 01646 /* skip the rest of the block */ 01647 FLAC__ASSERT(used_bits % 8 == 0); 01648 length -= (used_bits / 8); 01649 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) 01650 return false; /* read_callback_ sets the state for us */ 01651 01652 return true; 01653 } 01654 01655 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) 01656 { 01657 FLAC__uint32 i, x; 01658 FLAC__uint64 xx; 01659 01660 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01661 01662 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; 01663 decoder->private_->seek_table.is_last = is_last; 01664 decoder->private_->seek_table.length = length; 01665 01666 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; 01667 01668 /* use realloc since we may pass through here several times (e.g. after seeking) */ 01669 if(0 == (decoder->private_->seek_table.data.seek_table.points = 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)))) { 01670 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01671 return false; 01672 } 01673 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { 01674 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN)) 01675 return false; /* read_callback_ sets the state for us */ 01676 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; 01677 01678 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN)) 01679 return false; /* read_callback_ sets the state for us */ 01680 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; 01681 01682 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN)) 01683 return false; /* read_callback_ sets the state for us */ 01684 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; 01685 } 01686 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); 01687 /* if there is a partial point left, skip over it */ 01688 if(length > 0) { 01689 /*@@@ do a send_error_to_client_() here? there's an argument for either way */ 01690 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) 01691 return false; /* read_callback_ sets the state for us */ 01692 } 01693 01694 return true; 01695 } 01696 01697 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, unsigned length) 01698 { 01699 FLAC__uint32 i; 01700 01701 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01702 01703 /* read vendor string */ 01704 if (length >= 8) { 01705 length -= 8; /* vendor string length + num comments entries alone take 8 bytes */ 01706 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); 01707 if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length)) 01708 return false; /* read_callback_ sets the state for us */ 01709 if (obj->vendor_string.length > 0) { 01710 if (length < obj->vendor_string.length) { 01711 obj->vendor_string.length = 0; 01712 obj->vendor_string.entry = 0; 01713 goto skip; 01714 } 01715 else 01716 length -= obj->vendor_string.length; 01717 if (0 == (obj->vendor_string.entry = safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) { 01718 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01719 return false; 01720 } 01721 if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length)) 01722 return false; /* read_callback_ sets the state for us */ 01723 obj->vendor_string.entry[obj->vendor_string.length] = '\0'; 01724 } 01725 else 01726 obj->vendor_string.entry = 0; 01727 01728 /* read num comments */ 01729 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); 01730 if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments)) 01731 return false; /* read_callback_ sets the state for us */ 01732 01733 /* read comments */ 01734 if (obj->num_comments > 0) { 01735 if (0 == (obj->comments = safe_malloc_mul_2op_p(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { 01736 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01737 return false; 01738 } 01739 for (i = 0; i < obj->num_comments; i++) { 01740 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); 01741 if (length < 4) { 01742 obj->num_comments = i; 01743 goto skip; 01744 } 01745 else 01746 length -= 4; 01747 if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) 01748 return false; /* read_callback_ sets the state for us */ 01749 if (obj->comments[i].length > 0) { 01750 if (length < obj->comments[i].length) { 01751 obj->comments[i].length = 0; 01752 obj->comments[i].entry = 0; 01753 obj->num_comments = i; 01754 goto skip; 01755 } 01756 else 01757 length -= obj->comments[i].length; 01758 if (0 == (obj->comments[i].entry = safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) { 01759 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01760 return false; 01761 } 01762 if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length)) 01763 return false; /* read_callback_ sets the state for us */ 01764 obj->comments[i].entry[obj->comments[i].length] = '\0'; 01765 } 01766 else 01767 obj->comments[i].entry = 0; 01768 } 01769 } 01770 else 01771 obj->comments = 0; 01772 } 01773 01774 skip: 01775 if (length > 0) { 01776 /* This will only happen on files with invalid data in comments */ 01777 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) 01778 return false; /* read_callback_ sets the state for us */ 01779 } 01780 01781 return true; 01782 } 01783 01784 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj) 01785 { 01786 FLAC__uint32 i, j, x; 01787 01788 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01789 01790 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet)); 01791 01792 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); 01793 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)) 01794 return false; /* read_callback_ sets the state for us */ 01795 01796 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN)) 01797 return false; /* read_callback_ sets the state for us */ 01798 01799 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN)) 01800 return false; /* read_callback_ sets the state for us */ 01801 obj->is_cd = x? true : false; 01802 01803 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)) 01804 return false; /* read_callback_ sets the state for us */ 01805 01806 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN)) 01807 return false; /* read_callback_ sets the state for us */ 01808 obj->num_tracks = x; 01809 01810 if(obj->num_tracks > 0) { 01811 if(0 == (obj->tracks = safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) { 01812 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01813 return false; 01814 } 01815 for(i = 0; i < obj->num_tracks; i++) { 01816 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i]; 01817 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN)) 01818 return false; /* read_callback_ sets the state for us */ 01819 01820 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN)) 01821 return false; /* read_callback_ sets the state for us */ 01822 track->number = (FLAC__byte)x; 01823 01824 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); 01825 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8)) 01826 return false; /* read_callback_ sets the state for us */ 01827 01828 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN)) 01829 return false; /* read_callback_ sets the state for us */ 01830 track->type = x; 01831 01832 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN)) 01833 return false; /* read_callback_ sets the state for us */ 01834 track->pre_emphasis = x; 01835 01836 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)) 01837 return false; /* read_callback_ sets the state for us */ 01838 01839 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN)) 01840 return false; /* read_callback_ sets the state for us */ 01841 track->num_indices = (FLAC__byte)x; 01842 01843 if(track->num_indices > 0) { 01844 if(0 == (track->indices = safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) { 01845 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01846 return false; 01847 } 01848 for(j = 0; j < track->num_indices; j++) { 01849 FLAC__StreamMetadata_CueSheet_Index *indx = &track->indices[j]; 01850 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN)) 01851 return false; /* read_callback_ sets the state for us */ 01852 01853 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN)) 01854 return false; /* read_callback_ sets the state for us */ 01855 indx->number = (FLAC__byte)x; 01856 01857 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN)) 01858 return false; /* read_callback_ sets the state for us */ 01859 } 01860 } 01861 } 01862 } 01863 01864 return true; 01865 } 01866 01867 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj) 01868 { 01869 FLAC__uint32 x; 01870 01871 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 01872 01873 /* read type */ 01874 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN)) 01875 return false; /* read_callback_ sets the state for us */ 01876 obj->type = x; 01877 01878 /* read MIME type */ 01879 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) 01880 return false; /* read_callback_ sets the state for us */ 01881 if(0 == (obj->mime_type = safe_malloc_add_2op_(x, /*+*/1))) { 01882 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01883 return false; 01884 } 01885 if(x > 0) { 01886 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x)) 01887 return false; /* read_callback_ sets the state for us */ 01888 } 01889 obj->mime_type[x] = '\0'; 01890 01891 /* read description */ 01892 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) 01893 return false; /* read_callback_ sets the state for us */ 01894 if(0 == (obj->description = safe_malloc_add_2op_(x, /*+*/1))) { 01895 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01896 return false; 01897 } 01898 if(x > 0) { 01899 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x)) 01900 return false; /* read_callback_ sets the state for us */ 01901 } 01902 obj->description[x] = '\0'; 01903 01904 /* read width */ 01905 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN)) 01906 return false; /* read_callback_ sets the state for us */ 01907 01908 /* read height */ 01909 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN)) 01910 return false; /* read_callback_ sets the state for us */ 01911 01912 /* read depth */ 01913 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN)) 01914 return false; /* read_callback_ sets the state for us */ 01915 01916 /* read colors */ 01917 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN)) 01918 return false; /* read_callback_ sets the state for us */ 01919 01920 /* read data */ 01921 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) 01922 return false; /* read_callback_ sets the state for us */ 01923 if(0 == (obj->data = safe_malloc_(obj->data_length))) { 01924 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 01925 return false; 01926 } 01927 if(obj->data_length > 0) { 01928 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length)) 01929 return false; /* read_callback_ sets the state for us */ 01930 } 01931 01932 return true; 01933 } 01934 01935 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) 01936 { 01937 FLAC__uint32 x; 01938 unsigned i, skip; 01939 01940 /* skip the version and flags bytes */ 01941 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24)) 01942 return false; /* read_callback_ sets the state for us */ 01943 /* get the size (in bytes) to skip */ 01944 skip = 0; 01945 for(i = 0; i < 4; i++) { 01946 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 01947 return false; /* read_callback_ sets the state for us */ 01948 skip <<= 7; 01949 skip |= (x & 0x7f); 01950 } 01951 /* skip the rest of the tag */ 01952 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip)) 01953 return false; /* read_callback_ sets the state for us */ 01954 return true; 01955 } 01956 01957 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) 01958 { 01959 FLAC__uint32 x; 01960 FLAC__bool first = true; 01961 01962 /* If we know the total number of samples in the stream, stop if we've read that many. */ 01963 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */ 01964 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) { 01965 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) { 01966 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; 01967 return true; 01968 } 01969 } 01970 01971 /* make sure we're byte aligned */ 01972 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { 01973 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) 01974 return false; /* read_callback_ sets the state for us */ 01975 } 01976 01977 while(1) { 01978 if(decoder->private_->cached) { 01979 x = (FLAC__uint32)decoder->private_->lookahead; 01980 decoder->private_->cached = false; 01981 } 01982 else { 01983 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 01984 return false; /* read_callback_ sets the state for us */ 01985 } 01986 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 01987 decoder->private_->header_warmup[0] = (FLAC__byte)x; 01988 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 01989 return false; /* read_callback_ sets the state for us */ 01990 01991 /* 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 */ 01992 /* else we have to check if the second byte is the end of a sync code */ 01993 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 01994 decoder->private_->lookahead = (FLAC__byte)x; 01995 decoder->private_->cached = true; 01996 } 01997 else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */ 01998 decoder->private_->header_warmup[1] = (FLAC__byte)x; 01999 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; 02000 return true; 02001 } 02002 } 02003 if(first) { 02004 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 02005 first = false; 02006 } 02007 } 02008 02009 return true; 02010 } 02011 02012 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode) 02013 { 02014 unsigned channel; 02015 unsigned i; 02016 FLAC__int32 mid, side; 02017 unsigned frame_crc; /* the one we calculate from the input stream */ 02018 FLAC__uint32 x; 02019 02020 *got_a_frame = false; 02021 02022 /* init the CRC */ 02023 frame_crc = 0; 02024 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); 02025 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); 02026 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc); 02027 02028 if(!read_frame_header_(decoder)) 02029 return false; 02030 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */ 02031 return true; 02032 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels)) 02033 return false; 02034 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { 02035 /* 02036 * first figure the correct bits-per-sample of the subframe 02037 */ 02038 unsigned bps = decoder->private_->frame.header.bits_per_sample; 02039 switch(decoder->private_->frame.header.channel_assignment) { 02040 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: 02041 /* no adjustment needed */ 02042 break; 02043 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: 02044 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 02045 if(channel == 1) 02046 bps++; 02047 break; 02048 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: 02049 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 02050 if(channel == 0) 02051 bps++; 02052 break; 02053 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: 02054 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 02055 if(channel == 1) 02056 bps++; 02057 break; 02058 default: 02059 FLAC__ASSERT(0); 02060 } 02061 /* 02062 * now read it 02063 */ 02064 if(!read_subframe_(decoder, channel, bps, do_full_decode)) 02065 return false; 02066 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ 02067 return true; 02068 } 02069 if(!read_zero_padding_(decoder)) 02070 return false; 02071 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) */ 02072 return true; 02073 02074 /* 02075 * Read the frame CRC-16 from the footer and check 02076 */ 02077 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input); 02078 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN)) 02079 return false; /* read_callback_ sets the state for us */ 02080 if(frame_crc == x) { 02081 if(do_full_decode) { 02082 /* Undo any special channel coding */ 02083 switch(decoder->private_->frame.header.channel_assignment) { 02084 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: 02085 /* do nothing */ 02086 break; 02087 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: 02088 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 02089 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 02090 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; 02091 break; 02092 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: 02093 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 02094 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 02095 decoder->private_->output[0][i] += decoder->private_->output[1][i]; 02096 break; 02097 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: 02098 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 02099 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { 02100 #if 1 02101 mid = decoder->private_->output[0][i]; 02102 side = decoder->private_->output[1][i]; 02103 mid <<= 1; 02104 mid |= (side & 1); /* i.e. if 'side' is odd... */ 02105 decoder->private_->output[0][i] = (mid + side) >> 1; 02106 decoder->private_->output[1][i] = (mid - side) >> 1; 02107 #else 02108 /* OPT: without 'side' temp variable */ 02109 mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */ 02110 decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1; 02111 decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1; 02112 #endif 02113 } 02114 break; 02115 default: 02116 FLAC__ASSERT(0); 02117 break; 02118 } 02119 } 02120 } 02121 else { 02122 /* Bad frame, emit error and zero the output signal */ 02123 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH); 02124 if(do_full_decode) { 02125 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { 02126 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); 02127 } 02128 } 02129 } 02130 02131 *got_a_frame = true; 02132 02133 /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */ 02134 if(decoder->private_->next_fixed_block_size) 02135 decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size; 02136 02137 /* put the latest values into the public section of the decoder instance */ 02138 decoder->protected_->channels = decoder->private_->frame.header.channels; 02139 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; 02140 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; 02141 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; 02142 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; 02143 02144 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); 02145 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize; 02146 02147 /* write it */ 02148 if(do_full_decode) { 02149 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) 02150 return false; 02151 } 02152 02153 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02154 return true; 02155 } 02156 02157 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) 02158 { 02159 FLAC__uint32 x; 02160 FLAC__uint64 xx; 02161 unsigned i, blocksize_hint = 0, sample_rate_hint = 0; 02162 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */ 02163 unsigned raw_header_len; 02164 FLAC__bool is_unparseable = false; 02165 02166 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); 02167 02168 /* init the raw header with the saved bits from synchronization */ 02169 raw_header[0] = decoder->private_->header_warmup[0]; 02170 raw_header[1] = decoder->private_->header_warmup[1]; 02171 raw_header_len = 2; 02172 02173 /* check to make sure that reserved bit is 0 */ 02174 if(raw_header[1] & 0x02) /* MAGIC NUMBER */ 02175 is_unparseable = true; 02176 02177 /* 02178 * Note that along the way as we read the header, we look for a sync 02179 * code inside. If we find one it would indicate that our original 02180 * sync was bad since there cannot be a sync code in a valid header. 02181 * 02182 * Three kinds of things can go wrong when reading the frame header: 02183 * 1) We may have sync'ed incorrectly and not landed on a frame header. 02184 * If we don't find a sync code, it can end up looking like we read 02185 * a valid but unparseable header, until getting to the frame header 02186 * CRC. Even then we could get a false positive on the CRC. 02187 * 2) We may have sync'ed correctly but on an unparseable frame (from a 02188 * future encoder). 02189 * 3) We may be on a damaged frame which appears valid but unparseable. 02190 * 02191 * For all these reasons, we try and read a complete frame header as 02192 * long as it seems valid, even if unparseable, up until the frame 02193 * header CRC. 02194 */ 02195 02196 /* 02197 * read in the raw header as bytes so we can CRC it, and parse it on the way 02198 */ 02199 for(i = 0; i < 2; i++) { 02200 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 02201 return false; /* read_callback_ sets the state for us */ 02202 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 02203 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */ 02204 decoder->private_->lookahead = (FLAC__byte)x; 02205 decoder->private_->cached = true; 02206 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); 02207 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02208 return true; 02209 } 02210 raw_header[raw_header_len++] = (FLAC__byte)x; 02211 } 02212 02213 switch(x = raw_header[2] >> 4) { 02214 case 0: 02215 is_unparseable = true; 02216 break; 02217 case 1: 02218 decoder->private_->frame.header.blocksize = 192; 02219 break; 02220 case 2: 02221 case 3: 02222 case 4: 02223 case 5: 02224 decoder->private_->frame.header.blocksize = 576 << (x-2); 02225 break; 02226 case 6: 02227 case 7: 02228 blocksize_hint = x; 02229 break; 02230 case 8: 02231 case 9: 02232 case 10: 02233 case 11: 02234 case 12: 02235 case 13: 02236 case 14: 02237 case 15: 02238 decoder->private_->frame.header.blocksize = 256 << (x-8); 02239 break; 02240 default: 02241 FLAC__ASSERT(0); 02242 break; 02243 } 02244 02245 switch(x = raw_header[2] & 0x0f) { 02246 case 0: 02247 if(decoder->private_->has_stream_info) 02248 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate; 02249 else 02250 is_unparseable = true; 02251 break; 02252 case 1: 02253 decoder->private_->frame.header.sample_rate = 88200; 02254 break; 02255 case 2: 02256 decoder->private_->frame.header.sample_rate = 176400; 02257 break; 02258 case 3: 02259 decoder->private_->frame.header.sample_rate = 192000; 02260 break; 02261 case 4: 02262 decoder->private_->frame.header.sample_rate = 8000; 02263 break; 02264 case 5: 02265 decoder->private_->frame.header.sample_rate = 16000; 02266 break; 02267 case 6: 02268 decoder->private_->frame.header.sample_rate = 22050; 02269 break; 02270 case 7: 02271 decoder->private_->frame.header.sample_rate = 24000; 02272 break; 02273 case 8: 02274 decoder->private_->frame.header.sample_rate = 32000; 02275 break; 02276 case 9: 02277 decoder->private_->frame.header.sample_rate = 44100; 02278 break; 02279 case 10: 02280 decoder->private_->frame.header.sample_rate = 48000; 02281 break; 02282 case 11: 02283 decoder->private_->frame.header.sample_rate = 96000; 02284 break; 02285 case 12: 02286 case 13: 02287 case 14: 02288 sample_rate_hint = x; 02289 break; 02290 case 15: 02291 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); 02292 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02293 return true; 02294 default: 02295 FLAC__ASSERT(0); 02296 } 02297 02298 x = (unsigned)(raw_header[3] >> 4); 02299 if(x & 8) { 02300 decoder->private_->frame.header.channels = 2; 02301 switch(x & 7) { 02302 case 0: 02303 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; 02304 break; 02305 case 1: 02306 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; 02307 break; 02308 case 2: 02309 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; 02310 break; 02311 default: 02312 is_unparseable = true; 02313 break; 02314 } 02315 } 02316 else { 02317 decoder->private_->frame.header.channels = (unsigned)x + 1; 02318 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; 02319 } 02320 02321 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) { 02322 case 0: 02323 if(decoder->private_->has_stream_info) 02324 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample; 02325 else 02326 is_unparseable = true; 02327 break; 02328 case 1: 02329 decoder->private_->frame.header.bits_per_sample = 8; 02330 break; 02331 case 2: 02332 decoder->private_->frame.header.bits_per_sample = 12; 02333 break; 02334 case 4: 02335 decoder->private_->frame.header.bits_per_sample = 16; 02336 break; 02337 case 5: 02338 decoder->private_->frame.header.bits_per_sample = 20; 02339 break; 02340 case 6: 02341 decoder->private_->frame.header.bits_per_sample = 24; 02342 break; 02343 case 3: 02344 case 7: 02345 is_unparseable = true; 02346 break; 02347 default: 02348 FLAC__ASSERT(0); 02349 break; 02350 } 02351 02352 /* check to make sure that reserved bit is 0 */ 02353 if(raw_header[3] & 0x01) /* MAGIC NUMBER */ 02354 is_unparseable = true; 02355 02356 /* read the frame's starting sample number (or frame number as the case may be) */ 02357 if( 02358 raw_header[1] & 0x01 || 02359 /*@@@ 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 */ 02360 (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize) 02361 ) { /* variable blocksize */ 02362 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len)) 02363 return false; /* read_callback_ sets the state for us */ 02364 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */ 02365 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ 02366 decoder->private_->cached = true; 02367 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); 02368 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02369 return true; 02370 } 02371 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; 02372 decoder->private_->frame.header.number.sample_number = xx; 02373 } 02374 else { /* fixed blocksize */ 02375 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len)) 02376 return false; /* read_callback_ sets the state for us */ 02377 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ 02378 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ 02379 decoder->private_->cached = true; 02380 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); 02381 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02382 return true; 02383 } 02384 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; 02385 decoder->private_->frame.header.number.frame_number = x; 02386 } 02387 02388 if(blocksize_hint) { 02389 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 02390 return false; /* read_callback_ sets the state for us */ 02391 raw_header[raw_header_len++] = (FLAC__byte)x; 02392 if(blocksize_hint == 7) { 02393 FLAC__uint32 _x; 02394 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) 02395 return false; /* read_callback_ sets the state for us */ 02396 raw_header[raw_header_len++] = (FLAC__byte)_x; 02397 x = (x << 8) | _x; 02398 } 02399 decoder->private_->frame.header.blocksize = x+1; 02400 } 02401 02402 if(sample_rate_hint) { 02403 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 02404 return false; /* read_callback_ sets the state for us */ 02405 raw_header[raw_header_len++] = (FLAC__byte)x; 02406 if(sample_rate_hint != 12) { 02407 FLAC__uint32 _x; 02408 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) 02409 return false; /* read_callback_ sets the state for us */ 02410 raw_header[raw_header_len++] = (FLAC__byte)_x; 02411 x = (x << 8) | _x; 02412 } 02413 if(sample_rate_hint == 12) 02414 decoder->private_->frame.header.sample_rate = x*1000; 02415 else if(sample_rate_hint == 13) 02416 decoder->private_->frame.header.sample_rate = x; 02417 else 02418 decoder->private_->frame.header.sample_rate = x*10; 02419 } 02420 02421 /* read the CRC-8 byte */ 02422 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) 02423 return false; /* read_callback_ sets the state for us */ 02424 crc8 = (FLAC__byte)x; 02425 02426 if(FLAC__crc8(raw_header, raw_header_len) != crc8) { 02427 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); 02428 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02429 return true; 02430 } 02431 02432 /* calculate the sample number from the frame number if needed */ 02433 decoder->private_->next_fixed_block_size = 0; 02434 if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) { 02435 x = decoder->private_->frame.header.number.frame_number; 02436 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; 02437 if(decoder->private_->fixed_block_size) 02438 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x; 02439 else if(decoder->private_->has_stream_info) { 02440 if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) { 02441 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x; 02442 decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize; 02443 } 02444 else 02445 is_unparseable = true; 02446 } 02447 else if(x == 0) { 02448 decoder->private_->frame.header.number.sample_number = 0; 02449 decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize; 02450 } 02451 else { 02452 /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */ 02453 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x; 02454 } 02455 } 02456 02457 if(is_unparseable) { 02458 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); 02459 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02460 return true; 02461 } 02462 02463 return true; 02464 } 02465 02466 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) 02467 { 02468 FLAC__uint32 x; 02469 FLAC__bool wasted_bits; 02470 unsigned i; 02471 02472 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */ 02473 return false; /* read_callback_ sets the state for us */ 02474 02475 wasted_bits = (x & 1); 02476 x &= 0xfe; 02477 02478 if(wasted_bits) { 02479 unsigned u; 02480 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u)) 02481 return false; /* read_callback_ sets the state for us */ 02482 decoder->private_->frame.subframes[channel].wasted_bits = u+1; 02483 bps -= decoder->private_->frame.subframes[channel].wasted_bits; 02484 } 02485 else 02486 decoder->private_->frame.subframes[channel].wasted_bits = 0; 02487 02488 /* 02489 * Lots of magic numbers here 02490 */ 02491 if(x & 0x80) { 02492 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 02493 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02494 return true; 02495 } 02496 else if(x == 0) { 02497 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode)) 02498 return false; 02499 } 02500 else if(x == 2) { 02501 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode)) 02502 return false; 02503 } 02504 else if(x < 16) { 02505 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); 02506 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02507 return true; 02508 } 02509 else if(x <= 24) { 02510 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode)) 02511 return false; 02512 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ 02513 return true; 02514 } 02515 else if(x < 64) { 02516 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); 02517 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02518 return true; 02519 } 02520 else { 02521 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode)) 02522 return false; 02523 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ 02524 return true; 02525 } 02526 02527 if(wasted_bits && do_full_decode) { 02528 x = decoder->private_->frame.subframes[channel].wasted_bits; 02529 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 02530 decoder->private_->output[channel][i] <<= x; 02531 } 02532 02533 return true; 02534 } 02535 02536 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) 02537 { 02538 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; 02539 FLAC__int32 x; 02540 unsigned i; 02541 FLAC__int32 *output = decoder->private_->output[channel]; 02542 02543 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; 02544 02545 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) 02546 return false; /* read_callback_ sets the state for us */ 02547 02548 subframe->value = x; 02549 02550 /* decode the subframe */ 02551 if(do_full_decode) { 02552 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 02553 output[i] = x; 02554 } 02555 02556 return true; 02557 } 02558 02559 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) 02560 { 02561 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; 02562 FLAC__int32 i32; 02563 FLAC__uint32 u32; 02564 unsigned u; 02565 02566 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; 02567 02568 subframe->residual = decoder->private_->residual[channel]; 02569 subframe->order = order; 02570 02571 /* read warm-up samples */ 02572 for(u = 0; u < order; u++) { 02573 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps)) 02574 return false; /* read_callback_ sets the state for us */ 02575 subframe->warmup[u] = i32; 02576 } 02577 02578 /* read entropy coding method info */ 02579 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) 02580 return false; /* read_callback_ sets the state for us */ 02581 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; 02582 switch(subframe->entropy_coding_method.type) { 02583 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 02584 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: 02585 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) 02586 return false; /* read_callback_ sets the state for us */ 02587 subframe->entropy_coding_method.data.partitioned_rice.order = u32; 02588 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; 02589 break; 02590 default: 02591 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); 02592 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02593 return true; 02594 } 02595 02596 /* read residual */ 02597 switch(subframe->entropy_coding_method.type) { 02598 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 02599 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: 02600 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)) 02601 return false; 02602 break; 02603 default: 02604 FLAC__ASSERT(0); 02605 } 02606 02607 /* decode the subframe */ 02608 if(do_full_decode) { 02609 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); 02610 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); 02611 } 02612 02613 return true; 02614 } 02615 02616 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) 02617 { 02618 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; 02619 FLAC__int32 i32; 02620 FLAC__uint32 u32; 02621 unsigned u; 02622 02623 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; 02624 02625 subframe->residual = decoder->private_->residual[channel]; 02626 subframe->order = order; 02627 02628 /* read warm-up samples */ 02629 for(u = 0; u < order; u++) { 02630 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps)) 02631 return false; /* read_callback_ sets the state for us */ 02632 subframe->warmup[u] = i32; 02633 } 02634 02635 /* read qlp coeff precision */ 02636 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) 02637 return false; /* read_callback_ sets the state for us */ 02638 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { 02639 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 02640 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02641 return true; 02642 } 02643 subframe->qlp_coeff_precision = u32+1; 02644 02645 /* read qlp shift */ 02646 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN)) 02647 return false; /* read_callback_ sets the state for us */ 02648 subframe->quantization_level = i32; 02649 02650 /* read quantized lp coefficiencts */ 02651 for(u = 0; u < order; u++) { 02652 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision)) 02653 return false; /* read_callback_ sets the state for us */ 02654 subframe->qlp_coeff[u] = i32; 02655 } 02656 02657 /* read entropy coding method info */ 02658 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) 02659 return false; /* read_callback_ sets the state for us */ 02660 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; 02661 switch(subframe->entropy_coding_method.type) { 02662 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 02663 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: 02664 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) 02665 return false; /* read_callback_ sets the state for us */ 02666 subframe->entropy_coding_method.data.partitioned_rice.order = u32; 02667 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; 02668 break; 02669 default: 02670 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); 02671 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02672 return true; 02673 } 02674 02675 /* read residual */ 02676 switch(subframe->entropy_coding_method.type) { 02677 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 02678 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: 02679 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)) 02680 return false; 02681 break; 02682 default: 02683 FLAC__ASSERT(0); 02684 } 02685 02686 /* decode the subframe */ 02687 if(do_full_decode) { 02688 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); 02689 /*@@@@@@ technically not pessimistic enough, should be more like 02690 if( (FLAC__uint64)order * ((((FLAC__uint64)1)<<bps)-1) * ((1<<subframe->qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) ) 02691 */ 02692 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) 02693 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) 02694 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); 02695 else 02696 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); 02697 else 02698 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); 02699 } 02700 02701 return true; 02702 } 02703 02704 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) 02705 { 02706 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; 02707 FLAC__int32 x, *residual = decoder->private_->residual[channel]; 02708 unsigned i; 02709 02710 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; 02711 02712 subframe->data = residual; 02713 02714 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { 02715 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) 02716 return false; /* read_callback_ sets the state for us */ 02717 residual[i] = x; 02718 } 02719 02720 /* decode the subframe */ 02721 if(do_full_decode) 02722 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); 02723 02724 return true; 02725 } 02726 02727 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) 02728 { 02729 FLAC__uint32 rice_parameter; 02730 int i; 02731 unsigned partition, sample, u; 02732 const unsigned partitions = 1u << partition_order; 02733 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order; 02734 const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; 02735 const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; 02736 02737 /* sanity checks */ 02738 if(partition_order == 0) { 02739 if(decoder->private_->frame.header.blocksize < predictor_order) { 02740 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 02741 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02742 /* We have received a potentially malicious bit stream. All we can do is error out to avoid a heap overflow. */ 02743 return false; 02744 } 02745 } 02746 else { 02747 if(partition_samples < predictor_order) { 02748 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 02749 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02750 /* We have received a potentially malicious bit stream. All we can do is error out to avoid a heap overflow. */ 02751 return false; 02752 } 02753 } 02754 02755 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order))) { 02756 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 02757 return false; 02758 } 02759 02760 sample = 0; 02761 for(partition = 0; partition < partitions; partition++) { 02762 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen)) 02763 return false; /* read_callback_ sets the state for us */ 02764 partitioned_rice_contents->parameters[partition] = rice_parameter; 02765 if(rice_parameter < pesc) { 02766 partitioned_rice_contents->raw_bits[partition] = 0; 02767 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order; 02768 if(!FLAC__bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter)) 02769 return false; /* read_callback_ sets the state for us */ 02770 sample += u; 02771 } 02772 else { 02773 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN)) 02774 return false; /* read_callback_ sets the state for us */ 02775 partitioned_rice_contents->raw_bits[partition] = rice_parameter; 02776 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) { 02777 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter)) 02778 return false; /* read_callback_ sets the state for us */ 02779 residual[sample] = i; 02780 } 02781 } 02782 } 02783 02784 return true; 02785 } 02786 02787 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) 02788 { 02789 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { 02790 FLAC__uint32 zero = 0; 02791 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) 02792 return false; /* read_callback_ sets the state for us */ 02793 if(zero != 0) { 02794 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); 02795 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 02796 } 02797 } 02798 return true; 02799 } 02800 02801 FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data) 02802 { 02803 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; 02804 02805 if( 02806 #if FLAC__HAS_OGG 02807 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ 02808 !decoder->private_->is_ogg && 02809 #endif 02810 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) 02811 ) { 02812 *bytes = 0; 02813 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; 02814 return false; 02815 } 02816 else if(*bytes > 0) { 02817 /* While seeking, it is possible for our seek to land in the 02818 * middle of audio data that looks exactly like a frame header 02819 * from a future version of an encoder. When that happens, our 02820 * error callback will get an 02821 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its 02822 * unparseable_frame_count. But there is a remote possibility 02823 * that it is properly synced at such a "future-codec frame", 02824 * so to make sure, we wait to see many "unparseable" errors in 02825 * a row before bailing out. 02826 */ 02827 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) { 02828 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; 02829 return false; 02830 } 02831 else { 02832 const FLAC__StreamDecoderReadStatus status = 02833 #if FLAC__HAS_OGG 02834 decoder->private_->is_ogg? 02835 read_callback_ogg_aspect_(decoder, buffer, bytes) : 02836 #endif 02837 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data) 02838 ; 02839 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) { 02840 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; 02841 return false; 02842 } 02843 else if(*bytes == 0) { 02844 if( 02845 status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM || 02846 ( 02847 #if FLAC__HAS_OGG 02848 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ 02849 !decoder->private_->is_ogg && 02850 #endif 02851 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) 02852 ) 02853 ) { 02854 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; 02855 return false; 02856 } 02857 else 02858 return true; 02859 } 02860 else 02861 return true; 02862 } 02863 } 02864 else { 02865 /* abort to avoid a deadlock */ 02866 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; 02867 return false; 02868 } 02869 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around 02870 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync 02871 * and at the same time hit the end of the stream (for example, seeking 02872 * to a point that is after the beginning of the last Ogg page). There 02873 * is no way to report an Ogg sync loss through the callbacks (see note 02874 * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0. 02875 * So to keep the decoder from stopping at this point we gate the call 02876 * to the eof_callback and let the Ogg decoder aspect set the 02877 * end-of-stream state when it is needed. 02878 */ 02879 } 02880 02881 #if FLAC__HAS_OGG 02882 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes) 02883 { 02884 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) { 02885 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK: 02886 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; 02887 /* we don't really have a way to handle lost sync via read 02888 * callback so we'll let it pass and let the underlying 02889 * FLAC decoder catch the error 02890 */ 02891 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC: 02892 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; 02893 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM: 02894 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; 02895 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC: 02896 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION: 02897 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT: 02898 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR: 02899 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR: 02900 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; 02901 default: 02902 FLAC__ASSERT(0); 02903 /* double protection */ 02904 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; 02905 } 02906 } 02907 02908 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) 02909 { 02910 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder; 02911 02912 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) { 02913 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE: 02914 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK; 02915 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM: 02916 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM; 02917 case FLAC__STREAM_DECODER_READ_STATUS_ABORT: 02918 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; 02919 default: 02920 /* double protection: */ 02921 FLAC__ASSERT(0); 02922 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; 02923 } 02924 } 02925 #endif 02926 02927 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) 02928 { 02929 if(decoder->private_->is_seeking) { 02930 FLAC__uint64 this_frame_sample = frame->header.number.sample_number; 02931 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize; 02932 FLAC__uint64 target_sample = decoder->private_->target_sample; 02933 02934 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); 02935 02936 #if FLAC__HAS_OGG 02937 decoder->private_->got_a_frame = true; 02938 #endif 02939 decoder->private_->last_frame = *frame; /* save the frame */ 02940 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */ 02941 unsigned delta = (unsigned)(target_sample - this_frame_sample); 02942 /* kick out of seek mode */ 02943 decoder->private_->is_seeking = false; 02944 /* shift out the samples before target_sample */ 02945 if(delta > 0) { 02946 unsigned channel; 02947 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS]; 02948 for(channel = 0; channel < frame->header.channels; channel++) 02949 newbuffer[channel] = buffer[channel] + delta; 02950 decoder->private_->last_frame.header.blocksize -= delta; 02951 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta; 02952 /* write the relevant samples */ 02953 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data); 02954 } 02955 else { 02956 /* write the relevant samples */ 02957 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); 02958 } 02959 } 02960 else { 02961 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; 02962 } 02963 } 02964 else { 02965 /* 02966 * If we never got STREAMINFO, turn off MD5 checking to save 02967 * cycles since we don't have a sum to compare to anyway 02968 */ 02969 if(!decoder->private_->has_stream_info) 02970 decoder->private_->do_md5_checking = false; 02971 if(decoder->private_->do_md5_checking) { 02972 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8)) 02973 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; 02974 } 02975 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); 02976 } 02977 } 02978 02979 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status) 02980 { 02981 if(!decoder->private_->is_seeking) 02982 decoder->private_->error_callback(decoder, status, decoder->private_->client_data); 02983 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM) 02984 decoder->private_->unparseable_frame_count++; 02985 } 02986 02987 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) 02988 { 02989 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample; 02990 FLAC__int64 pos = -1; 02991 int i; 02992 unsigned approx_bytes_per_frame; 02993 FLAC__bool first_seek = true; 02994 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder); 02995 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize; 02996 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize; 02997 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize; 02998 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize; 02999 /* take these from the current frame in case they've changed mid-stream */ 03000 unsigned channels = FLAC__stream_decoder_get_channels(decoder); 03001 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder); 03002 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0; 03003 03004 /* use values from stream info if we didn't decode a frame */ 03005 if(channels == 0) 03006 channels = decoder->private_->stream_info.data.stream_info.channels; 03007 if(bps == 0) 03008 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample; 03009 03010 /* we are just guessing here */ 03011 if(max_framesize > 0) 03012 approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1; 03013 /* 03014 * Check if it's a known fixed-blocksize stream. Note that though 03015 * the spec doesn't allow zeroes in the STREAMINFO block, we may 03016 * never get a STREAMINFO block when decoding so the value of 03017 * min_blocksize might be zero. 03018 */ 03019 else if(min_blocksize == max_blocksize && min_blocksize > 0) { 03020 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */ 03021 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64; 03022 } 03023 else 03024 approx_bytes_per_frame = 4096 * channels * bps/8 + 64; 03025 03026 /* 03027 * First, we set an upper and lower bound on where in the 03028 * stream we will search. For now we assume the worst case 03029 * scenario, which is our best guess at the beginning of 03030 * the first frame and end of the stream. 03031 */ 03032 lower_bound = first_frame_offset; 03033 lower_bound_sample = 0; 03034 upper_bound = stream_length; 03035 upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/; 03036 03037 /* 03038 * Now we refine the bounds if we have a seektable with 03039 * suitable points. Note that according to the spec they 03040 * must be ordered by ascending sample number. 03041 * 03042 * Note: to protect against invalid seek tables we will ignore points 03043 * that have frame_samples==0 or sample_number>=total_samples 03044 */ 03045 if(seek_table) { 03046 FLAC__uint64 new_lower_bound = lower_bound; 03047 FLAC__uint64 new_upper_bound = upper_bound; 03048 FLAC__uint64 new_lower_bound_sample = lower_bound_sample; 03049 FLAC__uint64 new_upper_bound_sample = upper_bound_sample; 03050 03051 /* find the closest seek point <= target_sample, if it exists */ 03052 for(i = (int)seek_table->num_points - 1; i >= 0; i--) { 03053 if( 03054 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && 03055 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ 03056 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ 03057 seek_table->points[i].sample_number <= target_sample 03058 ) 03059 break; 03060 } 03061 if(i >= 0) { /* i.e. we found a suitable seek point... */ 03062 new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset; 03063 new_lower_bound_sample = seek_table->points[i].sample_number; 03064 } 03065 03066 /* find the closest seek point > target_sample, if it exists */ 03067 for(i = 0; i < (int)seek_table->num_points; i++) { 03068 if( 03069 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && 03070 seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ 03071 (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ 03072 seek_table->points[i].sample_number > target_sample 03073 ) 03074 break; 03075 } 03076 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */ 03077 new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset; 03078 new_upper_bound_sample = seek_table->points[i].sample_number; 03079 } 03080 /* final protection against unsorted seek tables; keep original values if bogus */ 03081 if(new_upper_bound >= new_lower_bound) { 03082 lower_bound = new_lower_bound; 03083 upper_bound = new_upper_bound; 03084 lower_bound_sample = new_lower_bound_sample; 03085 upper_bound_sample = new_upper_bound_sample; 03086 } 03087 } 03088 03089 FLAC__ASSERT(upper_bound_sample >= lower_bound_sample); 03090 /* there are 2 insidious ways that the following equality occurs, which 03091 * we need to fix: 03092 * 1) total_samples is 0 (unknown) and target_sample is 0 03093 * 2) total_samples is 0 (unknown) and target_sample happens to be 03094 * exactly equal to the last seek point in the seek table; this 03095 * means there is no seek point above it, and upper_bound_samples 03096 * remains equal to the estimate (of target_samples) we made above 03097 * in either case it does not hurt to move upper_bound_sample up by 1 03098 */ 03099 if(upper_bound_sample == lower_bound_sample) 03100 upper_bound_sample++; 03101 03102 decoder->private_->target_sample = target_sample; 03103 while(1) { 03104 /* check if the bounds are still ok */ 03105 if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) { 03106 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03107 return false; 03108 } 03109 #ifndef FLAC__INTEGER_ONLY_LIBRARY 03110 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; 03111 #else 03112 /* a little less accurate: */ 03113 if(upper_bound - lower_bound < 0xffffffff) 03114 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; 03115 else /* @@@ WATCHOUT, ~2TB limit */ 03116 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; 03117 #endif 03118 if(pos >= (FLAC__int64)upper_bound) 03119 pos = (FLAC__int64)upper_bound - 1; 03120 if(pos < (FLAC__int64)lower_bound) 03121 pos = (FLAC__int64)lower_bound; 03122 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { 03123 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03124 return false; 03125 } 03126 if(!FLAC__stream_decoder_flush(decoder)) { 03127 /* above call sets the state for us */ 03128 return false; 03129 } 03130 /* Now we need to get a frame. First we need to reset our 03131 * unparseable_frame_count; if we get too many unparseable 03132 * frames in a row, the read callback will return 03133 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing 03134 * FLAC__stream_decoder_process_single() to return false. 03135 */ 03136 decoder->private_->unparseable_frame_count = 0; 03137 if(!FLAC__stream_decoder_process_single(decoder)) { 03138 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03139 return false; 03140 } 03141 /* our write callback will change the state when it gets to the target frame */ 03142 /* 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 */ 03143 #if 0 03144 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */ 03145 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM) 03146 break; 03147 #endif 03148 if(!decoder->private_->is_seeking) 03149 break; 03150 03151 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); 03152 this_frame_sample = decoder->private_->last_frame.header.number.sample_number; 03153 03154 if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) { 03155 if (pos == (FLAC__int64)lower_bound) { 03156 /* can't move back any more than the first frame, something is fatally wrong */ 03157 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03158 return false; 03159 } 03160 /* our last move backwards wasn't big enough, try again */ 03161 approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16; 03162 continue; 03163 } 03164 /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */ 03165 first_seek = false; 03166 03167 /* make sure we are not seeking in corrupted stream */ 03168 if (this_frame_sample < lower_bound_sample) { 03169 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03170 return false; 03171 } 03172 03173 /* we need to narrow the search */ 03174 if(target_sample < this_frame_sample) { 03175 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; 03176 /*@@@@@@ what will decode position be if at end of stream? */ 03177 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) { 03178 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03179 return false; 03180 } 03181 approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16); 03182 } 03183 else { /* target_sample >= this_frame_sample + this frame's blocksize */ 03184 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; 03185 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) { 03186 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03187 return false; 03188 } 03189 approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16); 03190 } 03191 } 03192 03193 return true; 03194 } 03195 03196 #if FLAC__HAS_OGG 03197 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) 03198 { 03199 FLAC__uint64 left_pos = 0, right_pos = stream_length; 03200 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder); 03201 FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1; 03202 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */ 03203 FLAC__bool did_a_seek; 03204 unsigned iteration = 0; 03205 03206 /* In the first iterations, we will calculate the target byte position 03207 * by the distance from the target sample to left_sample and 03208 * right_sample (let's call it "proportional search"). After that, we 03209 * will switch to binary search. 03210 */ 03211 unsigned BINARY_SEARCH_AFTER_ITERATION = 2; 03212 03213 /* We will switch to a linear search once our current sample is less 03214 * than this number of samples ahead of the target sample 03215 */ 03216 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2; 03217 03218 /* If the total number of samples is unknown, use a large value, and 03219 * force binary search immediately. 03220 */ 03221 if(right_sample == 0) { 03222 right_sample = (FLAC__uint64)(-1); 03223 BINARY_SEARCH_AFTER_ITERATION = 0; 03224 } 03225 03226 decoder->private_->target_sample = target_sample; 03227 for( ; ; iteration++) { 03228 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) { 03229 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) { 03230 pos = (right_pos + left_pos) / 2; 03231 } 03232 else { 03233 #ifndef FLAC__INTEGER_ONLY_LIBRARY 03234 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos)); 03235 #else 03236 /* a little less accurate: */ 03237 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff)) 03238 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample)); 03239 else /* @@@ WATCHOUT, ~2TB limit */ 03240 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16)); 03241 #endif 03242 /* @@@ TODO: might want to limit pos to some distance 03243 * before EOF, to make sure we land before the last frame, 03244 * thereby getting a this_frame_sample and so having a better 03245 * estimate. 03246 */ 03247 } 03248 03249 /* physical seek */ 03250 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { 03251 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03252 return false; 03253 } 03254 if(!FLAC__stream_decoder_flush(decoder)) { 03255 /* above call sets the state for us */ 03256 return false; 03257 } 03258 did_a_seek = true; 03259 } 03260 else 03261 did_a_seek = false; 03262 03263 decoder->private_->got_a_frame = false; 03264 if(!FLAC__stream_decoder_process_single(decoder)) { 03265 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03266 return false; 03267 } 03268 if(!decoder->private_->got_a_frame) { 03269 if(did_a_seek) { 03270 /* this can happen if we seek to a point after the last frame; we drop 03271 * to binary search right away in this case to avoid any wasted 03272 * iterations of proportional search. 03273 */ 03274 right_pos = pos; 03275 BINARY_SEARCH_AFTER_ITERATION = 0; 03276 } 03277 else { 03278 /* this can probably only happen if total_samples is unknown and the 03279 * target_sample is past the end of the stream 03280 */ 03281 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03282 return false; 03283 } 03284 } 03285 /* our write callback will change the state when it gets to the target frame */ 03286 else if(!decoder->private_->is_seeking) { 03287 break; 03288 } 03289 else { 03290 this_frame_sample = decoder->private_->last_frame.header.number.sample_number; 03291 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); 03292 03293 if (did_a_seek) { 03294 if (this_frame_sample <= target_sample) { 03295 /* The 'equal' case should not happen, since 03296 * FLAC__stream_decoder_process_single() 03297 * should recognize that it has hit the 03298 * target sample and we would exit through 03299 * the 'break' above. 03300 */ 03301 FLAC__ASSERT(this_frame_sample != target_sample); 03302 03303 left_sample = this_frame_sample; 03304 /* sanity check to avoid infinite loop */ 03305 if (left_pos == pos) { 03306 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03307 return false; 03308 } 03309 left_pos = pos; 03310 } 03311 else if(this_frame_sample > target_sample) { 03312 right_sample = this_frame_sample; 03313 /* sanity check to avoid infinite loop */ 03314 if (right_pos == pos) { 03315 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; 03316 return false; 03317 } 03318 right_pos = pos; 03319 } 03320 } 03321 } 03322 } 03323 03324 return true; 03325 } 03326 #endif 03327 03328 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) 03329 { 03330 (void)client_data; 03331 03332 if(*bytes > 0) { 03333 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file); 03334 if(ferror(decoder->private_->file)) 03335 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; 03336 else if(*bytes == 0) 03337 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; 03338 else 03339 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; 03340 } 03341 else 03342 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */ 03343 } 03344 03345 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) 03346 { 03347 #if(1) /* mbed */ 03348 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; 03349 #else /* not mbed */ 03350 (void)client_data; 03351 03352 if(decoder->private_->file == stdin) 03353 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; 03354 else if(fseeko(decoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) 03355 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; 03356 else 03357 return FLAC__STREAM_DECODER_SEEK_STATUS_OK; 03358 #endif /* end mbed */ 03359 } 03360 03361 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) 03362 { 03363 #if(1) /* mbed */ 03364 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; 03365 #else /* not mbed */ 03366 FLAC__off_t pos; 03367 (void)client_data; 03368 03369 if(decoder->private_->file == stdin) 03370 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; 03371 else if((pos = ftello(decoder->private_->file)) < 0) 03372 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; 03373 else { 03374 *absolute_byte_offset = (FLAC__uint64)pos; 03375 return FLAC__STREAM_DECODER_TELL_STATUS_OK; 03376 } 03377 #endif /* end mbed */ 03378 } 03379 03380 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) 03381 { 03382 #if(1) /* mbed */ 03383 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; 03384 #else /* not mbed */ 03385 struct flac_stat_s filestats; 03386 (void)client_data; 03387 03388 if(decoder->private_->file == stdin) 03389 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; 03390 else if(flac_fstat(fileno(decoder->private_->file), &filestats) != 0) 03391 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; 03392 else { 03393 *stream_length = (FLAC__uint64)filestats.st_size; 03394 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; 03395 } 03396 #endif /* end mbed */ 03397 } 03398 03399 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data) 03400 { 03401 (void)client_data; 03402 03403 return feof(decoder->private_->file)? true : false; 03404 }
Generated on Tue Jul 12 2022 19:32:30 by 1.7.2