Changeset 104 for libmpcdec/branches/zorg/src/mpc_decoder.c
- Timestamp:
- 11/08/06 00:20:07 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libmpcdec/branches/zorg/src/mpc_decoder.c
r85 r104 42 42 #include "requant.h" 43 43 44 mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits); 45 mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table); 46 44 47 //SV7 tables 45 48 extern const mpc_huffman* mpc_table_HuffQ [2] [8]; … … 47 50 extern const mpc_huffman mpc_table_HuffSCFI [ 4]; 48 51 extern const mpc_huffman mpc_table_HuffDSCF [16]; 49 50 #ifndef MPC_LITTLE_ENDIAN51 #define SWAP(X) mpc_swap32(X)52 #else53 #define SWAP(X) (X)54 #endif55 52 56 53 //------------------------------------------------------------------------------ … … 70 67 // forward declarations 71 68 //------------------------------------------------------------------------------ 72 static void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking); 73 static void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band); 74 75 //------------------------------------------------------------------------------ 76 // utility functions 77 //------------------------------------------------------------------------------ 78 static mpc_int32_t f_read(mpc_decoder *d, void *ptr, mpc_int32_t size) 79 { 80 return d->r->read(d->r, ptr, size); 81 } 82 83 static mpc_bool_t f_seek(mpc_decoder *d, mpc_int32_t offset) 84 { 85 return d->r->seek(d->r, offset); 86 } 87 88 static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count) 89 { 90 return f_read(d, ptr, count << 2) >> 2; 91 } 92 93 static void mpc_decoder_seek(mpc_decoder *d, mpc_uint32_t bitpos) 94 { 95 f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos); 96 f_read_dword(d, d->Speicher, MEMSIZE); 97 d->dword = SWAP(d->Speicher[d->Zaehler = 0]); 98 d->pos = bitpos & 31; 99 d->WordsRead = bitpos >> 5; 100 } 101 102 // jump desired number of bits out of the bitstream 103 static void mpc_decoder_bitstream_jump(mpc_decoder *d, const mpc_uint32_t bits) 104 { 105 d->pos += bits; 106 107 if (d->pos >= 32) { 108 d->Zaehler = (d->Zaehler + (d->pos >> 5)) & MEMMASK; 109 d->dword = SWAP(d->Speicher[d->Zaehler]); 110 d->WordsRead += d->pos >> 5; 111 d->pos &= 31; 112 } 113 } 114 115 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING) 116 { 117 if ((RING ^ d->Zaehler) & MEMSIZE2 ) { 118 // update buffer 119 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2); 120 } 121 } 122 123 //------------------------------------------------------------------------------ 124 // huffman & bitstream functions 125 //------------------------------------------------------------------------------ 126 127 /* F U N C T I O N S */ 128 129 // resets bitstream decoding 130 static void 131 mpc_decoder_reset_bitstream_decode(mpc_decoder *d) 132 { 133 d->dword = 0; 134 d->pos = 0; 135 d->Zaehler = 0; 136 d->WordsRead = 0; 137 } 138 139 // reports the number of read bits 140 static mpc_uint32_t 141 mpc_decoder_bits_read(mpc_decoder *d) 142 { 143 return 32 * d->WordsRead + d->pos; 144 } 145 146 // read desired number of bits out of the bitstream (max 31) 147 static mpc_uint32_t 148 mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits) 149 { 150 mpc_uint32_t out = d->dword; 151 152 d->pos += bits; 153 154 if (d->pos < 32) { 155 out >>= (32 - d->pos); 156 } else { 157 d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]); 158 d->pos -= 32; 159 if (d->pos) { 160 out <<= d->pos; 161 out |= d->dword >> (32 - d->pos); 162 } 163 d->WordsRead++; 164 } 165 166 return out & ((1 << bits) - 1); 167 } 168 169 // basic huffman decoding routine 170 // works with maximum lengths up to max_length 171 static mpc_int32_t 172 mpc_decoder_huffman_decode(mpc_decoder *d, const mpc_huffman *Table, 173 const mpc_uint32_t max_length) 174 { 175 // load preview and decode 176 mpc_uint32_t code = d->dword << d->pos; 177 if (32 - d->pos < max_length) 178 code |= SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]) >> (32 - d->pos); 179 180 while (code < Table->Code) Table++; 181 182 // set the new position within bitstream without performing a dummy-read 183 if ((d->pos += Table->Length) >= 32) { 184 d->pos -= 32; 185 d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]); 186 d->WordsRead++; 187 } 188 189 return Table->Value; 190 } 191 192 void mpc_decoder_get_block(mpc_decoder *d, mpc_block_t * p_block) 193 { 194 unsigned char tmp; 195 unsigned int size = 2; 196 197 p_block->size = 0; 198 p_block->key[0] = mpc_decoder_bitstream_read(d, 8); 199 p_block->key[1] = mpc_decoder_bitstream_read(d, 8); 200 do { 201 tmp = mpc_decoder_bitstream_read(d, 8); 202 p_block->size = (p_block->size << 7) | (tmp & 0x7F); 203 size++; 204 } while((tmp & 0x80)); 205 206 p_block->size -= size; 207 } 208 209 static void 210 mpc_decoder_reset_v(mpc_decoder *d) 211 { 212 memset(d->V_L, 0, sizeof d->V_L); 213 memset(d->V_R, 0, sizeof d->V_R); 214 } 215 216 static void 217 mpc_decoder_reset_synthesis(mpc_decoder *d) 218 { 219 mpc_decoder_reset_v(d); 220 } 221 222 static void 223 mpc_decoder_reset_y(mpc_decoder *d) 224 { 225 memset(d->Y_L, 0, sizeof d->Y_L); 226 memset(d->Y_R, 0, sizeof d->Y_R); 227 } 228 229 static void 230 mpc_decoder_reset_globals(mpc_decoder *d) 231 { 232 mpc_decoder_reset_bitstream_decode(d); 233 234 d->DecodedFrames = 0; 235 d->StreamVersion = 0; 236 d->MS_used = 0; 237 238 memset(d->Y_L , 0, sizeof d->Y_L ); 239 memset(d->Y_R , 0, sizeof d->Y_R ); 240 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L ); 241 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R ); 242 memset(d->Res_L , 0, sizeof d->Res_L ); 243 memset(d->Res_R , 0, sizeof d->Res_R ); 244 memset(d->SCFI_L , 0, sizeof d->SCFI_L ); 245 memset(d->SCFI_R , 0, sizeof d->SCFI_R ); 246 memset(d->DSCF_Flag_L , 0, sizeof d->DSCF_Flag_L ); 247 memset(d->DSCF_Flag_R , 0, sizeof d->DSCF_Flag_R ); 248 memset(d->Q , 0, sizeof d->Q ); 249 memset(d->MS_Flag , 0, sizeof d->MS_Flag ); 250 memset(d->seeking_table , 0, sizeof d->seeking_table ); 251 } 252 253 mpc_uint32_t 254 mpc_decoder_decode_frame(mpc_decoder *d, mpc_uint32_t *in_buffer, 255 mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer) 256 { 257 mpc_decoder_reset_bitstream_decode(d); 258 if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher); 259 memcpy(d->Speicher, in_buffer, in_len); 260 d->dword = SWAP(d->Speicher[0]); 261 switch (d->StreamVersion) { 262 case 0x07: 263 case 0x17: 264 mpc_decoder_read_bitstream_sv7(d, MPC_FALSE); 265 break; 266 default: 267 return (mpc_uint32_t)(-1); 268 } 269 mpc_decoder_requantisierung(d, d->Max_Band); 270 mpc_decoder_synthese_filter_float(d, out_buffer); 271 return mpc_decoder_bits_read(d); 272 } 273 274 static mpc_uint32_t 275 mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer) 276 { 277 mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH; 278 mpc_uint32_t FwdJumpInfo = 0; 279 mpc_uint32_t FrameBitCnt = 0; 280 281 if (d->DecodedFrames >= d->OverallFrames) { 282 return (mpc_uint32_t)(-1); // end of file -> abort decoding 283 } 284 285 // add seeking info 286 if (d->seeking_table_frames < d->DecodedFrames && 287 (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) { 288 d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d); 289 d->seeking_table_frames = d->DecodedFrames; 290 } 291 292 switch (d->StreamVersion) { 293 case 0x07: 294 case 0x17: 295 // read jump-info for validity check of frame 296 FwdJumpInfo = mpc_decoder_bitstream_read(d, 20); 297 // decode data and check for validity of frame 298 FrameBitCnt = mpc_decoder_bits_read(d); 299 mpc_decoder_read_bitstream_sv7(d, MPC_FALSE); 300 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == FwdJumpInfo; 301 break; 302 case 0x08: 303 if (d->BlockBits < 8){ 304 mpc_block_t block; 305 // trouver le block 306 mpc_decoder_bitstream_read(d, d->BlockBits); 307 while (1) { 308 mpc_decoder_get_block(d, &block); 309 if (memcmp(block.key, "AD", 2) != 0) 310 mpc_decoder_seek(d, mpc_decoder_bits_read(d) + block.size * 8); 311 else 312 break; 313 } 314 d->BlockBits = block.size * 8; 69 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r); 70 static void mpc_decoder_requantisierung(mpc_decoder *d); 71 72 static void mpc_decoder_reset_y(mpc_decoder *d) 73 { 74 memset(d->Y_L, 0, sizeof d->Y_L); 75 memset(d->Y_R, 0, sizeof d->Y_R); 76 } 77 78 void mpc_decoder_setup(mpc_decoder *d) 79 { 80 memset(d, 0, sizeof *d); 81 82 d->__r1 = 1; 83 d->__r2 = 1; 84 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY; 85 86 mpc_decoder_init_quant(d, 1.0f); 87 } 88 89 void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si) 90 { 91 d->StreamVersion = si->stream_version; 92 d->MS_used = si->ms; 93 d->Max_Band = si->max_band; 94 d->OverallFrames = si->frames; 95 d->TrueGaplessPresent = si->is_true_gapless; 96 d->SampleRate = si->sample_freq; 97 } 98 99 mpc_decoder * mpc_decoder_init(mpc_streaminfo *si) 100 { 101 mpc_decoder* p_tmp = malloc(sizeof(mpc_decoder)); 102 103 if (p_tmp != 0) { 104 mpc_decoder_setup(p_tmp); 105 mpc_decoder_set_streaminfo(p_tmp, si); 106 } 107 108 return p_tmp; 109 } 110 111 void mpc_decoder_exit(mpc_decoder *d) 112 { 113 free(d); 114 } 115 116 void mpc_decoder_decode_frame(mpc_decoder * d, 117 mpc_bits_reader * r, 118 mpc_frame_info * i) 119 { 120 // FIXME : sauter décodage si d->samples_to_skip > MPC_FRAME_LENGTH pour sv7 121 switch (d->StreamVersion) { 122 case 0x07: 123 case 0x17: 124 case 0x08: 125 i->channels = 2; 126 i->sample_freq = d->SampleRate; 127 i->samples = MPC_FRAME_LENGTH; 128 mpc_decoder_read_bitstream_sv7(d, r); 129 break; 130 } 131 132 // synthesize signal 133 mpc_decoder_requantisierung(d); 134 mpc_decoder_synthese_filter_float(d, i->buffer); 135 136 d->DecodedFrames++; 137 138 // // cut off first MPC_DECODER_SYNTH_DELAY zero-samples 139 // if (d->DecodedFrames == d->OverallFrames && d->StreamVersion >= 6) { 140 // // reconstruct exact filelength 141 // mpc_int32_t mod_block = mpc_bits_read(r, 11); 142 // mpc_int32_t FilterDecay; 143 // 144 // if (mod_block == 0) { 145 // // Encoder bugfix 146 // mod_block = 1152; 147 // } 148 // FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH; 149 // 150 // // additional FilterDecay samples are needed for decay of synthesis filter 151 // if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) { 152 // if (!d->TrueGaplessPresent) { 153 // mpc_decoder_reset_y(d); 154 // } else { 155 // mpc_bits_read(r, 20); 156 // mpc_decoder_read_bitstream_sv7(d, FALSE); 157 // mpc_decoder_requantisierung(d, d->Max_Band); 158 // } 159 // 160 // mpc_decoder_synthese_filter_float(d, buffer + 2304); 161 // 162 // i->samples = MPC_FRAME_LENGTH + FilterDecay; 163 // } 164 // else { // there are only FilterDecay samples needed for this frame 165 // i->samples = FilterDecay; 166 // } 167 // } 168 169 if (d->samples_to_skip) { 170 if (i->samples < d->samples_to_skip) { 171 d->samples_to_skip -= i->samples; 172 i->samples = 0; 173 } else { 174 i->samples -= d->samples_to_skip; 175 memmove(i->buffer, i->buffer + d->samples_to_skip * i->channels, 176 i->samples * i->channels * sizeof (MPC_SAMPLE_FORMAT)); 177 d->samples_to_skip = 0; 315 178 } 316 FrameBitCnt = mpc_decoder_bits_read(d); 317 mpc_decoder_read_bitstream_sv7(d, MPC_FALSE); 318 d->BlockBits -= mpc_decoder_bits_read(d) - FrameBitCnt; 319 d->FrameWasValid = 1; 320 break; 321 default: 322 return (mpc_uint32_t)(-1); 323 } 324 325 // synthesize signal 326 mpc_decoder_requantisierung(d, d->Max_Band); 327 mpc_decoder_synthese_filter_float(d, buffer); 328 329 d->DecodedFrames++; 330 331 // cut off first MPC_DECODER_SYNTH_DELAY zero-samples 332 if (d->DecodedFrames == d->OverallFrames && d->StreamVersion >= 6) { 333 // reconstruct exact filelength 334 mpc_int32_t mod_block = mpc_decoder_bitstream_read(d, 11); 335 mpc_int32_t FilterDecay; 336 337 if (mod_block == 0) { 338 // Encoder bugfix 339 mod_block = 1152; 340 } 341 FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH; 342 343 // additional FilterDecay samples are needed for decay of synthesis filter 344 if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) { 345 if (!d->TrueGaplessPresent) { 346 mpc_decoder_reset_y(d); 347 } else { 348 mpc_decoder_bitstream_read(d, 20); 349 mpc_decoder_read_bitstream_sv7(d, MPC_FALSE); 350 mpc_decoder_requantisierung(d, d->Max_Band); 351 } 352 353 mpc_decoder_synthese_filter_float(d, buffer + 2304); 354 355 output_frame_length = MPC_FRAME_LENGTH + FilterDecay; 356 } 357 else { // there are only FilterDecay samples needed for this frame 358 output_frame_length = FilterDecay; 359 } 360 } 361 362 if (d->samples_to_skip) { 363 if (output_frame_length < d->samples_to_skip) { 364 d->samples_to_skip -= output_frame_length; 365 output_frame_length = 0; 366 } 367 else { 368 output_frame_length -= d->samples_to_skip; 369 memmove( 370 buffer, 371 buffer + d->samples_to_skip * 2, 372 output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT)); 373 d->samples_to_skip = 0; 374 } 375 } 376 377 return output_frame_length; 378 } 379 380 mpc_uint32_t mpc_decoder_decode( 381 mpc_decoder *d, 382 MPC_SAMPLE_FORMAT *buffer, 383 mpc_uint32_t *vbr_update_acc, 384 mpc_uint32_t *vbr_update_bits) 385 { 386 for(;;) 387 { 388 //const mpc_int32_t MaxBrokenFrames = 0; // PluginSettings.MaxBrokenFrames 389 390 mpc_uint32_t RING = d->Zaehler; 391 mpc_int32_t vbr_ring = (RING << 5) + d->pos; 392 393 mpc_uint32_t valid_samples = mpc_decoder_decode_internal(d, buffer); 394 395 if (valid_samples == (mpc_uint32_t)(-1) ) { 396 return 0; 397 } 398 399 /**************** ERROR CONCEALMENT *****************/ 400 if (d->FrameWasValid == 0 ) { 401 // error occurred in bitstream 402 return (mpc_uint32_t)(-1); 403 } 404 else { 405 if (vbr_update_acc && vbr_update_bits) { 406 (*vbr_update_acc) ++; 407 vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring; 408 if (vbr_ring < 0) { 409 vbr_ring += 524288; 410 } 411 (*vbr_update_bits) += vbr_ring; 412 } 413 414 } 415 mpc_decoder_update_buffer(d, RING); 416 417 if (valid_samples > 0) { 418 return valid_samples; 419 } 420 } 179 } 421 180 } 422 181 423 182 void 424 mpc_decoder_requantisierung(mpc_decoder *d , const mpc_int32_t Last_Band)183 mpc_decoder_requantisierung(mpc_decoder *d) 425 184 { 426 185 mpc_int32_t Band; … … 434 193 mpc_int32_t* L; 435 194 mpc_int32_t* R; 195 const mpc_int32_t Last_Band = d->Max_Band; 436 196 437 197 #ifdef MPC_FIXED_POINT … … 579 339 } 580 340 581 void 582 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking) 341 void mpc_decoder_read_bitstream_sv7(mpc_decoder * d, mpc_bits_reader * r) 583 342 { 584 343 // these arrays hold decoding results for bundled quantizers (3- and 5-step) … … 602 361 603 362 // first subband 604 *ResL = mpc_ decoder_bitstream_read(d, 4);605 *ResR = mpc_ decoder_bitstream_read(d, 4);363 *ResL = mpc_bits_read(r, 4); 364 *ResR = mpc_bits_read(r, 4); 606 365 if (d->MS_used && !(*ResL==0 && *ResR==0)) { 607 d->MS_Flag[0] = mpc_ decoder_bitstream_read(d, 1);366 d->MS_Flag[0] = mpc_bits_read(r, 1); 608 367 } 609 368 … … 612 371 for (n=1; n <= d->Max_Band; ++n, ++ResL, ++ResR) 613 372 { 614 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9);615 *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_ decoder_bitstream_read(d, 4);616 617 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9);618 *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_ decoder_bitstream_read(d, 4);373 idx = mpc_bits_huff_dec(r, mpc_table_HuffHdr); 374 *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_bits_read(r, 4); 375 376 idx = mpc_bits_huff_dec(r, mpc_table_HuffHdr); 377 *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_bits_read(r, 4); 619 378 620 379 if (d->MS_used && !(*ResL==0 && *ResR==0)) { 621 d->MS_Flag[n] = mpc_ decoder_bitstream_read(d, 1);380 d->MS_Flag[n] = mpc_bits_read(r, 1); 622 381 } 623 382 … … 633 392 ResR = d->Res_R; 634 393 for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) { 635 if (*ResL) *L = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3);636 if (*ResR) *R = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3);394 if (*ResL) *L = mpc_bits_huff_dec(r, mpc_table_HuffSCFI); 395 if (*ResR) *R = mpc_bits_huff_dec(r, mpc_table_HuffSCFI); 637 396 } 638 397 … … 648 407 { 649 408 case 1: 650 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);651 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);652 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);653 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_ decoder_bitstream_read(d, 6);409 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 410 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6); 411 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 412 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_bits_read(r, 6); 654 413 L[2] = L[1]; 655 414 break; 656 415 case 3: 657 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);658 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);416 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 417 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6); 659 418 L[1] = L[0]; 660 419 L[2] = L[1]; 661 420 break; 662 421 case 2: 663 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);664 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);422 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 423 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6); 665 424 L[1] = L[0]; 666 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);667 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_ decoder_bitstream_read(d, 6);425 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 426 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_bits_read(r, 6); 668 427 break; 669 428 case 0: 670 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);671 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);672 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);673 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_ decoder_bitstream_read(d, 6);674 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);675 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_ decoder_bitstream_read(d, 6);429 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 430 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_bits_read(r, 6); 431 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 432 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_bits_read(r, 6); 433 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 434 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_bits_read(r, 6); 676 435 break; 677 436 default: … … 690 449 { 691 450 case 1: 692 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);693 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);694 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);695 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_ decoder_bitstream_read(d, 6);451 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 452 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6); 453 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 454 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_bits_read(r, 6); 696 455 R[2] = R[1]; 697 456 break; 698 457 case 3: 699 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);700 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);458 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 459 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6); 701 460 R[1] = R[0]; 702 461 R[2] = R[1]; 703 462 break; 704 463 case 2: 705 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);706 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);464 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 465 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6); 707 466 R[1] = R[0]; 708 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);709 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_ decoder_bitstream_read(d, 6);467 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 468 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_bits_read(r, 6); 710 469 break; 711 470 case 0: 712 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);713 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_ decoder_bitstream_read(d, 6);714 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);715 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_ decoder_bitstream_read(d, 6);716 idx = mpc_ decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);717 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_ decoder_bitstream_read(d, 6);471 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 472 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_bits_read(r, 6); 473 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 474 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_bits_read(r, 6); 475 idx = mpc_bits_huff_dec(r, mpc_table_HuffDSCF); 476 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_bits_read(r, 6); 718 477 break; 719 478 default: … … 729 488 } 730 489 731 if (seeking == MPC_TRUE)732 return;490 // if (d->seeking == TRUE) 491 // return; 733 492 734 493 /***************************** Samples ****************************/ … … 756 515 break; 757 516 case 1: 758 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][1];517 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1]; 759 518 for (k=0; k<12; ++k) 760 519 { 761 idx = mpc_ decoder_huffman_decode(d, Table, 9);520 idx = mpc_bits_huff_dec(r, Table); 762 521 *L++ = idx30[idx]; 763 522 *L++ = idx31[idx]; … … 766 525 break; 767 526 case 2: 768 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][2];527 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2]; 769 528 for (k=0; k<18; ++k) 770 529 { 771 idx = mpc_ decoder_huffman_decode(d, Table, 10);530 idx = mpc_bits_huff_dec(r, Table); 772 531 *L++ = idx50[idx]; 773 532 *L++ = idx51[idx]; … … 776 535 case 3: 777 536 case 4: 778 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][*ResL];537 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL]; 779 538 for (k=0; k<36; ++k) 780 *L++ = mpc_ decoder_huffman_decode(d, Table, 5);539 *L++ = mpc_bits_huff_dec(r, Table); 781 540 break; 782 541 case 5: 783 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][*ResL];542 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL]; 784 543 for (k=0; k<36; ++k) 785 *L++ = mpc_ decoder_huffman_decode(d, Table, 8);544 *L++ = mpc_bits_huff_dec(r, Table); 786 545 break; 787 546 case 6: 788 547 case 7: 789 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][*ResL];548 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResL]; 790 549 for (k=0; k<36; ++k) 791 *L++ = mpc_ decoder_huffman_decode(d, Table, 14);550 *L++ = mpc_bits_huff_dec(r, Table); 792 551 break; 793 552 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: 794 553 tmp = Dc[*ResL]; 795 554 for (k=0; k<36; ++k) 796 *L++ = (mpc_int32_t)mpc_ decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;555 *L++ = (mpc_int32_t)mpc_bits_read(r, Res_bit[*ResL]) - tmp; 797 556 break; 798 557 default: … … 816 575 break; 817 576 case 1: 818 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][1];577 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][1]; 819 578 for (k=0; k<12; ++k) 820 579 { 821 idx = mpc_ decoder_huffman_decode(d, Table, 9);580 idx = mpc_bits_huff_dec(r, Table); 822 581 *R++ = idx30[idx]; 823 582 *R++ = idx31[idx]; … … 826 585 break; 827 586 case 2: 828 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][2];587 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][2]; 829 588 for (k=0; k<18; ++k) 830 589 { 831 idx = mpc_ decoder_huffman_decode(d, Table, 10);590 idx = mpc_bits_huff_dec(r, Table); 832 591 *R++ = idx50[idx]; 833 592 *R++ = idx51[idx]; … … 836 595 case 3: 837 596 case 4: 838 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][*ResR];597 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR]; 839 598 for (k=0; k<36; ++k) 840 *R++ = mpc_ decoder_huffman_decode(d, Table, 5);599 *R++ = mpc_bits_huff_dec(r, Table); 841 600 break; 842 601 case 5: 843 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][*ResR];602 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR]; 844 603 for (k=0; k<36; ++k) 845 *R++ = mpc_ decoder_huffman_decode(d, Table, 8);604 *R++ = mpc_bits_huff_dec(r, Table); 846 605 break; 847 606 case 6: 848 607 case 7: 849 Table = mpc_table_HuffQ[mpc_ decoder_bitstream_read(d, 1)][*ResR];608 Table = mpc_table_HuffQ[mpc_bits_read(r, 1)][*ResR]; 850 609 for (k=0; k<36; ++k) 851 *R++ = mpc_ decoder_huffman_decode(d, Table, 14);610 *R++ = mpc_bits_huff_dec(r, Table); 852 611 break; 853 612 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: 854 613 tmp = Dc[*ResR]; 855 614 for (k=0; k<36; ++k) 856 *R++ = (mpc_int32_t)mpc_ decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;615 *R++ = (mpc_int32_t)mpc_bits_read(r, Res_bit[*ResR]) - tmp; 857 616 break; 858 617 default: … … 862 621 } 863 622 864 void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r) 865 { 866 d->r = r; 867 868 d->MPCHeaderPos = 0; 869 d->StreamVersion = 0; 870 d->MS_used = 0; 871 d->FrameWasValid = 0; 872 d->OverallFrames = 0; 873 d->DecodedFrames = 0; 874 d->TrueGaplessPresent = 0; 875 d->WordsRead = 0; 876 d->Max_Band = 0; 877 d->SampleRate = 0; 878 d->__r1 = 1; 879 d->__r2 = 1; 880 d->BlockBits = 0; 881 882 d->Max_Band = 0; 883 d->seeking_window = FAST_SEEKING_WINDOW; 884 885 mpc_decoder_reset_bitstream_decode(d); 886 mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f); 887 } 888 889 static mpc_uint32_t get_initial_fpos(mpc_decoder *d) 890 { 891 mpc_uint32_t fpos = 0; 892 switch ( d->StreamVersion ) { // setting position to the beginning of the data-bitstream 893 case 0x04: fpos = 48; break; 894 case 0x05: 895 case 0x06: fpos = 64; break; 896 case 0x07: 897 case 0x17: fpos = 200; break; 898 case 0x08: fpos = 32; break; 899 } 900 return fpos; 901 } 902 903 void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si) 904 { 905 mpc_decoder_reset_synthesis(d); 906 mpc_decoder_reset_globals(d); 907 908 d->StreamVersion = si->stream_version; 909 d->MS_used = si->ms; 910 d->Max_Band = si->max_band; 911 d->OverallFrames = si->frames; 912 d->MPCHeaderPos = si->header_position; 913 d->TrueGaplessPresent = si->is_true_gapless; 914 d->SampleRate = (mpc_int32_t)si->sample_freq; 915 916 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY; 917 } 918 919 mpc_status mpc_decoder_init(mpc_decoder **d, mpc_reader *r, mpc_streaminfo *si) 920 { 921 mpc_decoder* p_tmp; 922 923 p_tmp = malloc(sizeof *p_tmp); 924 memset(p_tmp, 0, sizeof *p_tmp); 925 mpc_decoder_setup(p_tmp, r); 926 927 mpc_decoder_set_streaminfo(p_tmp, si); 928 929 // AB: setting position to the beginning of the data-bitstream 930 mpc_decoder_seek(p_tmp, get_initial_fpos(p_tmp)); 931 932 p_tmp->seeking_pwr = 0; 933 while (p_tmp->OverallFrames > (SEEKING_TABLE_SIZE << p_tmp->seeking_pwr)) 934 p_tmp->seeking_pwr++; 935 p_tmp->seeking_table_frames = 0; 936 p_tmp->seeking_table[0] = get_initial_fpos(p_tmp); 937 938 *d = p_tmp; 939 return MPC_STATUS_OK; 940 } 941 942 void mpc_decoder_exit(mpc_decoder *d) 943 { 944 free(d); 945 } 946 947 void mpc_decoder_set_seeking(mpc_decoder *d, mpc_streaminfo *si, mpc_bool_t fast_seeking) 948 { 949 d->seeking_window = FAST_SEEKING_WINDOW; 950 if (si->fast_seek == 0 && fast_seeking == 0) 951 d->seeking_window = SLOW_SEEKING_WINDOW; 952 } 953 954 mpc_bool_t mpc_decoder_seek_seconds(mpc_decoder *d, double seconds) 955 { 956 return mpc_decoder_seek_sample(d, (mpc_int64_t)(seconds * (double)d->SampleRate + 0.5)); 957 } 958 959 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample) 960 { 961 mpc_uint32_t fpos; 962 mpc_uint32_t fwd; 963 964 fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH); 965 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH); 966 967 // resetting synthesis filter to avoid "clicks" 968 mpc_decoder_reset_synthesis(d); 969 970 // prevent from desired position out of allowed range 971 fwd = fwd < d->OverallFrames ? fwd : d->OverallFrames; 972 973 if (fwd > d->DecodedFrames + d->seeking_window || fwd < d->DecodedFrames) { 974 memset(d->SCF_Index_L, 1, sizeof d->SCF_Index_L ); 975 memset(d->SCF_Index_R, 1, sizeof d->SCF_Index_R ); 976 } 977 978 if (d->seeking_table_frames > d->DecodedFrames || fwd < d->DecodedFrames) { 979 d->DecodedFrames = 0; 980 if (fwd > d->seeking_window) 981 d->DecodedFrames = (fwd - d->seeking_window) & (-1 << d->seeking_pwr); 982 if (d->DecodedFrames > d->seeking_table_frames) 983 d->DecodedFrames = d->seeking_table_frames; 984 fpos = d->seeking_table[d->DecodedFrames >> d->seeking_pwr]; 985 mpc_decoder_seek(d, fpos); 986 } 987 988 // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping) 989 for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) { 990 mpc_uint32_t RING = d->Zaehler; 991 mpc_uint32_t FwdJumpInfo; 992 993 // add seeking info 994 if (d->seeking_table_frames < d->DecodedFrames && 995 (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) { 996 d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d); 997 d->seeking_table_frames = d->DecodedFrames; 998 } 999 1000 FwdJumpInfo = mpc_decoder_bitstream_read(d, 20) + mpc_decoder_bits_read(d); // read jump-info 1001 1002 if (fwd <= d->DecodedFrames + d->seeking_window) { 1003 if (d->StreamVersion >= 7) 1004 mpc_decoder_read_bitstream_sv7(d, MPC_TRUE); 1005 else 1006 return MPC_FALSE; 1007 } 1008 mpc_decoder_bitstream_jump(d, FwdJumpInfo - mpc_decoder_bits_read(d)); 1009 1010 // update buffer 1011 mpc_decoder_update_buffer(d, RING); 1012 } 1013 1014 return MPC_TRUE; 1015 } 623 624
Note: See TracChangeset
for help on using the changeset viewer.