stream_decoder.c (87130B)
1 /* libFLAC - Free Lossless Audio Codec library 2 * Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of the Xiph.org Foundation nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <stdio.h> 33 #include <stdlib.h> /* for malloc() */ 34 #include <string.h> /* for memset/memcpy() */ 35 #include "FLAC/assert.h" 36 #include "protected/stream_decoder.h" 37 #include "private/bitbuffer.h" 38 #include "private/bitmath.h" 39 #include "private/cpu.h" 40 #include "private/crc.h" 41 #include "private/fixed.h" 42 #include "private/format.h" 43 #include "private/lpc.h" 44 #include "private/memory.h" 45 46 #ifdef HAVE_CONFIG_H 47 #include <config.h> 48 #endif 49 50 #ifdef max 51 #undef max 52 #endif 53 #define max(a,b) ((a)>(b)?(a):(b)) 54 55 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ 56 #ifdef _MSC_VER 57 #define FLAC__U64L(x) x 58 #else 59 #define FLAC__U64L(x) x##LLU 60 #endif 61 62 /*********************************************************************** 63 * 64 * Private static data 65 * 66 ***********************************************************************/ 67 68 static 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 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); 78 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); 79 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); 80 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); 81 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); 82 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); 83 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj); 84 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj); 85 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); 86 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); 87 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode); 88 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); 89 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); 90 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); 91 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); 92 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); 93 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); 94 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual); 95 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); 96 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data); 97 98 /*********************************************************************** 99 * 100 * Private class data 101 * 102 ***********************************************************************/ 103 104 typedef struct FLAC__StreamDecoderPrivate { 105 FLAC__StreamDecoderReadCallback read_callback; 106 FLAC__StreamDecoderWriteCallback write_callback; 107 FLAC__StreamDecoderMetadataCallback metadata_callback; 108 FLAC__StreamDecoderErrorCallback error_callback; 109 /* generic 32-bit datapath: */ 110 void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); 111 /* generic 64-bit datapath: */ 112 void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); 113 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */ 114 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); 115 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */ 116 void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); 117 void *client_data; 118 FLAC__BitBuffer *input; 119 FLAC__int32 *output[FLAC__MAX_CHANNELS]; 120 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ 121 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; 122 unsigned output_capacity, output_channels; 123 FLAC__uint32 last_frame_number; 124 FLAC__uint32 last_block_size; 125 FLAC__uint64 samples_decoded; 126 FLAC__bool has_stream_info, has_seek_table; 127 FLAC__StreamMetadata stream_info; 128 FLAC__StreamMetadata seek_table; 129 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */ 130 FLAC__byte *metadata_filter_ids; 131 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */ 132 FLAC__Frame frame; 133 FLAC__bool cached; /* true if there is a byte in lookahead */ 134 FLAC__CPUInfo cpuinfo; 135 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ 136 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ 137 /* unaligned (original) pointers to allocated data */ 138 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS]; 139 } FLAC__StreamDecoderPrivate; 140 141 /*********************************************************************** 142 * 143 * Public static class data 144 * 145 ***********************************************************************/ 146 147 FLAC_API const char * const FLAC__StreamDecoderStateString[] = { 148 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", 149 "FLAC__STREAM_DECODER_READ_METADATA", 150 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", 151 "FLAC__STREAM_DECODER_READ_FRAME", 152 "FLAC__STREAM_DECODER_END_OF_STREAM", 153 "FLAC__STREAM_DECODER_ABORTED", 154 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM", 155 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", 156 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED", 157 "FLAC__STREAM_DECODER_INVALID_CALLBACK", 158 "FLAC__STREAM_DECODER_UNINITIALIZED" 159 }; 160 161 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { 162 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", 163 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", 164 "FLAC__STREAM_DECODER_READ_STATUS_ABORT" 165 }; 166 167 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { 168 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", 169 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" 170 }; 171 172 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { 173 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", 174 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", 175 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH" 176 }; 177 178 /*********************************************************************** 179 * 180 * Class constructor/destructor 181 * 182 ***********************************************************************/ 183 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new() 184 { 185 FLAC__StreamDecoder *decoder; 186 unsigned i; 187 188 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ 189 190 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder)); 191 if(decoder == 0) { 192 return 0; 193 } 194 195 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected)); 196 if(decoder->protected_ == 0) { 197 free(decoder); 198 return 0; 199 } 200 201 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate)); 202 if(decoder->private_ == 0) { 203 free(decoder->protected_); 204 free(decoder); 205 return 0; 206 } 207 208 decoder->private_->input = FLAC__bitbuffer_new(); 209 if(decoder->private_->input == 0) { 210 free(decoder->private_); 211 free(decoder->protected_); 212 free(decoder); 213 return 0; 214 } 215 216 decoder->private_->metadata_filter_ids_capacity = 16; 217 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { 218 FLAC__bitbuffer_delete(decoder->private_->input); 219 free(decoder->private_); 220 free(decoder->protected_); 221 free(decoder); 222 return 0; 223 } 224 225 for(i = 0; i < FLAC__MAX_CHANNELS; i++) { 226 decoder->private_->output[i] = 0; 227 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; 228 } 229 230 decoder->private_->output_capacity = 0; 231 decoder->private_->output_channels = 0; 232 decoder->private_->has_seek_table = false; 233 234 for(i = 0; i < FLAC__MAX_CHANNELS; i++) 235 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]); 236 237 set_defaults_(decoder); 238 239 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; 240 241 return decoder; 242 } 243 244 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) 245 { 246 unsigned i; 247 248 FLAC__ASSERT(0 != decoder); 249 FLAC__ASSERT(0 != decoder->protected_); 250 FLAC__ASSERT(0 != decoder->private_); 251 FLAC__ASSERT(0 != decoder->private_->input); 252 253 FLAC__stream_decoder_finish(decoder); 254 255 if(0 != decoder->private_->metadata_filter_ids) 256 free(decoder->private_->metadata_filter_ids); 257 258 FLAC__bitbuffer_delete(decoder->private_->input); 259 260 for(i = 0; i < FLAC__MAX_CHANNELS; i++) 261 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]); 262 263 free(decoder->private_); 264 free(decoder->protected_); 265 free(decoder); 266 } 267 268 /*********************************************************************** 269 * 270 * Public class methods 271 * 272 ***********************************************************************/ 273 274 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder) 275 { 276 FLAC__ASSERT(0 != decoder); 277 278 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 279 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED; 280 281 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback) 282 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK; 283 284 if(!FLAC__bitbuffer_init(decoder->private_->input)) 285 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 286 287 decoder->private_->last_frame_number = 0; 288 decoder->private_->last_block_size = 0; 289 decoder->private_->samples_decoded = 0; 290 decoder->private_->has_stream_info = false; 291 decoder->private_->cached = false; 292 293 /* 294 * get the CPU info and set the function pointers 295 */ 296 FLAC__cpu_info(&decoder->private_->cpuinfo); 297 /* first default to the non-asm routines */ 298 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal; 299 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide; 300 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal; 301 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal; 302 /* now override with asm where appropriate */ 303 #ifndef FLAC__NO_ASM 304 if(decoder->private_->cpuinfo.use_asm) { 305 #ifdef FLAC__CPU_IA32 306 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); 307 #ifdef FLAC__HAS_NASM 308 if(decoder->private_->cpuinfo.data.ia32.mmx) { 309 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; 310 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx; 311 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx; 312 } 313 else { 314 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; 315 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32; 316 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32; 317 } 318 #endif 319 #elif defined FLAC__CPU_PPC 320 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC); 321 if(decoder->private_->cpuinfo.data.ppc.altivec) { 322 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16; 323 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8; 324 } 325 #endif 326 } 327 #endif 328 329 if(!FLAC__stream_decoder_reset(decoder)) 330 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 331 332 return decoder->protected_->state; 333 } 334 335 FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) 336 { 337 unsigned i; 338 FLAC__ASSERT(0 != decoder); 339 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) 340 return; 341 if(0 != decoder->private_->seek_table.data.seek_table.points) { 342 free(decoder->private_->seek_table.data.seek_table.points); 343 decoder->private_->seek_table.data.seek_table.points = 0; 344 decoder->private_->has_seek_table = false; 345 } 346 FLAC__bitbuffer_free(decoder->private_->input); 347 for(i = 0; i < FLAC__MAX_CHANNELS; i++) { 348 /* WATCHOUT: 349 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the 350 * output arrays have a buffer of up to 3 zeroes in front 351 * (at negative indices) for alignment purposes; we use 4 352 * to keep the data well-aligned. 353 */ 354 if(0 != decoder->private_->output[i]) { 355 free(decoder->private_->output[i]-4); 356 decoder->private_->output[i] = 0; 357 } 358 if(0 != decoder->private_->residual_unaligned[i]) { 359 free(decoder->private_->residual_unaligned[i]); 360 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; 361 } 362 } 363 decoder->private_->output_capacity = 0; 364 decoder->private_->output_channels = 0; 365 366 set_defaults_(decoder); 367 368 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; 369 } 370 371 FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value) 372 { 373 FLAC__ASSERT(0 != decoder); 374 FLAC__ASSERT(0 != decoder->private_); 375 FLAC__ASSERT(0 != decoder->protected_); 376 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 377 return false; 378 decoder->private_->read_callback = value; 379 return true; 380 } 381 382 FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value) 383 { 384 FLAC__ASSERT(0 != decoder); 385 FLAC__ASSERT(0 != decoder->private_); 386 FLAC__ASSERT(0 != decoder->protected_); 387 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 388 return false; 389 decoder->private_->write_callback = value; 390 return true; 391 } 392 393 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value) 394 { 395 FLAC__ASSERT(0 != decoder); 396 FLAC__ASSERT(0 != decoder->private_); 397 FLAC__ASSERT(0 != decoder->protected_); 398 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 399 return false; 400 decoder->private_->metadata_callback = value; 401 return true; 402 } 403 404 FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value) 405 { 406 FLAC__ASSERT(0 != decoder); 407 FLAC__ASSERT(0 != decoder->private_); 408 FLAC__ASSERT(0 != decoder->protected_); 409 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 410 return false; 411 decoder->private_->error_callback = value; 412 return true; 413 } 414 415 FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value) 416 { 417 FLAC__ASSERT(0 != decoder); 418 FLAC__ASSERT(0 != decoder->private_); 419 FLAC__ASSERT(0 != decoder->protected_); 420 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 421 return false; 422 decoder->private_->client_data = value; 423 return true; 424 } 425 426 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) 427 { 428 FLAC__ASSERT(0 != decoder); 429 FLAC__ASSERT(0 != decoder->private_); 430 FLAC__ASSERT(0 != decoder->protected_); 431 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); 432 /* double protection */ 433 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) 434 return false; 435 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 436 return false; 437 decoder->private_->metadata_filter[type] = true; 438 if(type == FLAC__METADATA_TYPE_APPLICATION) 439 decoder->private_->metadata_filter_ids_count = 0; 440 return true; 441 } 442 443 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) 444 { 445 FLAC__ASSERT(0 != decoder); 446 FLAC__ASSERT(0 != decoder->private_); 447 FLAC__ASSERT(0 != decoder->protected_); 448 FLAC__ASSERT(0 != id); 449 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 450 return false; 451 452 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) 453 return true; 454 455 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); 456 457 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { 458 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) 459 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 460 decoder->private_->metadata_filter_ids_capacity *= 2; 461 } 462 463 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)); 464 decoder->private_->metadata_filter_ids_count++; 465 466 return true; 467 } 468 469 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder) 470 { 471 unsigned i; 472 FLAC__ASSERT(0 != decoder); 473 FLAC__ASSERT(0 != decoder->private_); 474 FLAC__ASSERT(0 != decoder->protected_); 475 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 476 return false; 477 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) 478 decoder->private_->metadata_filter[i] = true; 479 decoder->private_->metadata_filter_ids_count = 0; 480 return true; 481 } 482 483 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) 484 { 485 FLAC__ASSERT(0 != decoder); 486 FLAC__ASSERT(0 != decoder->private_); 487 FLAC__ASSERT(0 != decoder->protected_); 488 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); 489 /* double protection */ 490 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) 491 return false; 492 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 493 return false; 494 decoder->private_->metadata_filter[type] = false; 495 if(type == FLAC__METADATA_TYPE_APPLICATION) 496 decoder->private_->metadata_filter_ids_count = 0; 497 return true; 498 } 499 500 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) 501 { 502 FLAC__ASSERT(0 != decoder); 503 FLAC__ASSERT(0 != decoder->private_); 504 FLAC__ASSERT(0 != decoder->protected_); 505 FLAC__ASSERT(0 != id); 506 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 507 return false; 508 509 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) 510 return true; 511 512 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); 513 514 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { 515 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) 516 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 517 decoder->private_->metadata_filter_ids_capacity *= 2; 518 } 519 520 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)); 521 decoder->private_->metadata_filter_ids_count++; 522 523 return true; 524 } 525 526 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder) 527 { 528 FLAC__ASSERT(0 != decoder); 529 FLAC__ASSERT(0 != decoder->private_); 530 FLAC__ASSERT(0 != decoder->protected_); 531 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) 532 return false; 533 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); 534 decoder->private_->metadata_filter_ids_count = 0; 535 return true; 536 } 537 538 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder) 539 { 540 FLAC__ASSERT(0 != decoder); 541 FLAC__ASSERT(0 != decoder->protected_); 542 return decoder->protected_->state; 543 } 544 545 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder) 546 { 547 return FLAC__StreamDecoderStateString[decoder->protected_->state]; 548 } 549 550 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) 551 { 552 FLAC__ASSERT(0 != decoder); 553 FLAC__ASSERT(0 != decoder->protected_); 554 return decoder->protected_->channels; 555 } 556 557 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder) 558 { 559 FLAC__ASSERT(0 != decoder); 560 FLAC__ASSERT(0 != decoder->protected_); 561 return decoder->protected_->channel_assignment; 562 } 563 564 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder) 565 { 566 FLAC__ASSERT(0 != decoder); 567 FLAC__ASSERT(0 != decoder->protected_); 568 return decoder->protected_->bits_per_sample; 569 } 570 571 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder) 572 { 573 FLAC__ASSERT(0 != decoder); 574 FLAC__ASSERT(0 != decoder->protected_); 575 return decoder->protected_->sample_rate; 576 } 577 578 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) 579 { 580 FLAC__ASSERT(0 != decoder); 581 FLAC__ASSERT(0 != decoder->protected_); 582 return decoder->protected_->blocksize; 583 } 584 585 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) 586 { 587 FLAC__ASSERT(0 != decoder); 588 FLAC__ASSERT(0 != decoder->private_); 589 FLAC__ASSERT(0 != decoder->protected_); 590 591 if(!FLAC__bitbuffer_clear(decoder->private_->input)) { 592 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 593 return false; 594 } 595 decoder->private_->last_frame_number = 0; 596 decoder->private_->last_block_size = 0; 597 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 598 599 return true; 600 } 601 602 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) 603 { 604 FLAC__ASSERT(0 != decoder); 605 FLAC__ASSERT(0 != decoder->private_); 606 FLAC__ASSERT(0 != decoder->protected_); 607 608 if(!FLAC__stream_decoder_flush(decoder)) { 609 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 610 return false; 611 } 612 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; 613 614 decoder->private_->samples_decoded = 0; 615 616 return true; 617 } 618 619 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) 620 { 621 FLAC__bool got_a_frame; 622 FLAC__ASSERT(0 != decoder); 623 FLAC__ASSERT(0 != decoder->protected_); 624 625 while(1) { 626 switch(decoder->protected_->state) { 627 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 628 if(!find_metadata_(decoder)) 629 return false; /* above function sets the status for us */ 630 break; 631 case FLAC__STREAM_DECODER_READ_METADATA: 632 if(!read_metadata_(decoder)) 633 return false; /* above function sets the status for us */ 634 else 635 return true; 636 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 637 if(!frame_sync_(decoder)) 638 return true; /* above function sets the status for us */ 639 break; 640 case FLAC__STREAM_DECODER_READ_FRAME: 641 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true)) 642 return false; /* above function sets the status for us */ 643 if(got_a_frame) 644 return true; /* above function sets the status for us */ 645 break; 646 case FLAC__STREAM_DECODER_END_OF_STREAM: 647 case FLAC__STREAM_DECODER_ABORTED: 648 return true; 649 default: 650 FLAC__ASSERT(0); 651 return false; 652 } 653 } 654 } 655 656 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder) 657 { 658 FLAC__ASSERT(0 != decoder); 659 FLAC__ASSERT(0 != decoder->protected_); 660 661 while(1) { 662 switch(decoder->protected_->state) { 663 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 664 if(!find_metadata_(decoder)) 665 return false; /* above function sets the status for us */ 666 break; 667 case FLAC__STREAM_DECODER_READ_METADATA: 668 if(!read_metadata_(decoder)) 669 return false; /* above function sets the status for us */ 670 break; 671 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 672 case FLAC__STREAM_DECODER_READ_FRAME: 673 case FLAC__STREAM_DECODER_END_OF_STREAM: 674 case FLAC__STREAM_DECODER_ABORTED: 675 return true; 676 default: 677 FLAC__ASSERT(0); 678 return false; 679 } 680 } 681 } 682 683 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder) 684 { 685 FLAC__bool dummy; 686 FLAC__ASSERT(0 != decoder); 687 FLAC__ASSERT(0 != decoder->protected_); 688 689 while(1) { 690 switch(decoder->protected_->state) { 691 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 692 if(!find_metadata_(decoder)) 693 return false; /* above function sets the status for us */ 694 break; 695 case FLAC__STREAM_DECODER_READ_METADATA: 696 if(!read_metadata_(decoder)) 697 return false; /* above function sets the status for us */ 698 break; 699 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 700 if(!frame_sync_(decoder)) 701 return true; /* above function sets the status for us */ 702 break; 703 case FLAC__STREAM_DECODER_READ_FRAME: 704 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) 705 return false; /* above function sets the status for us */ 706 break; 707 case FLAC__STREAM_DECODER_END_OF_STREAM: 708 case FLAC__STREAM_DECODER_ABORTED: 709 return true; 710 default: 711 FLAC__ASSERT(0); 712 return false; 713 } 714 } 715 } 716 717 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder) 718 { 719 FLAC__bool got_a_frame; 720 FLAC__ASSERT(0 != decoder); 721 FLAC__ASSERT(0 != decoder->protected_); 722 723 while(1) { 724 switch(decoder->protected_->state) { 725 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: 726 case FLAC__STREAM_DECODER_READ_METADATA: 727 return false; /* above function sets the status for us */ 728 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: 729 if(!frame_sync_(decoder)) 730 return true; /* above function sets the status for us */ 731 break; 732 case FLAC__STREAM_DECODER_READ_FRAME: 733 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false)) 734 return false; /* above function sets the status for us */ 735 if(got_a_frame) 736 return true; /* above function sets the status for us */ 737 break; 738 case FLAC__STREAM_DECODER_END_OF_STREAM: 739 case FLAC__STREAM_DECODER_ABORTED: 740 return true; 741 default: 742 FLAC__ASSERT(0); 743 return false; 744 } 745 } 746 } 747 748 /*********************************************************************** 749 * 750 * Protected class methods 751 * 752 ***********************************************************************/ 753 754 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) 755 { 756 FLAC__ASSERT(0 != decoder); 757 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input); 758 } 759 760 /*********************************************************************** 761 * 762 * Private class methods 763 * 764 ***********************************************************************/ 765 766 void set_defaults_(FLAC__StreamDecoder *decoder) 767 { 768 decoder->private_->read_callback = 0; 769 decoder->private_->write_callback = 0; 770 decoder->private_->metadata_callback = 0; 771 decoder->private_->error_callback = 0; 772 decoder->private_->client_data = 0; 773 774 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); 775 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; 776 decoder->private_->metadata_filter_ids_count = 0; 777 } 778 779 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) 780 { 781 unsigned i; 782 FLAC__int32 *tmp; 783 784 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels) 785 return true; 786 787 /* simply using realloc() is not practical because the number of channels may change mid-stream */ 788 789 for(i = 0; i < FLAC__MAX_CHANNELS; i++) { 790 if(0 != decoder->private_->output[i]) { 791 free(decoder->private_->output[i]-4); 792 decoder->private_->output[i] = 0; 793 } 794 if(0 != decoder->private_->residual_unaligned[i]) { 795 free(decoder->private_->residual_unaligned[i]); 796 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; 797 } 798 } 799 800 for(i = 0; i < channels; i++) { 801 /* WATCHOUT: 802 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the 803 * output arrays have a buffer of up to 3 zeroes in front 804 * (at negative indices) for alignment purposes; we use 4 805 * to keep the data well-aligned. 806 */ 807 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4)); 808 if(tmp == 0) { 809 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 810 return false; 811 } 812 memset(tmp, 0, sizeof(FLAC__int32)*4); 813 decoder->private_->output[i] = tmp + 4; 814 815 /* WATCHOUT: 816 * minimum of quadword alignment for PPC vector optimizations is REQUIRED: 817 */ 818 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) { 819 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 820 return false; 821 } 822 } 823 824 decoder->private_->output_capacity = size; 825 decoder->private_->output_channels = channels; 826 827 return true; 828 } 829 830 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) 831 { 832 unsigned i; 833 834 FLAC__ASSERT(0 != decoder); 835 FLAC__ASSERT(0 != decoder->private_); 836 837 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) 838 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) 839 return true; 840 841 return false; 842 } 843 844 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) 845 { 846 FLAC__uint32 x; 847 unsigned i, id; 848 FLAC__bool first = true; 849 850 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 851 852 for(i = id = 0; i < 4; ) { 853 if(decoder->private_->cached) { 854 x = (FLAC__uint32)decoder->private_->lookahead; 855 decoder->private_->cached = false; 856 } 857 else { 858 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 859 return false; /* the read_callback_ sets the state for us */ 860 } 861 if(x == FLAC__STREAM_SYNC_STRING[i]) { 862 first = true; 863 i++; 864 id = 0; 865 continue; 866 } 867 if(x == ID3V2_TAG_[id]) { 868 id++; 869 i = 0; 870 if(id == 3) { 871 if(!skip_id3v2_tag_(decoder)) 872 return false; /* the read_callback_ sets the state for us */ 873 } 874 continue; 875 } 876 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 877 decoder->private_->header_warmup[0] = (FLAC__byte)x; 878 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 879 return false; /* the read_callback_ sets the state for us */ 880 881 /* 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 */ 882 /* else we have to check if the second byte is the end of a sync code */ 883 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 884 decoder->private_->lookahead = (FLAC__byte)x; 885 decoder->private_->cached = true; 886 } 887 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ 888 decoder->private_->header_warmup[1] = (FLAC__byte)x; 889 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; 890 return true; 891 } 892 } 893 i = 0; 894 if(first) { 895 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 896 first = false; 897 } 898 } 899 900 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; 901 return true; 902 } 903 904 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) 905 { 906 FLAC__bool is_last; 907 FLAC__uint32 i, x, type, length; 908 909 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 910 911 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder)) 912 return false; /* the read_callback_ sets the state for us */ 913 is_last = x? true : false; 914 915 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder)) 916 return false; /* the read_callback_ sets the state for us */ 917 918 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder)) 919 return false; /* the read_callback_ sets the state for us */ 920 921 if(type == FLAC__METADATA_TYPE_STREAMINFO) { 922 if(!read_metadata_streaminfo_(decoder, is_last, length)) 923 return false; 924 925 decoder->private_->has_stream_info = true; 926 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO]) 927 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data); 928 } 929 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { 930 if(!read_metadata_seektable_(decoder, is_last, length)) 931 return false; 932 933 decoder->private_->has_seek_table = true; 934 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE]) 935 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data); 936 } 937 else { 938 FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; 939 unsigned real_length = length; 940 FLAC__StreamMetadata block; 941 942 block.is_last = is_last; 943 block.type = (FLAC__MetadataType)type; 944 block.length = length; 945 946 if(type == FLAC__METADATA_TYPE_APPLICATION) { 947 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder)) 948 return false; /* the read_callback_ sets the state for us */ 949 950 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; 951 952 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id)) 953 skip_it = !skip_it; 954 } 955 956 if(skip_it) { 957 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder)) 958 return false; /* the read_callback_ sets the state for us */ 959 } 960 else { 961 switch(type) { 962 case FLAC__METADATA_TYPE_PADDING: 963 /* skip the padding bytes */ 964 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder)) 965 return false; /* the read_callback_ sets the state for us */ 966 break; 967 case FLAC__METADATA_TYPE_APPLICATION: 968 /* remember, we read the ID already */ 969 if(real_length > 0) { 970 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) { 971 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 972 return false; 973 } 974 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder)) 975 return false; /* the read_callback_ sets the state for us */ 976 } 977 else 978 block.data.application.data = 0; 979 break; 980 case FLAC__METADATA_TYPE_VORBIS_COMMENT: 981 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment)) 982 return false; 983 break; 984 case FLAC__METADATA_TYPE_CUESHEET: 985 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet)) 986 return false; 987 break; 988 case FLAC__METADATA_TYPE_STREAMINFO: 989 case FLAC__METADATA_TYPE_SEEKTABLE: 990 FLAC__ASSERT(0); 991 break; 992 default: 993 if(real_length > 0) { 994 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) { 995 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 996 return false; 997 } 998 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder)) 999 return false; /* the read_callback_ sets the state for us */ 1000 } 1001 else 1002 block.data.unknown.data = 0; 1003 break; 1004 } 1005 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); 1006 1007 /* now we have to free any malloc'ed data in the block */ 1008 switch(type) { 1009 case FLAC__METADATA_TYPE_PADDING: 1010 break; 1011 case FLAC__METADATA_TYPE_APPLICATION: 1012 if(0 != block.data.application.data) 1013 free(block.data.application.data); 1014 break; 1015 case FLAC__METADATA_TYPE_VORBIS_COMMENT: 1016 if(0 != block.data.vorbis_comment.vendor_string.entry) 1017 free(block.data.vorbis_comment.vendor_string.entry); 1018 if(block.data.vorbis_comment.num_comments > 0) 1019 for(i = 0; i < block.data.vorbis_comment.num_comments; i++) 1020 if(0 != block.data.vorbis_comment.comments[i].entry) 1021 free(block.data.vorbis_comment.comments[i].entry); 1022 if(0 != block.data.vorbis_comment.comments) 1023 free(block.data.vorbis_comment.comments); 1024 break; 1025 case FLAC__METADATA_TYPE_CUESHEET: 1026 if(block.data.cue_sheet.num_tracks > 0) 1027 for(i = 0; i < block.data.cue_sheet.num_tracks; i++) 1028 if(0 != block.data.cue_sheet.tracks[i].indices) 1029 free(block.data.cue_sheet.tracks[i].indices); 1030 if(0 != block.data.cue_sheet.tracks) 1031 free(block.data.cue_sheet.tracks); 1032 break; 1033 case FLAC__METADATA_TYPE_STREAMINFO: 1034 case FLAC__METADATA_TYPE_SEEKTABLE: 1035 FLAC__ASSERT(0); 1036 default: 1037 if(0 != block.data.unknown.data) 1038 free(block.data.unknown.data); 1039 break; 1040 } 1041 } 1042 } 1043 1044 if(is_last) 1045 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1046 1047 return true; 1048 } 1049 1050 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) 1051 { 1052 FLAC__uint32 x; 1053 unsigned bits, used_bits = 0; 1054 1055 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 1056 1057 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO; 1058 decoder->private_->stream_info.is_last = is_last; 1059 decoder->private_->stream_info.length = length; 1060 1061 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; 1062 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder)) 1063 return false; /* the read_callback_ sets the state for us */ 1064 decoder->private_->stream_info.data.stream_info.min_blocksize = x; 1065 used_bits += bits; 1066 1067 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; 1068 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder)) 1069 return false; /* the read_callback_ sets the state for us */ 1070 decoder->private_->stream_info.data.stream_info.max_blocksize = x; 1071 used_bits += bits; 1072 1073 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; 1074 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder)) 1075 return false; /* the read_callback_ sets the state for us */ 1076 decoder->private_->stream_info.data.stream_info.min_framesize = x; 1077 used_bits += bits; 1078 1079 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; 1080 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder)) 1081 return false; /* the read_callback_ sets the state for us */ 1082 decoder->private_->stream_info.data.stream_info.max_framesize = x; 1083 used_bits += bits; 1084 1085 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; 1086 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder)) 1087 return false; /* the read_callback_ sets the state for us */ 1088 decoder->private_->stream_info.data.stream_info.sample_rate = x; 1089 used_bits += bits; 1090 1091 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; 1092 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder)) 1093 return false; /* the read_callback_ sets the state for us */ 1094 decoder->private_->stream_info.data.stream_info.channels = x+1; 1095 used_bits += bits; 1096 1097 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; 1098 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder)) 1099 return false; /* the read_callback_ sets the state for us */ 1100 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; 1101 used_bits += bits; 1102 1103 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; 1104 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder)) 1105 return false; /* the read_callback_ sets the state for us */ 1106 used_bits += bits; 1107 1108 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder)) 1109 return false; /* the read_callback_ sets the state for us */ 1110 used_bits += 16*8; 1111 1112 /* skip the rest of the block */ 1113 FLAC__ASSERT(used_bits % 8 == 0); 1114 length -= (used_bits / 8); 1115 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder)) 1116 return false; /* the read_callback_ sets the state for us */ 1117 1118 return true; 1119 } 1120 1121 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) 1122 { 1123 FLAC__uint32 i, x; 1124 FLAC__uint64 xx; 1125 1126 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 1127 1128 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; 1129 decoder->private_->seek_table.is_last = is_last; 1130 decoder->private_->seek_table.length = length; 1131 1132 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; 1133 1134 /* use realloc since we may pass through here several times (e.g. after seeking) */ 1135 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) { 1136 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 1137 return false; 1138 } 1139 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { 1140 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder)) 1141 return false; /* the read_callback_ sets the state for us */ 1142 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; 1143 1144 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder)) 1145 return false; /* the read_callback_ sets the state for us */ 1146 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; 1147 1148 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder)) 1149 return false; /* the read_callback_ sets the state for us */ 1150 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; 1151 } 1152 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); 1153 /* if there is a partial point left, skip over it */ 1154 if(length > 0) { 1155 /*@@@ do an error_callback() here? there's an argument for either way */ 1156 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder)) 1157 return false; /* the read_callback_ sets the state for us */ 1158 } 1159 1160 return true; 1161 } 1162 1163 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj) 1164 { 1165 FLAC__uint32 i; 1166 1167 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 1168 1169 /* read vendor string */ 1170 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); 1171 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder)) 1172 return false; /* the read_callback_ sets the state for us */ 1173 if(obj->vendor_string.length > 0) { 1174 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) { 1175 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 1176 return false; 1177 } 1178 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder)) 1179 return false; /* the read_callback_ sets the state for us */ 1180 obj->vendor_string.entry[obj->vendor_string.length] = '\0'; 1181 } 1182 else 1183 obj->vendor_string.entry = 0; 1184 1185 /* read num comments */ 1186 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); 1187 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder)) 1188 return false; /* the read_callback_ sets the state for us */ 1189 1190 /* read comments */ 1191 if(obj->num_comments > 0) { 1192 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { 1193 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 1194 return false; 1195 } 1196 for(i = 0; i < obj->num_comments; i++) { 1197 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); 1198 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder)) 1199 return false; /* the read_callback_ sets the state for us */ 1200 if(obj->comments[i].length > 0) { 1201 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) { 1202 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 1203 return false; 1204 } 1205 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder)) 1206 return false; /* the read_callback_ sets the state for us */ 1207 obj->comments[i].entry[obj->comments[i].length] = '\0'; 1208 } 1209 else 1210 obj->comments[i].entry = 0; 1211 } 1212 } 1213 else { 1214 obj->comments = 0; 1215 } 1216 1217 return true; 1218 } 1219 1220 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj) 1221 { 1222 FLAC__uint32 i, j, x; 1223 1224 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 1225 1226 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet)); 1227 1228 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); 1229 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder)) 1230 return false; /* the read_callback_ sets the state for us */ 1231 1232 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder)) 1233 return false; /* the read_callback_ sets the state for us */ 1234 1235 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder)) 1236 return false; /* the read_callback_ sets the state for us */ 1237 obj->is_cd = x? true : false; 1238 1239 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder)) 1240 return false; /* the read_callback_ sets the state for us */ 1241 1242 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder)) 1243 return false; /* the read_callback_ sets the state for us */ 1244 obj->num_tracks = x; 1245 1246 if(obj->num_tracks > 0) { 1247 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) { 1248 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 1249 return false; 1250 } 1251 for(i = 0; i < obj->num_tracks; i++) { 1252 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i]; 1253 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder)) 1254 return false; /* the read_callback_ sets the state for us */ 1255 1256 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder)) 1257 return false; /* the read_callback_ sets the state for us */ 1258 track->number = (FLAC__byte)x; 1259 1260 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); 1261 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder)) 1262 return false; /* the read_callback_ sets the state for us */ 1263 1264 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder)) 1265 return false; /* the read_callback_ sets the state for us */ 1266 track->type = x; 1267 1268 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder)) 1269 return false; /* the read_callback_ sets the state for us */ 1270 track->pre_emphasis = x; 1271 1272 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder)) 1273 return false; /* the read_callback_ sets the state for us */ 1274 1275 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder)) 1276 return false; /* the read_callback_ sets the state for us */ 1277 track->num_indices = (FLAC__byte)x; 1278 1279 if(track->num_indices > 0) { 1280 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) { 1281 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 1282 return false; 1283 } 1284 for(j = 0; j < track->num_indices; j++) { 1285 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j]; 1286 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder)) 1287 return false; /* the read_callback_ sets the state for us */ 1288 1289 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder)) 1290 return false; /* the read_callback_ sets the state for us */ 1291 index->number = (FLAC__byte)x; 1292 1293 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder)) 1294 return false; /* the read_callback_ sets the state for us */ 1295 } 1296 } 1297 } 1298 } 1299 1300 return true; 1301 } 1302 1303 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) 1304 { 1305 FLAC__uint32 x; 1306 unsigned i, skip; 1307 1308 /* skip the version and flags bytes */ 1309 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder)) 1310 return false; /* the read_callback_ sets the state for us */ 1311 /* get the size (in bytes) to skip */ 1312 skip = 0; 1313 for(i = 0; i < 4; i++) { 1314 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1315 return false; /* the read_callback_ sets the state for us */ 1316 skip <<= 7; 1317 skip |= (x & 0x7f); 1318 } 1319 /* skip the rest of the tag */ 1320 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder)) 1321 return false; /* the read_callback_ sets the state for us */ 1322 return true; 1323 } 1324 1325 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) 1326 { 1327 FLAC__uint32 x; 1328 FLAC__bool first = true; 1329 1330 /* If we know the total number of samples in the stream, stop if we've read that many. */ 1331 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */ 1332 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) { 1333 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) { 1334 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; 1335 return true; 1336 } 1337 } 1338 1339 /* make sure we're byte aligned */ 1340 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) { 1341 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder)) 1342 return false; /* the read_callback_ sets the state for us */ 1343 } 1344 1345 while(1) { 1346 if(decoder->private_->cached) { 1347 x = (FLAC__uint32)decoder->private_->lookahead; 1348 decoder->private_->cached = false; 1349 } 1350 else { 1351 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1352 return false; /* the read_callback_ sets the state for us */ 1353 } 1354 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 1355 decoder->private_->header_warmup[0] = (FLAC__byte)x; 1356 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1357 return false; /* the read_callback_ sets the state for us */ 1358 1359 /* 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 */ 1360 /* else we have to check if the second byte is the end of a sync code */ 1361 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 1362 decoder->private_->lookahead = (FLAC__byte)x; 1363 decoder->private_->cached = true; 1364 } 1365 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ 1366 decoder->private_->header_warmup[1] = (FLAC__byte)x; 1367 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; 1368 return true; 1369 } 1370 } 1371 if(first) { 1372 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 1373 first = false; 1374 } 1375 } 1376 1377 return true; 1378 } 1379 1380 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode) 1381 { 1382 unsigned channel; 1383 unsigned i; 1384 FLAC__int32 mid, side, left, right; 1385 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */ 1386 FLAC__uint32 x; 1387 1388 *got_a_frame = false; 1389 1390 /* init the CRC */ 1391 frame_crc = 0; 1392 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); 1393 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); 1394 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc); 1395 1396 if(!read_frame_header_(decoder)) 1397 return false; 1398 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) 1399 return true; 1400 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels)) 1401 return false; 1402 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { 1403 /* 1404 * first figure the correct bits-per-sample of the subframe 1405 */ 1406 unsigned bps = decoder->private_->frame.header.bits_per_sample; 1407 switch(decoder->private_->frame.header.channel_assignment) { 1408 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: 1409 /* no adjustment needed */ 1410 break; 1411 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: 1412 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 1413 if(channel == 1) 1414 bps++; 1415 break; 1416 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: 1417 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 1418 if(channel == 0) 1419 bps++; 1420 break; 1421 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: 1422 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 1423 if(channel == 1) 1424 bps++; 1425 break; 1426 default: 1427 FLAC__ASSERT(0); 1428 } 1429 /* 1430 * now read it 1431 */ 1432 if(!read_subframe_(decoder, channel, bps, do_full_decode)) 1433 return false; 1434 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) { 1435 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1436 return true; 1437 } 1438 } 1439 if(!read_zero_padding_(decoder)) 1440 return false; 1441 1442 /* 1443 * Read the frame CRC-16 from the footer and check 1444 */ 1445 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input); 1446 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder)) 1447 return false; /* the read_callback_ sets the state for us */ 1448 if(frame_crc == (FLAC__uint16)x) { 1449 if(do_full_decode) { 1450 /* Undo any special channel coding */ 1451 switch(decoder->private_->frame.header.channel_assignment) { 1452 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: 1453 /* do nothing */ 1454 break; 1455 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: 1456 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 1457 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 1458 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; 1459 break; 1460 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: 1461 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 1462 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 1463 decoder->private_->output[0][i] += decoder->private_->output[1][i]; 1464 break; 1465 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: 1466 FLAC__ASSERT(decoder->private_->frame.header.channels == 2); 1467 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { 1468 mid = decoder->private_->output[0][i]; 1469 side = decoder->private_->output[1][i]; 1470 mid <<= 1; 1471 if(side & 1) /* i.e. if 'side' is odd... */ 1472 mid++; 1473 left = mid + side; 1474 right = mid - side; 1475 decoder->private_->output[0][i] = left >> 1; 1476 decoder->private_->output[1][i] = right >> 1; 1477 } 1478 break; 1479 default: 1480 FLAC__ASSERT(0); 1481 break; 1482 } 1483 } 1484 } 1485 else { 1486 /* Bad frame, emit error and zero the output signal */ 1487 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data); 1488 if(do_full_decode) { 1489 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { 1490 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); 1491 } 1492 } 1493 } 1494 1495 *got_a_frame = true; 1496 1497 /* put the latest values into the public section of the decoder instance */ 1498 decoder->protected_->channels = decoder->private_->frame.header.channels; 1499 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; 1500 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; 1501 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; 1502 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; 1503 1504 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); 1505 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize; 1506 1507 /* write it */ 1508 if(do_full_decode) { 1509 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) 1510 return false; 1511 } 1512 1513 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1514 return true; 1515 } 1516 1517 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) 1518 { 1519 FLAC__uint32 x; 1520 FLAC__uint64 xx; 1521 unsigned i, blocksize_hint = 0, sample_rate_hint = 0; 1522 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */ 1523 unsigned raw_header_len; 1524 FLAC__bool is_unparseable = false; 1525 const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize); 1526 const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize); 1527 1528 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); 1529 1530 /* init the raw header with the saved bits from synchronization */ 1531 raw_header[0] = decoder->private_->header_warmup[0]; 1532 raw_header[1] = decoder->private_->header_warmup[1]; 1533 raw_header_len = 2; 1534 1535 /* 1536 * check to make sure that the reserved bits are 0 1537 */ 1538 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */ 1539 is_unparseable = true; 1540 } 1541 1542 /* 1543 * Note that along the way as we read the header, we look for a sync 1544 * code inside. If we find one it would indicate that our original 1545 * sync was bad since there cannot be a sync code in a valid header. 1546 */ 1547 1548 /* 1549 * read in the raw header as bytes so we can CRC it, and parse it on the way 1550 */ 1551 for(i = 0; i < 2; i++) { 1552 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1553 return false; /* the read_callback_ sets the state for us */ 1554 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ 1555 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */ 1556 decoder->private_->lookahead = (FLAC__byte)x; 1557 decoder->private_->cached = true; 1558 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); 1559 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1560 return true; 1561 } 1562 raw_header[raw_header_len++] = (FLAC__byte)x; 1563 } 1564 1565 switch(x = raw_header[2] >> 4) { 1566 case 0: 1567 if(is_known_fixed_blocksize_stream) 1568 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize; 1569 else 1570 is_unparseable = true; 1571 break; 1572 case 1: 1573 decoder->private_->frame.header.blocksize = 192; 1574 break; 1575 case 2: 1576 case 3: 1577 case 4: 1578 case 5: 1579 decoder->private_->frame.header.blocksize = 576 << (x-2); 1580 break; 1581 case 6: 1582 case 7: 1583 blocksize_hint = x; 1584 break; 1585 case 8: 1586 case 9: 1587 case 10: 1588 case 11: 1589 case 12: 1590 case 13: 1591 case 14: 1592 case 15: 1593 decoder->private_->frame.header.blocksize = 256 << (x-8); 1594 break; 1595 default: 1596 FLAC__ASSERT(0); 1597 break; 1598 } 1599 1600 switch(x = raw_header[2] & 0x0f) { 1601 case 0: 1602 if(decoder->private_->has_stream_info) 1603 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate; 1604 else 1605 is_unparseable = true; 1606 break; 1607 case 1: 1608 case 2: 1609 case 3: 1610 is_unparseable = true; 1611 break; 1612 case 4: 1613 decoder->private_->frame.header.sample_rate = 8000; 1614 break; 1615 case 5: 1616 decoder->private_->frame.header.sample_rate = 16000; 1617 break; 1618 case 6: 1619 decoder->private_->frame.header.sample_rate = 22050; 1620 break; 1621 case 7: 1622 decoder->private_->frame.header.sample_rate = 24000; 1623 break; 1624 case 8: 1625 decoder->private_->frame.header.sample_rate = 32000; 1626 break; 1627 case 9: 1628 decoder->private_->frame.header.sample_rate = 44100; 1629 break; 1630 case 10: 1631 decoder->private_->frame.header.sample_rate = 48000; 1632 break; 1633 case 11: 1634 decoder->private_->frame.header.sample_rate = 96000; 1635 break; 1636 case 12: 1637 case 13: 1638 case 14: 1639 sample_rate_hint = x; 1640 break; 1641 case 15: 1642 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); 1643 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1644 return true; 1645 default: 1646 FLAC__ASSERT(0); 1647 } 1648 1649 x = (unsigned)(raw_header[3] >> 4); 1650 if(x & 8) { 1651 decoder->private_->frame.header.channels = 2; 1652 switch(x & 7) { 1653 case 0: 1654 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; 1655 break; 1656 case 1: 1657 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; 1658 break; 1659 case 2: 1660 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; 1661 break; 1662 default: 1663 is_unparseable = true; 1664 break; 1665 } 1666 } 1667 else { 1668 decoder->private_->frame.header.channels = (unsigned)x + 1; 1669 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; 1670 } 1671 1672 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) { 1673 case 0: 1674 if(decoder->private_->has_stream_info) 1675 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample; 1676 else 1677 is_unparseable = true; 1678 break; 1679 case 1: 1680 decoder->private_->frame.header.bits_per_sample = 8; 1681 break; 1682 case 2: 1683 decoder->private_->frame.header.bits_per_sample = 12; 1684 break; 1685 case 4: 1686 decoder->private_->frame.header.bits_per_sample = 16; 1687 break; 1688 case 5: 1689 decoder->private_->frame.header.bits_per_sample = 20; 1690 break; 1691 case 6: 1692 decoder->private_->frame.header.bits_per_sample = 24; 1693 break; 1694 case 3: 1695 case 7: 1696 is_unparseable = true; 1697 break; 1698 default: 1699 FLAC__ASSERT(0); 1700 break; 1701 } 1702 1703 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */ 1704 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); 1705 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1706 return true; 1707 } 1708 1709 /* 1710 * Now we get to the regrettable consequences of not knowing for sure 1711 * whether we got a frame number or a sample number. There are no 1712 * encoders that do variable-blocksize encoding so unless we know from 1713 * the STREAMINFO that it is variable-blocksize we will assume it is 1714 * fixed-blocksize. The trouble comes when we have no STREAMINFO; again 1715 * we will guess that is fixed-blocksize. Where this can go wrong: 1) a 1716 * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize 1717 * stream that was edited such that one or more frames before or 1718 * including this one do not have the same number of samples as the 1719 * STREAMINFO's min and max blocksize. 1720 */ 1721 if(is_known_variable_blocksize_stream) { 1722 if(blocksize_hint) { 1723 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len)) 1724 return false; /* the read_callback_ sets the state for us */ 1725 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */ 1726 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ 1727 decoder->private_->cached = true; 1728 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); 1729 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1730 return true; 1731 } 1732 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; 1733 decoder->private_->frame.header.number.sample_number = xx; 1734 } 1735 else 1736 is_unparseable = true; 1737 } 1738 else { 1739 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len)) 1740 return false; /* the read_callback_ sets the state for us */ 1741 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ 1742 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ 1743 decoder->private_->cached = true; 1744 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); 1745 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1746 return true; 1747 } 1748 decoder->private_->last_frame_number = x; 1749 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; 1750 if(decoder->private_->has_stream_info) { 1751 FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize); 1752 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x; 1753 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize; 1754 } 1755 else if(blocksize_hint) { 1756 if(decoder->private_->last_block_size) 1757 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x; 1758 else 1759 is_unparseable = true; 1760 } 1761 else { 1762 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x; 1763 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize; 1764 } 1765 } 1766 1767 if(blocksize_hint) { 1768 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1769 return false; /* the read_callback_ sets the state for us */ 1770 raw_header[raw_header_len++] = (FLAC__byte)x; 1771 if(blocksize_hint == 7) { 1772 FLAC__uint32 _x; 1773 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder)) 1774 return false; /* the read_callback_ sets the state for us */ 1775 raw_header[raw_header_len++] = (FLAC__byte)_x; 1776 x = (x << 8) | _x; 1777 } 1778 decoder->private_->frame.header.blocksize = x+1; 1779 } 1780 1781 if(sample_rate_hint) { 1782 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1783 return false; /* the read_callback_ sets the state for us */ 1784 raw_header[raw_header_len++] = (FLAC__byte)x; 1785 if(sample_rate_hint != 12) { 1786 FLAC__uint32 _x; 1787 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder)) 1788 return false; /* the read_callback_ sets the state for us */ 1789 raw_header[raw_header_len++] = (FLAC__byte)_x; 1790 x = (x << 8) | _x; 1791 } 1792 if(sample_rate_hint == 12) 1793 decoder->private_->frame.header.sample_rate = x*1000; 1794 else if(sample_rate_hint == 13) 1795 decoder->private_->frame.header.sample_rate = x; 1796 else 1797 decoder->private_->frame.header.sample_rate = x*10; 1798 } 1799 1800 /* read the CRC-8 byte */ 1801 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) 1802 return false; /* the read_callback_ sets the state for us */ 1803 crc8 = (FLAC__byte)x; 1804 1805 if(FLAC__crc8(raw_header, raw_header_len) != crc8) { 1806 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); 1807 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1808 return true; 1809 } 1810 1811 if(is_unparseable) { 1812 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; 1813 return false; 1814 } 1815 1816 return true; 1817 } 1818 1819 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) 1820 { 1821 FLAC__uint32 x; 1822 FLAC__bool wasted_bits; 1823 1824 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */ 1825 return false; /* the read_callback_ sets the state for us */ 1826 1827 wasted_bits = (x & 1); 1828 x &= 0xfe; 1829 1830 if(wasted_bits) { 1831 unsigned u; 1832 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder)) 1833 return false; /* the read_callback_ sets the state for us */ 1834 decoder->private_->frame.subframes[channel].wasted_bits = u+1; 1835 bps -= decoder->private_->frame.subframes[channel].wasted_bits; 1836 } 1837 else 1838 decoder->private_->frame.subframes[channel].wasted_bits = 0; 1839 1840 /* 1841 * Lots of magic numbers here 1842 */ 1843 if(x & 0x80) { 1844 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 1845 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1846 return true; 1847 } 1848 else if(x == 0) { 1849 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode)) 1850 return false; 1851 } 1852 else if(x == 2) { 1853 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode)) 1854 return false; 1855 } 1856 else if(x < 16) { 1857 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; 1858 return false; 1859 } 1860 else if(x <= 24) { 1861 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode)) 1862 return false; 1863 } 1864 else if(x < 64) { 1865 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; 1866 return false; 1867 } 1868 else { 1869 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode)) 1870 return false; 1871 } 1872 1873 if(wasted_bits && do_full_decode) { 1874 unsigned i; 1875 x = decoder->private_->frame.subframes[channel].wasted_bits; 1876 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 1877 decoder->private_->output[channel][i] <<= x; 1878 } 1879 1880 return true; 1881 } 1882 1883 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) 1884 { 1885 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; 1886 FLAC__int32 x; 1887 unsigned i; 1888 FLAC__int32 *output = decoder->private_->output[channel]; 1889 1890 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; 1891 1892 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder)) 1893 return false; /* the read_callback_ sets the state for us */ 1894 1895 subframe->value = x; 1896 1897 /* decode the subframe */ 1898 if(do_full_decode) { 1899 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) 1900 output[i] = x; 1901 } 1902 1903 return true; 1904 } 1905 1906 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) 1907 { 1908 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; 1909 FLAC__int32 i32; 1910 FLAC__uint32 u32; 1911 unsigned u; 1912 1913 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; 1914 1915 subframe->residual = decoder->private_->residual[channel]; 1916 subframe->order = order; 1917 1918 /* read warm-up samples */ 1919 for(u = 0; u < order; u++) { 1920 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder)) 1921 return false; /* the read_callback_ sets the state for us */ 1922 subframe->warmup[u] = i32; 1923 } 1924 1925 /* read entropy coding method info */ 1926 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder)) 1927 return false; /* the read_callback_ sets the state for us */ 1928 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; 1929 switch(subframe->entropy_coding_method.type) { 1930 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 1931 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder)) 1932 return false; /* the read_callback_ sets the state for us */ 1933 subframe->entropy_coding_method.data.partitioned_rice.order = u32; 1934 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; 1935 break; 1936 default: 1937 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; 1938 return false; 1939 } 1940 1941 /* read residual */ 1942 switch(subframe->entropy_coding_method.type) { 1943 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 1944 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])) 1945 return false; 1946 break; 1947 default: 1948 FLAC__ASSERT(0); 1949 } 1950 1951 /* decode the subframe */ 1952 if(do_full_decode) { 1953 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); 1954 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); 1955 } 1956 1957 return true; 1958 } 1959 1960 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) 1961 { 1962 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; 1963 FLAC__int32 i32; 1964 FLAC__uint32 u32; 1965 unsigned u; 1966 1967 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; 1968 1969 subframe->residual = decoder->private_->residual[channel]; 1970 subframe->order = order; 1971 1972 /* read warm-up samples */ 1973 for(u = 0; u < order; u++) { 1974 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder)) 1975 return false; /* the read_callback_ sets the state for us */ 1976 subframe->warmup[u] = i32; 1977 } 1978 1979 /* read qlp coeff precision */ 1980 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder)) 1981 return false; /* the read_callback_ sets the state for us */ 1982 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { 1983 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 1984 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 1985 return true; 1986 } 1987 subframe->qlp_coeff_precision = u32+1; 1988 1989 /* read qlp shift */ 1990 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder)) 1991 return false; /* the read_callback_ sets the state for us */ 1992 subframe->quantization_level = i32; 1993 1994 /* read quantized lp coefficiencts */ 1995 for(u = 0; u < order; u++) { 1996 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder)) 1997 return false; /* the read_callback_ sets the state for us */ 1998 subframe->qlp_coeff[u] = i32; 1999 } 2000 2001 /* read entropy coding method info */ 2002 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder)) 2003 return false; /* the read_callback_ sets the state for us */ 2004 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; 2005 switch(subframe->entropy_coding_method.type) { 2006 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 2007 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder)) 2008 return false; /* the read_callback_ sets the state for us */ 2009 subframe->entropy_coding_method.data.partitioned_rice.order = u32; 2010 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; 2011 break; 2012 default: 2013 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; 2014 return false; 2015 } 2016 2017 /* read residual */ 2018 switch(subframe->entropy_coding_method.type) { 2019 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: 2020 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])) 2021 return false; 2022 break; 2023 default: 2024 FLAC__ASSERT(0); 2025 } 2026 2027 /* decode the subframe */ 2028 if(do_full_decode) { 2029 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); 2030 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) 2031 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) { 2032 if(order <= 8) 2033 decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); 2034 else 2035 decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); 2036 } 2037 else 2038 decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); 2039 else 2040 decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); 2041 } 2042 2043 return true; 2044 } 2045 2046 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) 2047 { 2048 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; 2049 FLAC__int32 x, *residual = decoder->private_->residual[channel]; 2050 unsigned i; 2051 2052 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; 2053 2054 subframe->data = residual; 2055 2056 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { 2057 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder)) 2058 return false; /* the read_callback_ sets the state for us */ 2059 residual[i] = x; 2060 } 2061 2062 /* decode the subframe */ 2063 if(do_full_decode) 2064 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); 2065 2066 return true; 2067 } 2068 2069 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual) 2070 { 2071 FLAC__uint32 rice_parameter; 2072 int i; 2073 unsigned partition, sample, u; 2074 const unsigned partitions = 1u << partition_order; 2075 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order; 2076 2077 /* sanity checks */ 2078 if(partition_order == 0) { 2079 if(decoder->private_->frame.header.blocksize < predictor_order) { 2080 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 2081 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 2082 return true; 2083 } 2084 } 2085 else { 2086 if(partition_samples < predictor_order) { 2087 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 2088 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 2089 return true; 2090 } 2091 } 2092 2093 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) { 2094 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; 2095 return false; 2096 } 2097 2098 sample = 0; 2099 for(partition = 0; partition < partitions; partition++) { 2100 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder)) 2101 return false; /* the read_callback_ sets the state for us */ 2102 partitioned_rice_contents->parameters[partition] = rice_parameter; 2103 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { 2104 #ifdef FLAC__SYMMETRIC_RICE 2105 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) { 2106 if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder)) 2107 return false; /* the read_callback_ sets the state for us */ 2108 residual[sample] = i; 2109 } 2110 #else 2111 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order; 2112 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder)) 2113 return false; /* the read_callback_ sets the state for us */ 2114 sample += u; 2115 #endif 2116 } 2117 else { 2118 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder)) 2119 return false; /* the read_callback_ sets the state for us */ 2120 partitioned_rice_contents->raw_bits[partition] = rice_parameter; 2121 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) { 2122 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder)) 2123 return false; /* the read_callback_ sets the state for us */ 2124 residual[sample] = i; 2125 } 2126 } 2127 } 2128 2129 return true; 2130 } 2131 2132 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) 2133 { 2134 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) { 2135 FLAC__uint32 zero = 0; 2136 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder)) 2137 return false; /* the read_callback_ sets the state for us */ 2138 if(zero != 0) { 2139 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); 2140 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; 2141 } 2142 } 2143 return true; 2144 } 2145 2146 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data) 2147 { 2148 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; 2149 FLAC__StreamDecoderReadStatus status; 2150 2151 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data); 2152 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM) 2153 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; 2154 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) 2155 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; 2156 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; 2157 }