| 1 |
/* libFLAC - Free Lossless Audio Codec library |
| 2 |
* Copyright (C) 2000-2009 Josh Coalson |
| 3 |
* Copyright (C) 2011-2025 Xiph.Org Foundation |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
//ppgb #ifdef HAVE_CONFIG_H |
| 34 |
#include "flac_config.h" |
| 35 |
//ppgb #endif |
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> /* for malloc() */ |
| 39 |
#include <string.h> /* for memset/memcpy() */ |
| 40 |
#include <sys/types.h> /* for off_t */ |
| 41 |
#include <sys/stat.h> /* for stat() */ |
| 42 |
#include "flac_share_compat.h" |
| 43 |
#include "flac_FLAC_assert.h" |
| 44 |
#include "flac_share_alloc.h" |
| 45 |
#include "flac_protected_stream_decoder.h" |
| 46 |
#include "flac_private_bitreader.h" |
| 47 |
#include "flac_private_bitmath.h" |
| 48 |
#include "flac_private_cpu.h" |
| 49 |
#include "flac_private_crc.h" |
| 50 |
#include "flac_private_fixed.h" |
| 51 |
#include "flac_private_format.h" |
| 52 |
#include "flac_private_lpc.h" |
| 53 |
#include "flac_private_md5.h" |
| 54 |
#include "flac_private_memory.h" |
| 55 |
#include "flac_private_macros.h" |
| 56 |
|
| 57 |
|
| 58 |
/* technically this should be in an "export.c" but this is convenient enough */ |
| 59 |
FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = FLAC__HAS_OGG; |
| 60 |
|
| 61 |
|
| 62 |
/*********************************************************************** |
| 63 |
* |
| 64 |
* Private static data |
| 65 |
* |
| 66 |
***********************************************************************/ |
| 67 |
|
| 68 |
static const FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' }; |
| 69 |
|
| 70 |
/*********************************************************************** |
| 71 |
* |
| 72 |
* Private class method prototypes |
| 73 |
* |
| 74 |
***********************************************************************/ |
| 75 |
|
| 76 |
static void set_defaults_(FLAC__StreamDecoder *decoder); |
| 77 |
static FILE *get_binary_stdin_(void); |
| 78 |
static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, uint32_t size, uint32_t channels, uint32_t bps); |
| 79 |
static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); |
| 80 |
static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); |
| 81 |
static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); |
| 82 |
static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length); |
| 83 |
static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length); |
| 84 |
static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, uint32_t length); |
| 85 |
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj); |
| 86 |
static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj); |
| 87 |
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); |
| 88 |
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); |
| 89 |
static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode); |
| 90 |
static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); |
| 91 |
static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode); |
| 92 |
static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode); |
| 93 |
static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode); |
| 94 |
static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode); |
| 95 |
static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode); |
| 96 |
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, uint32_t predictor_order, uint32_t partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended); |
| 97 |
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); |
| 98 |
static void undo_channel_coding(FLAC__StreamDecoder *decoder); |
| 99 |
static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 100 |
#if FLAC__HAS_OGG |
| 101 |
static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes); |
| 102 |
static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 103 |
#endif |
| 104 |
static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]); |
| 105 |
static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status); |
| 106 |
static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); |
| 107 |
#if FLAC__HAS_OGG |
| 108 |
static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); |
| 109 |
#endif |
| 110 |
static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 111 |
static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 112 |
static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
| 113 |
static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); |
| 114 |
static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data); |
| 115 |
static void reset_decoder_internal_(FLAC__StreamDecoder* decoder); |
| 116 |
|
| 117 |
/*********************************************************************** |
| 118 |
* |
| 119 |
* Private class data |
| 120 |
* |
| 121 |
***********************************************************************/ |
| 122 |
|
| 123 |
typedef struct FLAC__StreamDecoderPrivate { |
| 124 |
FLAC__bool is_ogg; |
| 125 |
FLAC__StreamDecoderReadCallback read_callback; |
| 126 |
FLAC__StreamDecoderSeekCallback seek_callback; |
| 127 |
FLAC__StreamDecoderTellCallback tell_callback; |
| 128 |
FLAC__StreamDecoderLengthCallback length_callback; |
| 129 |
FLAC__StreamDecoderEofCallback eof_callback; |
| 130 |
FLAC__StreamDecoderWriteCallback write_callback; |
| 131 |
FLAC__StreamDecoderMetadataCallback metadata_callback; |
| 132 |
FLAC__StreamDecoderErrorCallback error_callback; |
| 133 |
void *client_data; |
| 134 |
FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */ |
| 135 |
FLAC__BitReader *input; |
| 136 |
FLAC__int32 *output[FLAC__MAX_CHANNELS]; |
| 137 |
FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ |
| 138 |
FLAC__int64 *side_subframe; |
| 139 |
FLAC__bool side_subframe_in_use; |
| 140 |
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; |
| 141 |
uint32_t output_capacity, output_channels; |
| 142 |
FLAC__uint32 fixed_block_size, next_fixed_block_size; |
| 143 |
FLAC__uint64 samples_decoded; |
| 144 |
FLAC__bool has_stream_info, has_seek_table; |
| 145 |
FLAC__StreamMetadata stream_info; |
| 146 |
FLAC__StreamMetadata seek_table; |
| 147 |
FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */ |
| 148 |
FLAC__byte *metadata_filter_ids; |
| 149 |
size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */ |
| 150 |
FLAC__Frame frame; |
| 151 |
FLAC__bool cached; /* true if there is a byte in lookahead */ |
| 152 |
FLAC__CPUInfo cpuinfo; |
| 153 |
FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ |
| 154 |
FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ |
| 155 |
/* unaligned (original) pointers to allocated data */ |
| 156 |
FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS]; |
| 157 |
FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */ |
| 158 |
FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */ |
| 159 |
FLAC__bool is_seeking; |
| 160 |
FLAC__bool is_indexing; /* to be able to seek in chained streams */ |
| 161 |
FLAC__MD5Context md5context; |
| 162 |
FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */ |
| 163 |
/* (the rest of these are only used for seeking) */ |
| 164 |
FLAC__Frame last_frame; /* holds the info of the last frame we decoded or seeked to */ |
| 165 |
FLAC__bool last_frame_is_set; |
| 166 |
FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */ |
| 167 |
FLAC__uint64 last_seen_framesync; /* if tell callback works, the location of the last seen frame sync code, to rewind to if needed */ |
| 168 |
FLAC__uint64 target_sample; |
| 169 |
uint32_t unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */ |
| 170 |
FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine and find_total_samples to check when process_single() actually writes a frame */ |
| 171 |
FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], uint32_t nvals, uint32_t parameter); |
| 172 |
FLAC__bool error_has_been_sent; /* To check whether a missing frame has been signalled yet */ |
| 173 |
#if FLAC__HAS_OGG |
| 174 |
FLAC__bool ogg_decoder_aspect_allocation_failure; |
| 175 |
#endif |
| 176 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 177 |
uint32_t fuzzing_rewind_count; /* To stop excessive rewinding, as it causes timeouts */ |
| 178 |
#endif |
| 179 |
} FLAC__StreamDecoderPrivate; |
| 180 |
|
| 181 |
/*********************************************************************** |
| 182 |
* |
| 183 |
* Public static class data |
| 184 |
* |
| 185 |
***********************************************************************/ |
| 186 |
|
| 187 |
FLAC_API const char * const FLAC__StreamDecoderStateString[] = { |
| 188 |
"FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", |
| 189 |
"FLAC__STREAM_DECODER_READ_METADATA", |
| 190 |
"FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", |
| 191 |
"FLAC__STREAM_DECODER_READ_FRAME", |
| 192 |
"FLAC__STREAM_DECODER_END_OF_STREAM", |
| 193 |
"FLAC__STREAM_DECODER_OGG_ERROR", |
| 194 |
"FLAC__STREAM_DECODER_SEEK_ERROR", |
| 195 |
"FLAC__STREAM_DECODER_ABORTED", |
| 196 |
"FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", |
| 197 |
"FLAC__STREAM_DECODER_UNINITIALIZED", |
| 198 |
"FLAC__STREAM_DECODER_END_OF_LINK" |
| 199 |
}; |
| 200 |
|
| 201 |
FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = { |
| 202 |
"FLAC__STREAM_DECODER_INIT_STATUS_OK", |
| 203 |
"FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER", |
| 204 |
"FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS", |
| 205 |
"FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR", |
| 206 |
"FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE", |
| 207 |
"FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED" |
| 208 |
}; |
| 209 |
|
| 210 |
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { |
| 211 |
"FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", |
| 212 |
"FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", |
| 213 |
"FLAC__STREAM_DECODER_READ_STATUS_ABORT", |
| 214 |
"FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK" |
| 215 |
}; |
| 216 |
|
| 217 |
FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = { |
| 218 |
"FLAC__STREAM_DECODER_SEEK_STATUS_OK", |
| 219 |
"FLAC__STREAM_DECODER_SEEK_STATUS_ERROR", |
| 220 |
"FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED" |
| 221 |
}; |
| 222 |
|
| 223 |
FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = { |
| 224 |
"FLAC__STREAM_DECODER_TELL_STATUS_OK", |
| 225 |
"FLAC__STREAM_DECODER_TELL_STATUS_ERROR", |
| 226 |
"FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED" |
| 227 |
}; |
| 228 |
|
| 229 |
FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = { |
| 230 |
"FLAC__STREAM_DECODER_LENGTH_STATUS_OK", |
| 231 |
"FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR", |
| 232 |
"FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED" |
| 233 |
}; |
| 234 |
|
| 235 |
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { |
| 236 |
"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", |
| 237 |
"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" |
| 238 |
}; |
| 239 |
|
| 240 |
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { |
| 241 |
"FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", |
| 242 |
"FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", |
| 243 |
"FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH", |
| 244 |
"FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM", |
| 245 |
"FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA", |
| 246 |
"FLAC__STREAM_DECODER_ERROR_STATUS_OUT_OF_BOUNDS", |
| 247 |
"FLAC__STREAM_DECODER_ERROR_STATUS_MISSING_FRAME" |
| 248 |
}; |
| 249 |
|
| 250 |
/*********************************************************************** |
| 251 |
* |
| 252 |
* Class constructor/destructor |
| 253 |
* |
| 254 |
***********************************************************************/ |
| 255 |
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void) |
| 256 |
{ |
| 257 |
FLAC__StreamDecoder *decoder; |
| 258 |
uint32_t i; |
| 259 |
|
| 260 |
FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 261 |
|
| 262 |
decoder = safe_calloc_(1, sizeof(FLAC__StreamDecoder)); |
| 263 |
if(decoder == 0) { |
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
decoder->protected_ = safe_calloc_(1, sizeof(FLAC__StreamDecoderProtected)); |
| 268 |
if(decoder->protected_ == 0) { |
| 269 |
free(decoder); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
decoder->private_ = safe_calloc_(1, sizeof(FLAC__StreamDecoderPrivate)); |
| 274 |
if(decoder->private_ == 0) { |
| 275 |
free(decoder->protected_); |
| 276 |
free(decoder); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
decoder->private_->input = FLAC__bitreader_new(); |
| 281 |
if(decoder->private_->input == 0) { |
| 282 |
free(decoder->private_); |
| 283 |
free(decoder->protected_); |
| 284 |
free(decoder); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
decoder->private_->metadata_filter_ids_capacity = 16; |
| 289 |
if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { |
| 290 |
FLAC__bitreader_delete(decoder->private_->input); |
| 291 |
free(decoder->private_); |
| 292 |
free(decoder->protected_); |
| 293 |
free(decoder); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 298 |
decoder->private_->output[i] = 0; |
| 299 |
decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
| 300 |
} |
| 301 |
|
| 302 |
decoder->private_->side_subframe = 0; |
| 303 |
|
| 304 |
decoder->private_->output_capacity = 0; |
| 305 |
decoder->private_->output_channels = 0; |
| 306 |
decoder->private_->has_seek_table = false; |
| 307 |
|
| 308 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 309 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]); |
| 310 |
|
| 311 |
decoder->private_->file = 0; |
| 312 |
|
| 313 |
set_defaults_(decoder); |
| 314 |
|
| 315 |
decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
| 316 |
|
| 317 |
return decoder; |
| 318 |
} |
| 319 |
|
| 320 |
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) |
| 321 |
{ |
| 322 |
uint32_t i; |
| 323 |
|
| 324 |
if (decoder == NULL) |
| 325 |
return ; |
| 326 |
|
| 327 |
FLAC__ASSERT(0 != decoder->protected_); |
| 328 |
FLAC__ASSERT(0 != decoder->private_); |
| 329 |
FLAC__ASSERT(0 != decoder->private_->input); |
| 330 |
|
| 331 |
(void)FLAC__stream_decoder_finish(decoder); |
| 332 |
|
| 333 |
if(0 != decoder->private_->metadata_filter_ids) |
| 334 |
free(decoder->private_->metadata_filter_ids); |
| 335 |
|
| 336 |
FLAC__bitreader_delete(decoder->private_->input); |
| 337 |
|
| 338 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 339 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]); |
| 340 |
|
| 341 |
free(decoder->private_); |
| 342 |
free(decoder->protected_); |
| 343 |
free(decoder); |
| 344 |
} |
| 345 |
|
| 346 |
/*********************************************************************** |
| 347 |
* |
| 348 |
* Public class methods |
| 349 |
* |
| 350 |
***********************************************************************/ |
| 351 |
|
| 352 |
static FLAC__StreamDecoderInitStatus init_stream_internal_( |
| 353 |
FLAC__StreamDecoder *decoder, |
| 354 |
FLAC__StreamDecoderReadCallback read_callback, |
| 355 |
FLAC__StreamDecoderSeekCallback seek_callback, |
| 356 |
FLAC__StreamDecoderTellCallback tell_callback, |
| 357 |
FLAC__StreamDecoderLengthCallback length_callback, |
| 358 |
FLAC__StreamDecoderEofCallback eof_callback, |
| 359 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 360 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 361 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 362 |
void *client_data, |
| 363 |
FLAC__bool is_ogg |
| 364 |
) |
| 365 |
{ |
| 366 |
FLAC__ASSERT(0 != decoder); |
| 367 |
|
| 368 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 369 |
return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 370 |
|
| 371 |
if(FLAC__HAS_OGG == 0 && is_ogg) |
| 372 |
return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER; |
| 373 |
|
| 374 |
if( |
| 375 |
0 == read_callback || |
| 376 |
0 == write_callback || |
| 377 |
0 == error_callback || |
| 378 |
(seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback)) |
| 379 |
) |
| 380 |
return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; |
| 381 |
|
| 382 |
#if FLAC__HAS_OGG |
| 383 |
decoder->private_->is_ogg = is_ogg; |
| 384 |
if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect)) { |
| 385 |
FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect); |
| 386 |
return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; |
| 387 |
} |
| 388 |
#endif |
| 389 |
|
| 390 |
FLAC__cpu_info(&decoder->private_->cpuinfo); |
| 391 |
decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block; |
| 392 |
|
| 393 |
#ifdef FLAC__BMI2_SUPPORTED |
| 394 |
if (decoder->private_->cpuinfo.x86.bmi2) { |
| 395 |
decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_bmi2; |
| 396 |
} |
| 397 |
#endif |
| 398 |
|
| 399 |
/* from here on, errors are fatal */ |
| 400 |
|
| 401 |
if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) { |
| 402 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 403 |
return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; |
| 404 |
} |
| 405 |
|
| 406 |
decoder->private_->read_callback = read_callback; |
| 407 |
decoder->private_->seek_callback = seek_callback; |
| 408 |
decoder->private_->tell_callback = tell_callback; |
| 409 |
decoder->private_->length_callback = length_callback; |
| 410 |
decoder->private_->eof_callback = eof_callback; |
| 411 |
decoder->private_->write_callback = write_callback; |
| 412 |
decoder->private_->metadata_callback = metadata_callback; |
| 413 |
decoder->private_->error_callback = error_callback; |
| 414 |
decoder->private_->client_data = client_data; |
| 415 |
decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; |
| 416 |
decoder->private_->samples_decoded = 0; |
| 417 |
decoder->private_->has_stream_info = false; |
| 418 |
decoder->private_->cached = false; |
| 419 |
|
| 420 |
decoder->private_->do_md5_checking = decoder->protected_->md5_checking; |
| 421 |
decoder->private_->is_seeking = false; |
| 422 |
decoder->private_->is_indexing = false; |
| 423 |
|
| 424 |
decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */ |
| 425 |
if(!FLAC__stream_decoder_reset(decoder)) { |
| 426 |
/* above call sets the state for us */ |
| 427 |
return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; |
| 428 |
} |
| 429 |
|
| 430 |
return FLAC__STREAM_DECODER_INIT_STATUS_OK; |
| 431 |
} |
| 432 |
|
| 433 |
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream( |
| 434 |
FLAC__StreamDecoder *decoder, |
| 435 |
FLAC__StreamDecoderReadCallback read_callback, |
| 436 |
FLAC__StreamDecoderSeekCallback seek_callback, |
| 437 |
FLAC__StreamDecoderTellCallback tell_callback, |
| 438 |
FLAC__StreamDecoderLengthCallback length_callback, |
| 439 |
FLAC__StreamDecoderEofCallback eof_callback, |
| 440 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 441 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 442 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 443 |
void *client_data |
| 444 |
) |
| 445 |
{ |
| 446 |
return init_stream_internal_( |
| 447 |
decoder, |
| 448 |
read_callback, |
| 449 |
seek_callback, |
| 450 |
tell_callback, |
| 451 |
length_callback, |
| 452 |
eof_callback, |
| 453 |
write_callback, |
| 454 |
metadata_callback, |
| 455 |
error_callback, |
| 456 |
client_data, |
| 457 |
/*is_ogg=*/false |
| 458 |
); |
| 459 |
} |
| 460 |
|
| 461 |
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream( |
| 462 |
FLAC__StreamDecoder *decoder, |
| 463 |
FLAC__StreamDecoderReadCallback read_callback, |
| 464 |
FLAC__StreamDecoderSeekCallback seek_callback, |
| 465 |
FLAC__StreamDecoderTellCallback tell_callback, |
| 466 |
FLAC__StreamDecoderLengthCallback length_callback, |
| 467 |
FLAC__StreamDecoderEofCallback eof_callback, |
| 468 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 469 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 470 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 471 |
void *client_data |
| 472 |
) |
| 473 |
{ |
| 474 |
return init_stream_internal_( |
| 475 |
decoder, |
| 476 |
read_callback, |
| 477 |
seek_callback, |
| 478 |
tell_callback, |
| 479 |
length_callback, |
| 480 |
eof_callback, |
| 481 |
write_callback, |
| 482 |
metadata_callback, |
| 483 |
error_callback, |
| 484 |
client_data, |
| 485 |
/*is_ogg=*/true |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
static FLAC__StreamDecoderInitStatus init_FILE_internal_( |
| 490 |
FLAC__StreamDecoder *decoder, |
| 491 |
FILE *file, |
| 492 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 493 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 494 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 495 |
void *client_data, |
| 496 |
FLAC__bool is_ogg |
| 497 |
) |
| 498 |
{ |
| 499 |
FLAC__ASSERT(0 != decoder); |
| 500 |
FLAC__ASSERT(0 != file); |
| 501 |
|
| 502 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 503 |
return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 504 |
|
| 505 |
if(0 == write_callback || 0 == error_callback) |
| 506 |
return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; |
| 507 |
|
| 508 |
/* |
| 509 |
* To make sure that our file does not go unclosed after an error, we |
| 510 |
* must assign the FILE pointer before any further error can occur in |
| 511 |
* this routine. |
| 512 |
*/ |
| 513 |
if(file == stdin) |
| 514 |
file = get_binary_stdin_(); /* just to be safe */ |
| 515 |
|
| 516 |
decoder->private_->file = file; |
| 517 |
|
| 518 |
return init_stream_internal_( |
| 519 |
decoder, |
| 520 |
file_read_callback_, |
| 521 |
decoder->private_->file == stdin? 0: file_seek_callback_, |
| 522 |
decoder->private_->file == stdin? 0: file_tell_callback_, |
| 523 |
decoder->private_->file == stdin? 0: file_length_callback_, |
| 524 |
file_eof_callback_, |
| 525 |
write_callback, |
| 526 |
metadata_callback, |
| 527 |
error_callback, |
| 528 |
client_data, |
| 529 |
is_ogg |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( |
| 534 |
FLAC__StreamDecoder *decoder, |
| 535 |
FILE *file, |
| 536 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 537 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 538 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 539 |
void *client_data |
| 540 |
) |
| 541 |
{ |
| 542 |
return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); |
| 543 |
} |
| 544 |
|
| 545 |
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( |
| 546 |
FLAC__StreamDecoder *decoder, |
| 547 |
FILE *file, |
| 548 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 549 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 550 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 551 |
void *client_data |
| 552 |
) |
| 553 |
{ |
| 554 |
return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); |
| 555 |
} |
| 556 |
|
| 557 |
static FLAC__StreamDecoderInitStatus init_file_internal_( |
| 558 |
FLAC__StreamDecoder *decoder, |
| 559 |
const char *filename, |
| 560 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 561 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 562 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 563 |
void *client_data, |
| 564 |
FLAC__bool is_ogg |
| 565 |
) |
| 566 |
{ |
| 567 |
FILE *file; |
| 568 |
|
| 569 |
FLAC__ASSERT(0 != decoder); |
| 570 |
|
| 571 |
/* |
| 572 |
* To make sure that our file does not go unclosed after an error, we |
| 573 |
* have to do the same entrance checks here that are later performed |
| 574 |
* in FLAC__stream_decoder_init_FILE() before the FILE* is assigned. |
| 575 |
*/ |
| 576 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 577 |
return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 578 |
|
| 579 |
if(0 == write_callback || 0 == error_callback) |
| 580 |
return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; |
| 581 |
|
| 582 |
file = filename? flac_fopen(filename, "rb") : stdin; |
| 583 |
|
| 584 |
if(0 == file) |
| 585 |
return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; |
| 586 |
|
| 587 |
return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg); |
| 588 |
} |
| 589 |
|
| 590 |
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file( |
| 591 |
FLAC__StreamDecoder *decoder, |
| 592 |
const char *filename, |
| 593 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 594 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 595 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 596 |
void *client_data |
| 597 |
) |
| 598 |
{ |
| 599 |
return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); |
| 600 |
} |
| 601 |
|
| 602 |
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file( |
| 603 |
FLAC__StreamDecoder *decoder, |
| 604 |
const char *filename, |
| 605 |
FLAC__StreamDecoderWriteCallback write_callback, |
| 606 |
FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 607 |
FLAC__StreamDecoderErrorCallback error_callback, |
| 608 |
void *client_data |
| 609 |
) |
| 610 |
{ |
| 611 |
return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); |
| 612 |
} |
| 613 |
|
| 614 |
FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) |
| 615 |
{ |
| 616 |
FLAC__bool md5_failed = false; |
| 617 |
uint32_t i; |
| 618 |
|
| 619 |
FLAC__ASSERT(0 != decoder); |
| 620 |
FLAC__ASSERT(0 != decoder->private_); |
| 621 |
FLAC__ASSERT(0 != decoder->protected_); |
| 622 |
|
| 623 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
| 624 |
return true; |
| 625 |
|
| 626 |
/* see the comment in FLAC__stream_decoder_reset() as to why we |
| 627 |
* always call FLAC__MD5Final() |
| 628 |
*/ |
| 629 |
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); |
| 630 |
|
| 631 |
free(decoder->private_->seek_table.data.seek_table.points); |
| 632 |
decoder->private_->seek_table.data.seek_table.points = 0; |
| 633 |
decoder->private_->has_seek_table = false; |
| 634 |
|
| 635 |
FLAC__bitreader_free(decoder->private_->input); |
| 636 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 637 |
/* WATCHOUT: |
| 638 |
* FLAC__lpc_restore_signal_asm_ia32_mmx() and ..._intrin_sseN() |
| 639 |
* require that the output arrays have a buffer of up to 3 zeroes |
| 640 |
* in front (at negative indices) for alignment purposes; |
| 641 |
* we use 4 to keep the data well-aligned. |
| 642 |
*/ |
| 643 |
if(0 != decoder->private_->output[i]) { |
| 644 |
free(decoder->private_->output[i]-4); |
| 645 |
decoder->private_->output[i] = 0; |
| 646 |
} |
| 647 |
if(0 != decoder->private_->residual_unaligned[i]) { |
| 648 |
free(decoder->private_->residual_unaligned[i]); |
| 649 |
decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
| 650 |
} |
| 651 |
} |
| 652 |
if(0 != decoder->private_->side_subframe) { |
| 653 |
free(decoder->private_->side_subframe); |
| 654 |
decoder->private_->side_subframe = 0; |
| 655 |
} |
| 656 |
decoder->private_->output_capacity = 0; |
| 657 |
decoder->private_->output_channels = 0; |
| 658 |
|
| 659 |
#if FLAC__HAS_OGG |
| 660 |
if(decoder->private_->is_ogg) |
| 661 |
FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect); |
| 662 |
#endif |
| 663 |
|
| 664 |
if(0 != decoder->private_->file) { |
| 665 |
if(decoder->private_->file != stdin) |
| 666 |
fclose(decoder->private_->file); |
| 667 |
decoder->private_->file = 0; |
| 668 |
} |
| 669 |
|
| 670 |
if(decoder->private_->do_md5_checking) { |
| 671 |
if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16)) |
| 672 |
md5_failed = true; |
| 673 |
} |
| 674 |
decoder->private_->is_seeking = false; |
| 675 |
decoder->private_->is_indexing = false; |
| 676 |
|
| 677 |
set_defaults_(decoder); |
| 678 |
|
| 679 |
decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
| 680 |
|
| 681 |
return !md5_failed; |
| 682 |
} |
| 683 |
|
| 684 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value) |
| 685 |
{ |
| 686 |
FLAC__ASSERT(0 != decoder); |
| 687 |
FLAC__ASSERT(0 != decoder->private_); |
| 688 |
FLAC__ASSERT(0 != decoder->protected_); |
| 689 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 690 |
return false; |
| 691 |
#if FLAC__HAS_OGG |
| 692 |
/* can't check decoder->private_->is_ogg since that's not set until init time */ |
| 693 |
FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value); |
| 694 |
return true; |
| 695 |
#else |
| 696 |
(void)value; |
| 697 |
return false; |
| 698 |
#endif |
| 699 |
} |
| 700 |
|
| 701 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_decode_chained_stream(FLAC__StreamDecoder* decoder, FLAC__bool value) |
| 702 |
{ |
| 703 |
FLAC__ASSERT(0 != decoder); |
| 704 |
FLAC__ASSERT(0 != decoder->protected_); |
| 705 |
if (decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 706 |
return false; |
| 707 |
#if FLAC__HAS_OGG |
| 708 |
FLAC__ogg_decoder_aspect_set_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect, value); |
| 709 |
return true; |
| 710 |
#else |
| 711 |
(void)value; |
| 712 |
return false; |
| 713 |
#endif |
| 714 |
} |
| 715 |
|
| 716 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value) |
| 717 |
{ |
| 718 |
FLAC__ASSERT(0 != decoder); |
| 719 |
FLAC__ASSERT(0 != decoder->protected_); |
| 720 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 721 |
return false; |
| 722 |
decoder->protected_->md5_checking = value; |
| 723 |
return true; |
| 724 |
} |
| 725 |
|
| 726 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) |
| 727 |
{ |
| 728 |
FLAC__ASSERT(0 != decoder); |
| 729 |
FLAC__ASSERT(0 != decoder->private_); |
| 730 |
FLAC__ASSERT(0 != decoder->protected_); |
| 731 |
FLAC__ASSERT((uint32_t)type <= FLAC__MAX_METADATA_TYPE_CODE); |
| 732 |
/* double protection */ |
| 733 |
if((uint32_t)type > FLAC__MAX_METADATA_TYPE_CODE) |
| 734 |
return false; |
| 735 |
decoder->private_->metadata_filter[type] = true; |
| 736 |
if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 737 |
decoder->private_->metadata_filter_ids_count = 0; |
| 738 |
return true; |
| 739 |
} |
| 740 |
|
| 741 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) |
| 742 |
{ |
| 743 |
FLAC__ASSERT(0 != decoder); |
| 744 |
FLAC__ASSERT(0 != decoder->private_); |
| 745 |
FLAC__ASSERT(0 != decoder->protected_); |
| 746 |
FLAC__ASSERT(0 != id); |
| 747 |
|
| 748 |
if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 749 |
return true; |
| 750 |
|
| 751 |
FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 752 |
|
| 753 |
if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
| 754 |
if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) { |
| 755 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 756 |
return false; |
| 757 |
} |
| 758 |
decoder->private_->metadata_filter_ids_capacity *= 2; |
| 759 |
} |
| 760 |
|
| 761 |
memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); |
| 762 |
decoder->private_->metadata_filter_ids_count++; |
| 763 |
|
| 764 |
return true; |
| 765 |
} |
| 766 |
|
| 767 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder) |
| 768 |
{ |
| 769 |
uint32_t i; |
| 770 |
FLAC__ASSERT(0 != decoder); |
| 771 |
FLAC__ASSERT(0 != decoder->private_); |
| 772 |
FLAC__ASSERT(0 != decoder->protected_); |
| 773 |
for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) |
| 774 |
decoder->private_->metadata_filter[i] = true; |
| 775 |
decoder->private_->metadata_filter_ids_count = 0; |
| 776 |
return true; |
| 777 |
} |
| 778 |
|
| 779 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) |
| 780 |
{ |
| 781 |
FLAC__ASSERT(0 != decoder); |
| 782 |
FLAC__ASSERT(0 != decoder->private_); |
| 783 |
FLAC__ASSERT(0 != decoder->protected_); |
| 784 |
FLAC__ASSERT((uint32_t)type <= FLAC__MAX_METADATA_TYPE_CODE); |
| 785 |
/* double protection */ |
| 786 |
if((uint32_t)type > FLAC__MAX_METADATA_TYPE_CODE) |
| 787 |
return false; |
| 788 |
decoder->private_->metadata_filter[type] = false; |
| 789 |
if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 790 |
decoder->private_->metadata_filter_ids_count = 0; |
| 791 |
return true; |
| 792 |
} |
| 793 |
|
| 794 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) |
| 795 |
{ |
| 796 |
FLAC__ASSERT(0 != decoder); |
| 797 |
FLAC__ASSERT(0 != decoder->private_); |
| 798 |
FLAC__ASSERT(0 != decoder->protected_); |
| 799 |
FLAC__ASSERT(0 != id); |
| 800 |
|
| 801 |
if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 802 |
return true; |
| 803 |
|
| 804 |
FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 805 |
|
| 806 |
if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
| 807 |
if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) { |
| 808 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 809 |
return false; |
| 810 |
} |
| 811 |
decoder->private_->metadata_filter_ids_capacity *= 2; |
| 812 |
} |
| 813 |
|
| 814 |
memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); |
| 815 |
decoder->private_->metadata_filter_ids_count++; |
| 816 |
|
| 817 |
return true; |
| 818 |
} |
| 819 |
|
| 820 |
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder) |
| 821 |
{ |
| 822 |
FLAC__ASSERT(0 != decoder); |
| 823 |
FLAC__ASSERT(0 != decoder->private_); |
| 824 |
FLAC__ASSERT(0 != decoder->protected_); |
| 825 |
memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 826 |
decoder->private_->metadata_filter_ids_count = 0; |
| 827 |
return true; |
| 828 |
} |
| 829 |
|
| 830 |
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder) |
| 831 |
{ |
| 832 |
FLAC__ASSERT(0 != decoder); |
| 833 |
FLAC__ASSERT(0 != decoder->protected_); |
| 834 |
return decoder->protected_->state; |
| 835 |
} |
| 836 |
|
| 837 |
FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder) |
| 838 |
{ |
| 839 |
return FLAC__StreamDecoderStateString[decoder->protected_->state]; |
| 840 |
} |
| 841 |
|
| 842 |
FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_chained_stream(const FLAC__StreamDecoder* decoder) |
| 843 |
{ |
| 844 |
FLAC__ASSERT(0 != decoder); |
| 845 |
FLAC__ASSERT(0 != decoder->protected_); |
| 846 |
#if FLAC__HAS_OGG |
| 847 |
return FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect); |
| 848 |
#else |
| 849 |
(void)decoder; |
| 850 |
return false; |
| 851 |
#endif |
| 852 |
} |
| 853 |
|
| 854 |
FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder) |
| 855 |
{ |
| 856 |
FLAC__ASSERT(0 != decoder); |
| 857 |
FLAC__ASSERT(0 != decoder->protected_); |
| 858 |
return decoder->protected_->md5_checking; |
| 859 |
} |
| 860 |
|
| 861 |
FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder) |
| 862 |
{ |
| 863 |
FLAC__ASSERT(0 != decoder); |
| 864 |
FLAC__ASSERT(0 != decoder->protected_); |
| 865 |
return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0; |
| 866 |
} |
| 867 |
|
| 868 |
FLAC_API uint32_t FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) |
| 869 |
{ |
| 870 |
FLAC__ASSERT(0 != decoder); |
| 871 |
FLAC__ASSERT(0 != decoder->protected_); |
| 872 |
return decoder->protected_->channels; |
| 873 |
} |
| 874 |
|
| 875 |
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder) |
| 876 |
{ |
| 877 |
FLAC__ASSERT(0 != decoder); |
| 878 |
FLAC__ASSERT(0 != decoder->protected_); |
| 879 |
return decoder->protected_->channel_assignment; |
| 880 |
} |
| 881 |
|
| 882 |
FLAC_API uint32_t FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder) |
| 883 |
{ |
| 884 |
FLAC__ASSERT(0 != decoder); |
| 885 |
FLAC__ASSERT(0 != decoder->protected_); |
| 886 |
return decoder->protected_->bits_per_sample; |
| 887 |
} |
| 888 |
|
| 889 |
FLAC_API uint32_t FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder) |
| 890 |
{ |
| 891 |
FLAC__ASSERT(0 != decoder); |
| 892 |
FLAC__ASSERT(0 != decoder->protected_); |
| 893 |
return decoder->protected_->sample_rate; |
| 894 |
} |
| 895 |
|
| 896 |
FLAC_API uint32_t FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) |
| 897 |
{ |
| 898 |
FLAC__ASSERT(0 != decoder); |
| 899 |
FLAC__ASSERT(0 != decoder->protected_); |
| 900 |
return decoder->protected_->blocksize; |
| 901 |
} |
| 902 |
|
| 903 |
FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position) |
| 904 |
{ |
| 905 |
FLAC__ASSERT(0 != decoder); |
| 906 |
FLAC__ASSERT(0 != decoder->private_); |
| 907 |
FLAC__ASSERT(0 != position); |
| 908 |
|
| 909 |
if(FLAC__HAS_OGG && decoder->private_->is_ogg) |
| 910 |
return false; |
| 911 |
|
| 912 |
if(0 == decoder->private_->tell_callback) |
| 913 |
return false; |
| 914 |
if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) |
| 915 |
return false; |
| 916 |
/* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */ |
| 917 |
if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) |
| 918 |
return false; |
| 919 |
FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder)); |
| 920 |
*position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder); |
| 921 |
return true; |
| 922 |
} |
| 923 |
|
| 924 |
FLAC_API const void *FLAC__stream_decoder_get_client_data(FLAC__StreamDecoder *decoder) |
| 925 |
{ |
| 926 |
return decoder->private_->client_data; |
| 927 |
} |
| 928 |
|
| 929 |
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) |
| 930 |
{ |
| 931 |
FLAC__ASSERT(0 != decoder); |
| 932 |
FLAC__ASSERT(0 != decoder->private_); |
| 933 |
FLAC__ASSERT(0 != decoder->protected_); |
| 934 |
|
| 935 |
if(!decoder->private_->internal_reset_hack && decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
| 936 |
return false; |
| 937 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) |
| 938 |
return false; |
| 939 |
|
| 940 |
decoder->private_->samples_decoded = 0; |
| 941 |
decoder->private_->do_md5_checking = false; |
| 942 |
decoder->private_->last_seen_framesync = 0; |
| 943 |
decoder->private_->last_frame_is_set = false; |
| 944 |
|
| 945 |
#if FLAC__HAS_OGG |
| 946 |
if(decoder->private_->is_ogg) |
| 947 |
FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect); |
| 948 |
#endif |
| 949 |
|
| 950 |
if(!FLAC__bitreader_clear(decoder->private_->input)) { |
| 951 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 952 |
return false; |
| 953 |
} |
| 954 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 955 |
|
| 956 |
return true; |
| 957 |
} |
| 958 |
|
| 959 |
void reset_decoder_internal_(FLAC__StreamDecoder* decoder) { |
| 960 |
/* This code resets only the FLAC parser and decoder, not the Ogg part, |
| 961 |
* nor the input buffering etc. This is used to prepare the FLAC |
| 962 |
* decoder to receive another Ogg chain link */ |
| 963 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; |
| 964 |
|
| 965 |
decoder->private_->has_stream_info = false; |
| 966 |
|
| 967 |
free(decoder->private_->seek_table.data.seek_table.points); |
| 968 |
decoder->private_->seek_table.data.seek_table.points = 0; |
| 969 |
decoder->private_->has_seek_table = false; |
| 970 |
|
| 971 |
decoder->private_->do_md5_checking = decoder->protected_->md5_checking; |
| 972 |
/* |
| 973 |
* This goes in reset() and not flush() because according to the spec, a |
| 974 |
* fixed-blocksize stream must stay that way through the whole stream. |
| 975 |
*/ |
| 976 |
decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; |
| 977 |
|
| 978 |
/* We initialize the FLAC__MD5Context even though we may never use it. This |
| 979 |
* is because md5 checking may be turned on to start and then turned off if |
| 980 |
* a seek occurs. So we init the context here and finalize it in |
| 981 |
* FLAC__stream_decoder_finish() to make sure things are always cleaned up |
| 982 |
* properly. |
| 983 |
*/ |
| 984 |
if(!decoder->private_->internal_reset_hack) { |
| 985 |
/* Only finish MD5 context when it has been initialized |
| 986 |
* (i.e. when internal_reset_hack is not set) */ |
| 987 |
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); |
| 988 |
} |
| 989 |
else |
| 990 |
decoder->private_->internal_reset_hack = false; |
| 991 |
FLAC__MD5Init(&decoder->private_->md5context); |
| 992 |
|
| 993 |
decoder->private_->first_frame_offset = 0; |
| 994 |
decoder->private_->unparseable_frame_count = 0; |
| 995 |
decoder->private_->last_seen_framesync = 0; |
| 996 |
decoder->private_->last_frame_is_set = false; |
| 997 |
decoder->private_->error_has_been_sent = false; |
| 998 |
} |
| 999 |
|
| 1000 |
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) |
| 1001 |
{ |
| 1002 |
FLAC__ASSERT(0 != decoder); |
| 1003 |
FLAC__ASSERT(0 != decoder->private_); |
| 1004 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1005 |
|
| 1006 |
if(!FLAC__stream_decoder_flush(decoder)) { |
| 1007 |
/* above call sets the state for us */ |
| 1008 |
return false; |
| 1009 |
} |
| 1010 |
|
| 1011 |
#if FLAC__HAS_OGG |
| 1012 |
/*@@@ could go in !internal_reset_hack block below */ |
| 1013 |
if(decoder->private_->is_ogg) |
| 1014 |
FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect); |
| 1015 |
#endif |
| 1016 |
|
| 1017 |
/* Rewind if necessary. If FLAC__stream_decoder_init() is calling us, |
| 1018 |
* (internal_reset_hack) don't try to rewind since we are already at |
| 1019 |
* the beginning of the stream and don't want to fail if the input is |
| 1020 |
* not seekable. |
| 1021 |
*/ |
| 1022 |
if(!decoder->private_->internal_reset_hack) { |
| 1023 |
if(decoder->private_->file == stdin) |
| 1024 |
return false; /* can't rewind stdin, reset fails */ |
| 1025 |
if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) |
| 1026 |
return false; /* seekable and seek fails, reset fails */ |
| 1027 |
} |
| 1028 |
|
| 1029 |
reset_decoder_internal_(decoder); |
| 1030 |
|
| 1031 |
return true; |
| 1032 |
} |
| 1033 |
|
| 1034 |
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) |
| 1035 |
{ |
| 1036 |
FLAC__bool got_a_frame; |
| 1037 |
FLAC__ASSERT(0 != decoder); |
| 1038 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1039 |
|
| 1040 |
while(1) { |
| 1041 |
switch(decoder->protected_->state) { |
| 1042 |
case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1043 |
if(!find_metadata_(decoder)) |
| 1044 |
return false; /* above function sets the status for us */ |
| 1045 |
break; |
| 1046 |
case FLAC__STREAM_DECODER_READ_METADATA: |
| 1047 |
if(!read_metadata_(decoder)) |
| 1048 |
return false; /* above function sets the status for us */ |
| 1049 |
else |
| 1050 |
return true; |
| 1051 |
case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1052 |
if(!frame_sync_(decoder)) { |
| 1053 |
return true; /* above function sets the status for us */ |
| 1054 |
} |
| 1055 |
break; |
| 1056 |
case FLAC__STREAM_DECODER_READ_FRAME: |
| 1057 |
if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true)) |
| 1058 |
return false; /* above function sets the status for us */ |
| 1059 |
if(got_a_frame) |
| 1060 |
return true; /* above function sets the status for us */ |
| 1061 |
break; |
| 1062 |
case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1063 |
case FLAC__STREAM_DECODER_END_OF_LINK: |
| 1064 |
case FLAC__STREAM_DECODER_ABORTED: |
| 1065 |
return true; |
| 1066 |
default: |
| 1067 |
return false; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder) |
| 1073 |
{ |
| 1074 |
FLAC__ASSERT(0 != decoder); |
| 1075 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1076 |
|
| 1077 |
while(1) { |
| 1078 |
switch(decoder->protected_->state) { |
| 1079 |
case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1080 |
if(!find_metadata_(decoder)) |
| 1081 |
return false; /* above function sets the status for us */ |
| 1082 |
break; |
| 1083 |
case FLAC__STREAM_DECODER_READ_METADATA: |
| 1084 |
if(!read_metadata_(decoder)) |
| 1085 |
return false; /* above function sets the status for us */ |
| 1086 |
break; |
| 1087 |
case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1088 |
case FLAC__STREAM_DECODER_READ_FRAME: |
| 1089 |
case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1090 |
case FLAC__STREAM_DECODER_END_OF_LINK: |
| 1091 |
case FLAC__STREAM_DECODER_ABORTED: |
| 1092 |
return true; |
| 1093 |
default: |
| 1094 |
return false; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_link(FLAC__StreamDecoder *decoder) |
| 1100 |
{ |
| 1101 |
FLAC__bool dummy; |
| 1102 |
FLAC__ASSERT(0 != decoder); |
| 1103 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1104 |
|
| 1105 |
while(1) { |
| 1106 |
switch(decoder->protected_->state) { |
| 1107 |
case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1108 |
if(!find_metadata_(decoder)) |
| 1109 |
return false; /* above function sets the status for us */ |
| 1110 |
break; |
| 1111 |
case FLAC__STREAM_DECODER_READ_METADATA: |
| 1112 |
if(!read_metadata_(decoder)) |
| 1113 |
return false; /* above function sets the status for us */ |
| 1114 |
break; |
| 1115 |
case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1116 |
if(!frame_sync_(decoder)) { |
| 1117 |
return true; /* above function sets the status for us */ |
| 1118 |
} |
| 1119 |
break; |
| 1120 |
case FLAC__STREAM_DECODER_READ_FRAME: |
| 1121 |
if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) |
| 1122 |
return false; /* above function sets the status for us */ |
| 1123 |
break; |
| 1124 |
case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1125 |
case FLAC__STREAM_DECODER_END_OF_LINK: |
| 1126 |
case FLAC__STREAM_DECODER_ABORTED: |
| 1127 |
return true; |
| 1128 |
default: |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
FLAC_API FLAC__bool FLAC__stream_decoder_finish_link(FLAC__StreamDecoder *decoder) |
| 1135 |
{ |
| 1136 |
FLAC__bool md5_failed = false; |
| 1137 |
|
| 1138 |
FLAC__ASSERT(0 != decoder); |
| 1139 |
FLAC__ASSERT(0 != decoder->private_); |
| 1140 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1141 |
|
| 1142 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_LINK) { |
| 1143 |
FLAC__ASSERT(0); /* This function should not be called in any other state */ |
| 1144 |
return true; |
| 1145 |
} |
| 1146 |
|
| 1147 |
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); |
| 1148 |
|
| 1149 |
if(decoder->private_->do_md5_checking) { |
| 1150 |
if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16)) |
| 1151 |
md5_failed = true; |
| 1152 |
} |
| 1153 |
|
| 1154 |
reset_decoder_internal_(decoder); |
| 1155 |
|
| 1156 |
#if FLAC__HAS_OGG |
| 1157 |
if(decoder->private_->is_ogg) |
| 1158 |
FLAC__ogg_decoder_aspect_next_link(&decoder->protected_->ogg_decoder_aspect); |
| 1159 |
#endif |
| 1160 |
|
| 1161 |
return !md5_failed; |
| 1162 |
} |
| 1163 |
|
| 1164 |
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder) |
| 1165 |
{ |
| 1166 |
FLAC__bool dummy; |
| 1167 |
FLAC__ASSERT(0 != decoder); |
| 1168 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1169 |
|
| 1170 |
while(1) { |
| 1171 |
switch(decoder->protected_->state) { |
| 1172 |
case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1173 |
if(!find_metadata_(decoder)) |
| 1174 |
return false; /* above function sets the status for us */ |
| 1175 |
break; |
| 1176 |
case FLAC__STREAM_DECODER_READ_METADATA: |
| 1177 |
if(!read_metadata_(decoder)) |
| 1178 |
return false; /* above function sets the status for us */ |
| 1179 |
break; |
| 1180 |
case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1181 |
if(!frame_sync_(decoder) && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_LINK && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) { |
| 1182 |
return true; /* above function sets the status for us */ |
| 1183 |
} |
| 1184 |
break; |
| 1185 |
case FLAC__STREAM_DECODER_READ_FRAME: |
| 1186 |
if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) |
| 1187 |
return false; /* above function sets the status for us */ |
| 1188 |
break; |
| 1189 |
case FLAC__STREAM_DECODER_END_OF_LINK: |
| 1190 |
FLAC__stream_decoder_finish_link(decoder); |
| 1191 |
break; |
| 1192 |
case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1193 |
case FLAC__STREAM_DECODER_ABORTED: |
| 1194 |
return true; |
| 1195 |
default: |
| 1196 |
return false; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder) |
| 1202 |
{ |
| 1203 |
FLAC__bool got_a_frame; |
| 1204 |
FLAC__ASSERT(0 != decoder); |
| 1205 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1206 |
|
| 1207 |
while(1) { |
| 1208 |
switch(decoder->protected_->state) { |
| 1209 |
case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1210 |
case FLAC__STREAM_DECODER_READ_METADATA: |
| 1211 |
return false; |
| 1212 |
case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1213 |
if(!frame_sync_(decoder)) { |
| 1214 |
return true; /* above function sets the status for us */ |
| 1215 |
} |
| 1216 |
break; |
| 1217 |
case FLAC__STREAM_DECODER_READ_FRAME: |
| 1218 |
if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false)) |
| 1219 |
return false; /* above function sets the status for us */ |
| 1220 |
if(got_a_frame) |
| 1221 |
return true; /* above function sets the status for us */ |
| 1222 |
break; |
| 1223 |
case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1224 |
case FLAC__STREAM_DECODER_END_OF_LINK: |
| 1225 |
case FLAC__STREAM_DECODER_ABORTED: |
| 1226 |
return true; |
| 1227 |
default: |
| 1228 |
return false; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_link(FLAC__StreamDecoder *decoder) |
| 1234 |
{ |
| 1235 |
#if FLAC__HAS_OGG |
| 1236 |
FLAC__OggDecoderAspectReadStatus status; |
| 1237 |
FLAC__ASSERT_DECLARATION(FLAC__uint32 linknumber_start); |
| 1238 |
FLAC__ASSERT(0 != decoder); |
| 1239 |
FLAC__ASSERT(0 != decoder->protected_); |
| 1240 |
|
| 1241 |
if(!decoder->private_->is_ogg || |
| 1242 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED || |
| 1243 |
decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR || |
| 1244 |
decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
| 1245 |
return false; |
| 1246 |
|
| 1247 |
FLAC__ASSERT_DECLARATION(linknumber_start = decoder->protected_->ogg_decoder_aspect.current_linknumber); |
| 1248 |
|
| 1249 |
if(!FLAC__bitreader_clear(decoder->private_->input)) { |
| 1250 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1251 |
return false; |
| 1252 |
} |
| 1253 |
status = FLAC__ogg_decoder_aspect_skip_link(&decoder->protected_->ogg_decoder_aspect, read_callback_proxy_, decoder->private_->seek_callback, decoder->private_->tell_callback, decoder->private_->length_callback, decoder, decoder->private_->client_data); |
| 1254 |
if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM) { |
| 1255 |
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 1256 |
return true; |
| 1257 |
} |
| 1258 |
else if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_CALLBACKS_NONFUNCTIONAL) { |
| 1259 |
decoder->private_->is_seeking = true; |
| 1260 |
FLAC__stream_decoder_process_until_end_of_link(decoder); |
| 1261 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_LINK) |
| 1262 |
FLAC__stream_decoder_finish_link(decoder); |
| 1263 |
decoder->private_->is_seeking = false; |
| 1264 |
} |
| 1265 |
else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) { |
| 1266 |
decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR; |
| 1267 |
return false; |
| 1268 |
} |
| 1269 |
else { |
| 1270 |
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); |
| 1271 |
reset_decoder_internal_(decoder); |
| 1272 |
} |
| 1273 |
|
| 1274 |
FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM || decoder->protected_->ogg_decoder_aspect.current_linknumber > linknumber_start); |
| 1275 |
return true; |
| 1276 |
#else |
| 1277 |
(void)decoder; |
| 1278 |
return false; |
| 1279 |
#endif |
| 1280 |
} |
| 1281 |
|
| 1282 |
FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample) |
| 1283 |
{ |
| 1284 |
FLAC__uint64 length; |
| 1285 |
|
| 1286 |
FLAC__ASSERT(0 != decoder); |
| 1287 |
|
| 1288 |
if( |
| 1289 |
decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && |
| 1290 |
decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA && |
| 1291 |
decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && |
| 1292 |
decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME && |
| 1293 |
decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM |
| 1294 |
) |
| 1295 |
return false; |
| 1296 |
|
| 1297 |
if(0 == decoder->private_->seek_callback) |
| 1298 |
return false; |
| 1299 |
|
| 1300 |
FLAC__ASSERT(decoder->private_->seek_callback); |
| 1301 |
FLAC__ASSERT(decoder->private_->tell_callback); |
| 1302 |
FLAC__ASSERT(decoder->private_->length_callback); |
| 1303 |
FLAC__ASSERT(decoder->private_->eof_callback); |
| 1304 |
|
| 1305 |
if(!FLAC__stream_decoder_get_decode_chained_stream(decoder) && FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) |
| 1306 |
return false; |
| 1307 |
|
| 1308 |
decoder->private_->is_seeking = true; |
| 1309 |
|
| 1310 |
/* turn off md5 checking if a seek is attempted */ |
| 1311 |
decoder->private_->do_md5_checking = false; |
| 1312 |
|
| 1313 |
/* 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) */ |
| 1314 |
if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) { |
| 1315 |
decoder->private_->is_seeking = false; |
| 1316 |
return false; |
| 1317 |
} |
| 1318 |
|
| 1319 |
/* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */ |
| 1320 |
if( |
| 1321 |
decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA || |
| 1322 |
decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA |
| 1323 |
) { |
| 1324 |
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) { |
| 1325 |
/* above call sets the state for us */ |
| 1326 |
decoder->private_->is_seeking = false; |
| 1327 |
return false; |
| 1328 |
} |
| 1329 |
/* check this again in case we didn't know total_samples the first time */ |
| 1330 |
if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) { |
| 1331 |
decoder->private_->is_seeking = false; |
| 1332 |
return false; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
{ |
| 1337 |
const FLAC__bool ok = |
| 1338 |
#if FLAC__HAS_OGG |
| 1339 |
decoder->private_->is_ogg? |
| 1340 |
seek_to_absolute_sample_ogg_(decoder, length, sample) : |
| 1341 |
#endif |
| 1342 |
seek_to_absolute_sample_(decoder, length, sample) |
| 1343 |
; |
| 1344 |
decoder->private_->is_seeking = false; |
| 1345 |
return ok; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
FLAC_API FLAC__uint64 FLAC__stream_decoder_find_total_samples(FLAC__StreamDecoder *decoder) |
| 1350 |
{ |
| 1351 |
if( |
| 1352 |
decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && |
| 1353 |
decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA && |
| 1354 |
decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && |
| 1355 |
decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME && |
| 1356 |
decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM |
| 1357 |
) |
| 1358 |
return 0; |
| 1359 |
|
| 1360 |
if( |
| 1361 |
decoder->private_->length_callback == NULL || |
| 1362 |
decoder->private_->seek_callback == NULL || |
| 1363 |
decoder->private_->tell_callback == NULL |
| 1364 |
) |
| 1365 |
return 0; |
| 1366 |
|
| 1367 |
#if FLAC__HAS_OGG |
| 1368 |
if(decoder->private_->is_ogg && FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect)) { |
| 1369 |
/* Keep moving forward until reaching end-of-stream */ |
| 1370 |
uint32_t i; |
| 1371 |
FLAC__uint64 total_samples = 0; |
| 1372 |
decoder->private_->is_indexing = true; |
| 1373 |
while(1) { |
| 1374 |
FLAC__OggDecoderAspectReadStatus status; |
| 1375 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM || |
| 1376 |
decoder->protected_->state == FLAC__STREAM_DECODER_OGG_ERROR || |
| 1377 |
decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR || |
| 1378 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) { |
| 1379 |
decoder->private_->is_indexing = false; |
| 1380 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 1381 |
return 0; |
| 1382 |
} |
| 1383 |
status = FLAC__ogg_decoder_aspect_skip_link(&decoder->protected_->ogg_decoder_aspect, read_callback_proxy_, decoder->private_->seek_callback, decoder->private_->tell_callback, decoder->private_->length_callback, decoder, decoder->private_->client_data); |
| 1384 |
if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM) |
| 1385 |
break; |
| 1386 |
else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) { |
| 1387 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 1388 |
return 0; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
decoder->private_->is_indexing = false; |
| 1392 |
for(i = 0; i < decoder->protected_->ogg_decoder_aspect.number_of_links_indexed; i++) { |
| 1393 |
total_samples += decoder->protected_->ogg_decoder_aspect.linkdetails[i].samples; |
| 1394 |
} |
| 1395 |
return total_samples; |
| 1396 |
} |
| 1397 |
else |
| 1398 |
#endif /* FLAC__HAS_OGG */ |
| 1399 |
{ /* not decoding chained ogg */ |
| 1400 |
FLAC__uint64 length; |
| 1401 |
FLAC__uint64 pos; |
| 1402 |
uint32_t eof_distance = 1024; /* Some number, needs tuning */ |
| 1403 |
decoder->private_->is_seeking = true; |
| 1404 |
decoder->private_->target_sample = UINT64_MAX; |
| 1405 |
/* get the file length */ |
| 1406 |
if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) { |
| 1407 |
decoder->private_->is_indexing = false; |
| 1408 |
return 0; |
| 1409 |
} |
| 1410 |
pos = length; |
| 1411 |
for( ; ; eof_distance *= 2) { |
| 1412 |
if(eof_distance > (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) { |
| 1413 |
/* Could not find a frame within a reasonable distance of EOF */ |
| 1414 |
return 0; |
| 1415 |
} |
| 1416 |
else if(pos == 0) { |
| 1417 |
/* Did not find a frame while reading from the start of the file */ |
| 1418 |
return 0; |
| 1419 |
} |
| 1420 |
else if(eof_distance > length) { |
| 1421 |
pos = 0; |
| 1422 |
} |
| 1423 |
else { |
| 1424 |
pos = length - eof_distance; |
| 1425 |
} |
| 1426 |
|
| 1427 |
if(decoder->private_->seek_callback(decoder, pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { |
| 1428 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 1429 |
return 0; |
| 1430 |
} |
| 1431 |
if(!FLAC__stream_decoder_flush(decoder)) { |
| 1432 |
/* above call sets the state for us */ |
| 1433 |
return 0; |
| 1434 |
} |
| 1435 |
|
| 1436 |
decoder->private_->got_a_frame = false; |
| 1437 |
if(!FLAC__stream_decoder_process_single(decoder) || |
| 1438 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) { |
| 1439 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) |
| 1440 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 1441 |
return 0; |
| 1442 |
} |
| 1443 |
if(decoder->private_->got_a_frame) { |
| 1444 |
/* Found a frame, but we need the last frame and the frame before that, unless the last frame is |
| 1445 |
* also the first frame */ |
| 1446 |
if(decoder->private_->frame.header.number.sample_number > 0) { |
| 1447 |
/* For now, assume this is not the last frame, set blocksize, and continue. |
| 1448 |
* If it turns out this is not the last frame, we'll start over anyway */ |
| 1449 |
decoder->private_->fixed_block_size = decoder->private_->last_frame.header.blocksize; |
| 1450 |
if(!FLAC__stream_decoder_process_single(decoder) || |
| 1451 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) { |
| 1452 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) |
| 1453 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 1454 |
return 0; |
| 1455 |
} |
| 1456 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM) { |
| 1457 |
/* Found last frame, but need to find frame before that too */ |
| 1458 |
continue; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
if(!FLAC__stream_decoder_process_until_end_of_stream(decoder)) |
| 1462 |
return 0; |
| 1463 |
FLAC__ASSERT(decoder->private_->is_seeking); |
| 1464 |
FLAC__ASSERT(decoder->private_->last_frame_is_set); |
| 1465 |
decoder->private_->is_seeking = false; |
| 1466 |
return (FLAC__uint64)decoder->private_->last_frame.header.number.sample_number + (FLAC__uint64)decoder->private_->last_frame.header.blocksize; |
| 1467 |
|
| 1468 |
} |
| 1469 |
} |
| 1470 |
} |
| 1471 |
return 0; |
| 1472 |
} |
| 1473 |
|
| 1474 |
FLAC_API int32_t FLAC__stream_decoder_get_link_lengths(FLAC__StreamDecoder *decoder, FLAC__uint64 **link_lengths) |
| 1475 |
{ |
| 1476 |
/* If we don't have Ogg, this is (for now) useless, fail */ |
| 1477 |
#if FLAC__HAS_OGG |
| 1478 |
uint32_t i; |
| 1479 |
|
| 1480 |
/* Check whether we're decoding chaines ogg, and decoder is somewhat valid */ |
| 1481 |
if(!decoder->private_->is_ogg || |
| 1482 |
!FLAC__stream_decoder_get_decode_chained_stream(decoder) || |
| 1483 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED || |
| 1484 |
decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR || |
| 1485 |
decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
| 1486 |
return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_INVALID; |
| 1487 |
|
| 1488 |
/* Check whether link details are known. If not, fail */ |
| 1489 |
if(decoder->protected_->ogg_decoder_aspect.number_of_links_indexed == 0 || |
| 1490 |
!decoder->protected_->ogg_decoder_aspect.linkdetails[decoder->protected_->ogg_decoder_aspect.number_of_links_indexed - 1].is_last) |
| 1491 |
return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_NOT_INDEXED; |
| 1492 |
|
| 1493 |
if(link_lengths != NULL) { |
| 1494 |
*link_lengths = safe_malloc_mul_2op_p(sizeof(FLAC__uint64), decoder->protected_->ogg_decoder_aspect.number_of_links_indexed); |
| 1495 |
if(*link_lengths == NULL) |
| 1496 |
return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_MEMORY_ALLOCATION_ERROR; |
| 1497 |
|
| 1498 |
for(i = 0; i < decoder->protected_->ogg_decoder_aspect.number_of_links_indexed; i++) |
| 1499 |
(*link_lengths)[i] = decoder->protected_->ogg_decoder_aspect.linkdetails[i].samples; |
| 1500 |
} |
| 1501 |
|
| 1502 |
return decoder->protected_->ogg_decoder_aspect.number_of_links_indexed; |
| 1503 |
#else |
| 1504 |
(void)decoder; |
| 1505 |
(void)link_lengths; |
| 1506 |
return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_INVALID; |
| 1507 |
#endif |
| 1508 |
} |
| 1509 |
|
| 1510 |
|
| 1511 |
/*********************************************************************** |
| 1512 |
* |
| 1513 |
* Protected class methods |
| 1514 |
* |
| 1515 |
***********************************************************************/ |
| 1516 |
|
| 1517 |
uint32_t FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) |
| 1518 |
{ |
| 1519 |
FLAC__ASSERT(0 != decoder); |
| 1520 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1521 |
FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7)); |
| 1522 |
return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8; |
| 1523 |
} |
| 1524 |
|
| 1525 |
/*********************************************************************** |
| 1526 |
* |
| 1527 |
* Private class methods |
| 1528 |
* |
| 1529 |
***********************************************************************/ |
| 1530 |
|
| 1531 |
void set_defaults_(FLAC__StreamDecoder *decoder) |
| 1532 |
{ |
| 1533 |
decoder->private_->is_ogg = false; |
| 1534 |
decoder->private_->read_callback = 0; |
| 1535 |
decoder->private_->seek_callback = 0; |
| 1536 |
decoder->private_->tell_callback = 0; |
| 1537 |
decoder->private_->length_callback = 0; |
| 1538 |
decoder->private_->eof_callback = 0; |
| 1539 |
decoder->private_->write_callback = 0; |
| 1540 |
decoder->private_->metadata_callback = 0; |
| 1541 |
decoder->private_->error_callback = 0; |
| 1542 |
decoder->private_->client_data = 0; |
| 1543 |
|
| 1544 |
memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 1545 |
decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; |
| 1546 |
decoder->private_->metadata_filter_ids_count = 0; |
| 1547 |
|
| 1548 |
decoder->protected_->md5_checking = false; |
| 1549 |
|
| 1550 |
#if FLAC__HAS_OGG |
| 1551 |
FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect); |
| 1552 |
#endif |
| 1553 |
} |
| 1554 |
|
| 1555 |
/* |
| 1556 |
* This will forcibly set stdin to binary mode (for OSes that require it) |
| 1557 |
*/ |
| 1558 |
FILE *get_binary_stdin_(void) |
| 1559 |
{ |
| 1560 |
/* if something breaks here it is probably due to the presence or |
| 1561 |
* absence of an underscore before the identifiers 'setmode', |
| 1562 |
* 'fileno', and/or 'O_BINARY'; check your system header files. |
| 1563 |
*/ |
| 1564 |
#if defined _MSC_VER || defined __MINGW32__ |
| 1565 |
_setmode(_fileno(stdin), _O_BINARY); |
| 1566 |
#elif defined __EMX__ |
| 1567 |
setmode(fileno(stdin), O_BINARY); |
| 1568 |
#endif |
| 1569 |
|
| 1570 |
return stdin; |
| 1571 |
} |
| 1572 |
|
| 1573 |
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, uint32_t size, uint32_t channels, uint32_t bps) |
| 1574 |
{ |
| 1575 |
uint32_t i; |
| 1576 |
FLAC__int32 *tmp; |
| 1577 |
|
| 1578 |
if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels && |
| 1579 |
(bps < 32 || decoder->private_->side_subframe != 0)) |
| 1580 |
return true; |
| 1581 |
|
| 1582 |
/* simply using realloc() is not practical because the number of channels may change mid-stream */ |
| 1583 |
|
| 1584 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 1585 |
if(0 != decoder->private_->output[i]) { |
| 1586 |
free(decoder->private_->output[i]-4); |
| 1587 |
decoder->private_->output[i] = 0; |
| 1588 |
} |
| 1589 |
if(0 != decoder->private_->residual_unaligned[i]) { |
| 1590 |
free(decoder->private_->residual_unaligned[i]); |
| 1591 |
decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
if(0 != decoder->private_->side_subframe) { |
| 1596 |
free(decoder->private_->side_subframe); |
| 1597 |
decoder->private_->side_subframe = 0; |
| 1598 |
} |
| 1599 |
|
| 1600 |
/* Only grow per-channel buffers, even if number of channels increases */ |
| 1601 |
if(decoder->private_->output_capacity > size) { |
| 1602 |
size = decoder->private_->output_capacity; |
| 1603 |
} |
| 1604 |
|
| 1605 |
for(i = 0; i < channels; i++) { |
| 1606 |
/* WATCHOUT: |
| 1607 |
* FLAC__lpc_restore_signal_asm_ia32_mmx() and ..._intrin_sseN() |
| 1608 |
* require that the output arrays have a buffer of up to 3 zeroes |
| 1609 |
* in front (at negative indices) for alignment purposes; |
| 1610 |
* we use 4 to keep the data well-aligned. |
| 1611 |
*/ |
| 1612 |
tmp = safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/); |
| 1613 |
if(tmp == 0) { |
| 1614 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1615 |
return false; |
| 1616 |
} |
| 1617 |
memset(tmp, 0, sizeof(FLAC__int32)*4); |
| 1618 |
decoder->private_->output[i] = tmp + 4; |
| 1619 |
|
| 1620 |
if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) { |
| 1621 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1622 |
return false; |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
if(bps == 32) { |
| 1627 |
decoder->private_->side_subframe = safe_malloc_mul_2op_p(sizeof(FLAC__int64), /*times (*/size); |
| 1628 |
if(decoder->private_->side_subframe == NULL) { |
| 1629 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1630 |
return false; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
decoder->private_->output_capacity = size; |
| 1635 |
decoder->private_->output_channels = channels; |
| 1636 |
|
| 1637 |
return true; |
| 1638 |
} |
| 1639 |
|
| 1640 |
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) |
| 1641 |
{ |
| 1642 |
size_t i; |
| 1643 |
|
| 1644 |
FLAC__ASSERT(0 != decoder); |
| 1645 |
FLAC__ASSERT(0 != decoder->private_); |
| 1646 |
|
| 1647 |
for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) |
| 1648 |
if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) |
| 1649 |
return true; |
| 1650 |
|
| 1651 |
return false; |
| 1652 |
} |
| 1653 |
|
| 1654 |
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) |
| 1655 |
{ |
| 1656 |
FLAC__uint32 x; |
| 1657 |
uint32_t i, id; |
| 1658 |
FLAC__bool first = true; |
| 1659 |
|
| 1660 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1661 |
|
| 1662 |
for(i = id = 0; i < 4; ) { |
| 1663 |
if(decoder->private_->cached) { |
| 1664 |
x = (FLAC__uint32)decoder->private_->lookahead; |
| 1665 |
decoder->private_->cached = false; |
| 1666 |
} |
| 1667 |
else { |
| 1668 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1669 |
return false; /* read_callback_ sets the state for us */ |
| 1670 |
} |
| 1671 |
if(x == FLAC__STREAM_SYNC_STRING[i]) { |
| 1672 |
first = true; |
| 1673 |
i++; |
| 1674 |
id = 0; |
| 1675 |
continue; |
| 1676 |
} |
| 1677 |
|
| 1678 |
if(id >= 3) |
| 1679 |
return false; |
| 1680 |
|
| 1681 |
if(x == ID3V2_TAG_[id]) { |
| 1682 |
id++; |
| 1683 |
i = 0; |
| 1684 |
if(id == 3) { |
| 1685 |
if(!skip_id3v2_tag_(decoder)) |
| 1686 |
return false; /* skip_id3v2_tag_ sets the state for us */ |
| 1687 |
} |
| 1688 |
continue; |
| 1689 |
} |
| 1690 |
id = 0; |
| 1691 |
if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 1692 |
decoder->private_->header_warmup[0] = (FLAC__byte)x; |
| 1693 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1694 |
return false; /* read_callback_ sets the state for us */ |
| 1695 |
|
| 1696 |
/* 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 */ |
| 1697 |
/* else we have to check if the second byte is the end of a sync code */ |
| 1698 |
if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 1699 |
decoder->private_->lookahead = (FLAC__byte)x; |
| 1700 |
decoder->private_->cached = true; |
| 1701 |
} |
| 1702 |
else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */ |
| 1703 |
decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 1704 |
decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
| 1705 |
return true; |
| 1706 |
} |
| 1707 |
} |
| 1708 |
i = 0; |
| 1709 |
if(first) { |
| 1710 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 1711 |
first = false; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; |
| 1716 |
return true; |
| 1717 |
} |
| 1718 |
|
| 1719 |
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) |
| 1720 |
{ |
| 1721 |
FLAC__bool is_last; |
| 1722 |
FLAC__uint32 i, x, type, length; |
| 1723 |
|
| 1724 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1725 |
|
| 1726 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN)) |
| 1727 |
return false; /* read_callback_ sets the state for us */ |
| 1728 |
is_last = x? true : false; |
| 1729 |
|
| 1730 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN)) |
| 1731 |
return false; /* read_callback_ sets the state for us */ |
| 1732 |
|
| 1733 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN)) |
| 1734 |
return false; /* read_callback_ sets the state for us */ |
| 1735 |
|
| 1736 |
if(type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 1737 |
if(!read_metadata_streaminfo_(decoder, is_last, length)) |
| 1738 |
return false; |
| 1739 |
|
| 1740 |
decoder->private_->has_stream_info = true; |
| 1741 |
if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)) |
| 1742 |
decoder->private_->do_md5_checking = false; |
| 1743 |
if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback) |
| 1744 |
decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data); |
| 1745 |
} |
| 1746 |
else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 1747 |
/* just in case we already have a seek table, and reading the next one fails: */ |
| 1748 |
decoder->private_->has_seek_table = false; |
| 1749 |
|
| 1750 |
if(length > 0) { |
| 1751 |
if(!read_metadata_seektable_(decoder, is_last, length)) |
| 1752 |
return false; |
| 1753 |
|
| 1754 |
decoder->private_->has_seek_table = true; |
| 1755 |
if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback) |
| 1756 |
decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data); |
| 1757 |
} |
| 1758 |
} |
| 1759 |
else { |
| 1760 |
FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; |
| 1761 |
uint32_t real_length = length; |
| 1762 |
FLAC__StreamMetadata block; |
| 1763 |
|
| 1764 |
memset(&block, 0, sizeof(block)); |
| 1765 |
block.is_last = is_last; |
| 1766 |
block.type = (FLAC__MetadataType)type; |
| 1767 |
block.length = length; |
| 1768 |
|
| 1769 |
if(type == FLAC__METADATA_TYPE_APPLICATION) { |
| 1770 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)) |
| 1771 |
return false; /* read_callback_ sets the state for us */ |
| 1772 |
|
| 1773 |
if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */ |
| 1774 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/ |
| 1775 |
return false; |
| 1776 |
} |
| 1777 |
|
| 1778 |
real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; |
| 1779 |
|
| 1780 |
if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id)) |
| 1781 |
skip_it = !skip_it; |
| 1782 |
} |
| 1783 |
|
| 1784 |
if(skip_it) { |
| 1785 |
if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) |
| 1786 |
return false; /* read_callback_ sets the state for us */ |
| 1787 |
} |
| 1788 |
else { |
| 1789 |
FLAC__bool ok = true; |
| 1790 |
FLAC__bitreader_set_limit(decoder->private_->input, real_length*8); |
| 1791 |
switch(type) { |
| 1792 |
case FLAC__METADATA_TYPE_PADDING: |
| 1793 |
/* skip the padding bytes */ |
| 1794 |
if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) |
| 1795 |
ok = false; /* read_callback_ sets the state for us */ |
| 1796 |
break; |
| 1797 |
case FLAC__METADATA_TYPE_APPLICATION: |
| 1798 |
/* remember, we read the ID already */ |
| 1799 |
if(real_length > 0) { |
| 1800 |
if(0 == (block.data.application.data = malloc(real_length))) { |
| 1801 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1802 |
ok = false; |
| 1803 |
} |
| 1804 |
else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length)) |
| 1805 |
ok = false; /* read_callback_ sets the state for us */ |
| 1806 |
} |
| 1807 |
else |
| 1808 |
block.data.application.data = 0; |
| 1809 |
break; |
| 1810 |
case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 1811 |
if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment, real_length)) |
| 1812 |
ok = false; |
| 1813 |
break; |
| 1814 |
case FLAC__METADATA_TYPE_CUESHEET: |
| 1815 |
if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet)) |
| 1816 |
ok = false; |
| 1817 |
break; |
| 1818 |
case FLAC__METADATA_TYPE_PICTURE: |
| 1819 |
if(!read_metadata_picture_(decoder, &block.data.picture)) |
| 1820 |
ok = false; |
| 1821 |
break; |
| 1822 |
case FLAC__METADATA_TYPE_STREAMINFO: |
| 1823 |
case FLAC__METADATA_TYPE_SEEKTABLE: |
| 1824 |
FLAC__ASSERT(0); |
| 1825 |
break; |
| 1826 |
default: |
| 1827 |
if(real_length > 0) { |
| 1828 |
if(0 == (block.data.unknown.data = malloc(real_length))) { |
| 1829 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1830 |
ok = false; |
| 1831 |
} |
| 1832 |
else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length)) |
| 1833 |
ok = false; /* read_callback_ sets the state for us */ |
| 1834 |
} |
| 1835 |
else |
| 1836 |
block.data.unknown.data = 0; |
| 1837 |
break; |
| 1838 |
} |
| 1839 |
if(FLAC__bitreader_limit_remaining(decoder->private_->input) > 0) { |
| 1840 |
/* Content in metadata block didn't fit in block length |
| 1841 |
* We cannot know whether the length or the content was |
| 1842 |
* corrupt, so stop parsing metadata */ |
| 1843 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA); |
| 1844 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA) |
| 1845 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 1846 |
ok = false; |
| 1847 |
} |
| 1848 |
FLAC__bitreader_remove_limit(decoder->private_->input); |
| 1849 |
if(ok && !decoder->private_->is_seeking && decoder->private_->metadata_callback) |
| 1850 |
decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); |
| 1851 |
|
| 1852 |
/* now we have to free any malloc()ed data in the block */ |
| 1853 |
switch(type) { |
| 1854 |
case FLAC__METADATA_TYPE_PADDING: |
| 1855 |
break; |
| 1856 |
case FLAC__METADATA_TYPE_APPLICATION: |
| 1857 |
if(0 != block.data.application.data) |
| 1858 |
free(block.data.application.data); |
| 1859 |
break; |
| 1860 |
case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 1861 |
if(0 != block.data.vorbis_comment.vendor_string.entry) |
| 1862 |
free(block.data.vorbis_comment.vendor_string.entry); |
| 1863 |
if(block.data.vorbis_comment.num_comments > 0) |
| 1864 |
for(i = 0; i < block.data.vorbis_comment.num_comments; i++) |
| 1865 |
if(0 != block.data.vorbis_comment.comments[i].entry) |
| 1866 |
free(block.data.vorbis_comment.comments[i].entry); |
| 1867 |
if(0 != block.data.vorbis_comment.comments) |
| 1868 |
free(block.data.vorbis_comment.comments); |
| 1869 |
break; |
| 1870 |
case FLAC__METADATA_TYPE_CUESHEET: |
| 1871 |
if(block.data.cue_sheet.num_tracks > 0 && 0 != block.data.cue_sheet.tracks) |
| 1872 |
for(i = 0; i < block.data.cue_sheet.num_tracks; i++) |
| 1873 |
if(0 != block.data.cue_sheet.tracks[i].indices) |
| 1874 |
free(block.data.cue_sheet.tracks[i].indices); |
| 1875 |
if(0 != block.data.cue_sheet.tracks) |
| 1876 |
free(block.data.cue_sheet.tracks); |
| 1877 |
break; |
| 1878 |
case FLAC__METADATA_TYPE_PICTURE: |
| 1879 |
if(0 != block.data.picture.mime_type) |
| 1880 |
free(block.data.picture.mime_type); |
| 1881 |
if(0 != block.data.picture.description) |
| 1882 |
free(block.data.picture.description); |
| 1883 |
if(0 != block.data.picture.data) |
| 1884 |
free(block.data.picture.data); |
| 1885 |
break; |
| 1886 |
case FLAC__METADATA_TYPE_STREAMINFO: |
| 1887 |
case FLAC__METADATA_TYPE_SEEKTABLE: |
| 1888 |
FLAC__ASSERT(0); |
| 1889 |
default: |
| 1890 |
if(0 != block.data.unknown.data) |
| 1891 |
free(block.data.unknown.data); |
| 1892 |
break; |
| 1893 |
} |
| 1894 |
|
| 1895 |
if(!ok) /* anything that unsets "ok" should also make sure decoder->protected_->state is updated */ |
| 1896 |
return false; |
| 1897 |
} |
| 1898 |
} |
| 1899 |
|
| 1900 |
if(is_last) { |
| 1901 |
/* if this fails, it's OK, it's just a hint for the seek routine */ |
| 1902 |
if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset)) |
| 1903 |
decoder->private_->first_frame_offset = 0; |
| 1904 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 1905 |
} |
| 1906 |
|
| 1907 |
return true; |
| 1908 |
} |
| 1909 |
|
| 1910 |
FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length) |
| 1911 |
{ |
| 1912 |
FLAC__uint32 x; |
| 1913 |
uint32_t bits, used_bits = 0; |
| 1914 |
|
| 1915 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1916 |
|
| 1917 |
decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO; |
| 1918 |
decoder->private_->stream_info.is_last = is_last; |
| 1919 |
decoder->private_->stream_info.length = length; |
| 1920 |
|
| 1921 |
bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; |
| 1922 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits)) |
| 1923 |
return false; /* read_callback_ sets the state for us */ |
| 1924 |
decoder->private_->stream_info.data.stream_info.min_blocksize = x; |
| 1925 |
used_bits += bits; |
| 1926 |
|
| 1927 |
bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; |
| 1928 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN)) |
| 1929 |
return false; /* read_callback_ sets the state for us */ |
| 1930 |
decoder->private_->stream_info.data.stream_info.max_blocksize = x; |
| 1931 |
used_bits += bits; |
| 1932 |
|
| 1933 |
bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; |
| 1934 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) |
| 1935 |
return false; /* read_callback_ sets the state for us */ |
| 1936 |
decoder->private_->stream_info.data.stream_info.min_framesize = x; |
| 1937 |
used_bits += bits; |
| 1938 |
|
| 1939 |
bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; |
| 1940 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) |
| 1941 |
return false; /* read_callback_ sets the state for us */ |
| 1942 |
decoder->private_->stream_info.data.stream_info.max_framesize = x; |
| 1943 |
used_bits += bits; |
| 1944 |
|
| 1945 |
bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; |
| 1946 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN)) |
| 1947 |
return false; /* read_callback_ sets the state for us */ |
| 1948 |
decoder->private_->stream_info.data.stream_info.sample_rate = x; |
| 1949 |
used_bits += bits; |
| 1950 |
|
| 1951 |
bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; |
| 1952 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN)) |
| 1953 |
return false; /* read_callback_ sets the state for us */ |
| 1954 |
decoder->private_->stream_info.data.stream_info.channels = x+1; |
| 1955 |
used_bits += bits; |
| 1956 |
|
| 1957 |
bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; |
| 1958 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN)) |
| 1959 |
return false; /* read_callback_ sets the state for us */ |
| 1960 |
decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; |
| 1961 |
used_bits += bits; |
| 1962 |
|
| 1963 |
bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; |
| 1964 |
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) |
| 1965 |
return false; /* read_callback_ sets the state for us */ |
| 1966 |
used_bits += bits; |
| 1967 |
|
| 1968 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16)) |
| 1969 |
return false; /* read_callback_ sets the state for us */ |
| 1970 |
used_bits += 16*8; |
| 1971 |
|
| 1972 |
/* skip the rest of the block */ |
| 1973 |
FLAC__ASSERT(used_bits % 8 == 0); |
| 1974 |
if (length < (used_bits / 8)) |
| 1975 |
return false; /* read_callback_ sets the state for us */ |
| 1976 |
length -= (used_bits / 8); |
| 1977 |
if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) |
| 1978 |
return false; /* read_callback_ sets the state for us */ |
| 1979 |
|
| 1980 |
return true; |
| 1981 |
} |
| 1982 |
|
| 1983 |
FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length) |
| 1984 |
{ |
| 1985 |
FLAC__uint32 i, x; |
| 1986 |
FLAC__uint64 xx; |
| 1987 |
|
| 1988 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1989 |
|
| 1990 |
decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; |
| 1991 |
decoder->private_->seek_table.is_last = is_last; |
| 1992 |
decoder->private_->seek_table.length = length; |
| 1993 |
|
| 1994 |
if(length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) { |
| 1995 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 1996 |
return false; |
| 1997 |
} |
| 1998 |
|
| 1999 |
decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 2000 |
|
| 2001 |
/* use realloc since we may pass through here several times (e.g. after seeking) */ |
| 2002 |
if(0 == (decoder->private_->seek_table.data.seek_table.points = safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) { |
| 2003 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2004 |
return false; |
| 2005 |
} |
| 2006 |
for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { |
| 2007 |
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN)) |
| 2008 |
return false; /* read_callback_ sets the state for us */ |
| 2009 |
decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; |
| 2010 |
|
| 2011 |
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN)) |
| 2012 |
return false; /* read_callback_ sets the state for us */ |
| 2013 |
decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; |
| 2014 |
|
| 2015 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN)) |
| 2016 |
return false; /* read_callback_ sets the state for us */ |
| 2017 |
decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; |
| 2018 |
} |
| 2019 |
length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); |
| 2020 |
|
| 2021 |
FLAC__ASSERT(length == 0); |
| 2022 |
|
| 2023 |
return true; |
| 2024 |
} |
| 2025 |
|
| 2026 |
FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, uint32_t length) |
| 2027 |
{ |
| 2028 |
FLAC__uint32 i; |
| 2029 |
|
| 2030 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 2031 |
|
| 2032 |
/* read vendor string */ |
| 2033 |
if (length >= 8) { |
| 2034 |
length -= 8; /* vendor string length + num comments entries alone take 8 bytes */ |
| 2035 |
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 2036 |
if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length)) |
| 2037 |
return false; /* read_callback_ sets the state for us */ |
| 2038 |
if (length < obj->vendor_string.length) { |
| 2039 |
obj->vendor_string.length = 0; |
| 2040 |
obj->vendor_string.entry = 0; |
| 2041 |
goto skip; |
| 2042 |
} |
| 2043 |
else |
| 2044 |
length -= obj->vendor_string.length; |
| 2045 |
if (0 == (obj->vendor_string.entry = safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) { |
| 2046 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2047 |
return false; |
| 2048 |
} |
| 2049 |
if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length)) |
| 2050 |
return false; /* read_callback_ sets the state for us */ |
| 2051 |
obj->vendor_string.entry[obj->vendor_string.length] = '\0'; |
| 2052 |
|
| 2053 |
/* read num comments */ |
| 2054 |
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); |
| 2055 |
if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments)) |
| 2056 |
return false; /* read_callback_ sets the state for us */ |
| 2057 |
|
| 2058 |
/* read comments */ |
| 2059 |
if (obj->num_comments > 100000) { |
| 2060 |
/* Possibly malicious file. */ |
| 2061 |
obj->num_comments = 0; |
| 2062 |
return false; |
| 2063 |
} |
| 2064 |
if (obj->num_comments > 0) { |
| 2065 |
if (0 == (obj->comments = safe_malloc_mul_2op_p(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { |
| 2066 |
obj->num_comments = 0; |
| 2067 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2068 |
return false; |
| 2069 |
} |
| 2070 |
for (i = 0; i < obj->num_comments; i++) { |
| 2071 |
/* Initialize here just to make sure. */ |
| 2072 |
obj->comments[i].length = 0; |
| 2073 |
obj->comments[i].entry = 0; |
| 2074 |
|
| 2075 |
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 2076 |
if (length < 4) { |
| 2077 |
obj->num_comments = i; |
| 2078 |
goto skip; |
| 2079 |
} |
| 2080 |
else |
| 2081 |
length -= 4; |
| 2082 |
if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) { |
| 2083 |
obj->num_comments = i; |
| 2084 |
return false; /* read_callback_ sets the state for us */ |
| 2085 |
} |
| 2086 |
if (length < obj->comments[i].length) { |
| 2087 |
obj->num_comments = i; |
| 2088 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2089 |
return false; |
| 2090 |
} |
| 2091 |
else |
| 2092 |
length -= obj->comments[i].length; |
| 2093 |
if (0 == (obj->comments[i].entry = safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) { |
| 2094 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2095 |
obj->num_comments = i; |
| 2096 |
return false; |
| 2097 |
} |
| 2098 |
memset (obj->comments[i].entry, 0, obj->comments[i].length) ; |
| 2099 |
if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length)) { |
| 2100 |
/* Current i-th entry is bad, so we delete it. */ |
| 2101 |
free (obj->comments[i].entry) ; |
| 2102 |
obj->comments[i].entry = NULL ; |
| 2103 |
obj->num_comments = i; |
| 2104 |
goto skip; |
| 2105 |
} |
| 2106 |
obj->comments[i].entry[obj->comments[i].length] = '\0'; |
| 2107 |
} |
| 2108 |
} |
| 2109 |
} |
| 2110 |
else { |
| 2111 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2112 |
return false; |
| 2113 |
} |
| 2114 |
|
| 2115 |
skip: |
| 2116 |
if (length > 0) { |
| 2117 |
/* length > 0 can only happen on files with invalid data in comments */ |
| 2118 |
if(obj->num_comments < 1) { |
| 2119 |
free(obj->comments); |
| 2120 |
obj->comments = NULL; |
| 2121 |
} |
| 2122 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2123 |
return false; |
| 2124 |
} |
| 2125 |
|
| 2126 |
return true; |
| 2127 |
} |
| 2128 |
|
| 2129 |
FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj) |
| 2130 |
{ |
| 2131 |
FLAC__uint32 i, j, x; |
| 2132 |
|
| 2133 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 2134 |
|
| 2135 |
memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet)); |
| 2136 |
|
| 2137 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2138 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8)) |
| 2139 |
return false; /* read_callback_ sets the state for us */ |
| 2140 |
|
| 2141 |
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN)) |
| 2142 |
return false; /* read_callback_ sets the state for us */ |
| 2143 |
|
| 2144 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN)) |
| 2145 |
return false; /* read_callback_ sets the state for us */ |
| 2146 |
obj->is_cd = x? true : false; |
| 2147 |
|
| 2148 |
if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)) |
| 2149 |
return false; /* read_callback_ sets the state for us */ |
| 2150 |
|
| 2151 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN)) |
| 2152 |
return false; /* read_callback_ sets the state for us */ |
| 2153 |
obj->num_tracks = x; |
| 2154 |
|
| 2155 |
if(obj->num_tracks > 0) { |
| 2156 |
if(0 == (obj->tracks = safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) { |
| 2157 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2158 |
return false; |
| 2159 |
} |
| 2160 |
for(i = 0; i < obj->num_tracks; i++) { |
| 2161 |
FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i]; |
| 2162 |
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN)) |
| 2163 |
return false; /* read_callback_ sets the state for us */ |
| 2164 |
|
| 2165 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN)) |
| 2166 |
return false; /* read_callback_ sets the state for us */ |
| 2167 |
track->number = (FLAC__byte)x; |
| 2168 |
|
| 2169 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2170 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8)) |
| 2171 |
return false; /* read_callback_ sets the state for us */ |
| 2172 |
|
| 2173 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN)) |
| 2174 |
return false; /* read_callback_ sets the state for us */ |
| 2175 |
track->type = x; |
| 2176 |
|
| 2177 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN)) |
| 2178 |
return false; /* read_callback_ sets the state for us */ |
| 2179 |
track->pre_emphasis = x; |
| 2180 |
|
| 2181 |
if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)) |
| 2182 |
return false; /* read_callback_ sets the state for us */ |
| 2183 |
|
| 2184 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN)) |
| 2185 |
return false; /* read_callback_ sets the state for us */ |
| 2186 |
track->num_indices = (FLAC__byte)x; |
| 2187 |
|
| 2188 |
if(track->num_indices > 0) { |
| 2189 |
if(0 == (track->indices = safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) { |
| 2190 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2191 |
return false; |
| 2192 |
} |
| 2193 |
for(j = 0; j < track->num_indices; j++) { |
| 2194 |
FLAC__StreamMetadata_CueSheet_Index *indx = &track->indices[j]; |
| 2195 |
if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN)) |
| 2196 |
return false; /* read_callback_ sets the state for us */ |
| 2197 |
|
| 2198 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN)) |
| 2199 |
return false; /* read_callback_ sets the state for us */ |
| 2200 |
indx->number = (FLAC__byte)x; |
| 2201 |
|
| 2202 |
if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN)) |
| 2203 |
return false; /* read_callback_ sets the state for us */ |
| 2204 |
} |
| 2205 |
} |
| 2206 |
} |
| 2207 |
} |
| 2208 |
else { /* obj->num_tracks == 0 */ |
| 2209 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2210 |
return false; |
| 2211 |
} |
| 2212 |
|
| 2213 |
return true; |
| 2214 |
} |
| 2215 |
|
| 2216 |
FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj) |
| 2217 |
{ |
| 2218 |
FLAC__uint32 x; |
| 2219 |
|
| 2220 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 2221 |
|
| 2222 |
/* read type */ |
| 2223 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN)) |
| 2224 |
return false; /* read_callback_ sets the state for us */ |
| 2225 |
if(x < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED) |
| 2226 |
obj->type = x; |
| 2227 |
else |
| 2228 |
obj->type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER; |
| 2229 |
|
| 2230 |
/* read MIME type */ |
| 2231 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) |
| 2232 |
return false; /* read_callback_ sets the state for us */ |
| 2233 |
if(FLAC__bitreader_limit_remaining(decoder->private_->input) < x){ |
| 2234 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2235 |
return false; |
| 2236 |
} |
| 2237 |
if(0 == (obj->mime_type = safe_malloc_add_2op_(x, /*+*/1))) { |
| 2238 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2239 |
return false; |
| 2240 |
} |
| 2241 |
if(x > 0) { |
| 2242 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x)) |
| 2243 |
return false; /* read_callback_ sets the state for us */ |
| 2244 |
} |
| 2245 |
obj->mime_type[x] = '\0'; |
| 2246 |
|
| 2247 |
/* read description */ |
| 2248 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) |
| 2249 |
return false; /* read_callback_ sets the state for us */ |
| 2250 |
if(FLAC__bitreader_limit_remaining(decoder->private_->input) < x){ |
| 2251 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2252 |
return false; |
| 2253 |
} |
| 2254 |
if(0 == (obj->description = safe_malloc_add_2op_(x, /*+*/1))) { |
| 2255 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2256 |
return false; |
| 2257 |
} |
| 2258 |
if(x > 0) { |
| 2259 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x)) |
| 2260 |
return false; /* read_callback_ sets the state for us */ |
| 2261 |
} |
| 2262 |
obj->description[x] = '\0'; |
| 2263 |
|
| 2264 |
/* read width */ |
| 2265 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN)) |
| 2266 |
return false; /* read_callback_ sets the state for us */ |
| 2267 |
|
| 2268 |
/* read height */ |
| 2269 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN)) |
| 2270 |
return false; /* read_callback_ sets the state for us */ |
| 2271 |
|
| 2272 |
/* read depth */ |
| 2273 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN)) |
| 2274 |
return false; /* read_callback_ sets the state for us */ |
| 2275 |
|
| 2276 |
/* read colors */ |
| 2277 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN)) |
| 2278 |
return false; /* read_callback_ sets the state for us */ |
| 2279 |
|
| 2280 |
/* read data */ |
| 2281 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) |
| 2282 |
return false; /* read_callback_ sets the state for us */ |
| 2283 |
if(FLAC__bitreader_limit_remaining(decoder->private_->input) < obj->data_length){ |
| 2284 |
FLAC__bitreader_limit_invalidate(decoder->private_->input); |
| 2285 |
return false; |
| 2286 |
} |
| 2287 |
if(0 == (obj->data = safe_malloc_(obj->data_length))) { |
| 2288 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2289 |
return false; |
| 2290 |
} |
| 2291 |
if(obj->data_length > 0) { |
| 2292 |
if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length)) |
| 2293 |
return false; /* read_callback_ sets the state for us */ |
| 2294 |
} |
| 2295 |
|
| 2296 |
return true; |
| 2297 |
} |
| 2298 |
|
| 2299 |
FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) |
| 2300 |
{ |
| 2301 |
FLAC__uint32 x; |
| 2302 |
uint32_t i, skip; |
| 2303 |
|
| 2304 |
/* skip the version and flags bytes */ |
| 2305 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24)) |
| 2306 |
return false; /* read_callback_ sets the state for us */ |
| 2307 |
/* get the size (in bytes) to skip */ |
| 2308 |
skip = 0; |
| 2309 |
for(i = 0; i < 4; i++) { |
| 2310 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2311 |
return false; /* read_callback_ sets the state for us */ |
| 2312 |
skip <<= 7; |
| 2313 |
skip |= (x & 0x7f); |
| 2314 |
} |
| 2315 |
/* skip the rest of the tag */ |
| 2316 |
if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip)) |
| 2317 |
return false; /* read_callback_ sets the state for us */ |
| 2318 |
return true; |
| 2319 |
} |
| 2320 |
|
| 2321 |
FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) |
| 2322 |
{ |
| 2323 |
FLAC__uint32 x; |
| 2324 |
FLAC__bool first = true; |
| 2325 |
|
| 2326 |
/* make sure we're byte aligned */ |
| 2327 |
if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { |
| 2328 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) |
| 2329 |
return false; /* read_callback_ sets the state for us */ |
| 2330 |
} |
| 2331 |
|
| 2332 |
while(1) { |
| 2333 |
if(decoder->private_->cached) { |
| 2334 |
x = (FLAC__uint32)decoder->private_->lookahead; |
| 2335 |
decoder->private_->cached = false; |
| 2336 |
} |
| 2337 |
else { |
| 2338 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2339 |
return false; /* read_callback_ sets the state for us */ |
| 2340 |
} |
| 2341 |
if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 2342 |
decoder->private_->header_warmup[0] = (FLAC__byte)x; |
| 2343 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2344 |
return false; /* read_callback_ sets the state for us */ |
| 2345 |
|
| 2346 |
/* 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 */ |
| 2347 |
/* else we have to check if the second byte is the end of a sync code */ |
| 2348 |
if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 2349 |
decoder->private_->lookahead = (FLAC__byte)x; |
| 2350 |
decoder->private_->cached = true; |
| 2351 |
} |
| 2352 |
else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */ |
| 2353 |
decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 2354 |
decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
| 2355 |
|
| 2356 |
/* Save location so we can rewind in case the frame turns |
| 2357 |
* out to be invalid after the header */ |
| 2358 |
FLAC__bitreader_set_framesync_location(decoder->private_->input); |
| 2359 |
if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->last_seen_framesync)) |
| 2360 |
decoder->private_->last_seen_framesync = 0; |
| 2361 |
return true; |
| 2362 |
} |
| 2363 |
} |
| 2364 |
if(first) { |
| 2365 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2366 |
first = false; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
return true; |
| 2371 |
} |
| 2372 |
|
| 2373 |
FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode) |
| 2374 |
{ |
| 2375 |
uint32_t channel; |
| 2376 |
uint32_t i; |
| 2377 |
uint32_t frame_crc; /* the one we calculate from the input stream */ |
| 2378 |
FLAC__uint32 x; |
| 2379 |
|
| 2380 |
*got_a_frame = false; |
| 2381 |
decoder->private_->side_subframe_in_use = false; |
| 2382 |
|
| 2383 |
/* init the CRC */ |
| 2384 |
frame_crc = 0; |
| 2385 |
frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); |
| 2386 |
frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); |
| 2387 |
FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc); |
| 2388 |
|
| 2389 |
if(!read_frame_header_(decoder)) |
| 2390 |
return false; |
| 2391 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */ |
| 2392 |
return true; |
| 2393 |
if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels, decoder->private_->frame.header.bits_per_sample)) |
| 2394 |
return false; |
| 2395 |
for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
| 2396 |
/* |
| 2397 |
* first figure the correct bits-per-sample of the subframe |
| 2398 |
*/ |
| 2399 |
uint32_t bps = decoder->private_->frame.header.bits_per_sample; |
| 2400 |
switch(decoder->private_->frame.header.channel_assignment) { |
| 2401 |
case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 2402 |
/* no adjustment needed */ |
| 2403 |
break; |
| 2404 |
case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 2405 |
FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2406 |
if(channel == 1) |
| 2407 |
bps++; |
| 2408 |
break; |
| 2409 |
case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 2410 |
FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2411 |
if(channel == 0) |
| 2412 |
bps++; |
| 2413 |
break; |
| 2414 |
case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 2415 |
FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2416 |
if(channel == 1) |
| 2417 |
bps++; |
| 2418 |
break; |
| 2419 |
default: |
| 2420 |
FLAC__ASSERT(0); |
| 2421 |
} |
| 2422 |
/* |
| 2423 |
* now read it |
| 2424 |
*/ |
| 2425 |
if(!read_subframe_(decoder, channel, bps, do_full_decode)){ |
| 2426 |
/* read_callback_ sets the state for us */ |
| 2427 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM) |
| 2428 |
break; |
| 2429 |
else |
| 2430 |
return false; |
| 2431 |
} |
| 2432 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) |
| 2433 |
break; |
| 2434 |
} |
| 2435 |
|
| 2436 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM) |
| 2437 |
if(!read_zero_padding_(decoder)) |
| 2438 |
return false; |
| 2439 |
|
| 2440 |
/* |
| 2441 |
* Read the frame CRC-16 from the footer and check |
| 2442 |
*/ |
| 2443 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) { |
| 2444 |
frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input); |
| 2445 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN)) { |
| 2446 |
/* read_callback_ sets the state for us */ |
| 2447 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM) |
| 2448 |
return false; |
| 2449 |
} |
| 2450 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2451 |
} |
| 2452 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME && frame_crc == x) { |
| 2453 |
#endif |
| 2454 |
if(do_full_decode) { |
| 2455 |
/* Undo any special channel coding */ |
| 2456 |
undo_channel_coding(decoder); |
| 2457 |
/* Check whether decoded data actually fits bps */ |
| 2458 |
for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
| 2459 |
int shift_bits = 32 - decoder->private_->frame.header.bits_per_sample; |
| 2460 |
int lower_limit = INT32_MIN >> shift_bits; |
| 2461 |
int upper_limit = INT32_MAX >> shift_bits; |
| 2462 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 2463 |
/* Check whether shift_bits MSBs are 'empty' by shifting up and down */ |
| 2464 |
if((decoder->private_->output[channel][i] < lower_limit) || |
| 2465 |
(decoder->private_->output[channel][i] > upper_limit)) { |
| 2466 |
/* Bad frame, emit error */ |
| 2467 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_OUT_OF_BOUNDS); |
| 2468 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2469 |
break; |
| 2470 |
} |
| 2471 |
} |
| 2472 |
} |
| 2473 |
} |
| 2474 |
} |
| 2475 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2476 |
else if (decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) { |
| 2477 |
/* Bad frame, emit error */ |
| 2478 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH); |
| 2479 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2480 |
} |
| 2481 |
#endif |
| 2482 |
|
| 2483 |
/* Check whether frames are missing, if so, add silence to compensate */ |
| 2484 |
if(decoder->private_->last_frame_is_set && decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME && !decoder->private_->is_seeking && do_full_decode) { |
| 2485 |
FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2486 |
FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2487 |
if(decoder->private_->last_frame.header.number.sample_number + decoder->private_->last_frame.header.blocksize < decoder->private_->frame.header.number.sample_number) { |
| 2488 |
uint32_t padding_samples_needed = decoder->private_->frame.header.number.sample_number - (decoder->private_->last_frame.header.number.sample_number + decoder->private_->last_frame.header.blocksize); |
| 2489 |
/* Send an error that we lost sync, but only in case no error |
| 2490 |
* has been sent yet. This is the case if exactly one or more |
| 2491 |
* frames are missing, and the frames before and after it |
| 2492 |
* are complete */ |
| 2493 |
if(!decoder->private_->error_has_been_sent) |
| 2494 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_MISSING_FRAME); |
| 2495 |
/* Do some extra validation to assure last frame an current frame |
| 2496 |
* header are both valid before adding silence inbetween |
| 2497 |
* Technically both frames could be valid with differing sample_rates, |
| 2498 |
* channels and bits_per_sample, but it is quite rare */ |
| 2499 |
if(decoder->private_->last_frame.header.sample_rate == decoder->private_->frame.header.sample_rate && |
| 2500 |
decoder->private_->last_frame.header.channels == decoder->private_->frame.header.channels && |
| 2501 |
decoder->private_->last_frame.header.bits_per_sample == decoder->private_->frame.header.bits_per_sample && |
| 2502 |
decoder->private_->last_frame.header.blocksize >= 16) { |
| 2503 |
FLAC__Frame empty_frame; |
| 2504 |
FLAC__int32 * empty_buffer[FLAC__MAX_CHANNELS] = {NULL}; |
| 2505 |
empty_frame.header = decoder->private_->last_frame.header; |
| 2506 |
empty_frame.footer.crc = 0; |
| 2507 |
for(i = 0; i < empty_frame.header.channels; i++) { |
| 2508 |
empty_buffer[i] = safe_calloc_(empty_frame.header.blocksize, sizeof(FLAC__int32)); |
| 2509 |
if(empty_buffer[i] == NULL) { |
| 2510 |
for(i = 0; i < empty_frame.header.channels; i++) |
| 2511 |
if(empty_buffer[i] != NULL) |
| 2512 |
free(empty_buffer[i]); |
| 2513 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2514 |
return false; |
| 2515 |
} |
| 2516 |
} |
| 2517 |
/* No repairs larger than 5 seconds or 50 frames are made, to not |
| 2518 |
* unexpectedly create enormous files when one of the headers was |
| 2519 |
* corrupt after all */ |
| 2520 |
if(padding_samples_needed > (5*empty_frame.header.sample_rate)) |
| 2521 |
padding_samples_needed = 5*empty_frame.header.sample_rate; |
| 2522 |
if(padding_samples_needed > (50*empty_frame.header.blocksize)) |
| 2523 |
padding_samples_needed = 50*empty_frame.header.blocksize; |
| 2524 |
while(padding_samples_needed){ |
| 2525 |
empty_frame.header.number.sample_number += empty_frame.header.blocksize; |
| 2526 |
if(padding_samples_needed < empty_frame.header.blocksize) |
| 2527 |
empty_frame.header.blocksize = padding_samples_needed; |
| 2528 |
padding_samples_needed -= empty_frame.header.blocksize; |
| 2529 |
decoder->protected_->blocksize = empty_frame.header.blocksize; |
| 2530 |
|
| 2531 |
FLAC__ASSERT(empty_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2532 |
decoder->private_->samples_decoded = empty_frame.header.number.sample_number + empty_frame.header.blocksize; |
| 2533 |
|
| 2534 |
for(channel = 0; channel < empty_frame.header.channels; channel++) { |
| 2535 |
empty_frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 2536 |
empty_frame.subframes[channel].data.constant.value = 0; |
| 2537 |
empty_frame.subframes[channel].wasted_bits = 0; |
| 2538 |
} |
| 2539 |
|
| 2540 |
if(write_audio_frame_to_client_(decoder, &empty_frame, (const FLAC__int32 * const *)empty_buffer) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) { |
| 2541 |
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 2542 |
for(i = 0; i < empty_frame.header.channels; i++) |
| 2543 |
if(empty_buffer[i] != NULL) |
| 2544 |
free(empty_buffer[i]); |
| 2545 |
return false; |
| 2546 |
} |
| 2547 |
} |
| 2548 |
for(i = 0; i < empty_frame.header.channels; i++) |
| 2549 |
if(empty_buffer[i] != NULL) |
| 2550 |
free(empty_buffer[i]); |
| 2551 |
|
| 2552 |
} |
| 2553 |
} |
| 2554 |
} |
| 2555 |
|
| 2556 |
decoder->private_->error_has_been_sent = false; |
| 2557 |
|
| 2558 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC || decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM) { |
| 2559 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2560 |
decoder->private_->fuzzing_rewind_count++; /* To stop excessive rewinding, as it causes timeouts */ |
| 2561 |
#endif |
| 2562 |
/* Got corruption, rewind if possible. Return value of seek |
| 2563 |
* isn't checked, if the seek fails the decoder will continue anyway */ |
| 2564 |
if(!FLAC__bitreader_rewind_to_after_last_seen_framesync(decoder->private_->input)){ |
| 2565 |
#ifndef NDEBUG |
| 2566 |
flac_fprintf(stderr, "Rewinding, seeking necessary\n"); |
| 2567 |
#endif |
| 2568 |
if(decoder->private_->seek_callback && decoder->private_->last_seen_framesync){ |
| 2569 |
/* Last framesync isn't in bitreader anymore, rewind with seek if possible */ |
| 2570 |
#ifndef NDEBUG |
| 2571 |
FLAC__uint64 current_decode_position; |
| 2572 |
if(FLAC__stream_decoder_get_decode_position(decoder, ¤t_decode_position)) |
| 2573 |
flac_fprintf(stderr, "Bitreader was %" PRIu64 " bytes short\n", current_decode_position-decoder->private_->last_seen_framesync); |
| 2574 |
#endif |
| 2575 |
if(decoder->private_->seek_callback(decoder, decoder->private_->last_seen_framesync, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) { |
| 2576 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 2577 |
return false; |
| 2578 |
} |
| 2579 |
if(!FLAC__bitreader_clear(decoder->private_->input)) { |
| 2580 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2581 |
return false; |
| 2582 |
} |
| 2583 |
} |
| 2584 |
} |
| 2585 |
#ifndef NDEBUG |
| 2586 |
else{ |
| 2587 |
flac_fprintf(stderr, "Rewinding, seeking not necessary\n"); |
| 2588 |
} |
| 2589 |
#endif |
| 2590 |
} |
| 2591 |
else { |
| 2592 |
*got_a_frame = true; |
| 2593 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2594 |
decoder->private_->fuzzing_rewind_count = 0; |
| 2595 |
#endif |
| 2596 |
|
| 2597 |
/* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */ |
| 2598 |
if(decoder->private_->next_fixed_block_size) |
| 2599 |
decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size; |
| 2600 |
|
| 2601 |
/* put the latest values into the public section of the decoder instance */ |
| 2602 |
decoder->protected_->channels = decoder->private_->frame.header.channels; |
| 2603 |
decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; |
| 2604 |
decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; |
| 2605 |
decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; |
| 2606 |
decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; |
| 2607 |
|
| 2608 |
FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2609 |
decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize; |
| 2610 |
|
| 2611 |
/* write it */ |
| 2612 |
if(do_full_decode) { |
| 2613 |
if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) { |
| 2614 |
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 2615 |
return false; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
} |
| 2619 |
|
| 2620 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2621 |
return true; |
| 2622 |
} |
| 2623 |
|
| 2624 |
FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) |
| 2625 |
{ |
| 2626 |
FLAC__uint32 x; |
| 2627 |
FLAC__uint64 xx; |
| 2628 |
uint32_t i, blocksize_hint = 0, sample_rate_hint = 0; |
| 2629 |
FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */ |
| 2630 |
uint32_t raw_header_len; |
| 2631 |
FLAC__bool is_unparseable = false; |
| 2632 |
|
| 2633 |
FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 2634 |
|
| 2635 |
/* init the raw header with the saved bits from synchronization */ |
| 2636 |
raw_header[0] = decoder->private_->header_warmup[0]; |
| 2637 |
raw_header[1] = decoder->private_->header_warmup[1]; |
| 2638 |
raw_header_len = 2; |
| 2639 |
|
| 2640 |
/* check to make sure that reserved bit is 0 */ |
| 2641 |
if(raw_header[1] & 0x02) /* MAGIC NUMBER */ |
| 2642 |
is_unparseable = true; |
| 2643 |
|
| 2644 |
/* |
| 2645 |
* Note that along the way as we read the header, we look for a sync |
| 2646 |
* code inside. If we find one it would indicate that our original |
| 2647 |
* sync was bad since there cannot be a sync code in a valid header. |
| 2648 |
* |
| 2649 |
* Three kinds of things can go wrong when reading the frame header: |
| 2650 |
* 1) We may have sync'ed incorrectly and not landed on a frame header. |
| 2651 |
* If we don't find a sync code, it can end up looking like we read |
| 2652 |
* a valid but unparseable header, until getting to the frame header |
| 2653 |
* CRC. Even then we could get a false positive on the CRC. |
| 2654 |
* 2) We may have sync'ed correctly but on an unparseable frame (from a |
| 2655 |
* future encoder). |
| 2656 |
* 3) We may be on a damaged frame which appears valid but unparseable. |
| 2657 |
* |
| 2658 |
* For all these reasons, we try and read a complete frame header as |
| 2659 |
* long as it seems valid, even if unparseable, up until the frame |
| 2660 |
* header CRC. |
| 2661 |
*/ |
| 2662 |
|
| 2663 |
/* |
| 2664 |
* read in the raw header as bytes so we can CRC it, and parse it on the way |
| 2665 |
*/ |
| 2666 |
for(i = 0; i < 2; i++) { |
| 2667 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2668 |
return false; /* read_callback_ sets the state for us */ |
| 2669 |
if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 2670 |
/* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */ |
| 2671 |
decoder->private_->lookahead = (FLAC__byte)x; |
| 2672 |
decoder->private_->cached = true; |
| 2673 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2674 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2675 |
return true; |
| 2676 |
} |
| 2677 |
raw_header[raw_header_len++] = (FLAC__byte)x; |
| 2678 |
} |
| 2679 |
|
| 2680 |
switch(x = raw_header[2] >> 4) { |
| 2681 |
case 0: |
| 2682 |
is_unparseable = true; |
| 2683 |
break; |
| 2684 |
case 1: |
| 2685 |
decoder->private_->frame.header.blocksize = 192; |
| 2686 |
break; |
| 2687 |
case 2: |
| 2688 |
case 3: |
| 2689 |
case 4: |
| 2690 |
case 5: |
| 2691 |
decoder->private_->frame.header.blocksize = 576 << (x-2); |
| 2692 |
break; |
| 2693 |
case 6: |
| 2694 |
case 7: |
| 2695 |
blocksize_hint = x; |
| 2696 |
break; |
| 2697 |
case 8: |
| 2698 |
case 9: |
| 2699 |
case 10: |
| 2700 |
case 11: |
| 2701 |
case 12: |
| 2702 |
case 13: |
| 2703 |
case 14: |
| 2704 |
case 15: |
| 2705 |
decoder->private_->frame.header.blocksize = 256 << (x-8); |
| 2706 |
break; |
| 2707 |
default: |
| 2708 |
FLAC__ASSERT(0); |
| 2709 |
break; |
| 2710 |
} |
| 2711 |
|
| 2712 |
switch(x = raw_header[2] & 0x0f) { |
| 2713 |
case 0: |
| 2714 |
if(decoder->private_->has_stream_info) |
| 2715 |
decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate; |
| 2716 |
else |
| 2717 |
is_unparseable = true; |
| 2718 |
break; |
| 2719 |
case 1: |
| 2720 |
decoder->private_->frame.header.sample_rate = 88200; |
| 2721 |
break; |
| 2722 |
case 2: |
| 2723 |
decoder->private_->frame.header.sample_rate = 176400; |
| 2724 |
break; |
| 2725 |
case 3: |
| 2726 |
decoder->private_->frame.header.sample_rate = 192000; |
| 2727 |
break; |
| 2728 |
case 4: |
| 2729 |
decoder->private_->frame.header.sample_rate = 8000; |
| 2730 |
break; |
| 2731 |
case 5: |
| 2732 |
decoder->private_->frame.header.sample_rate = 16000; |
| 2733 |
break; |
| 2734 |
case 6: |
| 2735 |
decoder->private_->frame.header.sample_rate = 22050; |
| 2736 |
break; |
| 2737 |
case 7: |
| 2738 |
decoder->private_->frame.header.sample_rate = 24000; |
| 2739 |
break; |
| 2740 |
case 8: |
| 2741 |
decoder->private_->frame.header.sample_rate = 32000; |
| 2742 |
break; |
| 2743 |
case 9: |
| 2744 |
decoder->private_->frame.header.sample_rate = 44100; |
| 2745 |
break; |
| 2746 |
case 10: |
| 2747 |
decoder->private_->frame.header.sample_rate = 48000; |
| 2748 |
break; |
| 2749 |
case 11: |
| 2750 |
decoder->private_->frame.header.sample_rate = 96000; |
| 2751 |
break; |
| 2752 |
case 12: |
| 2753 |
case 13: |
| 2754 |
case 14: |
| 2755 |
sample_rate_hint = x; |
| 2756 |
break; |
| 2757 |
case 15: |
| 2758 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2759 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2760 |
return true; |
| 2761 |
default: |
| 2762 |
FLAC__ASSERT(0); |
| 2763 |
} |
| 2764 |
|
| 2765 |
x = (uint32_t)(raw_header[3] >> 4); |
| 2766 |
if(x & 8) { |
| 2767 |
decoder->private_->frame.header.channels = 2; |
| 2768 |
switch(x & 7) { |
| 2769 |
case 0: |
| 2770 |
decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; |
| 2771 |
break; |
| 2772 |
case 1: |
| 2773 |
decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; |
| 2774 |
break; |
| 2775 |
case 2: |
| 2776 |
decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; |
| 2777 |
break; |
| 2778 |
default: |
| 2779 |
is_unparseable = true; |
| 2780 |
break; |
| 2781 |
} |
| 2782 |
} |
| 2783 |
else { |
| 2784 |
decoder->private_->frame.header.channels = (uint32_t)x + 1; |
| 2785 |
decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
| 2786 |
} |
| 2787 |
|
| 2788 |
switch(x = (uint32_t)(raw_header[3] & 0x0e) >> 1) { |
| 2789 |
case 0: |
| 2790 |
if(decoder->private_->has_stream_info) |
| 2791 |
decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample; |
| 2792 |
else |
| 2793 |
is_unparseable = true; |
| 2794 |
break; |
| 2795 |
case 1: |
| 2796 |
decoder->private_->frame.header.bits_per_sample = 8; |
| 2797 |
break; |
| 2798 |
case 2: |
| 2799 |
decoder->private_->frame.header.bits_per_sample = 12; |
| 2800 |
break; |
| 2801 |
case 3: |
| 2802 |
is_unparseable = true; |
| 2803 |
break; |
| 2804 |
case 4: |
| 2805 |
decoder->private_->frame.header.bits_per_sample = 16; |
| 2806 |
break; |
| 2807 |
case 5: |
| 2808 |
decoder->private_->frame.header.bits_per_sample = 20; |
| 2809 |
break; |
| 2810 |
case 6: |
| 2811 |
decoder->private_->frame.header.bits_per_sample = 24; |
| 2812 |
break; |
| 2813 |
case 7: |
| 2814 |
decoder->private_->frame.header.bits_per_sample = 32; |
| 2815 |
break; |
| 2816 |
default: |
| 2817 |
FLAC__ASSERT(0); |
| 2818 |
break; |
| 2819 |
} |
| 2820 |
|
| 2821 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2822 |
/* check to make sure that reserved bit is 0 */ |
| 2823 |
if(raw_header[3] & 0x01) /* MAGIC NUMBER */ |
| 2824 |
is_unparseable = true; |
| 2825 |
#endif |
| 2826 |
|
| 2827 |
/* read the frame's starting sample number (or frame number as the case may be) */ |
| 2828 |
if( |
| 2829 |
raw_header[1] & 0x01 || |
| 2830 |
/*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */ |
| 2831 |
(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize) |
| 2832 |
) { /* variable blocksize */ |
| 2833 |
if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len)) |
| 2834 |
return false; /* read_callback_ sets the state for us */ |
| 2835 |
if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */ |
| 2836 |
decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 2837 |
decoder->private_->cached = true; |
| 2838 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2839 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2840 |
return true; |
| 2841 |
} |
| 2842 |
decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 2843 |
decoder->private_->frame.header.number.sample_number = xx; |
| 2844 |
} |
| 2845 |
else { /* fixed blocksize */ |
| 2846 |
if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len)) |
| 2847 |
return false; /* read_callback_ sets the state for us */ |
| 2848 |
if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ |
| 2849 |
decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 2850 |
decoder->private_->cached = true; |
| 2851 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2852 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2853 |
return true; |
| 2854 |
} |
| 2855 |
decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
| 2856 |
decoder->private_->frame.header.number.frame_number = x; |
| 2857 |
} |
| 2858 |
|
| 2859 |
if(blocksize_hint) { |
| 2860 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2861 |
return false; /* read_callback_ sets the state for us */ |
| 2862 |
raw_header[raw_header_len++] = (FLAC__byte)x; |
| 2863 |
if(blocksize_hint == 7) { |
| 2864 |
FLAC__uint32 _x; |
| 2865 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) |
| 2866 |
return false; /* read_callback_ sets the state for us */ |
| 2867 |
raw_header[raw_header_len++] = (FLAC__byte)_x; |
| 2868 |
x = (x << 8) | _x; |
| 2869 |
} |
| 2870 |
decoder->private_->frame.header.blocksize = x+1; |
| 2871 |
if(decoder->private_->frame.header.blocksize > 65535) { /* invalid blocksize (65536) specified */ |
| 2872 |
decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 2873 |
decoder->private_->cached = true; |
| 2874 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2875 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2876 |
return true; |
| 2877 |
} |
| 2878 |
|
| 2879 |
} |
| 2880 |
|
| 2881 |
if(sample_rate_hint) { |
| 2882 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2883 |
return false; /* read_callback_ sets the state for us */ |
| 2884 |
raw_header[raw_header_len++] = (FLAC__byte)x; |
| 2885 |
if(sample_rate_hint != 12) { |
| 2886 |
FLAC__uint32 _x; |
| 2887 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) |
| 2888 |
return false; /* read_callback_ sets the state for us */ |
| 2889 |
raw_header[raw_header_len++] = (FLAC__byte)_x; |
| 2890 |
x = (x << 8) | _x; |
| 2891 |
} |
| 2892 |
if(sample_rate_hint == 12) |
| 2893 |
decoder->private_->frame.header.sample_rate = x*1000; |
| 2894 |
else if(sample_rate_hint == 13) |
| 2895 |
decoder->private_->frame.header.sample_rate = x; |
| 2896 |
else |
| 2897 |
decoder->private_->frame.header.sample_rate = x*10; |
| 2898 |
} |
| 2899 |
|
| 2900 |
/* read the CRC-8 byte */ |
| 2901 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2902 |
return false; /* read_callback_ sets the state for us */ |
| 2903 |
crc8 = (FLAC__byte)x; |
| 2904 |
|
| 2905 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2906 |
if(FLAC__crc8(raw_header, raw_header_len) != crc8) { |
| 2907 |
#else |
| 2908 |
if(decoder->private_->fuzzing_rewind_count > 32 && (FLAC__crc8(raw_header, raw_header_len) << 4) != (crc8 << 4)) { |
| 2909 |
#endif |
| 2910 |
|
| 2911 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2912 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2913 |
return true; |
| 2914 |
} |
| 2915 |
|
| 2916 |
/* calculate the sample number from the frame number if needed */ |
| 2917 |
decoder->private_->next_fixed_block_size = 0; |
| 2918 |
if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) { |
| 2919 |
x = decoder->private_->frame.header.number.frame_number; |
| 2920 |
decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 2921 |
if(decoder->private_->fixed_block_size) |
| 2922 |
decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x; |
| 2923 |
else if(decoder->private_->has_stream_info) { |
| 2924 |
if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) { |
| 2925 |
decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x; |
| 2926 |
decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize; |
| 2927 |
} |
| 2928 |
else |
| 2929 |
is_unparseable = true; |
| 2930 |
} |
| 2931 |
else if(x == 0) { |
| 2932 |
decoder->private_->frame.header.number.sample_number = 0; |
| 2933 |
decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize; |
| 2934 |
} |
| 2935 |
else { |
| 2936 |
/* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */ |
| 2937 |
decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x; |
| 2938 |
} |
| 2939 |
} |
| 2940 |
|
| 2941 |
if(is_unparseable) { |
| 2942 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2943 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2944 |
return true; |
| 2945 |
} |
| 2946 |
|
| 2947 |
return true; |
| 2948 |
} |
| 2949 |
|
| 2950 |
FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode) |
| 2951 |
{ |
| 2952 |
FLAC__uint32 x; |
| 2953 |
FLAC__bool wasted_bits; |
| 2954 |
uint32_t i; |
| 2955 |
|
| 2956 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */ |
| 2957 |
return false; /* read_callback_ sets the state for us */ |
| 2958 |
|
| 2959 |
wasted_bits = (x & 1); |
| 2960 |
x &= 0xfe; |
| 2961 |
|
| 2962 |
if(wasted_bits) { |
| 2963 |
uint32_t u; |
| 2964 |
if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u)) |
| 2965 |
return false; /* read_callback_ sets the state for us */ |
| 2966 |
decoder->private_->frame.subframes[channel].wasted_bits = u+1; |
| 2967 |
if (decoder->private_->frame.subframes[channel].wasted_bits >= bps) { |
| 2968 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2969 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2970 |
return true; |
| 2971 |
} |
| 2972 |
bps -= decoder->private_->frame.subframes[channel].wasted_bits; |
| 2973 |
} |
| 2974 |
else |
| 2975 |
decoder->private_->frame.subframes[channel].wasted_bits = 0; |
| 2976 |
|
| 2977 |
/* |
| 2978 |
* Lots of magic numbers here |
| 2979 |
*/ |
| 2980 |
if(x & 0x80) { |
| 2981 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2982 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2983 |
return true; |
| 2984 |
} |
| 2985 |
else if(x == 0) { |
| 2986 |
if(!read_subframe_constant_(decoder, channel, bps, do_full_decode)) |
| 2987 |
return false; |
| 2988 |
} |
| 2989 |
else if(x == 2) { |
| 2990 |
if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode)) |
| 2991 |
return false; |
| 2992 |
} |
| 2993 |
else if(x < 16) { |
| 2994 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2995 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2996 |
return true; |
| 2997 |
} |
| 2998 |
else if(x <= 24) { |
| 2999 |
uint32_t predictor_order = (x>>1)&7; |
| 3000 |
if(decoder->private_->frame.header.blocksize <= predictor_order){ |
| 3001 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3002 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3003 |
return true; |
| 3004 |
} |
| 3005 |
if(!read_subframe_fixed_(decoder, channel, bps, predictor_order, do_full_decode)) |
| 3006 |
return false; |
| 3007 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ |
| 3008 |
return true; |
| 3009 |
} |
| 3010 |
else if(x < 64) { |
| 3011 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 3012 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3013 |
return true; |
| 3014 |
} |
| 3015 |
else { |
| 3016 |
uint32_t predictor_order = ((x>>1)&31)+1; |
| 3017 |
if(decoder->private_->frame.header.blocksize <= predictor_order){ |
| 3018 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3019 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3020 |
return true; |
| 3021 |
} |
| 3022 |
if(!read_subframe_lpc_(decoder, channel, bps, predictor_order, do_full_decode)) |
| 3023 |
return false; |
| 3024 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ |
| 3025 |
return true; |
| 3026 |
} |
| 3027 |
|
| 3028 |
if(wasted_bits && do_full_decode) { |
| 3029 |
x = decoder->private_->frame.subframes[channel].wasted_bits; |
| 3030 |
if((bps + x) < 33) { |
| 3031 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 3032 |
uint32_t val = decoder->private_->output[channel][i]; |
| 3033 |
decoder->private_->output[channel][i] = (val << x); |
| 3034 |
} |
| 3035 |
} |
| 3036 |
else { |
| 3037 |
/* When there are wasted bits, bps is never 33 and so |
| 3038 |
* side_subframe is never already in use */ |
| 3039 |
FLAC__ASSERT(!decoder->private_->side_subframe_in_use); |
| 3040 |
decoder->private_->side_subframe_in_use = true; |
| 3041 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 3042 |
uint64_t val = decoder->private_->output[channel][i]; |
| 3043 |
decoder->private_->side_subframe[i] = (val << x); |
| 3044 |
} |
| 3045 |
} |
| 3046 |
} |
| 3047 |
|
| 3048 |
return true; |
| 3049 |
} |
| 3050 |
|
| 3051 |
FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode) |
| 3052 |
{ |
| 3053 |
FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; |
| 3054 |
FLAC__int64 x; |
| 3055 |
uint32_t i; |
| 3056 |
|
| 3057 |
decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 3058 |
|
| 3059 |
if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &x, bps)) |
| 3060 |
return false; /* read_callback_ sets the state for us */ |
| 3061 |
|
| 3062 |
subframe->value = x; |
| 3063 |
|
| 3064 |
/* decode the subframe */ |
| 3065 |
if(do_full_decode) { |
| 3066 |
if(bps <= 32) { |
| 3067 |
FLAC__int32 *output = decoder->private_->output[channel]; |
| 3068 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 3069 |
output[i] = x; |
| 3070 |
} else { |
| 3071 |
FLAC__int64 *output = decoder->private_->side_subframe; |
| 3072 |
decoder->private_->side_subframe_in_use = true; |
| 3073 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 3074 |
output[i] = x; |
| 3075 |
} |
| 3076 |
} |
| 3077 |
|
| 3078 |
return true; |
| 3079 |
} |
| 3080 |
|
| 3081 |
FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode) |
| 3082 |
{ |
| 3083 |
FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; |
| 3084 |
FLAC__int64 i64; |
| 3085 |
FLAC__uint32 u32; |
| 3086 |
uint32_t u; |
| 3087 |
|
| 3088 |
decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; |
| 3089 |
|
| 3090 |
subframe->residual = decoder->private_->residual[channel]; |
| 3091 |
subframe->order = order; |
| 3092 |
|
| 3093 |
/* read warm-up samples */ |
| 3094 |
for(u = 0; u < order; u++) { |
| 3095 |
if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &i64, bps)) |
| 3096 |
return false; /* read_callback_ sets the state for us */ |
| 3097 |
subframe->warmup[u] = i64; |
| 3098 |
} |
| 3099 |
|
| 3100 |
/* read entropy coding method info */ |
| 3101 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) |
| 3102 |
return false; /* read_callback_ sets the state for us */ |
| 3103 |
subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; |
| 3104 |
switch(subframe->entropy_coding_method.type) { |
| 3105 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 3106 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 3107 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 3108 |
return false; /* read_callback_ sets the state for us */ |
| 3109 |
if((decoder->private_->frame.header.blocksize >> u32 < order) || |
| 3110 |
(decoder->private_->frame.header.blocksize % (1 << u32) > 0)) { |
| 3111 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3112 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3113 |
return true; |
| 3114 |
} |
| 3115 |
subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
| 3116 |
subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; |
| 3117 |
break; |
| 3118 |
default: |
| 3119 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 3120 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3121 |
return true; |
| 3122 |
} |
| 3123 |
|
| 3124 |
/* read residual */ |
| 3125 |
switch(subframe->entropy_coding_method.type) { |
| 3126 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 3127 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 3128 |
if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2)) |
| 3129 |
return false; |
| 3130 |
break; |
| 3131 |
default: |
| 3132 |
FLAC__ASSERT(0); |
| 3133 |
} |
| 3134 |
|
| 3135 |
/* decode the subframe */ |
| 3136 |
if(do_full_decode) { |
| 3137 |
if(bps < 33){ |
| 3138 |
uint32_t i; |
| 3139 |
for(i = 0; i < order; i++) |
| 3140 |
decoder->private_->output[channel][i] = subframe->warmup[i]; |
| 3141 |
if(bps+order <= 32) |
| 3142 |
FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); |
| 3143 |
else |
| 3144 |
FLAC__fixed_restore_signal_wide(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); |
| 3145 |
} |
| 3146 |
else { |
| 3147 |
decoder->private_->side_subframe_in_use = true; |
| 3148 |
memcpy(decoder->private_->side_subframe, subframe->warmup, sizeof(FLAC__int64) * order); |
| 3149 |
FLAC__fixed_restore_signal_wide_33bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->side_subframe+order); |
| 3150 |
} |
| 3151 |
} |
| 3152 |
|
| 3153 |
return true; |
| 3154 |
} |
| 3155 |
|
| 3156 |
FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode) |
| 3157 |
{ |
| 3158 |
FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; |
| 3159 |
FLAC__int32 i32; |
| 3160 |
FLAC__int64 i64; |
| 3161 |
FLAC__uint32 u32; |
| 3162 |
uint32_t u; |
| 3163 |
|
| 3164 |
decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; |
| 3165 |
|
| 3166 |
subframe->residual = decoder->private_->residual[channel]; |
| 3167 |
subframe->order = order; |
| 3168 |
|
| 3169 |
/* read warm-up samples */ |
| 3170 |
for(u = 0; u < order; u++) { |
| 3171 |
if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &i64, bps)) |
| 3172 |
return false; /* read_callback_ sets the state for us */ |
| 3173 |
subframe->warmup[u] = i64; |
| 3174 |
} |
| 3175 |
|
| 3176 |
/* read qlp coeff precision */ |
| 3177 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
| 3178 |
return false; /* read_callback_ sets the state for us */ |
| 3179 |
if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { |
| 3180 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3181 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3182 |
return true; |
| 3183 |
} |
| 3184 |
subframe->qlp_coeff_precision = u32+1; |
| 3185 |
|
| 3186 |
/* read qlp shift */ |
| 3187 |
if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN)) |
| 3188 |
return false; /* read_callback_ sets the state for us */ |
| 3189 |
if(i32 < 0) { |
| 3190 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3191 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3192 |
return true; |
| 3193 |
} |
| 3194 |
subframe->quantization_level = i32; |
| 3195 |
|
| 3196 |
/* read quantized lp coefficiencts */ |
| 3197 |
for(u = 0; u < order; u++) { |
| 3198 |
if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision)) |
| 3199 |
return false; /* read_callback_ sets the state for us */ |
| 3200 |
subframe->qlp_coeff[u] = i32; |
| 3201 |
} |
| 3202 |
|
| 3203 |
/* read entropy coding method info */ |
| 3204 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) |
| 3205 |
return false; /* read_callback_ sets the state for us */ |
| 3206 |
subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; |
| 3207 |
switch(subframe->entropy_coding_method.type) { |
| 3208 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 3209 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 3210 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 3211 |
return false; /* read_callback_ sets the state for us */ |
| 3212 |
if((decoder->private_->frame.header.blocksize >> u32 < order) || |
| 3213 |
(decoder->private_->frame.header.blocksize % (1 << u32) > 0)) { |
| 3214 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3215 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3216 |
return true; |
| 3217 |
} |
| 3218 |
subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
| 3219 |
subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; |
| 3220 |
break; |
| 3221 |
default: |
| 3222 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 3223 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3224 |
return true; |
| 3225 |
} |
| 3226 |
|
| 3227 |
/* read residual */ |
| 3228 |
switch(subframe->entropy_coding_method.type) { |
| 3229 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 3230 |
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 3231 |
if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2)) |
| 3232 |
return false; |
| 3233 |
break; |
| 3234 |
default: |
| 3235 |
FLAC__ASSERT(0); |
| 3236 |
} |
| 3237 |
|
| 3238 |
/* decode the subframe */ |
| 3239 |
if(do_full_decode) { |
| 3240 |
if(bps <= 32) { |
| 3241 |
uint32_t i; |
| 3242 |
for(i = 0; i < order; i++) |
| 3243 |
decoder->private_->output[channel][i] = subframe->warmup[i]; |
| 3244 |
if(FLAC__lpc_max_residual_bps(bps, subframe->qlp_coeff, order, subframe->quantization_level) <= 32 && |
| 3245 |
FLAC__lpc_max_prediction_before_shift_bps(bps, subframe->qlp_coeff, order) <= 32) |
| 3246 |
FLAC__lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 3247 |
else |
| 3248 |
FLAC__lpc_restore_signal_wide(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 3249 |
} |
| 3250 |
else { |
| 3251 |
decoder->private_->side_subframe_in_use = true; |
| 3252 |
memcpy(decoder->private_->side_subframe, subframe->warmup, sizeof(FLAC__int64) * order); |
| 3253 |
FLAC__lpc_restore_signal_wide_33bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->side_subframe+order); |
| 3254 |
} |
| 3255 |
} |
| 3256 |
|
| 3257 |
return true; |
| 3258 |
} |
| 3259 |
|
| 3260 |
FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode) |
| 3261 |
{ |
| 3262 |
FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; |
| 3263 |
uint32_t i; |
| 3264 |
|
| 3265 |
decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 3266 |
|
| 3267 |
if(bps < 33) { |
| 3268 |
FLAC__int32 x, *residual = decoder->private_->residual[channel]; |
| 3269 |
|
| 3270 |
subframe->data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32; |
| 3271 |
subframe->data.int32 = residual; |
| 3272 |
|
| 3273 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 3274 |
if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) |
| 3275 |
return false; /* read_callback_ sets the state for us */ |
| 3276 |
residual[i] = x; |
| 3277 |
} |
| 3278 |
|
| 3279 |
/* decode the subframe */ |
| 3280 |
if(do_full_decode) |
| 3281 |
memcpy(decoder->private_->output[channel], subframe->data.int32, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); |
| 3282 |
} |
| 3283 |
else { |
| 3284 |
FLAC__int64 x, *side = decoder->private_->side_subframe; |
| 3285 |
|
| 3286 |
subframe->data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT64; |
| 3287 |
subframe->data.int64 = side; |
| 3288 |
decoder->private_->side_subframe_in_use = true; |
| 3289 |
|
| 3290 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 3291 |
if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &x, bps)) |
| 3292 |
return false; /* read_callback_ sets the state for us */ |
| 3293 |
side[i] = x; |
| 3294 |
} |
| 3295 |
} |
| 3296 |
|
| 3297 |
return true; |
| 3298 |
} |
| 3299 |
|
| 3300 |
FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, uint32_t predictor_order, uint32_t partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended) |
| 3301 |
{ |
| 3302 |
FLAC__uint32 rice_parameter; |
| 3303 |
int i; |
| 3304 |
uint32_t partition, sample, u; |
| 3305 |
const uint32_t partitions = 1u << partition_order; |
| 3306 |
const uint32_t partition_samples = decoder->private_->frame.header.blocksize >> partition_order; |
| 3307 |
const uint32_t plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 3308 |
const uint32_t pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 3309 |
|
| 3310 |
/* invalid predictor and partition orders mush be handled in the callers */ |
| 3311 |
FLAC__ASSERT(partition_order > 0? partition_samples >= predictor_order : decoder->private_->frame.header.blocksize >= predictor_order); |
| 3312 |
|
| 3313 |
if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order))) { |
| 3314 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 3315 |
return false; |
| 3316 |
} |
| 3317 |
|
| 3318 |
sample = 0; |
| 3319 |
for(partition = 0; partition < partitions; partition++) { |
| 3320 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen)) |
| 3321 |
return false; /* read_callback_ sets the state for us */ |
| 3322 |
partitioned_rice_contents->parameters[partition] = rice_parameter; |
| 3323 |
if(rice_parameter < pesc) { |
| 3324 |
partitioned_rice_contents->raw_bits[partition] = 0; |
| 3325 |
u = (partition == 0) ? partition_samples - predictor_order : partition_samples; |
| 3326 |
if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter)){ |
| 3327 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) { |
| 3328 |
/* no error was set, read_callback_ didn't set it, so |
| 3329 |
* invalid rice symbol was found */ |
| 3330 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3331 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3332 |
return true; |
| 3333 |
} |
| 3334 |
else |
| 3335 |
return false; /* read_callback_ sets the state for us */ |
| 3336 |
} |
| 3337 |
sample += u; |
| 3338 |
} |
| 3339 |
else { |
| 3340 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN)) |
| 3341 |
return false; /* read_callback_ sets the state for us */ |
| 3342 |
partitioned_rice_contents->raw_bits[partition] = rice_parameter; |
| 3343 |
if(rice_parameter == 0) { |
| 3344 |
for(u = (partition == 0)? predictor_order : 0; u < partition_samples; u++, sample++) |
| 3345 |
residual[sample] = 0; |
| 3346 |
} |
| 3347 |
else{ |
| 3348 |
for(u = (partition == 0)? predictor_order : 0; u < partition_samples; u++, sample++) { |
| 3349 |
if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter)) |
| 3350 |
return false; /* read_callback_ sets the state for us */ |
| 3351 |
residual[sample] = i; |
| 3352 |
} |
| 3353 |
} |
| 3354 |
} |
| 3355 |
} |
| 3356 |
|
| 3357 |
return true; |
| 3358 |
} |
| 3359 |
|
| 3360 |
FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) |
| 3361 |
{ |
| 3362 |
if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { |
| 3363 |
FLAC__uint32 zero = 0; |
| 3364 |
if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) |
| 3365 |
return false; /* read_callback_ sets the state for us */ |
| 3366 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 3367 |
if(zero != 0) { |
| 3368 |
send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 3369 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 3370 |
} |
| 3371 |
#endif |
| 3372 |
} |
| 3373 |
return true; |
| 3374 |
} |
| 3375 |
|
| 3376 |
FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 3377 |
{ |
| 3378 |
FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; |
| 3379 |
|
| 3380 |
if( |
| 3381 |
#if FLAC__HAS_OGG |
| 3382 |
/* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ |
| 3383 |
!decoder->private_->is_ogg && |
| 3384 |
#endif |
| 3385 |
decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) |
| 3386 |
) { |
| 3387 |
*bytes = 0; |
| 3388 |
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 3389 |
return false; |
| 3390 |
} |
| 3391 |
else if(*bytes > 0) { |
| 3392 |
/* While seeking, it is possible for our seek to land in the |
| 3393 |
* middle of audio data that looks exactly like a frame header |
| 3394 |
* from a future version of an encoder. When that happens, our |
| 3395 |
* error callback will get an |
| 3396 |
* FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its |
| 3397 |
* unparseable_frame_count. But there is a remote possibility |
| 3398 |
* that it is properly synced at such a "future-codec frame", |
| 3399 |
* so to make sure, we wait to see many "unparseable" errors in |
| 3400 |
* a row before bailing out. |
| 3401 |
*/ |
| 3402 |
if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) { |
| 3403 |
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 3404 |
return false; |
| 3405 |
} |
| 3406 |
else { |
| 3407 |
const FLAC__StreamDecoderReadStatus status = |
| 3408 |
#if FLAC__HAS_OGG |
| 3409 |
decoder->private_->is_ogg? |
| 3410 |
read_callback_ogg_aspect_(decoder, buffer, bytes) : |
| 3411 |
#endif |
| 3412 |
decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data) |
| 3413 |
; |
| 3414 |
if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) { |
| 3415 |
#if FLAC__HAS_OGG |
| 3416 |
if(decoder->private_->ogg_decoder_aspect_allocation_failure) { |
| 3417 |
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 3418 |
return false; |
| 3419 |
} |
| 3420 |
#endif |
| 3421 |
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 3422 |
return false; |
| 3423 |
} |
| 3424 |
else if(*bytes == 0) { |
| 3425 |
if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK) { |
| 3426 |
if(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) |
| 3427 |
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_LINK; |
| 3428 |
else |
| 3429 |
decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR; |
| 3430 |
return false; |
| 3431 |
} |
| 3432 |
else if( |
| 3433 |
status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM || |
| 3434 |
( |
| 3435 |
#if FLAC__HAS_OGG |
| 3436 |
/* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ |
| 3437 |
!decoder->private_->is_ogg && |
| 3438 |
#endif |
| 3439 |
decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) |
| 3440 |
) |
| 3441 |
) { |
| 3442 |
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 3443 |
return false; |
| 3444 |
} |
| 3445 |
else |
| 3446 |
return true; |
| 3447 |
} |
| 3448 |
else |
| 3449 |
return true; |
| 3450 |
} |
| 3451 |
} |
| 3452 |
else { |
| 3453 |
/* abort to avoid a deadlock */ |
| 3454 |
decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 3455 |
return false; |
| 3456 |
} |
| 3457 |
/* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around |
| 3458 |
* for Ogg FLAC. This is because the ogg decoder aspect can lose sync |
| 3459 |
* and at the same time hit the end of the stream (for example, seeking |
| 3460 |
* to a point that is after the beginning of the last Ogg page). There |
| 3461 |
* is no way to report an Ogg sync loss through the callbacks (see note |
| 3462 |
* in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0. |
| 3463 |
* So to keep the decoder from stopping at this point we gate the call |
| 3464 |
* to the eof_callback and let the Ogg decoder aspect set the |
| 3465 |
* end-of-stream state when it is needed. |
| 3466 |
*/ |
| 3467 |
} |
| 3468 |
|
| 3469 |
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW) |
| 3470 |
/* The attribute below is to silence the undefined sanitizer of oss-fuzz. |
| 3471 |
* Because fuzzing feeds bogus predictors and residual samples to the |
| 3472 |
* decoder, having overflows in this section is unavoidable. Also, |
| 3473 |
* because the calculated values are audio path only, there is no |
| 3474 |
* potential for security problems */ |
| 3475 |
__attribute__((no_sanitize("signed-integer-overflow"))) |
| 3476 |
#endif |
| 3477 |
void undo_channel_coding(FLAC__StreamDecoder *decoder) { |
| 3478 |
uint32_t i; |
| 3479 |
switch(decoder->private_->frame.header.channel_assignment) { |
| 3480 |
case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 3481 |
/* do nothing */ |
| 3482 |
break; |
| 3483 |
case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 3484 |
FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 3485 |
FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32)); |
| 3486 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 3487 |
if(decoder->private_->side_subframe_in_use) |
| 3488 |
decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->side_subframe[i]; |
| 3489 |
else |
| 3490 |
decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; |
| 3491 |
break; |
| 3492 |
case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 3493 |
FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 3494 |
FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32)); |
| 3495 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 3496 |
if(decoder->private_->side_subframe_in_use) |
| 3497 |
decoder->private_->output[0][i] = decoder->private_->output[1][i] + decoder->private_->side_subframe[i]; |
| 3498 |
else |
| 3499 |
decoder->private_->output[0][i] += decoder->private_->output[1][i]; |
| 3500 |
break; |
| 3501 |
case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 3502 |
FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 3503 |
FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32)); |
| 3504 |
for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 3505 |
if(!decoder->private_->side_subframe_in_use){ |
| 3506 |
FLAC__int32 mid, side; |
| 3507 |
mid = decoder->private_->output[0][i]; |
| 3508 |
side = decoder->private_->output[1][i]; |
| 3509 |
mid = ((uint32_t) mid) << 1; |
| 3510 |
mid |= (side & 1); /* i.e. if 'side' is odd... */ |
| 3511 |
decoder->private_->output[0][i] = (mid + side) >> 1; |
| 3512 |
decoder->private_->output[1][i] = (mid - side) >> 1; |
| 3513 |
} |
| 3514 |
else { /* bps == 32 */ |
| 3515 |
FLAC__int64 mid; |
| 3516 |
mid = ((uint64_t)decoder->private_->output[0][i]) << 1; |
| 3517 |
mid |= (decoder->private_->side_subframe[i] & 1); /* i.e. if 'side' is odd... */ |
| 3518 |
decoder->private_->output[0][i] = (mid + decoder->private_->side_subframe[i]) >> 1; |
| 3519 |
decoder->private_->output[1][i] = (mid - decoder->private_->side_subframe[i]) >> 1; |
| 3520 |
} |
| 3521 |
} |
| 3522 |
break; |
| 3523 |
default: |
| 3524 |
FLAC__ASSERT(0); |
| 3525 |
break; |
| 3526 |
} |
| 3527 |
} |
| 3528 |
|
| 3529 |
#if FLAC__HAS_OGG |
| 3530 |
FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes) |
| 3531 |
{ |
| 3532 |
switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder->private_->tell_callback, decoder, decoder->private_->client_data)) { |
| 3533 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK: |
| 3534 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 3535 |
/* we don't really have a way to handle lost sync via read |
| 3536 |
* callback so we'll let it pass and let the underlying |
| 3537 |
* FLAC decoder catch the error |
| 3538 |
*/ |
| 3539 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC: |
| 3540 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 3541 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM: |
| 3542 |
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 3543 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_LINK: |
| 3544 |
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK; |
| 3545 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC: |
| 3546 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION: |
| 3547 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT: |
| 3548 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR: |
| 3549 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 3550 |
case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR: |
| 3551 |
decoder->private_->ogg_decoder_aspect_allocation_failure = true; |
| 3552 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 3553 |
default: |
| 3554 |
FLAC__ASSERT(0); |
| 3555 |
/* double protection */ |
| 3556 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 3557 |
} |
| 3558 |
} |
| 3559 |
|
| 3560 |
FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 3561 |
{ |
| 3562 |
FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder; |
| 3563 |
|
| 3564 |
switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) { |
| 3565 |
case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE: |
| 3566 |
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK; |
| 3567 |
case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM: |
| 3568 |
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM; |
| 3569 |
case FLAC__STREAM_DECODER_READ_STATUS_ABORT: |
| 3570 |
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; |
| 3571 |
default: |
| 3572 |
/* double protection: */ |
| 3573 |
FLAC__ASSERT(0); |
| 3574 |
return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; |
| 3575 |
} |
| 3576 |
} |
| 3577 |
#endif |
| 3578 |
|
| 3579 |
FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) |
| 3580 |
{ |
| 3581 |
decoder->private_->last_frame = *frame; /* save the frame */ |
| 3582 |
decoder->private_->last_frame_is_set = true; |
| 3583 |
if(decoder->private_->is_seeking && !decoder->private_->is_indexing) { |
| 3584 |
FLAC__uint64 this_frame_sample = frame->header.number.sample_number; |
| 3585 |
FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize; |
| 3586 |
FLAC__uint64 target_sample = decoder->private_->target_sample; |
| 3587 |
|
| 3588 |
FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 3589 |
|
| 3590 |
decoder->private_->got_a_frame = true; |
| 3591 |
|
| 3592 |
if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */ |
| 3593 |
uint32_t delta = (uint32_t)(target_sample - this_frame_sample); |
| 3594 |
/* kick out of seek mode */ |
| 3595 |
decoder->private_->is_seeking = false; |
| 3596 |
/* shift out the samples before target_sample */ |
| 3597 |
if(delta > 0) { |
| 3598 |
uint32_t channel; |
| 3599 |
const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS]; |
| 3600 |
for(channel = 0; channel < frame->header.channels; channel++) { |
| 3601 |
newbuffer[channel] = buffer[channel] + delta; |
| 3602 |
decoder->private_->last_frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 3603 |
decoder->private_->last_frame.subframes[channel].data.verbatim.data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32; |
| 3604 |
decoder->private_->last_frame.subframes[channel].data.verbatim.data.int32 = newbuffer[channel]; |
| 3605 |
} |
| 3606 |
decoder->private_->last_frame.header.blocksize -= delta; |
| 3607 |
decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta; |
| 3608 |
/* write the relevant samples */ |
| 3609 |
return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data); |
| 3610 |
} |
| 3611 |
else { |
| 3612 |
/* write the relevant samples */ |
| 3613 |
return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); |
| 3614 |
} |
| 3615 |
} |
| 3616 |
else { |
| 3617 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 3618 |
} |
| 3619 |
} |
| 3620 |
else if(!decoder->private_->is_indexing) { |
| 3621 |
/* |
| 3622 |
* If we never got STREAMINFO, turn off MD5 checking to save |
| 3623 |
* cycles since we don't have a sum to compare to anyway |
| 3624 |
*/ |
| 3625 |
if(!decoder->private_->has_stream_info) |
| 3626 |
decoder->private_->do_md5_checking = false; |
| 3627 |
if(decoder->private_->do_md5_checking) { |
| 3628 |
if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8)) |
| 3629 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 3630 |
} |
| 3631 |
return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); |
| 3632 |
} |
| 3633 |
else { /* decoder->private_->is_indexing == true */ |
| 3634 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 3635 |
} |
| 3636 |
} |
| 3637 |
|
| 3638 |
void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status) |
| 3639 |
{ |
| 3640 |
if(!decoder->private_->is_seeking) { |
| 3641 |
decoder->private_->error_has_been_sent = true; |
| 3642 |
decoder->private_->error_callback(decoder, status, decoder->private_->client_data); |
| 3643 |
} |
| 3644 |
else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM) |
| 3645 |
decoder->private_->unparseable_frame_count++; |
| 3646 |
} |
| 3647 |
|
| 3648 |
FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) |
| 3649 |
{ |
| 3650 |
FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample; |
| 3651 |
FLAC__int64 pos = -1; |
| 3652 |
int i; |
| 3653 |
uint32_t approx_bytes_per_frame; |
| 3654 |
FLAC__bool first_seek = true, seek_from_lower_bound = false; |
| 3655 |
const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder); |
| 3656 |
const uint32_t min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize; |
| 3657 |
const uint32_t max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize; |
| 3658 |
const uint32_t max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize; |
| 3659 |
const uint32_t min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize; |
| 3660 |
/* take these from the current frame in case they've changed mid-stream */ |
| 3661 |
uint32_t channels = FLAC__stream_decoder_get_channels(decoder); |
| 3662 |
uint32_t bps = FLAC__stream_decoder_get_bits_per_sample(decoder); |
| 3663 |
const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0; |
| 3664 |
|
| 3665 |
/* use values from stream info if we didn't decode a frame */ |
| 3666 |
if(channels == 0) |
| 3667 |
channels = decoder->private_->stream_info.data.stream_info.channels; |
| 3668 |
if(bps == 0) |
| 3669 |
bps = decoder->private_->stream_info.data.stream_info.bits_per_sample; |
| 3670 |
|
| 3671 |
/* we are just guessing here */ |
| 3672 |
if(max_framesize > 0) |
| 3673 |
approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1; |
| 3674 |
/* |
| 3675 |
* Check if it's a known fixed-blocksize stream. Note that though |
| 3676 |
* the spec doesn't allow zeroes in the STREAMINFO block, we may |
| 3677 |
* never get a STREAMINFO block when decoding so the value of |
| 3678 |
* min_blocksize might be zero. |
| 3679 |
*/ |
| 3680 |
else if(min_blocksize == max_blocksize && min_blocksize > 0) { |
| 3681 |
/* note there are no () around 'bps/8' to keep precision up since it's an integer calculation */ |
| 3682 |
approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64; |
| 3683 |
} |
| 3684 |
else |
| 3685 |
approx_bytes_per_frame = 4096 * channels * bps/8 + 64; |
| 3686 |
|
| 3687 |
/* |
| 3688 |
* First, we set an upper and lower bound on where in the |
| 3689 |
* stream we will search. For now we take the current position |
| 3690 |
* as one bound and, depending on where the target position lies, |
| 3691 |
* the beginning of the first frame or the end of the stream as |
| 3692 |
* the other bound. |
| 3693 |
*/ |
| 3694 |
lower_bound = first_frame_offset; |
| 3695 |
lower_bound_sample = 0; |
| 3696 |
upper_bound = stream_length; |
| 3697 |
upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/; |
| 3698 |
|
| 3699 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && |
| 3700 |
decoder->private_->samples_decoded != 0) { |
| 3701 |
if(target_sample < decoder->private_->samples_decoded) { |
| 3702 |
if(FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) |
| 3703 |
upper_bound_sample = decoder->private_->samples_decoded; |
| 3704 |
} else { |
| 3705 |
if(FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) |
| 3706 |
lower_bound_sample = decoder->private_->samples_decoded; |
| 3707 |
} |
| 3708 |
} |
| 3709 |
|
| 3710 |
/* |
| 3711 |
* Now we refine the bounds if we have a seektable with |
| 3712 |
* suitable points. Note that according to the spec they |
| 3713 |
* must be ordered by ascending sample number. |
| 3714 |
* |
| 3715 |
* Note: to protect against invalid seek tables we will ignore points |
| 3716 |
* that have frame_samples==0 or sample_number>=total_samples. Also, |
| 3717 |
* because math is limited to 64-bit ints, seekpoints with an offset |
| 3718 |
* larger than 2^63 (8 exbibyte) are rejected. |
| 3719 |
*/ |
| 3720 |
if(seek_table) { |
| 3721 |
FLAC__uint64 new_lower_bound = lower_bound; |
| 3722 |
FLAC__uint64 new_upper_bound = upper_bound; |
| 3723 |
FLAC__uint64 new_lower_bound_sample = lower_bound_sample; |
| 3724 |
FLAC__uint64 new_upper_bound_sample = upper_bound_sample; |
| 3725 |
|
| 3726 |
/* find the closest seek point <= target_sample, if it exists */ |
| 3727 |
for(i = (int)seek_table->num_points - 1; i >= 0; i--) { |
| 3728 |
if( |
| 3729 |
seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && |
| 3730 |
seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ |
| 3731 |
(total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ |
| 3732 |
seek_table->points[i].sample_number <= target_sample |
| 3733 |
) |
| 3734 |
break; |
| 3735 |
} |
| 3736 |
if(i >= 0) { /* i.e. we found a suitable seek point... */ |
| 3737 |
new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset; |
| 3738 |
new_lower_bound_sample = seek_table->points[i].sample_number; |
| 3739 |
} |
| 3740 |
|
| 3741 |
/* find the closest seek point > target_sample, if it exists */ |
| 3742 |
for(i = 0; i < (int)seek_table->num_points; i++) { |
| 3743 |
if( |
| 3744 |
seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && |
| 3745 |
seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ |
| 3746 |
(total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ |
| 3747 |
seek_table->points[i].sample_number > target_sample |
| 3748 |
) |
| 3749 |
break; |
| 3750 |
} |
| 3751 |
if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */ |
| 3752 |
new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset; |
| 3753 |
new_upper_bound_sample = seek_table->points[i].sample_number; |
| 3754 |
} |
| 3755 |
/* final protection against unsorted seek tables; keep original values if bogus */ |
| 3756 |
if(new_upper_bound >= new_lower_bound) { |
| 3757 |
lower_bound = new_lower_bound; |
| 3758 |
upper_bound = new_upper_bound; |
| 3759 |
lower_bound_sample = new_lower_bound_sample; |
| 3760 |
upper_bound_sample = new_upper_bound_sample; |
| 3761 |
} |
| 3762 |
} |
| 3763 |
|
| 3764 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 3765 |
FLAC__ASSERT(upper_bound_sample >= lower_bound_sample); |
| 3766 |
#endif |
| 3767 |
/* there are 3 insidious ways that the following equality occurs, which |
| 3768 |
* we need to fix: |
| 3769 |
* 1) total_samples is 0 (unknown) and target_sample is 0 |
| 3770 |
* 2) total_samples is 0 (unknown) and target_sample happens to be |
| 3771 |
* exactly equal to the last seek point in the seek table; this |
| 3772 |
* means there is no seek point above it, and upper_bound_samples |
| 3773 |
* remains equal to the estimate (of target_samples) we made above |
| 3774 |
* in either case it does not hurt to move upper_bound_sample up by 1 |
| 3775 |
* 3) the file is corrupt |
| 3776 |
*/ |
| 3777 |
if(upper_bound_sample == lower_bound_sample) { |
| 3778 |
if(total_samples == 0) |
| 3779 |
upper_bound_sample++; |
| 3780 |
else { |
| 3781 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3782 |
return false; |
| 3783 |
} |
| 3784 |
} |
| 3785 |
|
| 3786 |
decoder->private_->target_sample = target_sample; |
| 3787 |
while(1) { |
| 3788 |
/* check whether decoder is still valid so bad state isn't overwritten |
| 3789 |
* with seek error */ |
| 3790 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR || |
| 3791 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) |
| 3792 |
return false; |
| 3793 |
/* check if the bounds are still ok */ |
| 3794 |
if (lower_bound_sample >= upper_bound_sample || |
| 3795 |
lower_bound > upper_bound || |
| 3796 |
upper_bound >= INT64_MAX || |
| 3797 |
target_sample > upper_bound_sample) { |
| 3798 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3799 |
return false; |
| 3800 |
} |
| 3801 |
if(seek_from_lower_bound) { |
| 3802 |
pos = lower_bound; |
| 3803 |
} |
| 3804 |
else { |
| 3805 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3806 |
pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)(target_sample - lower_bound_sample) / (double)(upper_bound_sample - lower_bound_sample) * (double)(upper_bound - lower_bound)) - approx_bytes_per_frame; |
| 3807 |
#else |
| 3808 |
/* a little less accurate: */ |
| 3809 |
if(upper_bound - lower_bound < 0xffffffff) |
| 3810 |
pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame; |
| 3811 |
else { /* @@@ WATCHOUT, ~2TB limit */ |
| 3812 |
FLAC__uint64 ratio = (1<<16) / (upper_bound_sample - lower_bound_sample); |
| 3813 |
pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8) * ratio)) - approx_bytes_per_frame; |
| 3814 |
} |
| 3815 |
#endif |
| 3816 |
} |
| 3817 |
if(pos >= (FLAC__int64)upper_bound) |
| 3818 |
pos = (FLAC__int64)upper_bound - 1; |
| 3819 |
if(pos < (FLAC__int64)lower_bound) |
| 3820 |
pos = (FLAC__int64)lower_bound; |
| 3821 |
if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { |
| 3822 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3823 |
return false; |
| 3824 |
} |
| 3825 |
if(!FLAC__stream_decoder_flush(decoder)) { |
| 3826 |
/* above call sets the state for us */ |
| 3827 |
return false; |
| 3828 |
} |
| 3829 |
/* Now we need to get a frame. First we need to reset our |
| 3830 |
* unparseable_frame_count; if we get too many unparseable |
| 3831 |
* frames in a row, the read callback will return |
| 3832 |
* FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing |
| 3833 |
* FLAC__stream_decoder_process_single() to return false. |
| 3834 |
*/ |
| 3835 |
decoder->private_->unparseable_frame_count = 0; |
| 3836 |
if(!FLAC__stream_decoder_process_single(decoder) || decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED || 0 == decoder->private_->samples_decoded) { |
| 3837 |
/* No frame could be decoded */ |
| 3838 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->private_->eof_callback(decoder, decoder->private_->client_data) && !seek_from_lower_bound){ |
| 3839 |
/* decoder has hit end of stream while processing corrupt |
| 3840 |
* frame. To remedy this, try decoding a frame at the lower |
| 3841 |
* bound so the seek after that hopefully ends up somewhere |
| 3842 |
* else */ |
| 3843 |
seek_from_lower_bound = true; |
| 3844 |
continue; |
| 3845 |
} |
| 3846 |
else { |
| 3847 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) |
| 3848 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3849 |
return false; |
| 3850 |
} |
| 3851 |
} |
| 3852 |
seek_from_lower_bound = false; |
| 3853 |
|
| 3854 |
/* our write callback will change the state when it gets to the target frame */ |
| 3855 |
/* 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 */ |
| 3856 |
if(!decoder->private_->is_seeking) |
| 3857 |
break; |
| 3858 |
|
| 3859 |
FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 3860 |
this_frame_sample = decoder->private_->last_frame.header.number.sample_number; |
| 3861 |
|
| 3862 |
if(this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek) { |
| 3863 |
if (pos == (FLAC__int64)lower_bound) { |
| 3864 |
/* can't move back any more than the first frame, something is fatally wrong */ |
| 3865 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3866 |
return false; |
| 3867 |
} |
| 3868 |
/* our last move backwards wasn't big enough, try again */ |
| 3869 |
approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16; |
| 3870 |
continue; |
| 3871 |
} |
| 3872 |
/* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */ |
| 3873 |
first_seek = false; |
| 3874 |
|
| 3875 |
/* make sure we are not seeking in corrupted stream */ |
| 3876 |
if (this_frame_sample < lower_bound_sample) { |
| 3877 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3878 |
return false; |
| 3879 |
} |
| 3880 |
|
| 3881 |
/* we need to narrow the search */ |
| 3882 |
if(target_sample < this_frame_sample) { |
| 3883 |
upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; |
| 3884 |
/*@@@@@@ what will decode position be if at end of stream? */ |
| 3885 |
if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) { |
| 3886 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3887 |
return false; |
| 3888 |
} |
| 3889 |
approx_bytes_per_frame = (uint32_t)(2 * (upper_bound - pos) / 3 + 16); |
| 3890 |
} |
| 3891 |
else { /* target_sample >= this_frame_sample + this frame's blocksize */ |
| 3892 |
lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; |
| 3893 |
if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) { |
| 3894 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3895 |
return false; |
| 3896 |
} |
| 3897 |
approx_bytes_per_frame = (uint32_t)(2 * (lower_bound - pos) / 3 + 16); |
| 3898 |
} |
| 3899 |
} |
| 3900 |
|
| 3901 |
return true; |
| 3902 |
} |
| 3903 |
|
| 3904 |
#if FLAC__HAS_OGG |
| 3905 |
FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) |
| 3906 |
{ |
| 3907 |
FLAC__uint64 left_pos = 0, right_pos = stream_length; |
| 3908 |
FLAC__uint64 left_sample = 0, right_sample = 0; |
| 3909 |
FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1; |
| 3910 |
FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */ |
| 3911 |
FLAC__bool did_a_seek; |
| 3912 |
FLAC__OggDecoderAspect_TargetLink *target_link; |
| 3913 |
uint32_t current_linknumber = decoder->protected_->ogg_decoder_aspect.current_linknumber; |
| 3914 |
uint32_t iteration = 0; |
| 3915 |
|
| 3916 |
/* In the first iterations, we will calculate the target byte position |
| 3917 |
* by the distance from the target sample to left_sample and |
| 3918 |
* right_sample (let's call it "proportional search"). After that, we |
| 3919 |
* will switch to binary search. |
| 3920 |
*/ |
| 3921 |
uint32_t BINARY_SEARCH_AFTER_ITERATION = 2; |
| 3922 |
|
| 3923 |
/* We will switch to a linear search once our current sample is less |
| 3924 |
* than this number of samples ahead of the target sample |
| 3925 |
*/ |
| 3926 |
static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2; |
| 3927 |
|
| 3928 |
if(FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect)) { |
| 3929 |
/* Check whether target sample is contained by indexed links. |
| 3930 |
* If it is not keep moving forward until found */ |
| 3931 |
decoder->private_->is_indexing = true; |
| 3932 |
while(NULL == (target_link = FLAC__ogg_decoder_aspect_get_target_link(&decoder->protected_->ogg_decoder_aspect, target_sample))) { |
| 3933 |
FLAC__OggDecoderAspectReadStatus status; |
| 3934 |
if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM || |
| 3935 |
decoder->protected_->state == FLAC__STREAM_DECODER_OGG_ERROR || |
| 3936 |
decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR || |
| 3937 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) { |
| 3938 |
/* Target sample is not contained in stream or other error */ |
| 3939 |
decoder->private_->is_indexing = false; |
| 3940 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3941 |
return false; |
| 3942 |
} |
| 3943 |
status = FLAC__ogg_decoder_aspect_skip_link(&decoder->protected_->ogg_decoder_aspect, read_callback_proxy_, decoder->private_->seek_callback, decoder->private_->tell_callback, decoder->private_->length_callback, decoder, decoder->private_->client_data); |
| 3944 |
if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM) |
| 3945 |
decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 3946 |
else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) { |
| 3947 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3948 |
return false; |
| 3949 |
} |
| 3950 |
} |
| 3951 |
decoder->private_->is_indexing = false; |
| 3952 |
/* Target link found, send metadata if necessary */ |
| 3953 |
FLAC__ogg_decoder_aspect_set_seek_parameters(&decoder->protected_->ogg_decoder_aspect, target_link); |
| 3954 |
if(target_link->linknumber != current_linknumber) { |
| 3955 |
if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)target_link->start_byte, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { |
| 3956 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3957 |
return false; |
| 3958 |
} |
| 3959 |
if(!FLAC__stream_decoder_flush(decoder)) { |
| 3960 |
/* above call sets the state for us */ |
| 3961 |
return false; |
| 3962 |
} |
| 3963 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; |
| 3964 |
decoder->private_->is_seeking = false; |
| 3965 |
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) { |
| 3966 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3967 |
return false; |
| 3968 |
} |
| 3969 |
decoder->private_->is_seeking = true; |
| 3970 |
} |
| 3971 |
/* set boundaries */ |
| 3972 |
left_pos = target_link->start_byte; |
| 3973 |
left_sample = 0; |
| 3974 |
right_pos = target_link->end_byte; |
| 3975 |
right_sample = target_link->samples_this_link; |
| 3976 |
target_sample -= target_link->samples_in_preceding_links; |
| 3977 |
} |
| 3978 |
else { |
| 3979 |
right_sample = FLAC__stream_decoder_get_total_samples(decoder); |
| 3980 |
|
| 3981 |
/* If the total number of samples is unknown, use a large value, and |
| 3982 |
* force binary search immediately. |
| 3983 |
*/ |
| 3984 |
if(right_sample == 0) { |
| 3985 |
right_sample = (FLAC__uint64)(-1); |
| 3986 |
BINARY_SEARCH_AFTER_ITERATION = 0; |
| 3987 |
} |
| 3988 |
} |
| 3989 |
|
| 3990 |
decoder->private_->target_sample = target_sample; |
| 3991 |
for( ; ; iteration++) { |
| 3992 |
/* Do sanity checks on bounds */ |
| 3993 |
if(right_pos <= left_pos || right_pos - left_pos < 9) { |
| 3994 |
/* FLAC frame is at least 9 byte in size */ |
| 3995 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3996 |
return false; |
| 3997 |
} |
| 3998 |
if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) { |
| 3999 |
if (iteration >= BINARY_SEARCH_AFTER_ITERATION) { |
| 4000 |
pos = (right_pos + left_pos) / 2; |
| 4001 |
} |
| 4002 |
else { |
| 4003 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4004 |
pos = (FLAC__uint64)((double)(target_sample - left_sample) / (double)(right_sample - left_sample) * (double)(right_pos - left_pos)) + left_pos; |
| 4005 |
#else |
| 4006 |
/* a little less accurate: */ |
| 4007 |
if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff)) |
| 4008 |
pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample)) + left_pos; |
| 4009 |
else /* @@@ WATCHOUT, ~2TB limit */ |
| 4010 |
pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16)) + left_pos; |
| 4011 |
#endif |
| 4012 |
/* @@@ TODO: might want to limit pos to some distance |
| 4013 |
* before EOF, to make sure we land before the last frame, |
| 4014 |
* thereby getting a this_frame_sample and so having a better |
| 4015 |
* estimate. |
| 4016 |
*/ |
| 4017 |
} |
| 4018 |
|
| 4019 |
/* physical seek */ |
| 4020 |
if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { |
| 4021 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 4022 |
return false; |
| 4023 |
} |
| 4024 |
if(!FLAC__stream_decoder_flush(decoder)) { |
| 4025 |
/* above call sets the state for us */ |
| 4026 |
return false; |
| 4027 |
} |
| 4028 |
did_a_seek = true; |
| 4029 |
} |
| 4030 |
else |
| 4031 |
did_a_seek = false; |
| 4032 |
|
| 4033 |
decoder->private_->got_a_frame = false; |
| 4034 |
if(!FLAC__stream_decoder_process_single(decoder) || |
| 4035 |
decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) { |
| 4036 |
if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) |
| 4037 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 4038 |
return false; |
| 4039 |
} |
| 4040 |
if(!decoder->private_->got_a_frame) { |
| 4041 |
if(did_a_seek) { |
| 4042 |
/* this can happen if we seek to a point after the last frame; we drop |
| 4043 |
* to binary search right away in this case to avoid any wasted |
| 4044 |
* iterations of proportional search. |
| 4045 |
*/ |
| 4046 |
right_pos = pos; |
| 4047 |
BINARY_SEARCH_AFTER_ITERATION = 0; |
| 4048 |
} |
| 4049 |
else { |
| 4050 |
/* this can probably only happen if total_samples is unknown and the |
| 4051 |
* target_sample is past the end of the stream |
| 4052 |
*/ |
| 4053 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 4054 |
return false; |
| 4055 |
} |
| 4056 |
} |
| 4057 |
/* our write callback will change the state when it gets to the target frame */ |
| 4058 |
else if(!decoder->private_->is_seeking) { |
| 4059 |
break; |
| 4060 |
} |
| 4061 |
else { |
| 4062 |
this_frame_sample = decoder->private_->last_frame.header.number.sample_number; |
| 4063 |
FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 4064 |
|
| 4065 |
if (did_a_seek) { |
| 4066 |
if (this_frame_sample <= target_sample) { |
| 4067 |
/* The 'equal' case should not happen, since |
| 4068 |
* FLAC__stream_decoder_process_single() |
| 4069 |
* should recognize that it has hit the |
| 4070 |
* target sample and we would exit through |
| 4071 |
* the 'break' above. |
| 4072 |
*/ |
| 4073 |
FLAC__ASSERT(this_frame_sample != target_sample); |
| 4074 |
|
| 4075 |
left_sample = this_frame_sample; |
| 4076 |
/* sanity check to avoid infinite loop */ |
| 4077 |
if (left_pos == pos) { |
| 4078 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 4079 |
return false; |
| 4080 |
} |
| 4081 |
left_pos = pos; |
| 4082 |
} |
| 4083 |
else { |
| 4084 |
right_sample = this_frame_sample; |
| 4085 |
/* sanity check to avoid infinite loop */ |
| 4086 |
if (right_pos == pos) { |
| 4087 |
decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 4088 |
return false; |
| 4089 |
} |
| 4090 |
right_pos = pos; |
| 4091 |
} |
| 4092 |
} |
| 4093 |
} |
| 4094 |
} |
| 4095 |
/* Tell ogg decoder aspect that seeking is done */ |
| 4096 |
FLAC__ogg_decoder_aspect_set_seek_parameters(&decoder->protected_->ogg_decoder_aspect, 0); |
| 4097 |
return true; |
| 4098 |
} |
| 4099 |
#endif |
| 4100 |
|
| 4101 |
FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 4102 |
{ |
| 4103 |
(void)client_data; |
| 4104 |
|
| 4105 |
if(*bytes > 0) { |
| 4106 |
*bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file); |
| 4107 |
if(ferror(decoder->private_->file)) |
| 4108 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 4109 |
else if(*bytes == 0) |
| 4110 |
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 4111 |
else |
| 4112 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 4113 |
} |
| 4114 |
else |
| 4115 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */ |
| 4116 |
} |
| 4117 |
|
| 4118 |
FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) |
| 4119 |
{ |
| 4120 |
(void)client_data; |
| 4121 |
|
| 4122 |
if(decoder->private_->file == stdin) |
| 4123 |
return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; |
| 4124 |
else if(fseeko(decoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) |
| 4125 |
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; |
| 4126 |
else |
| 4127 |
return FLAC__STREAM_DECODER_SEEK_STATUS_OK; |
| 4128 |
} |
| 4129 |
|
| 4130 |
FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 4131 |
{ |
| 4132 |
FLAC__off_t pos; |
| 4133 |
(void)client_data; |
| 4134 |
|
| 4135 |
if(decoder->private_->file == stdin) |
| 4136 |
return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; |
| 4137 |
else if((pos = ftello(decoder->private_->file)) < 0) |
| 4138 |
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; |
| 4139 |
else { |
| 4140 |
*absolute_byte_offset = (FLAC__uint64)pos; |
| 4141 |
return FLAC__STREAM_DECODER_TELL_STATUS_OK; |
| 4142 |
} |
| 4143 |
} |
| 4144 |
|
| 4145 |
FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) |
| 4146 |
{ |
| 4147 |
struct flac_stat_s filestats; |
| 4148 |
(void)client_data; |
| 4149 |
|
| 4150 |
if(decoder->private_->file == stdin) |
| 4151 |
return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; |
| 4152 |
|
| 4153 |
#ifndef FLAC__USE_FILELENGTHI64 |
| 4154 |
if(flac_fstat(fileno(decoder->private_->file), &filestats) != 0) |
| 4155 |
#else |
| 4156 |
filestats.st_size = _filelengthi64(fileno(decoder->private_->file)); |
| 4157 |
if(filestats.st_size < 0) |
| 4158 |
#endif |
| 4159 |
return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; |
| 4160 |
else { |
| 4161 |
*stream_length = (FLAC__uint64)filestats.st_size; |
| 4162 |
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; |
| 4163 |
} |
| 4164 |
} |
| 4165 |
|
| 4166 |
FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data) |
| 4167 |
{ |
| 4168 |
(void)client_data; |
| 4169 |
|
| 4170 |
return feof(decoder->private_->file)? true : false; |
| 4171 |
} |