Changeset 37 for libmpcdec/trunk/src/mpc_decoder.c
- Timestamp:
- 09/22/06 22:00:44 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libmpcdec/trunk/src/mpc_decoder.c
r11 r37 59 59 #endif 60 60 61 #ifndef MPC_LITTLE_ENDIAN 62 #define SWAP(X) mpc_swap32(X) 63 #else 64 #define SWAP(X) (X) 65 #endif 66 61 67 //------------------------------------------------------------------------------ 62 68 // types … … 75 81 // forward declarations 76 82 //------------------------------------------------------------------------------ 77 void mpc_decoder_read_bitstream_sv6(mpc_decoder *d); 78 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d); 79 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING); 83 void mpc_decoder_read_bitstream_sv6(mpc_decoder *d, mpc_bool_t seeking); 84 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking); 80 85 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample); 81 86 void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band); … … 96 101 static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count) 97 102 { 98 mpc_uint32_t n; 99 count = f_read(d, ptr, count << 2) >> 2; 100 #ifndef MPC_LITTLE_ENDIAN 101 for(n = 0; n< count; n++) { 102 ptr[n] = mpc_swap32(ptr[n]); 103 } 104 #endif 105 return count; 103 return f_read(d, ptr, count << 2) >> 2; 104 } 105 106 static void mpc_decoder_seek(mpc_decoder *d, mpc_uint32_t bitpos) 107 { 108 f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos); 109 f_read_dword(d, d->Speicher, MEMSIZE); 110 d->dword = SWAP(d->Speicher[d->Zaehler = 0]); 111 d->pos = bitpos & 31; 112 d->WordsRead = bitpos >> 5; 113 } 114 115 // jump desired number of bits out of the bitstream 116 static void mpc_decoder_bitstream_jump(mpc_decoder *d, const mpc_uint32_t bits) 117 { 118 d->pos += bits; 119 120 if (d->pos >= 32) { 121 d->Zaehler = (d->Zaehler + (d->pos >> 5)) & MEMMASK; 122 d->dword = SWAP(d->Speicher[d->Zaehler]); 123 d->WordsRead += d->pos >> 5; 124 d->pos &= 31; 125 } 126 } 127 128 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING) 129 { 130 if ((RING ^ d->Zaehler) & MEMSIZE2 ) { 131 // update buffer 132 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2); 133 } 106 134 } 107 135 … … 109 137 // huffman & bitstream functions 110 138 //------------------------------------------------------------------------------ 111 static const mpc_uint32_t mask [33] = {112 0x00000000, 0x00000001, 0x00000003, 0x00000007,113 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,114 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,115 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,116 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,117 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,118 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,119 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,120 0xFFFFFFFF121 };122 139 123 140 /* F U N C T I O N S */ … … 140 157 } 141 158 142 // read desired number of bits out of the bitstream 159 // read desired number of bits out of the bitstream (max 31) 143 160 static mpc_uint32_t 144 161 mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits) … … 150 167 if (d->pos < 32) { 151 168 out >>= (32 - d->pos); 152 } 153 else { 154 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 169 } else { 170 d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]); 155 171 d->pos -= 32; 156 172 if (d->pos) { … … 158 174 out |= d->dword >> (32 - d->pos); 159 175 } 160 ++(d->WordsRead);161 } 162 163 return out & mask[bits];164 } 165 166 // decode SCFI-bundle (sv4,5,6)167 static void 168 mpc_decoder_scfi_bundle_read( 169 mpc_decoder *d,170 const HuffmanTyp* Table, mpc_int32_t* SCFI, mpc_bool_t* DSCF)176 d->WordsRead++; 177 } 178 179 return out & ((1 << bits) - 1); 180 } 181 182 // basic huffman decoding routine 183 // works with maximum lengths up to max_length 184 static mpc_int32_t 185 mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table, 186 const mpc_uint32_t max_length) 171 187 { 172 188 // load preview and decode 173 mpc_uint32_t code = d->dword << d->pos; 174 if (d->pos > 26) { 175 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos); 176 } 177 while (code < Table->Code) { 178 Table++; 179 } 189 mpc_uint32_t code = d->dword << d->pos; 190 if (32 - d->pos < max_length) 191 code |= SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]) >> (32 - d->pos); 192 193 while (code < Table->Code) Table++; 180 194 181 195 // set the new position within bitstream without performing a dummy-read 182 196 if ((d->pos += Table->Length) >= 32) { 183 197 d->pos -= 32; 184 d->dword = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK]; 185 ++(d->WordsRead); 186 } 187 188 *SCFI = Table->Value >> 1; 189 *DSCF = Table->Value & 1; 190 } 191 192 // basic huffman decoding routine 193 // works with maximum lengths up to 14 194 static mpc_int32_t 195 mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table) 196 { 197 // load preview and decode 198 mpc_uint32_t code = d->dword << d->pos; 199 if (d->pos > 18) { 200 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos); 201 } 202 while (code < Table->Code) { 203 Table++; 204 } 205 206 // set the new position within bitstream without performing a dummy-read 207 if ((d->pos += Table->Length) >= 32) { 208 d->pos -= 32; 209 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 210 ++(d->WordsRead); 198 d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]); 199 d->WordsRead++; 211 200 } 212 201 … … 214 203 } 215 204 216 // faster huffman through previewing less bits 217 // works with maximum lengths up to 10 218 static mpc_int32_t 219 mpc_decoder_huffman_decode_fast(mpc_decoder *d, const HuffmanTyp* Table) 220 { 221 // load preview and decode 222 mpc_uint32_t code = d->dword << d->pos; 223 if (d->pos > 22) { 224 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos); 225 } 226 while (code < Table->Code) { 227 Table++; 228 } 229 230 // set the new position within bitstream without performing a dummy-read 231 if ((d->pos += Table->Length) >= 32) { 232 d->pos -= 32; 233 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 234 ++(d->WordsRead); 235 } 236 237 return Table->Value; 238 } 239 240 // even faster huffman through previewing even less bits 241 // works with maximum lengths up to 5 242 static mpc_int32_t 243 mpc_decoder_huffman_decode_faster(mpc_decoder *d, const HuffmanTyp* Table) 244 { 245 // load preview and decode 246 mpc_uint32_t code = d->dword << d->pos; 247 if (d->pos > 27) { 248 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos); 249 } 250 while (code < Table->Code) { 251 Table++; 252 } 253 254 // set the new position within bitstream without performing a dummy-read 255 if ((d->pos += Table->Length) >= 32) { 256 d->pos -= 32; 257 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 258 ++(d->WordsRead); 259 } 260 261 return Table->Value; 205 // decode SCFI-bundle (sv4,5,6) 206 static void 207 mpc_decoder_scfi_bundle_read(mpc_decoder *d, const HuffmanTyp* Table, 208 mpc_int32_t* SCFI, mpc_bool_t* DSCF) 209 { 210 mpc_uint32_t value = mpc_decoder_huffman_decode(d, Table, 6); 211 212 *SCFI = value >> 1; 213 *DSCF = value & 1; 262 214 } 263 215 … … 301 253 memset(d->DSCF_Flag_L , 0, sizeof d->DSCF_Flag_L ); 302 254 memset(d->DSCF_Flag_R , 0, sizeof d->DSCF_Flag_R ); 303 memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );304 memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );305 255 memset(d->Q , 0, sizeof d->Q ); 306 256 memset(d->MS_Flag , 0, sizeof d->MS_Flag ); 257 memset(d->seeking_table , 0, sizeof d->seeking_table ); 307 258 } 308 259 … … 311 262 mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer) 312 263 { 313 unsigned int i;314 264 mpc_decoder_reset_bitstream_decode(d); 315 265 if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher); 316 266 memcpy(d->Speicher, in_buffer, in_len); 317 #ifdef MPC_LITTLE_ENDIAN 318 for (i = 0; i < (in_len + 3) / 4; i++) 319 d->Speicher[i] = mpc_swap32(d->Speicher[i]); 320 #endif 321 d->dword = d->Speicher[0]; 267 d->dword = SWAP(d->Speicher[0]); 322 268 switch (d->StreamVersion) { 323 269 #ifdef MPC_SUPPORT_SV456 … … 325 271 case 0x05: 326 272 case 0x06: 327 mpc_decoder_read_bitstream_sv6(d );273 mpc_decoder_read_bitstream_sv6(d, FALSE); 328 274 break; 329 275 #endif 330 276 case 0x07: 331 277 case 0x17: 332 mpc_decoder_read_bitstream_sv7(d );278 mpc_decoder_read_bitstream_sv7(d, FALSE); 333 279 break; 334 280 default: … … 344 290 { 345 291 mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH; 346 292 mpc_uint32_t FwdJumpInfo = 0; 347 293 mpc_uint32_t FrameBitCnt = 0; 348 294 … … 351 297 } 352 298 299 // add seeking info 300 if (d->seeking_table_frames < d->DecodedFrames && 301 (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) { 302 d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d); 303 d->seeking_table_frames = d->DecodedFrames; 304 } 305 353 306 // read jump-info for validity check of frame 354 d->FwdJumpInfo = mpc_decoder_bitstream_read(d, 20); 355 356 d->ActDecodePos = (d->Zaehler << 5) + d->pos; 307 FwdJumpInfo = mpc_decoder_bitstream_read(d, 20); 357 308 358 309 // decode data and check for validity of frame … … 363 314 case 0x05: 364 315 case 0x06: 365 mpc_decoder_read_bitstream_sv6(d );316 mpc_decoder_read_bitstream_sv6(d, FALSE); 366 317 break; 367 318 #endif 368 319 case 0x07: 369 320 case 0x17: 370 mpc_decoder_read_bitstream_sv7(d );321 mpc_decoder_read_bitstream_sv7(d, FALSE); 371 322 break; 372 323 default: 373 324 return (mpc_uint32_t)(-1); 374 325 } 375 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;326 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == FwdJumpInfo; 376 327 377 328 // synthesize signal 378 329 mpc_decoder_requantisierung(d, d->Max_Band); 379 380 //if ( d->EQ_activated && PluginSettings.EQbyMPC )381 // perform_EQ ();382 383 330 mpc_decoder_synthese_filter_float(d, buffer); 384 331 … … 403 350 } else { 404 351 mpc_decoder_bitstream_read(d, 20); 405 mpc_decoder_read_bitstream_sv7(d );352 mpc_decoder_read_bitstream_sv7(d, FALSE); 406 353 mpc_decoder_requantisierung(d, d->Max_Band); 407 354 } … … 673 620 /****************************************** SV 6 ******************************************/ 674 621 void 675 mpc_decoder_read_bitstream_sv6(mpc_decoder *d )622 mpc_decoder_read_bitstream_sv6(mpc_decoder *d, mpc_bool_t seeking) 676 623 { 677 624 mpc_int32_t n,k; … … 694 641 else /*if (n>=23)*/ Table = mpc_table_Region_C; 695 642 696 *ResL = Q_res[n][mpc_decoder_huffman_decode(d, Table )];643 *ResL = Q_res[n][mpc_decoder_huffman_decode(d, Table, 14)]; 697 644 if (d->MS_used) { 698 645 d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1); 699 646 } 700 *ResR = Q_res[n][mpc_decoder_huffman_decode(d, Table )];647 *ResR = Q_res[n][mpc_decoder_huffman_decode(d, Table, 14)]; 701 648 702 649 // only perform the following procedure up to the maximum non-zero subband … … 724 671 if (d->DSCF_Flag_L[n]==1) 725 672 { 726 L[2] = d->DSCF_Reference_L[n];727 673 switch (d->SCFI_L[n]) 728 674 { 729 675 case 3: 730 L[0] = L[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);676 L[0] = L[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 731 677 L[1] = L[0]; 732 678 L[2] = L[1]; 733 679 break; 734 680 case 1: 735 L[0] = L[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);736 L[1] = L[0] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);681 L[0] = L[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 682 L[1] = L[0] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 737 683 L[2] = L[1]; 738 684 break; 739 685 case 2: 740 L[0] = L[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);686 L[0] = L[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 741 687 L[1] = L[0]; 742 L[2] = L[1] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);688 L[2] = L[1] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 743 689 break; 744 690 case 0: 745 L[0] = L[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);746 L[1] = L[0] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);747 L[2] = L[1] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);691 L[0] = L[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 692 L[1] = L[0] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 693 L[2] = L[1] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 748 694 break; 749 695 default: 750 696 return; 751 break;752 697 } 698 if (L[0] > 1024) 699 L[0] = 0x8080; 700 if (L[1] > 1024) 701 L[1] = 0x8080; 702 if (L[2] > 1024) 703 L[2] = 0x8080; 753 704 } 754 705 /************ SCF ************/ … … 779 730 default: 780 731 return; 781 break;782 732 } 783 733 } 784 // update Reference for DSCF785 d->DSCF_Reference_L[n] = L[2];786 734 } 787 735 if (*ResR) 788 736 { 789 R[2] = d->DSCF_Reference_R[n];790 737 /*********** DSCF ************/ 791 738 if (d->DSCF_Flag_R[n]==1) … … 794 741 { 795 742 case 3: 796 R[0] = R[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);743 R[0] = R[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 797 744 R[1] = R[0]; 798 745 R[2] = R[1]; 799 746 break; 800 747 case 1: 801 R[0] = R[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);802 R[1] = R[0] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);748 R[0] = R[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 749 R[1] = R[0] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 803 750 R[2] = R[1]; 804 751 break; 805 752 case 2: 806 R[0] = R[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);753 R[0] = R[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 807 754 R[1] = R[0]; 808 R[2] = R[1] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);755 R[2] = R[1] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 809 756 break; 810 757 case 0: 811 R[0] = R[2] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);812 R[1] = R[0] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);813 R[2] = R[1] + mpc_decoder_huffman_decode _fast(d, mpc_table_DSCF_Entropie);758 R[0] = R[2] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 759 R[1] = R[0] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 760 R[2] = R[1] + mpc_decoder_huffman_decode(d, mpc_table_DSCF_Entropie, 6); 814 761 break; 815 762 default: 816 763 return; 817 break;818 764 } 765 if (R[0] > 1024) 766 R[0] = 0x8080; 767 if (R[1] > 1024) 768 R[1] = 0x8080; 769 if (R[2] > 1024) 770 R[2] = 0x8080; 819 771 } 820 772 /************ SCF ************/ … … 848 800 } 849 801 } 850 // update Reference for DSCF 851 d->DSCF_Reference_R[n] = R[2]; 852 } 853 } 802 } 803 } 804 805 if (seeking == TRUE) 806 return; 854 807 855 808 /**************************** Samples ****************************/ … … 867 820 for (k=0; k<36; ++k) 868 821 { 869 if (x1 != NULL) *L++ = mpc_decoder_huffman_decode _fast(d, x1);870 if (x2 != NULL) *R++ = mpc_decoder_huffman_decode _fast(d, x2);822 if (x1 != NULL) *L++ = mpc_decoder_huffman_decode(d, x1, 8); 823 if (x2 != NULL) *R++ = mpc_decoder_huffman_decode(d, x2, 8); 871 824 } 872 825 … … 882 835 /****************************************** SV 7 ******************************************/ 883 836 void 884 mpc_decoder_read_bitstream_sv7(mpc_decoder *d )837 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking) 885 838 { 886 839 // these arrays hold decoding results for bundled quantizers (3- and 5-step) 887 /*static*/mpc_int32_t idx30[] = { -1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1};888 /*static*/mpc_int32_t idx31[] = { -1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1};889 /*static*/mpc_int32_t idx32[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};890 /*static*/mpc_int32_t idx50[] = { -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};891 /*static*/mpc_int32_t idx51[] = { -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};840 static const mpc_int32_t idx30[] = { -1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1}; 841 static const mpc_int32_t idx31[] = { -1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1}; 842 static const mpc_int32_t idx32[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 843 static const mpc_int32_t idx50[] = { -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2}; 844 static const mpc_int32_t idx51[] = { -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; 892 845 893 846 mpc_int32_t n,k; … … 914 867 for (n=1; n <= d->Max_Band; ++n, ++ResL, ++ResR) 915 868 { 916 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffHdr);869 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9); 917 870 *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_decoder_bitstream_read(d, 4); 918 871 919 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffHdr);872 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9); 920 873 *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_decoder_bitstream_read(d, 4); 921 874 … … 935 888 ResR = d->Res_R; 936 889 for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) { 937 if (*ResL) *L = mpc_decoder_huffman_decode _faster(d, mpc_table_HuffSCFI);938 if (*ResR) *R = mpc_decoder_huffman_decode _faster(d, mpc_table_HuffSCFI);890 if (*ResL) *L = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3); 891 if (*ResR) *R = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3); 939 892 } 940 893 … … 947 900 if (*ResL) 948 901 { 949 L[2] = d->DSCF_Reference_L[n];950 902 switch (d->SCFI_L[n]) 951 903 { 952 904 case 1: 953 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);905 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 954 906 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 955 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);907 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 956 908 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6); 957 909 L[2] = L[1]; 958 910 break; 959 911 case 3: 960 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);912 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 961 913 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 962 914 L[1] = L[0]; … … 964 916 break; 965 917 case 2: 966 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);918 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 967 919 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 968 920 L[1] = L[0]; 969 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);921 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 970 922 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6); 971 923 break; 972 924 case 0: 973 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);925 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 974 926 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 975 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);927 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 976 928 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6); 977 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);929 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 978 930 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6); 979 931 break; 980 932 default: 981 933 return; 982 break; 983 } 984 // update Reference for DSCF 985 d->DSCF_Reference_L[n] = L[2]; 934 } 935 if (L[0] > 1024) 936 L[0] = 0x8080; 937 if (L[1] > 1024) 938 L[1] = 0x8080; 939 if (L[2] > 1024) 940 L[2] = 0x8080; 986 941 } 987 942 if (*ResR) 988 943 { 989 R[2] = d->DSCF_Reference_R[n];990 944 switch (d->SCFI_R[n]) 991 945 { 992 946 case 1: 993 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);947 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 994 948 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 995 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);949 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 996 950 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6); 997 951 R[2] = R[1]; 998 952 break; 999 953 case 3: 1000 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);954 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 1001 955 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 1002 956 R[1] = R[0]; … … 1004 958 break; 1005 959 case 2: 1006 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);960 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 1007 961 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 1008 962 R[1] = R[0]; 1009 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);963 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 1010 964 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6); 1011 965 break; 1012 966 case 0: 1013 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);967 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 1014 968 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6); 1015 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);969 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 1016 970 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6); 1017 idx = mpc_decoder_huffman_decode _fast(d, mpc_table_HuffDSCF);971 idx = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6); 1018 972 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6); 1019 973 break; 1020 974 default: 1021 975 return; 1022 break; 1023 } 1024 // update Reference for DSCF 1025 d->DSCF_Reference_R[n] = R[2]; 1026 } 1027 } 976 } 977 if (R[0] > 1024) 978 R[0] = 0x8080; 979 if (R[1] > 1024) 980 R[1] = 0x8080; 981 if (R[2] > 1024) 982 R[2] = 0x8080; 983 } 984 } 985 986 if (seeking == TRUE) 987 return; 988 1028 989 /***************************** Samples ****************************/ 1029 990 ResL = d->Res_L; … … 1053 1014 for (k=0; k<12; ++k) 1054 1015 { 1055 idx = mpc_decoder_huffman_decode _fast(d, Table);1016 idx = mpc_decoder_huffman_decode(d, Table, 9); 1056 1017 *L++ = idx30[idx]; 1057 1018 *L++ = idx31[idx]; … … 1063 1024 for (k=0; k<18; ++k) 1064 1025 { 1065 idx = mpc_decoder_huffman_decode _fast(d, Table);1026 idx = mpc_decoder_huffman_decode(d, Table, 10); 1066 1027 *L++ = idx50[idx]; 1067 1028 *L++ = idx51[idx]; … … 1072 1033 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL]; 1073 1034 for (k=0; k<36; ++k) 1074 *L++ = mpc_decoder_huffman_decode _faster(d, Table);1035 *L++ = mpc_decoder_huffman_decode(d, Table, 5); 1075 1036 break; 1076 1037 case 5: 1077 1038 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL]; 1078 1039 for (k=0; k<36; ++k) 1079 *L++ = mpc_decoder_huffman_decode _fast(d, Table);1040 *L++ = mpc_decoder_huffman_decode(d, Table, 8); 1080 1041 break; 1081 1042 case 6: … … 1083 1044 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL]; 1084 1045 for (k=0; k<36; ++k) 1085 *L++ = mpc_decoder_huffman_decode(d, Table );1046 *L++ = mpc_decoder_huffman_decode(d, Table, 14); 1086 1047 break; 1087 1048 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: … … 1113 1074 for (k=0; k<12; ++k) 1114 1075 { 1115 idx = mpc_decoder_huffman_decode _fast(d, Table);1076 idx = mpc_decoder_huffman_decode(d, Table, 9); 1116 1077 *R++ = idx30[idx]; 1117 1078 *R++ = idx31[idx]; … … 1123 1084 for (k=0; k<18; ++k) 1124 1085 { 1125 idx = mpc_decoder_huffman_decode _fast(d, Table);1086 idx = mpc_decoder_huffman_decode(d, Table, 10); 1126 1087 *R++ = idx50[idx]; 1127 1088 *R++ = idx51[idx]; … … 1132 1093 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR]; 1133 1094 for (k=0; k<36; ++k) 1134 *R++ = mpc_decoder_huffman_decode _faster(d, Table);1095 *R++ = mpc_decoder_huffman_decode(d, Table, 5); 1135 1096 break; 1136 1097 case 5: 1137 1098 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR]; 1138 1099 for (k=0; k<36; ++k) 1139 *R++ = mpc_decoder_huffman_decode _fast(d, Table);1100 *R++ = mpc_decoder_huffman_decode(d, Table, 8); 1140 1101 break; 1141 1102 case 6: … … 1143 1104 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR]; 1144 1105 for (k=0; k<36; ++k) 1145 *R++ = mpc_decoder_huffman_decode(d, Table );1106 *R++ = mpc_decoder_huffman_decode(d, Table, 14); 1146 1107 break; 1147 1108 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: … … 1163 1124 d->StreamVersion = 0; 1164 1125 d->MS_used = 0; 1165 d->FwdJumpInfo = 0;1166 d->ActDecodePos = 0;1167 1126 d->FrameWasValid = 0; 1168 1127 d->OverallFrames = 0; … … 1175 1134 d->__r2 = 1; 1176 1135 1177 d->dword = 0;1178 d->pos = 0;1179 d->Zaehler = 0;1180 d->WordsRead = 0;1181 1136 d->Max_Band = 0; 1182 1137 d->seeking_window = FAST_SEEKING_WINDOW; 1138 1139 mpc_decoder_reset_bitstream_decode(d); 1183 1140 mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f); 1184 1141 #if 0 … … 1186 1143 mpc_decoder_init_huffman_sv7(d); 1187 1144 #endif 1145 } 1146 1147 static mpc_uint32_t get_initial_fpos(mpc_decoder *d) 1148 { 1149 mpc_uint32_t fpos = 0; 1150 switch ( d->StreamVersion ) { // setting position to the beginning of the data-bitstream 1151 case 0x04: fpos = 48; break; 1152 case 0x05: 1153 case 0x06: fpos = 64; break; 1154 case 0x07: 1155 case 0x17: fpos = 200; break; 1156 } 1157 return fpos; 1188 1158 } 1189 1159 … … 1209 1179 1210 1180 // AB: setting position to the beginning of the data-bitstream 1211 switch (d->StreamVersion) { 1212 case 0x04: f_seek(d, 4 + d->MPCHeaderPos); d->pos = 16; break; // Geht auch über eine der Helperfunktionen 1213 case 0x05: 1214 case 0x06: f_seek(d, 8 + d->MPCHeaderPos); d->pos = 0; break; 1215 case 0x07: 1216 case 0x17: /*f_seek ( 24 + d->MPCHeaderPos );*/ d->pos = 8; break; 1217 default: return FALSE; 1218 } 1219 1220 // AB: fill buffer and initialize decoder 1221 f_read_dword(d, d->Speicher, MEMSIZE ); 1222 d->dword = d->Speicher[d->Zaehler = 0]; 1181 mpc_decoder_seek(d, get_initial_fpos(d)); 1182 1183 d->seeking_pwr = 0; 1184 while (d->OverallFrames > (SEEKING_TABLE_SIZE << d->seeking_pwr)) 1185 d->seeking_pwr++; 1186 d->seeking_table_frames = 0; 1187 d->seeking_table[0] = get_initial_fpos(d); 1223 1188 1224 1189 return TRUE; 1225 1190 } 1226 1191 1227 //--------------------------------------------------------------- 1228 // will seek from the beginning of the file to the desired 1229 // position in ms (given by seek_needed) 1230 //--------------------------------------------------------------- 1231 #if 0 1232 static void 1233 helper1(mpc_decoder *d, mpc_uint32_t bitpos) 1234 { 1235 f_seek(d, (bitpos >> 5) * 4 + d->MPCHeaderPos); 1236 f_read_dword(d, d->Speicher, 2); 1237 d->dword = d->Speicher[d->Zaehler = 0]; 1238 d->pos = bitpos & 31; 1239 } 1240 #endif 1241 1242 static void 1243 helper2(mpc_decoder *d, mpc_uint32_t bitpos) 1244 { 1245 f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos); 1246 f_read_dword(d, d->Speicher, MEMSIZE); 1247 d->dword = d->Speicher[d->Zaehler = 0]; 1248 d->pos = bitpos & 31; 1249 } 1250 1251 #if 0 1252 static void 1253 helper3(mpc_decoder *d, mpc_uint32_t bitpos, mpc_uint32_t* buffoffs) 1254 { 1255 d->pos = bitpos & 31; 1256 bitpos >>= 5; 1257 if ((mpc_uint32_t)(bitpos - *buffoffs) >= MEMSIZE - 2) { 1258 *buffoffs = bitpos; 1259 f_seek(d, bitpos * 4L + d->MPCHeaderPos); 1260 f_read_dword(d, d->Speicher, MEMSIZE ); 1261 } 1262 d->dword = d->Speicher[d->Zaehler = bitpos - *buffoffs ]; 1263 } 1264 #endif 1265 1266 static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion) 1267 { 1268 mpc_uint32_t fpos = 0; 1269 (void) StreamVersion; 1270 switch ( d->StreamVersion ) { // setting position to the beginning of the data-bitstream 1271 case 0x04: fpos = 48; break; 1272 case 0x05: 1273 case 0x06: fpos = 64; break; 1274 case 0x07: 1275 case 0x17: fpos = 200; break; 1276 } 1277 return fpos; 1192 void mpc_decoder_set_seeking(mpc_decoder *d, mpc_streaminfo *si, mpc_bool_t fast_seeking) 1193 { 1194 d->seeking_window = FAST_SEEKING_WINDOW; 1195 if (si->fast_seek == 0 && fast_seeking == 0) 1196 d->seeking_window = SLOW_SEEKING_WINDOW; 1278 1197 } 1279 1198 … … 1291 1210 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH); 1292 1211 1293 memset(d->Y_L , 0, sizeof d->Y_L );1294 memset(d->Y_R , 0, sizeof d->Y_R );1295 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L );1296 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R );1297 memset(d->Res_L , 0, sizeof d->Res_L );1298 memset(d->Res_R , 0, sizeof d->Res_R );1299 memset(d->SCFI_L , 0, sizeof d->SCFI_L );1300 memset(d->SCFI_R , 0, sizeof d->SCFI_R );1301 memset(d->DSCF_Flag_L , 0, sizeof d->DSCF_Flag_L );1302 memset(d->DSCF_Flag_R , 0, sizeof d->DSCF_Flag_R );1303 memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );1304 memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );1305 memset(d->Q , 0, sizeof d->Q );1306 memset(d->MS_Flag , 0, sizeof d->MS_Flag );1307 1308 1212 // resetting synthesis filter to avoid "clicks" 1309 1213 mpc_decoder_reset_synthesis(d); … … 1312 1216 fwd = fwd < d->OverallFrames ? fwd : d->OverallFrames; 1313 1217 1314 // reset number of decoded frames 1315 d->DecodedFrames = 0; 1316 1317 fpos = get_initial_fpos(d, d->StreamVersion); 1318 if (fpos == 0) { 1319 return FALSE; 1320 } 1321 1322 helper2(d, fpos); 1218 if (fwd > d->DecodedFrames + d->seeking_window || fwd < d->DecodedFrames) { 1219 memset(d->SCF_Index_L, 1, sizeof d->SCF_Index_L ); 1220 memset(d->SCF_Index_R, 1, sizeof d->SCF_Index_R ); 1221 } 1222 1223 if (d->seeking_table_frames > d->DecodedFrames || fwd < d->DecodedFrames) { 1224 d->DecodedFrames = 0; 1225 if (fwd > d->seeking_window) 1226 d->DecodedFrames = (fwd - d->seeking_window) & (-1 << d->seeking_pwr); 1227 if (d->DecodedFrames > d->seeking_table_frames) 1228 d->DecodedFrames = d->seeking_table_frames; 1229 fpos = d->seeking_table[d->DecodedFrames >> d->seeking_pwr]; 1230 mpc_decoder_seek(d, fpos); 1231 } 1323 1232 1324 1233 // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping) 1325 1234 for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) { 1326 mpc_uint32_t FrameBitCnt; 1327 mpc_uint32_t RING; 1328 RING = d->Zaehler; 1329 d->FwdJumpInfo = mpc_decoder_bitstream_read(d, 20); // read jump-info 1330 d->ActDecodePos = (d->Zaehler << 5) + d->pos; 1331 FrameBitCnt = mpc_decoder_bits_read(d); // scanning the scalefactors and check for validity of frame 1332 if (d->StreamVersion >= 7) { 1333 mpc_decoder_read_bitstream_sv7(d); 1334 } 1335 else { 1235 mpc_uint32_t RING = d->Zaehler; 1236 mpc_uint32_t FwdJumpInfo; 1237 1238 // add seeking info 1239 if (d->seeking_table_frames < d->DecodedFrames && 1240 (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) { 1241 d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d); 1242 d->seeking_table_frames = d->DecodedFrames; 1243 } 1244 1245 FwdJumpInfo = mpc_decoder_bitstream_read(d, 20) + mpc_decoder_bits_read(d); // read jump-info 1246 1247 if (fwd <= d->DecodedFrames + d->seeking_window) { 1248 if (d->StreamVersion >= 7) { 1249 mpc_decoder_read_bitstream_sv7(d, TRUE); 1250 } else { 1336 1251 #ifdef MPC_SUPPORT_SV456 1337 mpc_decoder_read_bitstream_sv6(d);1252 mpc_decoder_read_bitstream_sv6(d, TRUE); 1338 1253 #else 1339 return FALSE;1254 return FALSE; 1340 1255 #endif 1341 } 1342 if (mpc_decoder_bits_read(d) - FrameBitCnt != d->FwdJumpInfo ) { 1343 // Box ("Bug in perform_jump"); 1344 return FALSE; 1345 } 1256 } 1257 } 1258 mpc_decoder_bitstream_jump(d, FwdJumpInfo - mpc_decoder_bits_read(d)); 1259 1346 1260 // update buffer 1347 if ((RING ^ d->Zaehler) & MEMSIZE2) { 1348 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2); 1349 } 1350 } 1351 1352 // LastBitsRead = BitsRead (); 1353 // LastFrame = d->DecodedFrames; 1261 mpc_decoder_update_buffer(d, RING); 1262 } 1354 1263 1355 1264 return TRUE; 1356 1265 } 1357 1358 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)1359 {1360 if ((RING ^ d->Zaehler) & MEMSIZE2 ) {1361 // update buffer1362 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);1363 }1364 }1365 1366
Note: See TracChangeset
for help on using the changeset viewer.