00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00037
00038 #include "musepack/musepack.h"
00039 #include "musepack/internal.h"
00040 #include "musepack/requant.h"
00041 #include "musepack/huffman.h"
00042
00043
00044
00045
00046 enum
00047 {
00048 EQ_TAP = 13,
00049 DELAY = ((EQ_TAP + 1) / 2),
00050 FIR_BANDS = 4,
00051 MEMSIZE = MPC_DECODER_MEMSIZE,
00052 MEMSIZE2 = (MEMSIZE/2),
00053 MEMMASK = (MEMSIZE-1)
00054 };
00055
00056
00057
00058
00059 void mpc_decoder_init_huffman_sv6(mpc_decoder *d);
00060 void mpc_decoder_init_huffman_sv7(mpc_decoder *d);
00061 void mpc_decoder_read_bitstream_sv6(mpc_decoder *d);
00062 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d);
00063 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING);
00064 BOOL mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample);
00065 void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
00066
00067
00068
00069
00070 static mpc_int32_t f_read(mpc_decoder *d, void *ptr, size_t size)
00071 {
00072 return d->r->read(d->r->data, ptr, size);
00073 };
00074
00075 static BOOL f_seek(mpc_decoder *d, mpc_int32_t offset)
00076 {
00077 return d->r->seek(d->r->data, offset);
00078 };
00079
00080 static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count)
00081 {
00082 count = f_read(d, ptr, count << 2) >> 2;
00083 #ifndef MPC_LITTLE_ENDIAN
00084 mpc_uint32_t n;
00085 for(n = 0; n< count; n++) {
00086 ptr[n] = swap32(ptr[n]);
00087 }
00088 #endif
00089 return count;
00090 }
00091
00092
00093
00094
00095 static const mpc_uint32_t mask [33] = {
00096 0x00000000, 0x00000001, 0x00000003, 0x00000007,
00097 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
00098 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
00099 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
00100 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
00101 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
00102 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
00103 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
00104 0xFFFFFFFF
00105 };
00106
00107
00108
00109
00110 static void
00111 mpc_decoder_reset_bitstream_decode(mpc_decoder *d)
00112 {
00113 d->dword = 0;
00114 d->pos = 0;
00115 d->Zaehler = 0;
00116 d->WordsRead = 0;
00117 }
00118
00119
00120 static mpc_uint32_t
00121 mpc_decoder_bits_read(mpc_decoder *d)
00122 {
00123 return 32 * d->WordsRead + d->pos;
00124 }
00125
00126
00127 static mpc_uint32_t
00128 mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits)
00129 {
00130 mpc_uint32_t out = d->dword;
00131
00132 d->pos += bits;
00133
00134 if (d->pos < 32) {
00135 out >>= (32 - d->pos);
00136 }
00137 else {
00138 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00139 d->pos -= 32;
00140 if (d->pos) {
00141 out <<= d->pos;
00142 out |= d->dword >> (32 - d->pos);
00143 }
00144 ++(d->WordsRead);
00145 }
00146
00147 return out & mask[bits];
00148 }
00149
00150
00151 static void
00152 mpc_decoder_scfi_bundle_read(
00153 mpc_decoder *d,
00154 HuffmanTyp* Table, mpc_int32_t* SCFI, mpc_int32_t* DSCF)
00155 {
00156
00157 mpc_uint32_t code = d->dword << d->pos;
00158 if (d->pos > 26) {
00159 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00160 }
00161 while (code < Table->Code) {
00162 Table++;
00163 }
00164
00165
00166 if ((d->pos += Table->Length) >= 32) {
00167 d->pos -= 32;
00168 d->dword = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK];
00169 ++(d->WordsRead);
00170 }
00171
00172 *SCFI = Table->Value >> 1;
00173 *DSCF = Table->Value & 1;
00174 }
00175
00176 static int
00177 mpc_decoder_huffman_typ_cmpfn(const void* p1, const void* p2)
00178 {
00179 if (((HuffmanTyp*) p1)->Code < ((HuffmanTyp*) p2)->Code ) return +1;
00180 if (((HuffmanTyp*) p1)->Code > ((HuffmanTyp*) p2)->Code ) return -1;
00181 return 0;
00182 }
00183
00184
00185
00186 void
00187 mpc_decoder_resort_huff_tables(
00188 const mpc_uint32_t elements, HuffmanTyp* Table, const mpc_int32_t offset )
00189 {
00190 mpc_uint32_t i;
00191
00192 for ( i = 0; i < elements; i++ ) {
00193 Table[i].Code <<= 32 - Table[i].Length;
00194 Table[i].Value = i - offset;
00195 }
00196 qsort(Table, elements, sizeof(*Table), mpc_decoder_huffman_typ_cmpfn);
00197 }
00198
00199
00200
00201 static mpc_int32_t
00202 mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table)
00203 {
00204
00205 mpc_uint32_t code = d->dword << d->pos;
00206 if (d->pos > 18) {
00207 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00208 }
00209 while (code < Table->Code) {
00210 Table++;
00211 }
00212
00213
00214 if ((d->pos += Table->Length) >= 32) {
00215 d->pos -= 32;
00216 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00217 ++(d->WordsRead);
00218 }
00219
00220 return Table->Value;
00221 }
00222
00223
00224
00225 static mpc_int32_t
00226 mpc_decoder_huffman_decode_fast(mpc_decoder *d, const HuffmanTyp* Table)
00227 {
00228
00229 mpc_uint32_t code = d->dword << d->pos;
00230 if (d->pos > 22) {
00231 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00232 }
00233 while (code < Table->Code) {
00234 Table++;
00235 }
00236
00237
00238 if ((d->pos += Table->Length) >= 32) {
00239 d->pos -= 32;
00240 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00241 ++(d->WordsRead);
00242 }
00243
00244 return Table->Value;
00245 }
00246
00247
00248
00249 static mpc_int32_t
00250 mpc_decoder_huffman_decode_faster(mpc_decoder *d, const HuffmanTyp* Table)
00251 {
00252
00253 mpc_uint32_t code = d->dword << d->pos;
00254 if (d->pos > 27) {
00255 code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00256 }
00257 while (code < Table->Code) {
00258 Table++;
00259 }
00260
00261
00262 if ((d->pos += Table->Length) >= 32) {
00263 d->pos -= 32;
00264 d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00265 ++(d->WordsRead);
00266 }
00267
00268 return Table->Value;
00269 }
00270
00271 static void
00272 mpc_decoder_reset_v(mpc_decoder *d)
00273 {
00274 memset(d->V_L, 0, sizeof d->V_L);
00275 memset(d->V_R, 0, sizeof d->V_R);
00276 }
00277
00278 static void
00279 mpc_decoder_reset_synthesis(mpc_decoder *d)
00280 {
00281 mpc_decoder_reset_v(d);
00282 }
00283
00284 static void
00285 mpc_decoder_reset_y(mpc_decoder *d)
00286 {
00287 memset(d->Y_L, 0, sizeof d->Y_L);
00288 memset(d->Y_R, 0, sizeof d->Y_R);
00289 }
00290
00291 static void
00292 mpc_decoder_reset_globals(mpc_decoder *d)
00293 {
00294 mpc_decoder_reset_bitstream_decode(d);
00295
00296 d->DecodedFrames = 0;
00297 d->StreamVersion = 0;
00298 d->MS_used = 0;
00299
00300 memset(d->Y_L , 0, sizeof d->Y_L );
00301 memset(d->Y_R , 0, sizeof d->Y_R );
00302 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L );
00303 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R );
00304 memset(d->Res_L , 0, sizeof d->Res_L );
00305 memset(d->Res_R , 0, sizeof d->Res_R );
00306 memset(d->SCFI_L , 0, sizeof d->SCFI_L );
00307 memset(d->SCFI_R , 0, sizeof d->SCFI_R );
00308 memset(d->DSCF_Flag_L , 0, sizeof d->DSCF_Flag_L );
00309 memset(d->DSCF_Flag_R , 0, sizeof d->DSCF_Flag_R );
00310 memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );
00311 memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );
00312 memset(d->Q , 0, sizeof d->Q );
00313 memset(d->MS_Flag , 0, sizeof d->MS_Flag );
00314 }
00315
00316 static mpc_uint32_t
00317 mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer)
00318 {
00319 mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH;
00320
00321 mpc_uint32_t FrameBitCnt = 0;
00322
00323 if (d->DecodedFrames >= d->OverallFrames) {
00324 return (mpc_uint32_t)(-1);
00325 }
00326
00327
00328 d->FwdJumpInfo = mpc_decoder_bitstream_read(d, 20);
00329
00330 d->ActDecodePos = (d->Zaehler << 5) + d->pos;
00331
00332
00333 FrameBitCnt = mpc_decoder_bits_read(d);
00334 switch (d->StreamVersion) {
00335 case 0x04:
00336 case 0x05:
00337 case 0x06:
00338 mpc_decoder_read_bitstream_sv6(d);
00339 break;
00340 case 0x07:
00341 case 0x17:
00342 mpc_decoder_read_bitstream_sv7(d);
00343 break;
00344 default:
00345 return (mpc_uint32_t)(-1);
00346 }
00347 d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;
00348
00349
00350 mpc_decoder_requantisierung(d, d->Max_Band);
00351
00352
00353
00354
00355 mpc_decoder_synthese_filter_float(d, buffer);
00356
00357 d->DecodedFrames++;
00358
00359
00360 if (d->DecodedFrames == d->OverallFrames && d->StreamVersion >= 6) {
00361
00362 mpc_int32_t mod_block = mpc_decoder_bitstream_read(d, 11);
00363 mpc_int32_t FilterDecay;
00364
00365 if (mod_block == 0) {
00366
00367 mod_block = 1152;
00368 }
00369 FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH;
00370
00371
00372 if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
00373
00374
00375
00376
00377
00378
00379
00380 if (!d->TrueGaplessPresent) {
00381 mpc_decoder_reset_y(d);
00382 }
00383 else {
00384
00385 mpc_decoder_bitstream_read(d, 20);
00386 mpc_decoder_read_bitstream_sv7(d);
00387 mpc_decoder_requantisierung(d, d->Max_Band);
00388
00389
00390
00391
00392
00393 }
00394
00395 mpc_decoder_synthese_filter_float(d, buffer + 2304);
00396
00397 output_frame_length = MPC_FRAME_LENGTH + FilterDecay;
00398 }
00399 else {
00400 output_frame_length = FilterDecay;
00401 }
00402 }
00403
00404 if (d->samples_to_skip) {
00405 if (output_frame_length < d->samples_to_skip) {
00406 d->samples_to_skip -= output_frame_length;
00407 output_frame_length = 0;
00408 }
00409 else {
00410 output_frame_length -= d->samples_to_skip;
00411 memmove(
00412 buffer,
00413 buffer + d->samples_to_skip * 2,
00414 output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT));
00415 d->samples_to_skip = 0;
00416 }
00417 }
00418
00419 return output_frame_length;
00420 }
00421
00422 mpc_uint32_t mpc_decoder_decode(
00423 mpc_decoder *d,
00424 MPC_SAMPLE_FORMAT *buffer,
00425 mpc_uint32_t *vbr_update_acc,
00426 mpc_uint32_t *vbr_update_bits)
00427 {
00428 for(;;)
00429 {
00430
00431
00432 mpc_uint32_t RING = d->Zaehler;
00433 mpc_int32_t vbr_ring = (RING << 5) + d->pos;
00434
00435 mpc_uint32_t valid_samples = mpc_decoder_decode_internal(d, buffer);
00436
00437 if (valid_samples == (mpc_uint32_t)(-1) ) {
00438 return 0;
00439 }
00440
00441
00442 if (d->FrameWasValid == 0 ) {
00443
00444 return (mpc_uint32_t)(-1);
00445 }
00446 else {
00447 if (vbr_update_acc && vbr_update_bits) {
00448 (*vbr_update_acc) ++;
00449 vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
00450 if (vbr_ring < 0) {
00451 vbr_ring += 524288;
00452 }
00453 (*vbr_update_bits) += vbr_ring;
00454 }
00455
00456 }
00457 mpc_decoder_update_buffer(d, RING);
00458
00459 if (valid_samples > 0) {
00460 return valid_samples;
00461 }
00462 }
00463 }
00464
00465 void
00466 mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band)
00467 {
00468 mpc_int32_t Band;
00469 mpc_int32_t n;
00470 MPC_SAMPLE_FORMAT facL;
00471 MPC_SAMPLE_FORMAT facR;
00472 MPC_SAMPLE_FORMAT templ;
00473 MPC_SAMPLE_FORMAT tempr;
00474 MPC_SAMPLE_FORMAT* YL;
00475 MPC_SAMPLE_FORMAT* YR;
00476 mpc_int32_t* L;
00477 mpc_int32_t* R;
00478
00479 #ifdef MPC_FIXED_POINT
00480 #if MPC_FIXED_POINT_FRACTPART == 14
00481 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
00482 MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
00483 #else
00484
00485 #error FIXME, Cc table is in 18.14 format
00486
00487 #endif
00488 #else
00489 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
00490 MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
00491 #endif
00492
00493 for ( Band = 0; Band <= Last_Band; Band++ ) {
00494 YL = d->Y_L[0] + Band;
00495 YR = d->Y_R[0] + Band;
00496 L = d->Q[Band].L;
00497 R = d->Q[Band].R;
00498
00499 if ( d->MS_Flag [Band] ) {
00500 if ( d->Res_L [Band] ) {
00501 if ( d->Res_R [Band] ) {
00502 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00503 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00504 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00505 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
00506 *YR = templ - tempr;
00507 }
00508 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00509 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00510 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00511 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
00512 *YR = templ - tempr;
00513 }
00514 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00515 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00516 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00517 *YL = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
00518 *YR = templ - tempr;
00519 }
00520 } else {
00521 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00522 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00523 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00524 }
00525 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00526 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00527 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00528 }
00529 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00530 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00531 *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00532 }
00533 }
00534 } else {
00535 if (d->Res_R[Band])
00536 {
00537 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00538 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00539 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
00540 }
00541 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00542 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00543 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
00544 }
00545 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00546 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00547 *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
00548 }
00549 } else {
00550 for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
00551 *YR = *YL = 0;
00552 }
00553 }
00554 }
00555 }
00556
00557 else {
00558 if ( d->Res_L [Band] ) {
00559 if ( d->Res_R [Band] ) {
00560 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00561 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00562 for (n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00563 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00564 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00565 }
00566 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00567 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00568 for (; n < 24; n++, YL += 32, YR += 32 ) {
00569 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00570 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00571 }
00572 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00573 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00574 for (; n < 36; n++, YL += 32, YR += 32 ) {
00575 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00576 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00577 }
00578 } else {
00579 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00580 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00581 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00582 *YR = 0;
00583 }
00584 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00585 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00586 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00587 *YR = 0;
00588 }
00589 facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00590 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00591 *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00592 *YR = 0;
00593 }
00594 }
00595 }
00596 else {
00597 if ( d->Res_R [Band] ) {
00598 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00599 for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00600 *YL = 0;
00601 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00602 }
00603 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00604 for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00605 *YL = 0;
00606 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00607 }
00608 facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00609 for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00610 *YL = 0;
00611 *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00612 }
00613 } else {
00614 for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
00615 *YR = *YL = 0;
00616 }
00617 }
00618 }
00619 }
00620 }
00621 }
00622
00623
00624 void
00625 mpc_decoder_read_bitstream_sv6(mpc_decoder *d)
00626 {
00627 mpc_int32_t n,k;
00628 mpc_int32_t Max_used_Band=0;
00629 HuffmanTyp *Table;
00630 const HuffmanTyp *x1;
00631 const HuffmanTyp *x2;
00632 mpc_int32_t *L;
00633 mpc_int32_t *R;
00634 mpc_int32_t *ResL = d->Res_L;
00635 mpc_int32_t *ResR = d->Res_R;
00636
00637
00638 ResL = d->Res_L;
00639 ResR = d->Res_R;
00640 for (n=0; n <= d->Max_Band; ++n, ++ResL, ++ResR)
00641 {
00642 if (n<11) Table = d->Region_A;
00643 else if (n>=11 && n<=22) Table = d->Region_B;
00644 else Table = d->Region_C;
00645
00646 *ResL = d->Q_res[n][mpc_decoder_huffman_decode(d, Table)];
00647 if (d->MS_used) {
00648 d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1);
00649 }
00650 *ResR = d->Q_res[n][mpc_decoder_huffman_decode(d, Table)];
00651
00652
00653 if (*ResL || *ResR) Max_used_Band = n;
00654 }
00655
00656
00657 ResL = d->Res_L;
00658 ResR = d->Res_R;
00659 for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR) {
00660 if (*ResL) mpc_decoder_scfi_bundle_read(d, d->SCFI_Bundle, &(d->SCFI_L[n]), &(d->DSCF_Flag_L[n]));
00661 if (*ResR) mpc_decoder_scfi_bundle_read(d, d->SCFI_Bundle, &(d->SCFI_R[n]), &(d->DSCF_Flag_R[n]));
00662 }
00663
00664
00665 ResL = d->Res_L;
00666 ResR = d->Res_R;
00667 L = d->SCF_Index_L[0];
00668 R = d->SCF_Index_R[0];
00669 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3)
00670 {
00671 if (*ResL)
00672 {
00673
00674 if (d->DSCF_Flag_L[n]==1)
00675 {
00676 L[2] = d->DSCF_Reference_L[n];
00677 switch (d->SCFI_L[n])
00678 {
00679 case 3:
00680 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00681 L[1] = L[0];
00682 L[2] = L[1];
00683 break;
00684 case 1:
00685 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00686 L[1] = L[0] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00687 L[2] = L[1];
00688 break;
00689 case 2:
00690 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00691 L[1] = L[0];
00692 L[2] = L[1] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00693 break;
00694 case 0:
00695 L[0] = L[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00696 L[1] = L[0] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00697 L[2] = L[1] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00698 break;
00699 default:
00700 return;
00701 break;
00702 }
00703 }
00704
00705 else
00706 {
00707 switch (d->SCFI_L[n])
00708 {
00709 case 3:
00710 L[0] = mpc_decoder_bitstream_read(d, 6);
00711 L[1] = L[0];
00712 L[2] = L[1];
00713 break;
00714 case 1:
00715 L[0] = mpc_decoder_bitstream_read(d, 6);
00716 L[1] = mpc_decoder_bitstream_read(d, 6);
00717 L[2] = L[1];
00718 break;
00719 case 2:
00720 L[0] = mpc_decoder_bitstream_read(d, 6);
00721 L[1] = L[0];
00722 L[2] = mpc_decoder_bitstream_read(d, 6);
00723 break;
00724 case 0:
00725 L[0] = mpc_decoder_bitstream_read(d, 6);
00726 L[1] = mpc_decoder_bitstream_read(d, 6);
00727 L[2] = mpc_decoder_bitstream_read(d, 6);
00728 break;
00729 default:
00730 return;
00731 break;
00732 }
00733 }
00734
00735 d->DSCF_Reference_L[n] = L[2];
00736 }
00737 if (*ResR)
00738 {
00739 R[2] = d->DSCF_Reference_R[n];
00740
00741 if (d->DSCF_Flag_R[n]==1)
00742 {
00743 switch (d->SCFI_R[n])
00744 {
00745 case 3:
00746 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00747 R[1] = R[0];
00748 R[2] = R[1];
00749 break;
00750 case 1:
00751 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00752 R[1] = R[0] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00753 R[2] = R[1];
00754 break;
00755 case 2:
00756 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00757 R[1] = R[0];
00758 R[2] = R[1] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00759 break;
00760 case 0:
00761 R[0] = R[2] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00762 R[1] = R[0] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00763 R[2] = R[1] + mpc_decoder_huffman_decode_fast(d, d->DSCF_Entropie);
00764 break;
00765 default:
00766 return;
00767 break;
00768 }
00769 }
00770
00771 else
00772 {
00773 switch (d->SCFI_R[n])
00774 {
00775 case 3:
00776 R[0] = mpc_decoder_bitstream_read(d, 6);
00777 R[1] = R[0];
00778 R[2] = R[1];
00779 break;
00780 case 1:
00781 R[0] = mpc_decoder_bitstream_read(d, 6);
00782 R[1] = mpc_decoder_bitstream_read(d, 6);
00783 R[2] = R[1];
00784 break;
00785 case 2:
00786 R[0] = mpc_decoder_bitstream_read(d, 6);
00787 R[1] = R[0];
00788 R[2] = mpc_decoder_bitstream_read(d, 6);
00789 break;
00790 case 0:
00791 R[0] = mpc_decoder_bitstream_read(d, 6);
00792 R[1] = mpc_decoder_bitstream_read(d, 6);
00793 R[2] = mpc_decoder_bitstream_read(d, 6);
00794 break;
00795 default:
00796 return;
00797 break;
00798 }
00799 }
00800
00801 d->DSCF_Reference_R[n] = R[2];
00802 }
00803 }
00804
00805
00806 ResL = d->Res_L;
00807 ResR = d->Res_R;
00808 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR)
00809 {
00810
00811 x1 = d->SampleHuff[*ResL];
00812 x2 = d->SampleHuff[*ResR];
00813 L = d->Q[n].L;
00814 R = d->Q[n].R;
00815
00816 if (x1!=NULL || x2!=NULL)
00817 for (k=0; k<36; ++k)
00818 {
00819 if (x1 != NULL) *L++ = mpc_decoder_huffman_decode_fast(d, x1);
00820 if (x2 != NULL) *R++ = mpc_decoder_huffman_decode_fast(d, x2);
00821 }
00822
00823 if (*ResL>7 || *ResR>7)
00824 for (k=0; k<36; ++k)
00825 {
00826 if (*ResL>7) *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - Dc[*ResL];
00827 if (*ResR>7) *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - Dc[*ResR];
00828 }
00829 }
00830 }
00831
00832
00833 void
00834 mpc_decoder_read_bitstream_sv7(mpc_decoder *d)
00835 {
00836
00837 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};
00838 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};
00839 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};
00840 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};
00841 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};
00842
00843 mpc_int32_t n,k;
00844 mpc_int32_t Max_used_Band=0;
00845 const HuffmanTyp *Table;
00846 mpc_int32_t idx;
00847 mpc_int32_t *L ,*R;
00848 mpc_int32_t *ResL,*ResR;
00849 mpc_uint32_t tmp;
00850
00851
00852 ResL = d->Res_L;
00853 ResR = d->Res_R;
00854
00855
00856 *ResL = mpc_decoder_bitstream_read(d, 4);
00857 *ResR = mpc_decoder_bitstream_read(d, 4);
00858 if (d->MS_used && !(*ResL==0 && *ResR==0)) {
00859 d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1);
00860 }
00861
00862
00863 ++ResL; ++ResR;
00864 for (n=1; n <= d->Max_Band; ++n, ++ResL, ++ResR)
00865 {
00866 idx = mpc_decoder_huffman_decode_fast(d, d->HuffHdr);
00867 *ResL = (idx!=4) ? *(ResL-1) + idx : mpc_decoder_bitstream_read(d, 4);
00868
00869 idx = mpc_decoder_huffman_decode_fast(d, d->HuffHdr);
00870 *ResR = (idx!=4) ? *(ResR-1) + idx : mpc_decoder_bitstream_read(d, 4);
00871
00872 if (d->MS_used && !(*ResL==0 && *ResR==0)) {
00873 d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1);
00874 }
00875
00876
00877 if (*ResL!=0 || *ResR!=0) {
00878 Max_used_Band = n;
00879 }
00880 }
00881
00882 L = d->SCFI_L;
00883 R = d->SCFI_R;
00884 ResL = d->Res_L;
00885 ResR = d->Res_R;
00886 for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) {
00887 if (*ResL) *L = mpc_decoder_huffman_decode_faster(d, d->HuffSCFI);
00888 if (*ResR) *R = mpc_decoder_huffman_decode_faster(d, d->HuffSCFI);
00889 }
00890
00891
00892 ResL = d->Res_L;
00893 ResR = d->Res_R;
00894 L = d->SCF_Index_L[0];
00895 R = d->SCF_Index_R[0];
00896 for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3) {
00897 if (*ResL)
00898 {
00899 L[2] = d->DSCF_Reference_L[n];
00900 switch (d->SCFI_L[n])
00901 {
00902 case 1:
00903 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00904 L[0] = (idx!=8) ? L[2] + idx : mpc_decoder_bitstream_read(d, 6);
00905 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00906 L[1] = (idx!=8) ? L[0] + idx : mpc_decoder_bitstream_read(d, 6);
00907 L[2] = L[1];
00908 break;
00909 case 3:
00910 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00911 L[0] = (idx!=8) ? L[2] + idx : mpc_decoder_bitstream_read(d, 6);
00912 L[1] = L[0];
00913 L[2] = L[1];
00914 break;
00915 case 2:
00916 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00917 L[0] = (idx!=8) ? L[2] + idx : mpc_decoder_bitstream_read(d, 6);
00918 L[1] = L[0];
00919 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00920 L[2] = (idx!=8) ? L[1] + idx : mpc_decoder_bitstream_read(d, 6);
00921 break;
00922 case 0:
00923 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00924 L[0] = (idx!=8) ? L[2] + idx : mpc_decoder_bitstream_read(d, 6);
00925 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00926 L[1] = (idx!=8) ? L[0] + idx : mpc_decoder_bitstream_read(d, 6);
00927 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00928 L[2] = (idx!=8) ? L[1] + idx : mpc_decoder_bitstream_read(d, 6);
00929 break;
00930 default:
00931 return;
00932 break;
00933 }
00934
00935 d->DSCF_Reference_L[n] = L[2];
00936 }
00937 if (*ResR)
00938 {
00939 R[2] = d->DSCF_Reference_R[n];
00940 switch (d->SCFI_R[n])
00941 {
00942 case 1:
00943 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00944 R[0] = (idx!=8) ? R[2] + idx : mpc_decoder_bitstream_read(d, 6);
00945 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00946 R[1] = (idx!=8) ? R[0] + idx : mpc_decoder_bitstream_read(d, 6);
00947 R[2] = R[1];
00948 break;
00949 case 3:
00950 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00951 R[0] = (idx!=8) ? R[2] + idx : mpc_decoder_bitstream_read(d, 6);
00952 R[1] = R[0];
00953 R[2] = R[1];
00954 break;
00955 case 2:
00956 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00957 R[0] = (idx!=8) ? R[2] + idx : mpc_decoder_bitstream_read(d, 6);
00958 R[1] = R[0];
00959 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00960 R[2] = (idx!=8) ? R[1] + idx : mpc_decoder_bitstream_read(d, 6);
00961 break;
00962 case 0:
00963 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00964 R[0] = (idx!=8) ? R[2] + idx : mpc_decoder_bitstream_read(d, 6);
00965 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00966 R[1] = (idx!=8) ? R[0] + idx : mpc_decoder_bitstream_read(d, 6);
00967 idx = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00968 R[2] = (idx!=8) ? R[1] + idx : mpc_decoder_bitstream_read(d, 6);
00969 break;
00970 default:
00971 return;
00972 break;
00973 }
00974
00975 d->DSCF_Reference_R[n] = R[2];
00976 }
00977 }
00978
00979 ResL = d->Res_L;
00980 ResR = d->Res_R;
00981 L = d->Q[0].L;
00982 R = d->Q[0].R;
00983 for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=36, R+=36)
00984 {
00985
00986 switch (*ResL)
00987 {
00988 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
00989 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
00990 L += 36;
00991 break;
00992 case -1:
00993 for (k=0; k<36; k++ ) {
00994 tmp = random_int(d);
00995 *L++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
00996 }
00997 break;
00998 case 0:
00999 L += 36;
01000 break;
01001 case 1:
01002 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
01003 for (k=0; k<12; ++k)
01004 {
01005 idx = mpc_decoder_huffman_decode_fast(d, Table);
01006 *L++ = idx30[idx];
01007 *L++ = idx31[idx];
01008 *L++ = idx32[idx];
01009 }
01010 break;
01011 case 2:
01012 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
01013 for (k=0; k<18; ++k)
01014 {
01015 idx = mpc_decoder_huffman_decode_fast(d, Table);
01016 *L++ = idx50[idx];
01017 *L++ = idx51[idx];
01018 }
01019 break;
01020 case 3:
01021 case 4:
01022 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
01023 for (k=0; k<36; ++k)
01024 *L++ = mpc_decoder_huffman_decode_faster(d, Table);
01025 break;
01026 case 5:
01027 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
01028 for (k=0; k<36; ++k)
01029 *L++ = mpc_decoder_huffman_decode_fast(d, Table);
01030 break;
01031 case 6:
01032 case 7:
01033 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
01034 for (k=0; k<36; ++k)
01035 *L++ = mpc_decoder_huffman_decode(d, Table);
01036 break;
01037 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
01038 tmp = Dc[*ResL];
01039 for (k=0; k<36; ++k)
01040 *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
01041 break;
01042 default:
01043 return;
01044 }
01045
01046 switch (*ResR)
01047 {
01048 case -2: case -3: case -4: case -5: case -6: case -7: case -8: case -9:
01049 case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
01050 R += 36;
01051 break;
01052 case -1:
01053 for (k=0; k<36; k++ ) {
01054 tmp = random_int(d);
01055 *R++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
01056 }
01057 break;
01058 case 0:
01059 R += 36;
01060 break;
01061 case 1:
01062 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
01063 for (k=0; k<12; ++k)
01064 {
01065 idx = mpc_decoder_huffman_decode_fast(d, Table);
01066 *R++ = idx30[idx];
01067 *R++ = idx31[idx];
01068 *R++ = idx32[idx];
01069 }
01070 break;
01071 case 2:
01072 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
01073 for (k=0; k<18; ++k)
01074 {
01075 idx = mpc_decoder_huffman_decode_fast(d, Table);
01076 *R++ = idx50[idx];
01077 *R++ = idx51[idx];
01078 }
01079 break;
01080 case 3:
01081 case 4:
01082 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
01083 for (k=0; k<36; ++k)
01084 *R++ = mpc_decoder_huffman_decode_faster(d, Table);
01085 break;
01086 case 5:
01087 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
01088 for (k=0; k<36; ++k)
01089 *R++ = mpc_decoder_huffman_decode_fast(d, Table);
01090 break;
01091 case 6:
01092 case 7:
01093 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
01094 for (k=0; k<36; ++k)
01095 *R++ = mpc_decoder_huffman_decode(d, Table);
01096 break;
01097 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
01098 tmp = Dc[*ResR];
01099 for (k=0; k<36; ++k)
01100 *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
01101 break;
01102 default:
01103 return;
01104 }
01105 }
01106 }
01107
01108 void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r)
01109 {
01110 d->r = r;
01111
01112 d->HuffQ[0][0] = 0;
01113 d->HuffQ[1][0] = 0;
01114 d->HuffQ[0][1] = d->HuffQ1[0];
01115 d->HuffQ[1][1] = d->HuffQ1[1];
01116 d->HuffQ[0][2] = d->HuffQ2[0];
01117 d->HuffQ[1][2] = d->HuffQ2[1];
01118 d->HuffQ[0][3] = d->HuffQ3[0];
01119 d->HuffQ[1][3] = d->HuffQ3[1];
01120 d->HuffQ[0][4] = d->HuffQ4[0];
01121 d->HuffQ[1][4] = d->HuffQ4[1];
01122 d->HuffQ[0][5] = d->HuffQ5[0];
01123 d->HuffQ[1][5] = d->HuffQ5[1];
01124 d->HuffQ[0][6] = d->HuffQ6[0];
01125 d->HuffQ[1][6] = d->HuffQ6[1];
01126 d->HuffQ[0][7] = d->HuffQ7[0];
01127 d->HuffQ[1][7] = d->HuffQ7[1];
01128
01129 d->SampleHuff[0] = NULL;
01130 d->SampleHuff[1] = d->Entropie_1;
01131 d->SampleHuff[2] = d->Entropie_2;
01132 d->SampleHuff[3] = d->Entropie_3;
01133 d->SampleHuff[4] = d->Entropie_4;
01134 d->SampleHuff[5] = d->Entropie_5;
01135 d->SampleHuff[6] = d->Entropie_6;
01136 d->SampleHuff[7] = d->Entropie_7;
01137 d->SampleHuff[8] = NULL;
01138 d->SampleHuff[9] = NULL;
01139 d->SampleHuff[10] = NULL;
01140 d->SampleHuff[11] = NULL;
01141 d->SampleHuff[12] = NULL;
01142 d->SampleHuff[13] = NULL;
01143 d->SampleHuff[14] = NULL;
01144 d->SampleHuff[15] = NULL;
01145 d->SampleHuff[16] = NULL;
01146 d->SampleHuff[17] = NULL;
01147
01148 d->EQ_activated = 0;
01149 d->MPCHeaderPos = 0;
01150 d->StreamVersion = 0;
01151 d->MS_used = 0;
01152 d->FwdJumpInfo = 0;
01153 d->ActDecodePos = 0;
01154 d->FrameWasValid = 0;
01155 d->OverallFrames = 0;
01156 d->DecodedFrames = 0;
01157 d->LastValidSamples = 0;
01158 d->TrueGaplessPresent = 0;
01159 d->WordsRead = 0;
01160 d->Max_Band = 0;
01161 d->SampleRate = 0;
01162
01163 d->__r1 = 1;
01164 d->__r2 = 1;
01165
01166 d->dword = 0;
01167 d->pos = 0;
01168 d->Zaehler = 0;
01169 d->WordsRead = 0;
01170 d->Max_Band = 0;
01171
01172 mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f);
01173 mpc_decoder_init_huffman_sv6(d);
01174 mpc_decoder_init_huffman_sv7(d);
01175 }
01176
01177 static void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
01178 {
01179 mpc_decoder_reset_synthesis(d);
01180 mpc_decoder_reset_globals(d);
01181
01182 d->StreamVersion = si->stream_version;
01183 d->MS_used = si->ms;
01184 d->Max_Band = si->max_band;
01185 d->OverallFrames = si->frames;
01186 d->MPCHeaderPos = si->header_position;
01187 d->LastValidSamples = si->last_frame_samples;
01188 d->TrueGaplessPresent = si->is_true_gapless;
01189 d->SampleRate = (mpc_int32_t)si->sample_freq;
01190
01191 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY;
01192 }
01193
01194 BOOL mpc_decoder_initialize(mpc_decoder *d, mpc_streaminfo *si)
01195 {
01196 mpc_decoder_set_streaminfo(d, si);
01197
01198
01199 switch (d->StreamVersion) {
01200 case 0x04: f_seek(d, 4 + d->MPCHeaderPos); d->pos = 16; break;
01201 case 0x05:
01202 case 0x06: f_seek(d, 8 + d->MPCHeaderPos); d->pos = 0; break;
01203 case 0x07:
01204 case 0x17: d->pos = 8; break;
01205 default: return FALSE;
01206 }
01207
01208
01209 f_read_dword(d, d->Speicher, MEMSIZE );
01210 d->dword = d->Speicher[d->Zaehler = 0];
01211
01212 return TRUE;
01213 }
01214
01215
01216
01217
01218
01219 #if 0
01220 static void
01221 helper1(mpc_decoder *d, mpc_uint32_t bitpos)
01222 {
01223 f_seek(d, (bitpos >> 5) * 4 + d->MPCHeaderPos);
01224 f_read_dword(d, d->Speicher, 2);
01225 d->dword = d->Speicher[d->Zaehler = 0];
01226 d->pos = bitpos & 31;
01227 }
01228 #endif
01229
01230 static void
01231 helper2(mpc_decoder *d, mpc_uint32_t bitpos)
01232 {
01233 f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
01234 f_read_dword(d, d->Speicher, MEMSIZE);
01235 d->dword = d->Speicher[d->Zaehler = 0];
01236 d->pos = bitpos & 31;
01237 }
01238
01239 #if 0
01240 static void
01241 helper3(mpc_decoder *d, mpc_uint32_t bitpos, mpc_uint32_t* buffoffs)
01242 {
01243 d->pos = bitpos & 31;
01244 bitpos >>= 5;
01245 if ((mpc_uint32_t)(bitpos - *buffoffs) >= MEMSIZE - 2) {
01246 *buffoffs = bitpos;
01247 f_seek(d, bitpos * 4L + d->MPCHeaderPos);
01248 f_read_dword(d, d->Speicher, MEMSIZE );
01249 }
01250 d->dword = d->Speicher[d->Zaehler = bitpos - *buffoffs ];
01251 }
01252 #endif
01253
01254 static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion)
01255 {
01256 mpc_uint32_t fpos = 0;
01257 switch ( d->StreamVersion ) {
01258 case 0x04: fpos = 48; break;
01259 case 0x05:
01260 case 0x06: fpos = 64; break;
01261 case 0x07:
01262 case 0x17: fpos = 200; break;
01263 }
01264 return fpos;
01265 }
01266
01267 BOOL mpc_decoder_seek_seconds(mpc_decoder *d, double seconds)
01268 {
01269 return mpc_decoder_seek_sample(d, (mpc_int64_t)(seconds * (double)d->SampleRate + 0.5));
01270 }
01271
01272 BOOL mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample)
01273 {
01274 mpc_uint32_t fpos;
01275 mpc_uint32_t fwd;
01276
01277 fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH);
01278 d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH);
01279
01280 memset(d->Y_L , 0, sizeof d->Y_L );
01281 memset(d->Y_R , 0, sizeof d->Y_R );
01282 memset(d->SCF_Index_L , 0, sizeof d->SCF_Index_L );
01283 memset(d->SCF_Index_R , 0, sizeof d->SCF_Index_R );
01284 memset(d->Res_L , 0, sizeof d->Res_L );
01285 memset(d->Res_R , 0, sizeof d->Res_R );
01286 memset(d->SCFI_L , 0, sizeof d->SCFI_L );
01287 memset(d->SCFI_R , 0, sizeof d->SCFI_R );
01288 memset(d->DSCF_Flag_L , 0, sizeof d->DSCF_Flag_L );
01289 memset(d->DSCF_Flag_R , 0, sizeof d->DSCF_Flag_R );
01290 memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );
01291 memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );
01292 memset(d->Q , 0, sizeof d->Q );
01293 memset(d->MS_Flag , 0, sizeof d->MS_Flag );
01294
01295
01296 mpc_decoder_reset_synthesis(d);
01297
01298
01299 fwd = fwd < d->OverallFrames ? fwd : d->OverallFrames;
01300
01301
01302 d->DecodedFrames = 0;
01303
01304 fpos = get_initial_fpos(d, d->StreamVersion);
01305 if (fpos == 0) {
01306 return FALSE;
01307 }
01308
01309 helper2(d, fpos);
01310
01311
01312 for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) {
01313 mpc_uint32_t FrameBitCnt;
01314 mpc_uint32_t RING;
01315 RING = d->Zaehler;
01316 d->FwdJumpInfo = mpc_decoder_bitstream_read(d, 20);
01317 d->ActDecodePos = (d->Zaehler << 5) + d->pos;
01318 FrameBitCnt = mpc_decoder_bits_read(d);
01319 if (d->StreamVersion >= 7) {
01320 mpc_decoder_read_bitstream_sv7(d);
01321 }
01322 else {
01323 mpc_decoder_read_bitstream_sv6(d);
01324 }
01325 if (mpc_decoder_bits_read(d) - FrameBitCnt != d->FwdJumpInfo ) {
01326
01327 return FALSE;
01328 }
01329
01330 if ((RING ^ d->Zaehler) & MEMSIZE2) {
01331 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
01332 }
01333 }
01334
01335
01336
01337
01338 return TRUE;
01339 }
01340
01341 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)
01342 {
01343 if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
01344
01345 f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
01346 }
01347 }
01348
01349