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