Changeset 24 for branches/zorg/src/mpc_decoder.c
- Timestamp:
- 09/17/06 19:35:17 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/zorg/src/mpc_decoder.c
r23 r24 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 67 #ifdef SCF_HACK 68 #define SCF_DIFF(SCF, D) (SCF == -128 ? -128 : SCF + D) 69 #else 70 #define SCF_DIFF(SCF, D) SCF + D 71 #endif 72 73 #define LOOKUP(x, e, q) mpc_decoder_make_huffman_lookup ( (q), sizeof(q), (x), (e) ) 74 #define DECODE_DSCF() HUFFMAN_DECODE_FASTEST ( d, mpc_table_HuffDSCF, LUTDSCF, 6 ) 75 #define HUFFMAN_DECODE_FASTEST(d,a,b,c) mpc_decoder_huffman_decode_fastest ( (d), (a), (b), 32-(c) ) 76 #define HUFFMAN_DECODE_FASTERER(d,a,b,c) mpc_decoder_huffman_decode_fasterer ( (d), (a), (b), 32-(c) ) 77 78 mpc_uint8_t LUT1_0 [1<< 6]; 79 mpc_uint8_t LUT1_1 [1<< 9]; // 576 Bytes 80 mpc_uint8_t LUT2_0 [1<< 7]; 81 mpc_uint8_t LUT2_1 [1<<10]; // 1152 Bytes 82 mpc_uint8_t LUT3_0 [1<< 4]; 83 mpc_uint8_t LUT3_1 [1<< 5]; // 48 Bytes 84 mpc_uint8_t LUT4_0 [1<< 4]; 85 mpc_uint8_t LUT4_1 [1<< 5]; // 48 Bytes 86 mpc_uint8_t LUT5_0 [1<< 6]; 87 mpc_uint8_t LUT5_1 [1<< 8]; // 320 Bytes 88 mpc_uint8_t LUT6_0 [1<< 7]; 89 mpc_uint8_t LUT6_1 [1<< 7]; // 256 Bytes 90 mpc_uint8_t LUT7_0 [1<< 8]; 91 mpc_uint8_t LUT7_1 [1<< 8]; // 512 Bytes 92 mpc_uint8_t LUTDSCF [1<< 6]; // 64 Bytes = 2976 Bytes 93 61 94 //------------------------------------------------------------------------------ 62 95 // types … … 76 109 //------------------------------------------------------------------------------ 77 110 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);111 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t isFastSeeking); 112 void mpc_decoder_update_buffer(mpc_decoder *d); 80 113 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample); 81 114 void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band); 115 void mpc_decoder_seek_to(mpc_decoder *d, mpc_uint32_t bitPos); 116 void mpc_decoder_seek_forward(mpc_decoder *d, mpc_uint32_t bits); 117 mpc_uint32_t mpc_decoder_jump_frame(mpc_decoder *d); 118 void mpc_decoder_fill_buffer(mpc_decoder *d); 119 void mpc_decoder_reset_state(mpc_decoder *d); 120 static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion); 121 static __inline mpc_int32_t mpc_decoder_huffman_decode_fastest(mpc_decoder *d, const HuffmanTyp* Table, const mpc_uint8_t* tab, mpc_uint16_t unused_bits); 122 static void mpc_move_next(mpc_decoder *d); 82 123 83 124 //------------------------------------------------------------------------------ … … 130 171 d->dword = 0; 131 172 d->pos = 0; 173 d->next = 0; 132 174 d->Zaehler = 0; 133 175 d->WordsRead = 0; … … 141 183 } 142 184 185 static void 186 mpc_move_next(mpc_decoder *d) 187 { 188 d->Zaehler = (d->Zaehler + 1) & MEMMASK; 189 d->dword = d->next; 190 d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]); 191 d->pos -= 32; 192 ++(d->WordsRead); 193 } 194 143 195 // read desired number of bits out of the bitstream 144 static mpc_uint32_t196 static __inline mpc_uint32_t 145 197 mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits) 146 198 { … … 153 205 } 154 206 else { 155 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 156 d->pos -= 32; 207 mpc_move_next(d); 157 208 if (d->pos) { 158 209 out <<= d->pos; 159 210 out |= d->dword >> (32 - d->pos); 160 211 } 161 ++(d->WordsRead);162 212 } 163 213 164 214 return out & mask[bits]; 215 } 216 217 static void 218 mpc_decoder_make_huffman_lookup( 219 mpc_uint8_t* lookup, size_t length, const HuffmanTyp* Table, size_t elements) 220 { 221 size_t i, idx = elements; 222 mpc_uint32_t dval = (mpc_uint32_t)0x80000000L / length * 2; 223 mpc_uint32_t val = dval - 1; 224 225 for ( i = 0; i < length; i++, val += dval ) { 226 while ( idx > 0 && val >= Table[idx-1].Code ) 227 idx--; 228 *lookup++ = (mpc_uint8_t)idx; 229 } 230 231 return; 165 232 } 166 233 … … 174 241 mpc_uint32_t code = d->dword << d->pos; 175 242 if (d->pos > 26) { 176 code |= d-> Speicher[(d->Zaehler + 1) & MEMMASK]>> (32 - d->pos);243 code |= d->next >> (32 - d->pos); 177 244 } 178 245 while (code < Table->Code) { … … 182 249 // set the new position within bitstream without performing a dummy-read 183 250 if ((d->pos += Table->Length) >= 32) { 184 d->pos -= 32; 185 d->dword = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK]; 186 ++(d->WordsRead); 251 mpc_move_next(d); 187 252 } 188 253 … … 199 264 mpc_uint32_t code = d->dword << d->pos; 200 265 if (d->pos > 18) { 201 code |= d-> Speicher[(d->Zaehler + 1) & MEMMASK]>> (32 - d->pos);266 code |= d->next >> (32 - d->pos); 202 267 } 203 268 while (code < Table->Code) { … … 207 272 // set the new position within bitstream without performing a dummy-read 208 273 if ((d->pos += Table->Length) >= 32) { 209 d->pos -= 32; 210 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 211 ++(d->WordsRead); 274 mpc_move_next(d); 212 275 } 213 276 … … 223 286 mpc_uint32_t code = d->dword << d->pos; 224 287 if (d->pos > 22) { 225 code |= d-> Speicher[(d->Zaehler + 1) & MEMMASK]>> (32 - d->pos);288 code |= d->next >> (32 - d->pos); 226 289 } 227 290 while (code < Table->Code) { … … 231 294 // set the new position within bitstream without performing a dummy-read 232 295 if ((d->pos += Table->Length) >= 32) { 233 d->pos -= 32; 234 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 235 ++(d->WordsRead); 296 mpc_move_next(d); 236 297 } 237 298 … … 247 308 mpc_uint32_t code = d->dword << d->pos; 248 309 if (d->pos > 27) { 249 code |= d-> Speicher[(d->Zaehler + 1) & MEMMASK]>> (32 - d->pos);310 code |= d->next >> (32 - d->pos); 250 311 } 251 312 while (code < Table->Code) { … … 255 316 // set the new position within bitstream without performing a dummy-read 256 317 if ((d->pos += Table->Length) >= 32) { 257 d->pos -= 32; 258 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]; 259 ++(d->WordsRead); 318 mpc_move_next(d); 319 } 320 321 return Table->Value; 322 } 323 324 /* partial lookup table decode */ 325 static mpc_int32_t 326 mpc_decoder_huffman_decode_fasterer( 327 mpc_decoder *d, 328 const HuffmanTyp* Table, const mpc_uint8_t* tab, mpc_uint16_t unused_bits) 329 { 330 // load preview and decode 331 mpc_uint32_t code = d->dword << d->pos; 332 333 if (d->pos > 18) { // preview 14 bits 334 code |= d->next >> (32 - d->pos); 335 } 336 337 Table += tab [(size_t)(code >> unused_bits) ]; 338 339 while (code < Table->Code) { 340 Table++; 341 } 342 343 // set the new position within bitstream without performing a dummy-read 344 if ((d->pos += Table->Length) >= 32) { 345 mpc_move_next(d); 346 } 347 348 return Table->Value; 349 } 350 351 /* full decode using lookup table */ 352 static __inline mpc_int32_t 353 mpc_decoder_huffman_decode_fastest( 354 mpc_decoder *d, 355 const HuffmanTyp* Table, const mpc_uint8_t* tab, mpc_uint16_t unused_bits) 356 { 357 // load preview and decode 358 mpc_uint32_t code = d->dword << d->pos; 359 360 if (d->pos > unused_bits) { 361 code |= d->next >> (32 - d->pos); 362 } 363 364 Table+=tab [(size_t)(code >> unused_bits) ]; 365 366 // set the new position within bitstream without performing a dummy-read 367 if ((d->pos += Table->Length) >= 32) { 368 mpc_move_next(d); 260 369 } 261 370 … … 289 398 290 399 d->DecodedFrames = 0; 400 d->SeekTableIndex = 0; 401 d->MaxDecodedFrames = 0; 291 402 d->StreamVersion = 0; 292 403 d->MS_used = 0; … … 314 425 mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer) 315 426 { 316 unsigned int i;317 427 mpc_decoder_reset_bitstream_decode(d); 318 428 if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher); 319 429 memcpy(d->Speicher, in_buffer, in_len); 320 #ifdef MPC_LITTLE_ENDIAN 321 for (i = 0; i < (in_len + 3) / 4; i++) 322 d->Speicher[i] = mpc_swap32(d->Speicher[i]); 323 #endif 324 d->dword = d->Speicher[0]; 430 d->dword = SWAP(d->Speicher[0]); 431 d->next = SWAP(d->Speicher[1]); 325 432 switch (d->StreamVersion) { 326 433 #ifdef MPC_SUPPORT_SV456 … … 333 440 case 0x07: 334 441 case 0x17: 335 mpc_decoder_read_bitstream_sv7(d );442 mpc_decoder_read_bitstream_sv7(d, FALSE); 336 443 break; 337 444 default: … … 353 460 return (mpc_uint32_t)(-1); // end of file -> abort decoding 354 461 } 462 463 if (d->DecodedFrames == 0 && d->Use_SeekTable) 464 d->SeekTable[0] = mpc_decoder_bits_read(d); 355 465 356 466 // read jump-info for validity check of frame … … 371 481 case 0x07: 372 482 case 0x17: 373 mpc_decoder_read_bitstream_sv7(d );483 mpc_decoder_read_bitstream_sv7(d, FALSE); 374 484 break; 375 485 default: … … 378 488 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo; 379 489 490 d->DecodedFrames++; 491 if(d->DecodedFrames>13500) 492 printf("%u\n", d->DecodedFrames); 493 494 if (d->Use_SeekTable) { 495 if (d->SeekTable_Step == 1) { 496 d->SeekTable [d->DecodedFrames] = d->FwdJumpInfo + 20; 497 } else { 498 if ((d->DecodedFrames-1) % d->SeekTable_Step == 0) { 499 d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter; 500 d->SeekTableIndex += 1; 501 d->SeekTableCounter = 0; 502 } 503 d->SeekTableCounter += d->FwdJumpInfo + 20; 504 } 505 } 506 380 507 // synthesize signal 381 508 mpc_decoder_requantisierung(d, d->Max_Band); … … 385 512 386 513 mpc_decoder_synthese_filter_float(d, buffer); 387 388 d->DecodedFrames++;389 514 390 515 // cut off first MPC_DECODER_SYNTH_DELAY zero-samples … … 406 531 } else { 407 532 mpc_decoder_bitstream_read(d, 20); 408 mpc_decoder_read_bitstream_sv7(d );533 mpc_decoder_read_bitstream_sv7(d, FALSE); 409 534 mpc_decoder_requantisierung(d, d->Max_Band); 410 535 } … … 472 597 473 598 } 474 mpc_decoder_update_buffer(d , RING);599 mpc_decoder_update_buffer(d); 475 600 476 601 if (valid_samples > 0) { … … 887 1012 /****************************************** SV 7 ******************************************/ 888 1013 void 889 mpc_decoder_read_bitstream_sv7(mpc_decoder *d )1014 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t isFastSeeking) 890 1015 { 891 1016 // these arrays hold decoding results for bundled quantizers (3- and 5-step) … … 904 1029 mpc_int8_t *ResL, *ResR; 905 1030 mpc_uint32_t tmp; 1031 mpc_uint8_t *LUT; 1032 mpc_uint8_t max_length; 906 1033 907 1034 /***************************** Header *****************************/ … … 914 1041 if (d->MS_used && !(*ResL==0 && *ResR==0)) { 915 1042 d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1); 1043 } else { 1044 d->MS_Flag[0] = 0; 916 1045 } 917 1046 … … 928 1057 if (d->MS_used && !(*ResL==0 && *ResR==0)) { 929 1058 d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1); 1059 } else { 1060 d->MS_Flag[n] = 0; 930 1061 } 931 1062 … … 957 1088 { 958 1089 case 1: 959 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);960 L[0] = (idx!=8) ? L[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);961 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);962 L[1] = (idx!=8) ? L[0] + idx : (int)mpc_decoder_bitstream_read(d, 6);1090 idx = DECODE_DSCF(); 1091 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : mpc_decoder_bitstream_read(d, 6); 1092 idx = DECODE_DSCF(); 1093 L[1] = (idx!=8) ? SCF_DIFF(L[0], idx) : mpc_decoder_bitstream_read(d, 6); 963 1094 L[2] = L[1]; 964 1095 break; 965 1096 case 3: 966 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);967 L[0] = (idx!=8) ? L[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);1097 idx = DECODE_DSCF(); 1098 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : mpc_decoder_bitstream_read(d, 6); 968 1099 L[1] = L[0]; 969 1100 L[2] = L[1]; 970 1101 break; 971 1102 case 2: 972 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);973 L[0] = (idx!=8) ? L[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);1103 idx = DECODE_DSCF(); 1104 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : mpc_decoder_bitstream_read(d, 6); 974 1105 L[1] = L[0]; 975 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);976 L[2] = (idx!=8) ? L[1] + idx : (int)mpc_decoder_bitstream_read(d, 6);1106 idx = DECODE_DSCF(); 1107 L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : mpc_decoder_bitstream_read(d, 6); 977 1108 break; 978 1109 case 0: 979 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);980 L[0] = (idx!=8) ? L[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);981 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);982 L[1] = (idx!=8) ? L[0] + idx : (int)mpc_decoder_bitstream_read(d, 6);983 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);984 L[2] = (idx!=8) ? L[1] + idx : (int)mpc_decoder_bitstream_read(d, 6);1110 idx = DECODE_DSCF(); 1111 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : mpc_decoder_bitstream_read(d, 6); 1112 idx = DECODE_DSCF(); 1113 L[1] = (idx!=8) ? SCF_DIFF(L[0], idx) : mpc_decoder_bitstream_read(d, 6); 1114 idx = DECODE_DSCF(); 1115 L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : mpc_decoder_bitstream_read(d, 6); 985 1116 break; 986 1117 default: … … 997 1128 { 998 1129 case 1: 999 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1000 R[0] = (idx!=8) ? R[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);1001 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1002 R[1] = (idx!=8) ? R[0] + idx : (int)mpc_decoder_bitstream_read(d, 6);1130 idx = DECODE_DSCF(); 1131 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : mpc_decoder_bitstream_read(d, 6); 1132 idx = DECODE_DSCF(); 1133 R[1] = (idx!=8) ? SCF_DIFF(R[0], idx) : mpc_decoder_bitstream_read(d, 6); 1003 1134 R[2] = R[1]; 1004 1135 break; 1005 1136 case 3: 1006 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1007 R[0] = (idx!=8) ? R[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);1137 idx = DECODE_DSCF(); 1138 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : mpc_decoder_bitstream_read(d, 6); 1008 1139 R[1] = R[0]; 1009 1140 R[2] = R[1]; 1010 1141 break; 1011 1142 case 2: 1012 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1013 R[0] = (idx!=8) ? R[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);1143 idx = DECODE_DSCF(); 1144 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : mpc_decoder_bitstream_read(d, 6); 1014 1145 R[1] = R[0]; 1015 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1016 R[2] = (idx!=8) ? R[1] + idx : (int)mpc_decoder_bitstream_read(d, 6);1146 idx = DECODE_DSCF(); 1147 R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : mpc_decoder_bitstream_read(d, 6); 1017 1148 break; 1018 1149 case 0: 1019 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1020 R[0] = (idx!=8) ? R[2] + idx : (int)mpc_decoder_bitstream_read(d, 6);1021 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1022 R[1] = (idx!=8) ? R[0] + idx : (int)mpc_decoder_bitstream_read(d, 6);1023 idx = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);1024 R[2] = (idx!=8) ? R[1] + idx : (int)mpc_decoder_bitstream_read(d, 6);1150 idx = DECODE_DSCF(); 1151 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : mpc_decoder_bitstream_read(d, 6); 1152 idx = DECODE_DSCF(); 1153 R[1] = (idx!=8) ? SCF_DIFF(R[0], idx) : mpc_decoder_bitstream_read(d, 6); 1154 idx = DECODE_DSCF(); 1155 R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : mpc_decoder_bitstream_read(d, 6); 1025 1156 break; 1026 1157 default: … … 1032 1163 } 1033 1164 } 1165 1166 if(isFastSeeking) 1167 return; 1168 1034 1169 /***************************** Samples ****************************/ 1035 1170 ResL = d->Res_L; … … 1056 1191 break; 1057 1192 case 1: 1058 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1]; 1193 if (mpc_decoder_bitstream_read(d, 1)) { 1194 Table = mpc_table_HuffQ[1][1]; 1195 LUT = LUT1_1; 1196 max_length = 9; 1197 } else { 1198 Table = mpc_table_HuffQ[0][1]; 1199 LUT = LUT1_0; 1200 max_length = 6; 1201 } 1059 1202 for (k=0; k<12; ++k) 1060 1203 { 1061 idx = mpc_decoder_huffman_decode_fast(d, Table);1204 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1062 1205 *LQ++ = idx30[idx]; 1063 1206 *LQ++ = idx31[idx]; … … 1066 1209 break; 1067 1210 case 2: 1068 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2]; 1211 if (mpc_decoder_bitstream_read(d, 1)) { 1212 Table = mpc_table_HuffQ[1][2]; 1213 LUT = LUT2_1; 1214 max_length = 10; 1215 } else { 1216 Table = mpc_table_HuffQ[0][2]; 1217 LUT = LUT2_0; 1218 max_length = 7; 1219 } 1069 1220 for (k=0; k<18; ++k) 1070 1221 { 1071 idx = mpc_decoder_huffman_decode_fast(d, Table);1222 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1072 1223 *LQ++ = idx50[idx]; 1073 1224 *LQ++ = idx51[idx]; … … 1075 1226 break; 1076 1227 case 3: 1228 if (mpc_decoder_bitstream_read(d, 1)) { 1229 Table = mpc_table_HuffQ[1][3]; 1230 LUT = LUT3_1; 1231 max_length = 5; 1232 } else { 1233 Table = mpc_table_HuffQ[0][3]; 1234 LUT = LUT3_0; 1235 max_length = 4; 1236 } 1237 for (k=0; k<36; ++k) 1238 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1239 break; 1077 1240 case 4: 1078 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL]; 1241 if (mpc_decoder_bitstream_read(d, 1)) { 1242 Table = mpc_table_HuffQ[1][4]; 1243 LUT = LUT4_1; 1244 max_length = 5; 1245 } else { 1246 Table = mpc_table_HuffQ[0][4]; 1247 LUT = LUT4_0; 1248 max_length = 4; 1249 } 1079 1250 for (k=0; k<36; ++k) 1080 *LQ++ = mpc_decoder_huffman_decode_faster(d, Table);1251 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1081 1252 break; 1082 1253 case 5: 1083 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL]; 1254 if (mpc_decoder_bitstream_read(d, 1)) { 1255 Table = mpc_table_HuffQ[1][5]; 1256 LUT = LUT5_1; 1257 max_length = 8; 1258 } else { 1259 Table = mpc_table_HuffQ[0][5]; 1260 LUT = LUT5_0; 1261 max_length = 6; 1262 } 1084 1263 for (k=0; k<36; ++k) 1085 *LQ++ = mpc_decoder_huffman_decode_fast(d, Table);1264 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1086 1265 break; 1087 1266 case 6: 1267 if (mpc_decoder_bitstream_read(d, 1)) { 1268 Table = mpc_table_HuffQ[1][6]; 1269 LUT = LUT6_1; 1270 max_length = 7; 1271 for (k=0; k<36; ++k) 1272 *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length ); 1273 } else { 1274 Table = mpc_table_HuffQ[0][6]; 1275 LUT = LUT6_0; 1276 max_length = 7; 1277 for (k=0; k<36; ++k) 1278 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1279 } 1280 break; 1088 1281 case 7: 1089 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL]; 1090 for (k=0; k<36; ++k) 1091 *LQ++ = mpc_decoder_huffman_decode(d, Table); 1282 if (mpc_decoder_bitstream_read(d, 1)) { 1283 Table = mpc_table_HuffQ[1][7]; 1284 LUT = LUT7_1; 1285 max_length = 8; 1286 for (k=0; k<36; ++k) 1287 *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length ); 1288 } else { 1289 Table = mpc_table_HuffQ[0][7]; 1290 LUT = LUT7_0; 1291 max_length = 8; 1292 for (k=0; k<36; ++k) 1293 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1294 } 1092 1295 break; 1093 1296 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: 1094 1297 tmp = Dc[*ResL]; 1095 1298 for (k=0; k<36; ++k) 1096 *LQ++ = (mpc_int 32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;1299 *LQ++ = (mpc_int16_t) mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp; 1097 1300 break; 1098 1301 default: … … 1116 1319 break; 1117 1320 case 1: 1118 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1]; 1321 if (mpc_decoder_bitstream_read(d, 1)) { 1322 Table = mpc_table_HuffQ[1][1]; 1323 LUT = LUT1_1; 1324 max_length = 9; 1325 } else { 1326 Table = mpc_table_HuffQ[0][1]; 1327 LUT = LUT1_0; 1328 max_length = 6; 1329 } 1119 1330 for (k=0; k<12; ++k) 1120 1331 { 1121 idx = mpc_decoder_huffman_decode_fast(d, Table);1332 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1122 1333 *RQ++ = idx30[idx]; 1123 1334 *RQ++ = idx31[idx]; … … 1126 1337 break; 1127 1338 case 2: 1128 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2]; 1339 if (mpc_decoder_bitstream_read(d, 1)) { 1340 Table = mpc_table_HuffQ[1][2]; 1341 LUT = LUT2_1; 1342 max_length = 10; 1343 } else { 1344 Table = mpc_table_HuffQ[0][2]; 1345 LUT = LUT2_0; 1346 max_length = 7; 1347 } 1129 1348 for (k=0; k<18; ++k) 1130 1349 { 1131 idx = mpc_decoder_huffman_decode_fast(d, Table);1350 idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1132 1351 *RQ++ = idx50[idx]; 1133 1352 *RQ++ = idx51[idx]; … … 1135 1354 break; 1136 1355 case 3: 1356 if (mpc_decoder_bitstream_read(d, 1)) { 1357 Table = mpc_table_HuffQ[1][3]; 1358 LUT = LUT3_1; 1359 max_length = 5; 1360 } else { 1361 Table = mpc_table_HuffQ[0][3]; 1362 LUT = LUT3_0; 1363 max_length = 4; 1364 } 1365 for (k=0; k<36; ++k) 1366 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1367 break; 1137 1368 case 4: 1138 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR]; 1369 if (mpc_decoder_bitstream_read(d, 1)) { 1370 Table = mpc_table_HuffQ[1][4]; 1371 LUT = LUT4_1; 1372 max_length = 5; 1373 } else { 1374 Table = mpc_table_HuffQ[0][4]; 1375 LUT = LUT4_0; 1376 max_length = 4; 1377 } 1139 1378 for (k=0; k<36; ++k) 1140 *RQ++ = mpc_decoder_huffman_decode_faster(d, Table);1379 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1141 1380 break; 1142 1381 case 5: 1143 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR]; 1382 if (mpc_decoder_bitstream_read(d, 1)) { 1383 Table = mpc_table_HuffQ[1][5]; 1384 LUT = LUT5_1; 1385 max_length = 8; 1386 } else { 1387 Table = mpc_table_HuffQ[0][5]; 1388 LUT = LUT5_0; 1389 max_length = 6; 1390 } 1144 1391 for (k=0; k<36; ++k) 1145 *RQ++ = mpc_decoder_huffman_decode_fast(d, Table);1392 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1146 1393 break; 1147 1394 case 6: 1395 if (mpc_decoder_bitstream_read(d, 1)) { 1396 Table = mpc_table_HuffQ[1][6]; 1397 LUT = LUT6_1; 1398 max_length = 7; 1399 for (k=0; k<36; ++k) 1400 *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length ); 1401 } else { 1402 Table = mpc_table_HuffQ[0][6]; 1403 LUT = LUT6_0; 1404 max_length = 7; 1405 for (k=0; k<36; ++k) 1406 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1407 } 1408 break; 1148 1409 case 7: 1149 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR]; 1150 for (k=0; k<36; ++k) 1151 *RQ++ = mpc_decoder_huffman_decode(d, Table); 1410 if (mpc_decoder_bitstream_read(d, 1)) { 1411 Table = mpc_table_HuffQ[1][7]; 1412 LUT = LUT7_1; 1413 max_length = 8; 1414 for (k=0; k<36; ++k) 1415 *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length ); 1416 } else { 1417 Table = mpc_table_HuffQ[0][7]; 1418 LUT = LUT7_0; 1419 max_length = 8; 1420 for (k=0; k<36; ++k) 1421 *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length ); 1422 } 1152 1423 break; 1153 1424 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: 1154 1425 tmp = Dc[*ResR]; 1155 1426 for (k=0; k<36; ++k) 1156 *RQ++ = (mpc_int 32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;1427 *RQ++ = (mpc_int16_t) mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp; 1157 1428 break; 1158 1429 default: … … 1174 1445 d->OverallFrames = 0; 1175 1446 d->DecodedFrames = 0; 1447 d->MaxDecodedFrames = 0; 1176 1448 d->TrueGaplessPresent = 0; 1177 1449 d->WordsRead = 0; … … 1184 1456 d->pos = 0; 1185 1457 d->Zaehler = 0; 1458 d->Ring = 0; 1186 1459 d->WordsRead = 0; 1187 1460 d->Max_Band = 0; 1461 d->SeekTable = NULL; 1462 d->Use_FastSeek = TRUE; 1463 d->Use_SeekTable = TRUE; 1464 d->Use_StaticSeekTable = FALSE; 1465 d->SeekTable_Step = 1; 1466 d->SeekTableIndex = 0; 1467 d->SeekTableCounter = 0; 1468 d->Max_SeekTable_Size = 0; 1188 1469 1189 1470 mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f); 1190 1471 #if 0 1191 mpc_decoder_init_huffman_sv6(d);1192 mpc_decoder_init_huffman_sv7(d);1472 mpc_decoder_init_huffman_sv6(d); 1473 mpc_decoder_init_huffman_sv7(d); 1193 1474 #endif 1475 1476 LOOKUP ( mpc_table_HuffQ[0][1], 27, LUT1_0 ); 1477 LOOKUP ( mpc_table_HuffQ[1][1], 27, LUT1_1 ); 1478 LOOKUP ( mpc_table_HuffQ[0][2], 25, LUT2_0 ); 1479 LOOKUP ( mpc_table_HuffQ[1][2], 25, LUT2_1 ); 1480 LOOKUP ( mpc_table_HuffQ[0][3], 7, LUT3_0 ); 1481 LOOKUP ( mpc_table_HuffQ[1][3], 7, LUT3_1 ); 1482 LOOKUP ( mpc_table_HuffQ[0][4], 9, LUT4_0 ); 1483 LOOKUP ( mpc_table_HuffQ[1][4], 9, LUT4_1 ); 1484 LOOKUP ( mpc_table_HuffQ[0][5], 15, LUT5_0 ); 1485 LOOKUP ( mpc_table_HuffQ[1][5], 15, LUT5_1 ); 1486 LOOKUP ( mpc_table_HuffQ[0][6], 31, LUT6_0 ); 1487 LOOKUP ( mpc_table_HuffQ[1][6], 31, LUT6_1 ); 1488 LOOKUP ( mpc_table_HuffQ[0][7], 63, LUT7_0 ); 1489 LOOKUP ( mpc_table_HuffQ[1][7], 63, LUT7_1 ); 1490 LOOKUP ( mpc_table_HuffDSCF, 16, LUTDSCF ); 1491 } 1492 1493 void mpc_decoder_destroy(mpc_decoder *d) 1494 { 1495 free(d->Speicher); 1496 if (d->SeekTable != NULL && d->Use_StaticSeekTable == FALSE) 1497 free(d->SeekTable); 1194 1498 } 1195 1499 1196 1500 void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si) 1197 1501 { 1502 mpc_uint16_t seekTableSize; 1503 1198 1504 mpc_decoder_reset_synthesis(d); 1199 1505 mpc_decoder_reset_globals(d); … … 1208 1514 1209 1515 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY; 1516 1517 if (d->SeekTable != NULL && d->Use_StaticSeekTable == FALSE) 1518 free(d->SeekTable); 1519 1520 if (d->Use_SeekTable) { 1521 if (d->Use_StaticSeekTable == FALSE) { 1522 if (d->Max_SeekTable_Size == 0) { 1523 seekTableSize = si->frames +1; 1524 } else { 1525 seekTableSize = min(si->frames +1, d->Max_SeekTable_Size / sizeof(mpc_uint32_t)); 1526 } 1527 d->SeekTable = (mpc_uint32_t*) calloc(seekTableSize, sizeof(mpc_uint32_t)); 1528 d->SeekTable_Step = (si->frames +1) / seekTableSize; 1529 if ((si->frames +1) % seekTableSize) 1530 d->SeekTable_Step+=1; 1531 } else { 1532 seekTableSize = d->Max_SeekTable_Size / sizeof(mpc_uint32_t); 1533 d->SeekTable_Step = (si->frames +1) / seekTableSize; 1534 if ((si->frames +1) % seekTableSize) 1535 d->SeekTable_Step+=1; 1536 } 1537 } 1210 1538 } 1211 1539 1212 1540 mpc_bool_t mpc_decoder_initialize(mpc_decoder *d, mpc_streaminfo *si) 1213 1541 { 1542 mpc_uint32_t bitPos; 1543 mpc_uint32_t fpos; 1544 1545 d->Speicher = malloc(MPC_DECODER_MEMSIZE * sizeof(mpc_uint32_t)); 1214 1546 mpc_decoder_set_streaminfo(d, si); 1215 1547 1216 // AB: setting position to the beginning of the data-bitstream 1217 switch (d->StreamVersion) { 1218 case 0x04: f_seek(d, 4 + d->MPCHeaderPos); d->pos = 16; break; // Geht auch über eine der Helperfunktionen 1219 case 0x05: 1220 case 0x06: f_seek(d, 8 + d->MPCHeaderPos); d->pos = 0; break; 1221 case 0x07: 1222 case 0x17: /*f_seek ( 24 + d->MPCHeaderPos );*/ d->pos = 8; break; 1223 default: return FALSE; 1224 } 1225 1226 // AB: fill buffer and initialize decoder 1548 // setting position to the beginning of the data-bitstream 1549 bitPos = get_initial_fpos(d, d->StreamVersion); 1550 fpos = bitPos >> 5; 1551 1552 // fill buffer and initialize decoder 1553 f_seek(d, fpos*4 + d->MPCHeaderPos); 1227 1554 f_read_dword(d, d->Speicher, MEMSIZE ); 1228 d->dword = d->Speicher[d->Zaehler = 0]; 1555 d->Ring = 0; 1556 d->Zaehler = 0; 1557 d->pos = bitPos & 31; 1558 d->WordsRead = fpos; 1559 d->dword = SWAP(d->Speicher[0]); 1560 d->next = SWAP(d->Speicher[1]); 1561 d->SeekTable_Step = 1; 1229 1562 1230 1563 return TRUE; … … 1241 1574 f_seek(d, (bitpos >> 5) * 4 + d->MPCHeaderPos); 1242 1575 f_read_dword(d, d->Speicher, 2); 1243 d->dword = d->Speicher[d->Zaehler = 0];1576 d->dword = SWAP(d->Speicher[d->Zaehler = 0]); 1244 1577 d->pos = bitpos & 31; 1245 1578 } 1246 #endif1247 1579 1248 1580 static void … … 1251 1583 f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos); 1252 1584 f_read_dword(d, d->Speicher, MEMSIZE); 1253 d->dword = d->Speicher[d->Zaehler = 0];1585 d->dword = SWAP(d->Speicher[d->Zaehler = 0]); 1254 1586 d->pos = bitpos & 31; 1255 1587 } 1256 1588 1257 #if 01258 1589 static void 1259 1590 helper3(mpc_decoder *d, mpc_uint32_t bitpos, mpc_uint32_t* buffoffs) … … 1266 1597 f_read_dword(d, d->Speicher, MEMSIZE ); 1267 1598 } 1268 d->dword = d->Speicher[d->Zaehler = bitpos - *buffoffs ];1599 d->dword = SWAP(d->Speicher[d->Zaehler = bitpos - *buffoffs ]); 1269 1600 } 1270 1601 #endif 1602 1603 // jumps over the current frame 1604 mpc_uint32_t mpc_decoder_jump_frame(mpc_decoder *d) { 1605 1606 mpc_uint32_t frameSize; 1607 1608 // ensure the buffer is full 1609 mpc_decoder_update_buffer(d); 1610 1611 // bits in frame 1612 frameSize = mpc_decoder_bitstream_read(d, 20); 1613 1614 // jump forward 1615 mpc_decoder_seek_forward(d, frameSize); 1616 1617 return frameSize + 20; 1618 1619 } 1271 1620 1272 1621 static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion) … … 1289 1638 } 1290 1639 1291 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample) 1292 { 1293 mpc_uint32_t fpos; 1294 mpc_uint32_t fwd; 1295 1296 fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH); 1297 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH); 1640 void mpc_decoder_reset_state(mpc_decoder *d) { 1298 1641 1299 1642 memset(d->Y_L , 0, sizeof d->Y_L ); 1300 1643 memset(d->Y_R , 0, sizeof d->Y_R ); 1644 #ifdef SCF_HACK 1645 memset(d->SCF_Index_L , -128, sizeof d->SCF_Index_L ); 1646 memset(d->SCF_Index_R , -128, sizeof d->SCF_Index_R ); 1647 #else 1301 1648 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L ); 1302 1649 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R ); 1650 #endif 1303 1651 memset(d->Res_L , 0, sizeof d->Res_L ); 1304 1652 memset(d->Res_R , 0, sizeof d->Res_R ); … … 1313 1661 memset(d->Q , 0, sizeof d->Q ); 1314 1662 memset(d->MS_Flag , 0, sizeof d->MS_Flag ); 1315 1316 // resetting synthesis filter to avoid "clicks" 1317 mpc_decoder_reset_synthesis(d); 1663 } 1664 1665 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample) 1666 { 1667 mpc_uint32_t fpos; 1668 mpc_uint32_t seekFrame = 0; // the frame to seek to 1669 mpc_uint32_t lastFrame = 0; // last frame to seek to before scanning scale factors 1670 mpc_uint32_t delta = 0; // direction of seeks 1671 1672 destsample += MPC_DECODER_SYNTH_DELAY; 1673 seekFrame = (mpc_uint32_t) ((destsample) / MPC_FRAME_LENGTH); 1674 d->samples_to_skip = (mpc_uint32_t)((destsample) % MPC_FRAME_LENGTH); 1318 1675 1319 1676 // prevent from desired position out of allowed range 1320 fwd = fwd < d->OverallFrames ? fwd : d->OverallFrames; 1321 1322 // reset number of decoded frames 1323 d->DecodedFrames = 0; 1324 1325 fpos = get_initial_fpos(d, d->StreamVersion); 1326 if (fpos == 0) { 1327 return FALSE; 1328 } 1329 1330 helper2(d, fpos); 1331 1332 // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping) 1333 for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) { 1677 seekFrame = seekFrame < d->OverallFrames ? seekFrame : d->OverallFrames; 1678 1679 // seek direction (note: avoids casting to int64) 1680 delta = (d->DecodedFrames > seekFrame ? -(mpc_int32_t)(d->DecodedFrames - seekFrame) : (mpc_int32_t)(seekFrame - d->DecodedFrames)); 1681 1682 // update max decoded frames 1683 if (d->DecodedFrames > d->MaxDecodedFrames) 1684 d->MaxDecodedFrames = d->DecodedFrames; 1685 1686 if (seekFrame > 33) 1687 lastFrame = seekFrame - 33 + 1 - d->SeekTable_Step; 1688 1689 if ((!d->Use_SeekTable && delta < 0) || d->MaxDecodedFrames == 0) 1690 { 1691 1692 mpc_decoder_reset_state(d); 1693 1694 // starts from the beginning since no frames have been decoded yet, or not using seek table 1695 fpos = get_initial_fpos(d, d->StreamVersion); 1696 1697 // seek to the first frame 1698 mpc_decoder_seek_to(d, fpos); 1699 1700 // reset number of decoded frames 1701 d->DecodedFrames = 0; 1702 1703 if (d->Use_SeekTable) 1704 { 1705 // jump to the last frame, updating seek table 1706 if (d->SeekTable_Step == 1) 1707 { 1708 d->SeekTable[0] = (mpc_uint32_t)fpos; 1709 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1710 d->SeekTable[d->DecodedFrames+1] = mpc_decoder_jump_frame(d); 1711 } 1712 else 1713 { 1714 d->SeekTableIndex = 0; 1715 d->SeekTableCounter = (mpc_uint32_t)fpos; 1716 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1717 { 1718 if (d->DecodedFrames % d->SeekTable_Step == 0) 1719 { 1720 d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter; 1721 d->SeekTableIndex += 1; 1722 d->SeekTableCounter = 0; 1723 } 1724 d->SeekTableCounter += mpc_decoder_jump_frame(d); 1725 } 1726 } 1727 } 1728 else 1729 { 1730 // just jump to the last frame 1731 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1732 mpc_decoder_jump_frame(d); 1733 } 1734 } 1735 else if (delta < 0) 1736 { 1737 1738 mpc_decoder_reset_state(d); 1739 1740 // jumps backwards using the seek table 1741 fpos = d->SeekTable[0]; 1742 if (d->SeekTable_Step == 1) 1743 { 1744 for (d->DecodedFrames = 0;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1745 fpos += d->SeekTable[d->DecodedFrames+1]; 1746 } 1747 else 1748 { 1749 d->SeekTableIndex = 0; 1750 //d->SeekTableCounter = 0; 1751 for (d->DecodedFrames = 0;d->DecodedFrames < lastFrame; d->DecodedFrames+=d->SeekTable_Step, d->SeekTableIndex++) 1752 fpos += d->SeekTable[d->SeekTableIndex+1]; 1753 d->SeekTableCounter = d->SeekTable[d->SeekTableIndex]; 1754 } 1755 mpc_decoder_seek_to(d, fpos); 1756 1757 } 1758 else if (delta > 33) 1759 { 1760 1761 mpc_decoder_reset_state(d); 1762 1763 // jumps forward from the current position 1764 if (d->Use_SeekTable) 1765 { 1766 if (d->MaxDecodedFrames > lastFrame) // REVIEW: Correct?? or (d->MaxDecodedFrames > d->DecodedFrames) 1767 { 1768 // jump to the last usable position in the seek table 1769 if (d->SeekTable_Step == 1) 1770 { 1771 fpos = mpc_decoder_bits_read(d); 1772 for (; d->DecodedFrames < d->MaxDecodedFrames && d->DecodedFrames < lastFrame; d->DecodedFrames++) 1773 fpos += d->SeekTable[d->DecodedFrames+1]; 1774 } 1775 else 1776 { 1777 // could test SeekTable offset and jump to next entry but this is easier for now... 1778 //d->SeekTableIndex = 0; 1779 //d->SeekTableCounter = 0; 1780 fpos = d->SeekTable[0]; 1781 d->SeekTableIndex = 0; 1782 for (d->DecodedFrames = 0;d->DecodedFrames < d->MaxDecodedFrames && d->DecodedFrames < lastFrame; d->DecodedFrames+=d->SeekTable_Step, d->SeekTableIndex++) 1783 fpos += d->SeekTable[d->SeekTableIndex+1]; 1784 d->SeekTableCounter = d->SeekTable[d->SeekTableIndex]; 1785 } 1786 mpc_decoder_seek_to(d, fpos); 1787 } 1788 1789 if (d->SeekTable_Step == 1) 1790 { 1791 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1792 d->SeekTable[d->DecodedFrames+1] = mpc_decoder_jump_frame(d); 1793 } 1794 else 1795 { 1796 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1797 { 1798 if (d->DecodedFrames % d->SeekTable_Step == 0) 1799 { 1800 d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter; 1801 d->SeekTableIndex += 1; 1802 d->SeekTableCounter = 0; 1803 } 1804 d->SeekTableCounter += mpc_decoder_jump_frame(d); 1805 } 1806 } 1807 } 1808 else 1809 { 1810 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) 1811 mpc_decoder_jump_frame(d); 1812 } 1813 } 1814 1815 // REVIEW: Needed? 1816 mpc_decoder_update_buffer(d); 1817 1818 for (;d->DecodedFrames < seekFrame; d->DecodedFrames++) { 1334 1819 mpc_uint32_t FrameBitCnt; 1335 mpc_uint32_t RING;1336 RING = d->Zaehler;1337 1820 d->FwdJumpInfo = mpc_decoder_bitstream_read(d, 20); // read jump-info 1338 1821 d->ActDecodePos = (d->Zaehler << 5) + d->pos; 1339 FrameBitCnt = mpc_decoder_bits_read(d); // scanning the scalefactors and check for validity of frame 1822 FrameBitCnt = mpc_decoder_bits_read(d); 1823 // scanning the scalefactors (and check for validity of frame) 1340 1824 if (d->StreamVersion >= 7) { 1341 mpc_decoder_read_bitstream_sv7(d );1825 mpc_decoder_read_bitstream_sv7(d, d->Use_FastSeek && (d->DecodedFrames < seekFrame - 1)); 1342 1826 } 1343 1827 else { … … 1348 1832 #endif 1349 1833 } 1350 if (mpc_decoder_bits_read(d) - FrameBitCnt != d->FwdJumpInfo ) { 1351 // Box ("Bug in perform_jump"); 1834 FrameBitCnt = mpc_decoder_bits_read(d) - FrameBitCnt; 1835 1836 if (d->Use_FastSeek && d->FwdJumpInfo > FrameBitCnt) 1837 mpc_decoder_seek_forward(d, d->FwdJumpInfo - FrameBitCnt); 1838 else if (FrameBitCnt != d->FwdJumpInfo ) 1839 // Bug in perform_jump; 1352 1840 return FALSE; 1353 } 1841 1842 // REVIEW: Only if decodedFrames < maxDecodedFrames?? 1843 if (d->Use_SeekTable) 1844 { 1845 if (d->SeekTable_Step == 1) 1846 { 1847 // check that the frame length corresponds with any data already in the seek table 1848 if (d->SeekTable[d->DecodedFrames+1] != 0 && d->SeekTable[d->DecodedFrames+1] != d->FwdJumpInfo + 20) 1849 return FALSE; 1850 d->SeekTable [d->DecodedFrames+1] = d->FwdJumpInfo + 20; 1851 } 1852 else 1853 { 1854 if (d->DecodedFrames % d->SeekTable_Step == 0) 1855 { 1856 if (d->SeekTable[d->SeekTableIndex] != 0 && d->SeekTable[d->SeekTableIndex] != d->SeekTableCounter) 1857 return FALSE; 1858 d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter; 1859 d->SeekTableIndex += 1; 1860 d->SeekTableCounter = 0; 1861 } 1862 d->SeekTableCounter += d->FwdJumpInfo + 20; 1863 } 1864 } 1865 1354 1866 // update buffer 1355 if ((RING ^ d->Zaehler) & MEMSIZE2) { 1356 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2); 1357 } 1358 } 1359 1360 // LastBitsRead = BitsRead (); 1361 // LastFrame = d->DecodedFrames; 1867 mpc_decoder_update_buffer(d); 1868 1869 if (d->DecodedFrames == seekFrame - 1) 1870 { 1871 // initialize the synth correctly for perfect decoding 1872 mpc_decoder_requantisierung(d, d->Max_Band); 1873 mpc_decoder_synthese_filter_float(d, NULL); 1874 } 1875 } 1362 1876 1363 1877 return TRUE; 1364 1878 } 1365 1879 1366 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING) 1367 { 1368 if ((RING ^ d->Zaehler) & MEMSIZE2 ) { 1880 void mpc_decoder_fill_buffer(mpc_decoder *d) 1881 { 1882 f_read_dword(d, d->Speicher, MEMSIZE); 1883 d->dword = SWAP(d->Speicher[d->Zaehler = 0]); 1884 d->next = SWAP(d->Speicher[1]); 1885 d->Ring = 0; 1886 } 1887 1888 void mpc_decoder_update_buffer(mpc_decoder *d) 1889 { 1890 if ((d->Ring ^ d->Zaehler) & MEMSIZE2) { 1369 1891 // update buffer 1370 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2); 1371 } 1372 } 1373 1374 1892 f_read_dword(d, d->Speicher + (d->Ring & MEMSIZE2), MEMSIZE2); 1893 d->Ring = d->Zaehler; 1894 } 1895 } 1896 1897 1898 void mpc_decoder_seek_to(mpc_decoder *d, mpc_uint32_t bitPos) 1899 { 1900 // required dword 1901 mpc_uint32_t fpos = (bitPos >> 5); 1902 mpc_uint32_t bufferStart = d->WordsRead - d->Zaehler; 1903 if ((d->Zaehler & MEMSIZE2) != FALSE) 1904 bufferStart += MEMSIZE2; 1905 1906 if (fpos >= bufferStart && fpos < bufferStart + MEMSIZE) 1907 { 1908 // required position is within the buffer, no need to seek 1909 d->Zaehler = (fpos - bufferStart + ((d->Zaehler & MEMSIZE2) != FALSE ? MEMSIZE2 : 0)) & MEMMASK; 1910 d->pos = bitPos & 31; 1911 d->WordsRead = fpos; 1912 d->dword = SWAP(d->Speicher[d->Zaehler]); 1913 d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]); 1914 1915 mpc_decoder_update_buffer(d); 1916 } 1917 else 1918 { 1919 // DWORD aligned 1920 f_seek(d, fpos*4 + d->MPCHeaderPos); 1921 d->Zaehler = 0; 1922 d->pos = bitPos & 31; 1923 d->WordsRead = fpos; 1924 1925 mpc_decoder_fill_buffer(d); 1926 } 1927 } 1928 1929 void mpc_decoder_seek_forward(mpc_decoder *d, mpc_uint32_t bits) 1930 { 1931 bits += d->pos; 1932 d->pos = bits & 31; 1933 bits = bits >> 5; // to DWORDs 1934 d->Zaehler = (d->Zaehler + bits) & MEMMASK; 1935 d->dword = SWAP(d->Speicher[d->Zaehler]); 1936 d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]); 1937 d->WordsRead += bits; 1938 } 1939 1940 void mpc_decoder_set_seek_table(mpc_decoder *d, mpc_uint32_t *seek_table, mpc_uint32_t max_table_size) 1941 { 1942 d->Use_StaticSeekTable = TRUE; 1943 d->SeekTable = seek_table; 1944 d->Max_SeekTable_Size = max_table_size; 1945 }
Note: See TracChangeset
for help on using the changeset viewer.