Ignore:
Timestamp:
09/17/06 16:19:05 (18 years ago)
Author:
zorg
Message:

Revert broken changes from r21

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/zorg/src/mpc_decoder.c

    r21 r23  
    5959#endif
    6060
    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];mpc_uint8_t     LUT7_1  [1<< 8];            //  512 Bytes
    91 mpc_uint8_t     LUTDSCF [1<< 6];            //   64 Bytes = 2976 Bytes
    92 
    9361//------------------------------------------------------------------------------
    9462// types
     
    10876//------------------------------------------------------------------------------
    10977void mpc_decoder_read_bitstream_sv6(mpc_decoder *d);
    110 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t fastSeeking);
    111 void mpc_decoder_update_buffer(mpc_decoder *d);
     78void mpc_decoder_read_bitstream_sv7(mpc_decoder *d);
     79void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING);
    11280mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample);
    11381void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
    114 void mpc_decoder_seek_to(mpc_decoder *d, mpc_uint32_t bitPos);
    115 void mpc_decoder_seek_forward(mpc_decoder *d, mpc_uint32_t bits);
    116 mpc_uint32_t mpc_decoder_jump_frame(mpc_decoder *d);
    117 void mpc_decoder_fill_buffer(mpc_decoder *d);
    118 void mpc_decoder_reset_state(mpc_decoder *d);
    119 static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion);
    120 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);
    121 static void mpc_move_next(mpc_decoder *d);
    122 
    123 mpc_uint32_t  Speicher[MPC_DECODER_MEMSIZE];
    124 MPC_SAMPLE_FORMAT Y_L[36][32];
    125 MPC_SAMPLE_FORMAT Y_R[36][32];
    12682
    12783//------------------------------------------------------------------------------
     
    173129{
    174130    d->dword = 0;
    175     d->next = 0;
    176131    d->pos = 0;
    177132    d->Zaehler = 0;
     
    186141}
    187142
    188 static void mpc_move_next(mpc_decoder *d) {
    189     d->Zaehler = (d->Zaehler + 1) & MEMMASK;
    190     d->dword = d->next;
    191     d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]);
    192     d->pos -= 32;
    193     ++(d->WordsRead);
    194 }
    195 
    196143// read desired number of bits out of the bitstream
    197 static __inline mpc_uint32_t
     144static mpc_uint32_t
    198145mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits)
    199146{
     
    206153    }
    207154    else {
    208         mpc_move_next(d);
     155        d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
     156        d->pos -= 32;
    209157        if (d->pos) {
    210158            out <<= d->pos;
    211159            out |= d->dword >> (32 - d->pos);
    212160        }
     161        ++(d->WordsRead);
    213162    }
    214163
    215164    return out & mask[bits];
    216 }
    217 
    218 static void
    219 mpc_decoder_make_huffman_lookup(
    220     mpc_uint8_t* lookup, size_t length, const HuffmanTyp* Table, size_t elements )
    221 {
    222     size_t    i;
    223     size_t    idx  = elements;
    224     mpc_uint32_t  dval = (mpc_uint32_t)0x80000000L / length * 2;
    225     mpc_uint32_t  val  = dval - 1;
    226 
    227     for ( i = 0; i < length; i++, val += dval ) {
    228         while ( idx > 0  &&  val >= Table[idx-1].Code )
    229             idx--;
    230         *lookup++ = (mpc_uint8_t)idx;
    231     }
    232 
    233     return;
    234165}
    235166
     
    242173    // load preview and decode
    243174    mpc_uint32_t code  = d->dword << d->pos;
    244 
    245175    if (d->pos > 26) {
    246         code |= d->next >> (32 - d->pos);
     176        code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
    247177    }
    248178    while (code < Table->Code) {
     
    252182    // set the new position within bitstream without performing a dummy-read
    253183    if ((d->pos += Table->Length) >= 32) {
    254         mpc_move_next(d);
     184        d->pos -= 32;
     185        d->dword = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK];
     186        ++(d->WordsRead);
    255187    }
    256188
     
    266198    // load preview and decode
    267199    mpc_uint32_t code = d->dword << d->pos;
    268    
    269200    if (d->pos > 18) {
    270         code |= d->next >> (32 - d->pos);
     201        code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
    271202    }
    272203    while (code < Table->Code) {
     
    276207    // set the new position within bitstream without performing a dummy-read
    277208    if ((d->pos += Table->Length) >= 32) {
    278         mpc_move_next(d);
     209        d->pos -= 32;
     210        d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
     211        ++(d->WordsRead);
    279212    }
    280213
     
    289222    // load preview and decode
    290223    mpc_uint32_t code  = d->dword << d->pos;
    291    
    292224    if (d->pos > 22) {
    293         code |= d->next >> (32 - d->pos);
     225        code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
    294226    }
    295227    while (code < Table->Code) {
     
    299231    // set the new position within bitstream without performing a dummy-read
    300232    if ((d->pos += Table->Length) >= 32) {
    301         mpc_move_next(d);
     233        d->pos -= 32;
     234        d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
     235        ++(d->WordsRead);
    302236    }
    303237
     
    312246    // load preview and decode
    313247    mpc_uint32_t code  = d->dword << d->pos;
    314    
    315248    if (d->pos > 27) {
    316         code |= d->next >> (32 - d->pos);
     249        code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
    317250    }
    318251    while (code < Table->Code) {
     
    322255    // set the new position within bitstream without performing a dummy-read
    323256    if ((d->pos += Table->Length) >= 32) {
    324         mpc_move_next(d);
    325     }
    326 
    327     return Table->Value;
    328 }
    329 
    330 /* partial lookup table decode */
    331 static mpc_int32_t
    332 mpc_decoder_huffman_decode_fasterer(mpc_decoder *d, const HuffmanTyp* Table, const mpc_uint8_t* tab, mpc_uint16_t unused_bits)
    333 {
    334     // load preview and decode
    335     mpc_uint32_t code  = d->dword << d->pos;
    336    
    337     if (d->pos > 18) { // preview 14 bits
    338         code |= d->next >> (32 - d->pos);
    339     }
    340 
    341     Table += tab [(size_t)(code >> unused_bits) ];
    342 
    343     while (code < Table->Code) {
    344         Table++;
    345     }
    346 
    347     // set the new position within bitstream without performing a dummy-read
    348     if ((d->pos += Table->Length) >= 32) {
    349         mpc_move_next(d);
    350     }
    351 
    352     return Table->Value;
    353 }
    354 
    355 /* full decode using lookup table */
    356 static __inline mpc_int32_t
    357 mpc_decoder_huffman_decode_fastest(mpc_decoder *d, const HuffmanTyp* Table, const mpc_uint8_t* tab, mpc_uint16_t unused_bits)
    358 {
    359     // load preview and decode
    360     mpc_uint32_t code  = d->dword << d->pos;
    361 
    362     if (d->pos > unused_bits) {
    363         code |= d->next >> (32 - d->pos);
    364     }
    365 
    366     Table+=tab [(size_t)(code >> unused_bits) ];
    367 
    368     // set the new position within bitstream without performing a dummy-read
    369     if ((d->pos += Table->Length) >= 32) {
    370         mpc_move_next(d);
     257        d->pos -= 32;
     258        d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
     259        ++(d->WordsRead);
    371260    }
    372261
     
    390279mpc_decoder_reset_y(mpc_decoder *d)
    391280{
    392     memset(d->Y_L, 0, sizeof Y_L);
    393     memset(d->Y_R, 0, sizeof Y_R);
     281    memset(d->Y_L, 0, sizeof d->Y_L);
     282    memset(d->Y_R, 0, sizeof d->Y_R);
    394283}
    395284
     
    400289
    401290    d->DecodedFrames  = 0;
    402     d->SeekTableIndex = 0;
    403     d->MaxDecodedFrames = 0;
    404291    d->StreamVersion  = 0;
    405292    d->MS_used        = 0;
    406293
    407     memset(d->Y_L          , 0, sizeof Y_L           );
    408     memset(d->Y_R          , 0, sizeof Y_R           );
     294    memset(d->Y_L          , 0, sizeof d->Y_L           );
     295    memset(d->Y_R          , 0, sizeof d->Y_R           );
    409296    memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
    410297    memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
     
    427314                         mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer)
    428315{
     316  unsigned int i;
    429317  mpc_decoder_reset_bitstream_decode(d);
    430   if (in_len > sizeof(Speicher)) in_len = sizeof(Speicher);
     318  if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher);
    431319  memcpy(d->Speicher, in_buffer, in_len);
    432   d->dword = SWAP(d->Speicher[0]);
    433   d->next = SWAP(d->Speicher[1]);
     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];
    434325  switch (d->StreamVersion) {
    435326#ifdef MPC_SUPPORT_SV456
     
    442333    case 0x07:
    443334    case 0x17:
    444         mpc_decoder_read_bitstream_sv7(d, FALSE);
     335        mpc_decoder_read_bitstream_sv7(d);
    445336        break;
    446337    default:
     
    459350    mpc_uint32_t  FrameBitCnt = 0;
    460351
    461     // output the last part of the last frame here, if needed
    462     if (d->last_block_samples > 0) {
    463         output_frame_length = d->last_block_samples;
    464         d->last_block_samples = 0; // it's going to be handled now, so reset it
    465         if (!d->TrueGaplessPresent) {
    466             mpc_decoder_reset_y(d);
    467         } else {
    468             mpc_decoder_bitstream_read(d, 20);
    469             mpc_decoder_read_bitstream_sv7(d, FALSE);
    470             mpc_decoder_requantisierung(d, d->Max_Band);
    471         }
    472         mpc_decoder_synthese_filter_float(d, buffer);
    473         return output_frame_length;
    474     }
    475    
    476352    if (d->DecodedFrames >= d->OverallFrames) {
    477353        return (mpc_uint32_t)(-1);                           // end of file -> abort decoding
    478354    }
    479 
    480     if (d->DecodedFrames == 0 && d->Use_SeekTable)
    481         d->SeekTable[0] = mpc_decoder_bits_read(d);
    482355
    483356    // read jump-info for validity check of frame
     
    498371    case 0x07:
    499372    case 0x17:
    500         mpc_decoder_read_bitstream_sv7(d, FALSE);
     373        mpc_decoder_read_bitstream_sv7(d);
    501374        break;
    502375    default:
     
    505378    d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;
    506379
    507     d->DecodedFrames++;
    508 
    509     if (d->Use_SeekTable) {
    510         if (d->SeekTable_Step == 1) {
    511             d->SeekTable [d->DecodedFrames] = d->FwdJumpInfo + 20;
    512         } else {
    513             if ((d->DecodedFrames-1) % d->SeekTable_Step == 0) {
    514                 d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter;
    515                 d->SeekTableIndex += 1;
    516                 d->SeekTableCounter = 0;
    517             }
    518             d->SeekTableCounter += d->FwdJumpInfo + 20;
    519         }
    520     }
    521 
    522380    // synthesize signal
    523381    mpc_decoder_requantisierung(d, d->Max_Band);
     
    527385
    528386    mpc_decoder_synthese_filter_float(d, buffer);
     387
     388    d->DecodedFrames++;
    529389
    530390    // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
     
    542402        // additional FilterDecay samples are needed for decay of synthesis filter
    543403        if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
    544             // this variable will be checked for at the top of the function
    545             d->last_block_samples = FilterDecay;
    546             }
     404            if (!d->TrueGaplessPresent) {
     405                mpc_decoder_reset_y(d);
     406            } else {
     407                mpc_decoder_bitstream_read(d, 20);
     408                mpc_decoder_read_bitstream_sv7(d);
     409                mpc_decoder_requantisierung(d, d->Max_Band);
     410            }
     411
     412            mpc_decoder_synthese_filter_float(d, buffer + 2304);
     413
     414            output_frame_length = MPC_FRAME_LENGTH + FilterDecay;
     415        }
    547416        else {                              // there are only FilterDecay samples needed for this frame
    548417            output_frame_length = FilterDecay;
     
    559428            memmove(
    560429                buffer,
    561                 buffer + d->samples_to_skip,
    562                 output_frame_length * sizeof (MPC_SAMPLE_FORMAT));
    563             memmove(
    564                 buffer + MPC_FRAME_LENGTH,
    565                 buffer + MPC_FRAME_LENGTH + d->samples_to_skip,
    566                 output_frame_length * sizeof (MPC_SAMPLE_FORMAT));
     430                buffer + d->samples_to_skip * 2,
     431                output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT));
    567432            d->samples_to_skip = 0;
    568433        }
     
    607472
    608473        }
    609         mpc_decoder_update_buffer(d);
     474        mpc_decoder_update_buffer(d, RING);
    610475
    611476        if (valid_samples > 0) {
     
    818683    const HuffmanTyp *x1;
    819684    const HuffmanTyp *x2;
    820     mpc_int8_t *L;
    821     mpc_int8_t *R;
     685    mpc_int8_t  *L;
     686    mpc_int8_t  *R;
    822687    mpc_int16_t *QL;
    823688    mpc_int16_t *QR;
     
    1022887/****************************************** SV 7 ******************************************/
    1023888void
    1024 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t fastSeeking)
     889mpc_decoder_read_bitstream_sv7(mpc_decoder *d)
    1025890{
    1026891    // these arrays hold decoding results for bundled quantizers (3- and 5-step)
     
    1031896    /*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};
    1032897
    1033     mpc_int32_t n,k;
     898    mpc_int32_t n, k;
    1034899    mpc_int32_t Max_used_Band=0;
    1035900    const HuffmanTyp *Table;
    1036     mpc_int32_t idx;
    1037     mpc_int8_t *L   ,*R;
    1038     mpc_int16_t *LQ   ,*RQ;
    1039     mpc_int8_t *ResL,*ResR;
     901    mpc_int32_t  idx;
     902    mpc_int8_t   *L, *R;
     903    mpc_int16_t  *LQ, *RQ;
     904    mpc_int8_t   *ResL, *ResR;
    1040905    mpc_uint32_t tmp;
    1041     mpc_uint8_t *LUT;
    1042     mpc_uint8_t max_length;
    1043906
    1044907    /***************************** Header *****************************/
     
    1051914    if (d->MS_used && !(*ResL==0 && *ResR==0)) {
    1052915        d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1);
    1053     } else {
    1054         d->MS_Flag[0] = 0;
    1055916    }
    1056917
     
    1072933        if (*ResL!=0 || *ResR!=0) {
    1073934            Max_used_Band = n;
    1074         } else {
    1075             d->MS_Flag[n] = 0;
    1076935        }
    1077936    }
     
    1098957            {
    1099958            case 1:
    1100                 idx = Decode_DSCF ();
    1101                 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    1102                 idx = Decode_DSCF ();
    1103                 L[1] = (idx!=8) ? SCF_DIFF(L[0], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    1104963                L[2] = L[1];
    1105964                break;
    1106965            case 3:
    1107                 idx = Decode_DSCF ();
    1108                 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    1109968                L[1] = L[0];
    1110969                L[2] = L[1];
    1111970                break;
    1112971            case 2:
    1113                 idx = Decode_DSCF ();
    1114                 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    1115974                L[1] = L[0];
    1116                 idx = Decode_DSCF ();
    1117                 L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    1118977                break;
    1119978            case 0:
    1120                 idx = Decode_DSCF ();
    1121                 L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    1122                 idx = Decode_DSCF ();
    1123                 L[1] = (idx!=8) ? SCF_DIFF(L[0], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    1124                 idx = Decode_DSCF ();
    1125                 L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    1126985                break;
    1127986            default:
     
    1129988                break;
    1130989            }
     990            // update Reference for DSCF
     991            //d->DSCF_Reference_L[n] = L[2];
    1131992        }
    1132993        if (*ResR)
    1133994        {
     995            //R[2] = d->DSCF_Reference_R[n];
    1134996            switch (d->SCFI_R[n])
    1135997            {
    1136998            case 1:
    1137                 idx = Decode_DSCF ();
    1138                 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    1139                 idx = Decode_DSCF ();
    1140                 R[1] = (idx!=8) ? SCF_DIFF(R[0], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    11411003                R[2] = R[1];
    11421004                break;
    11431005            case 3:
    1144                 idx = Decode_DSCF ();
    1145                 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    11461008                R[1] = R[0];
    11471009                R[2] = R[1];
    11481010                break;
    11491011            case 2:
    1150                 idx = Decode_DSCF ();
    1151                 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    11521014                R[1] = R[0];
    1153                 idx = Decode_DSCF ();
    1154                 R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    11551017                break;
    11561018            case 0:
    1157                 idx = Decode_DSCF ();
    1158                 R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    1159                 idx = Decode_DSCF ();
    1160                 R[1] = (idx!=8) ? SCF_DIFF(R[0], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    1161                 idx = Decode_DSCF ();
    1162                 R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    11631025                break;
    11641026            default:
     
    11661028                break;
    11671029            }
    1168         }
    1169     }
    1170 
    1171     if (fastSeeking)
    1172         return;
    1173 
     1030            // update Reference for DSCF
     1031            //d->DSCF_Reference_R[n] = R[2];
     1032        }
     1033    }
    11741034    /***************************** Samples ****************************/
    11751035    ResL = d->Res_L;
    11761036    ResR = d->Res_R;
    1177     LQ    = d->Q[0].L;
    1178     RQ    = d->Q[0].R;
     1037    LQ   = d->Q[0].L;
     1038    RQ   = d->Q[0].R;
    11791039    for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, LQ+=36, RQ+=36)
    11801040    {
     
    11961056            break;
    11971057        case 1:
    1198             if (mpc_decoder_bitstream_read(d, 1)) {
    1199                 Table = mpc_table_HuffQ[1][1];
    1200                 LUT = LUT1_1;
    1201                 max_length = 9;
    1202             } else {
    1203                 Table = mpc_table_HuffQ[0][1];
    1204                 LUT = LUT1_0;
    1205                 max_length = 6;
    1206             }
     1058            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
    12071059            for (k=0; k<12; ++k)
    12081060            {
    1209                 idx  = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1061                idx = mpc_decoder_huffman_decode_fast(d,  Table);
    12101062                *LQ++ = idx30[idx];
    12111063                *LQ++ = idx31[idx];
     
    12141066            break;
    12151067        case 2:
    1216             if (mpc_decoder_bitstream_read(d, 1)) {
    1217                 Table = mpc_table_HuffQ[1][2];
    1218                 LUT = LUT2_1;
    1219                 max_length = 10;
    1220             } else {
    1221                 Table = mpc_table_HuffQ[0][2];
    1222                 LUT = LUT2_0;
    1223                 max_length = 7;
    1224             }
     1068            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
    12251069            for (k=0; k<18; ++k)
    12261070            {
    1227                 idx  = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1071                idx = mpc_decoder_huffman_decode_fast(d,  Table);
    12281072                *LQ++ = idx50[idx];
    12291073                *LQ++ = idx51[idx];
     
    12311075            break;
    12321076        case 3:
    1233             if (mpc_decoder_bitstream_read(d, 1)) {
    1234                 Table = mpc_table_HuffQ[1][3];
    1235                 LUT = LUT3_1;
    1236                 max_length = 5;
    1237             } else {
    1238                 Table = mpc_table_HuffQ[0][3];
    1239                 LUT = LUT3_0;
    1240                 max_length = 4;
    1241             }
     1077        case 4:
     1078            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
    12421079            for (k=0; k<36; ++k)
    1243                 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1244             break;
    1245         case 4:
    1246             if (mpc_decoder_bitstream_read(d, 1)) {
    1247                 Table = mpc_table_HuffQ[1][4];
    1248                 LUT = LUT4_1;
    1249                 max_length = 5;
    1250             } else {
    1251                 Table = mpc_table_HuffQ[0][4];
    1252                 LUT = LUT4_0;
    1253                 max_length = 4;
    1254             }
    1255             for (k=0; k<36; ++k)
    1256                 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1080                *LQ++ = mpc_decoder_huffman_decode_faster(d, Table);
    12571081            break;
    12581082        case 5:
    1259             if (mpc_decoder_bitstream_read(d, 1)) {
    1260                 Table = mpc_table_HuffQ[1][5];
    1261                 LUT = LUT5_1;
    1262                 max_length = 8;
    1263             } else {
    1264                 Table = mpc_table_HuffQ[0][5];
    1265                 LUT = LUT5_0;
    1266                 max_length = 6;
    1267             }
     1083            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
    12681084            for (k=0; k<36; ++k)
    1269                 *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1085                *LQ++ = mpc_decoder_huffman_decode_fast(d, Table);
    12701086            break;
    12711087        case 6:
    1272             if (mpc_decoder_bitstream_read(d, 1)) {
    1273                 Table = mpc_table_HuffQ[1][6];
    1274                 LUT = LUT6_1;
    1275                 max_length = 7;
    1276                 for (k=0; k<36; ++k)
    1277                     *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
    1278             } else {
    1279                 Table = mpc_table_HuffQ[0][6];
    1280                 LUT = LUT6_0;
    1281                 max_length = 7;
    1282                 for (k=0; k<36; ++k)
    1283                     *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1284             }
    1285             break;
    12861088        case 7:
    1287             if (mpc_decoder_bitstream_read(d, 1)) {
    1288                 Table = mpc_table_HuffQ[1][7];
    1289                 LUT = LUT7_1;
    1290                 max_length = 8;
     1089            Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
    12911090            for (k=0; k<36; ++k)
    1292                     *LQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
    1293             } else {
    1294                 Table = mpc_table_HuffQ[0][7];
    1295                 LUT = LUT7_0;
    1296                 max_length = 8;
    1297                 for (k=0; k<36; ++k)
    1298                     *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1299             }           
     1091                *LQ++ = mpc_decoder_huffman_decode(d, Table);
    13001092            break;
    13011093        case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
    13021094            tmp = Dc[*ResL];
    13031095            for (k=0; k<36; ++k)
    1304                 *LQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
     1096                *LQ++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
    13051097            break;
    13061098        default:
     
    13241116                break;
    13251117            case 1:
    1326                 if (mpc_decoder_bitstream_read(d, 1)) {
    1327                     Table = mpc_table_HuffQ[1][1];
    1328                     LUT = LUT1_1;
    1329                     max_length = 9;
    1330                 } else {
    1331                     Table = mpc_table_HuffQ[0][1];
    1332                     LUT = LUT1_0;
    1333                     max_length = 6;
    1334                 }
     1118                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
    13351119                for (k=0; k<12; ++k)
    13361120                {
    1337                     idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1121                    idx = mpc_decoder_huffman_decode_fast(d, Table);
    13381122                    *RQ++ = idx30[idx];
    13391123                    *RQ++ = idx31[idx];
     
    13421126                break;
    13431127            case 2:
    1344                 if (mpc_decoder_bitstream_read(d, 1)) {
    1345                     Table = mpc_table_HuffQ[1][2];
    1346                     LUT = LUT2_1;
    1347                     max_length = 10;
    1348                 } else {
    1349                     Table = mpc_table_HuffQ[0][2];
    1350                     LUT = LUT2_0;
    1351                     max_length = 7;
    1352                 }
     1128                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
    13531129                for (k=0; k<18; ++k)
    13541130                {
    1355                     idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1131                    idx = mpc_decoder_huffman_decode_fast(d, Table);
    13561132                    *RQ++ = idx50[idx];
    13571133                    *RQ++ = idx51[idx];
     
    13591135                break;
    13601136            case 3:
    1361                 if (mpc_decoder_bitstream_read(d, 1)) {
    1362                     Table = mpc_table_HuffQ[1][3];
    1363                     LUT = LUT3_1;
    1364                     max_length = 5;
    1365                 } else {
    1366                     Table = mpc_table_HuffQ[0][3];
    1367                     LUT = LUT3_0;
    1368                     max_length = 4;
    1369                 }
     1137            case 4:
     1138                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
    13701139                for (k=0; k<36; ++k)
    1371                     *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1372                 break;
    1373             case 4:
    1374                 if (mpc_decoder_bitstream_read(d, 1)) {
    1375                     Table = mpc_table_HuffQ[1][4];
    1376                     LUT = LUT4_1;
    1377                     max_length = 5;
    1378                 } else {
    1379                     Table = mpc_table_HuffQ[0][4];
    1380                     LUT = LUT4_0;
    1381                     max_length = 4;
    1382                 }
     1140                    *RQ++ = mpc_decoder_huffman_decode_faster(d, Table);
     1141                break;
     1142            case 5:
     1143                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
    13831144                for (k=0; k<36; ++k)
    1384                     *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1385                 break;
    1386             case 5:
    1387                 if (mpc_decoder_bitstream_read(d, 1)) {
    1388                     Table = mpc_table_HuffQ[1][5];
    1389                     LUT = LUT5_1;
    1390                     max_length = 8;
    1391                 } else {
    1392                     Table = mpc_table_HuffQ[0][5];
    1393                     LUT = LUT5_0;
    1394                     max_length = 6;
    1395                 }
     1145                    *RQ++ = mpc_decoder_huffman_decode_fast(d, Table);
     1146                break;
     1147            case 6:
     1148            case 7:
     1149                Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
    13961150                for (k=0; k<36; ++k)
    1397                     *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1398                 break;
    1399             case 6:
    1400             if (mpc_decoder_bitstream_read(d, 1)) {
    1401                 Table = mpc_table_HuffQ[1][6];
    1402                 LUT = LUT6_1;
    1403                 max_length = 7;
    1404                 for (k=0; k<36; ++k)
    1405                     *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
    1406             } else {
    1407                 Table = mpc_table_HuffQ[0][6];
    1408                 LUT = LUT6_0;
    1409                 max_length = 7;
    1410                 for (k=0; k<36; ++k)
    1411                     *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1412             }
    1413             break;
    1414             case 7:
    1415             if (mpc_decoder_bitstream_read(d, 1)) {
    1416                 Table = mpc_table_HuffQ[1][7];
    1417                 LUT = LUT7_1;
    1418                 max_length = 8;
    1419                 for (k=0; k<36; ++k)
    1420                     *RQ++ = HUFFMAN_DECODE_FASTERER ( d, Table, LUT, max_length );
    1421             } else {
    1422                 Table = mpc_table_HuffQ[0][7];
    1423                 LUT = LUT7_0;
    1424                 max_length = 8;
    1425                 for (k=0; k<36; ++k)
    1426                     *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    1427             }           
     1151                    *RQ++ = mpc_decoder_huffman_decode(d, Table);
    14281152                break;
    14291153            case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
    14301154                tmp = Dc[*ResR];
    14311155                for (k=0; k<36; ++k)
    1432                     *RQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
     1156                    *RQ++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
    14331157                break;
    14341158            default:
     
    14501174  d->OverallFrames = 0;
    14511175  d->DecodedFrames = 0;
    1452   d->MaxDecodedFrames = 0;
    14531176  d->TrueGaplessPresent = 0;
    1454   d->last_block_samples = 0;
    14551177  d->WordsRead = 0;
    14561178  d->Max_Band = 0;
     
    14621184  d->pos = 0;
    14631185  d->Zaehler = 0;
    1464   d->Ring = 0;
    14651186  d->WordsRead = 0;
    14661187  d->Max_Band = 0;
    1467   d->SeekTable = NULL;
    1468   d->Use_FastSeek = TRUE;
    1469   d->Use_SeekTable = TRUE;
    1470   d->Use_StaticSeekTable = FALSE;
    1471   d->SeekTable_Step = 1;
    1472   d->SeekTableIndex = 0;
    1473   d->SeekTableCounter = 0;
    1474   d->Max_SeekTable_Size = 0;
    14751188
    14761189  mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f);
     
    14791192  mpc_decoder_init_huffman_sv7(d);
    14801193#endif
    1481 
    1482   LOOKUP ( mpc_table_HuffQ[0][1], 27, LUT1_0  );
    1483   LOOKUP ( mpc_table_HuffQ[1][1], 27, LUT1_1  );
    1484   LOOKUP ( mpc_table_HuffQ[0][2], 25, LUT2_0  );
    1485   LOOKUP ( mpc_table_HuffQ[1][2], 25, LUT2_1  );
    1486   LOOKUP ( mpc_table_HuffQ[0][3], 7,  LUT3_0  );
    1487   LOOKUP ( mpc_table_HuffQ[1][3], 7,  LUT3_1  );
    1488   LOOKUP ( mpc_table_HuffQ[0][4], 9,  LUT4_0  );
    1489   LOOKUP ( mpc_table_HuffQ[1][4], 9,  LUT4_1  );
    1490   LOOKUP ( mpc_table_HuffQ[0][5], 15, LUT5_0  );
    1491   LOOKUP ( mpc_table_HuffQ[1][5], 15, LUT5_1  );
    1492   LOOKUP ( mpc_table_HuffQ[0][6], 31, LUT6_0  );
    1493   LOOKUP ( mpc_table_HuffQ[1][6], 31, LUT6_1  );
    1494   LOOKUP ( mpc_table_HuffQ[0][7], 63, LUT7_0  );
    1495   LOOKUP ( mpc_table_HuffQ[1][7], 63, LUT7_1  );
    1496   LOOKUP ( mpc_table_HuffDSCF,    16, LUTDSCF );
    1497 
    1498   d->Speicher = Speicher;
    1499   d->Y_L = Y_L;
    1500   d->Y_R = Y_R;
    1501 
    1502 }
    1503 
    1504 void mpc_decoder_destroy(mpc_decoder *d) {
    1505 
    1506     if (d->SeekTable != NULL && d->Use_StaticSeekTable == FALSE)
    1507         free(d->SeekTable);
    1508 
    15091194}
    15101195
    15111196void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
    15121197{
    1513     mpc_uint16_t seekTableSize;
    1514 
    15151198    mpc_decoder_reset_synthesis(d);
    15161199    mpc_decoder_reset_globals(d);
     
    15251208
    15261209    d->samples_to_skip = MPC_DECODER_SYNTH_DELAY;
    1527 
    1528     if (d->SeekTable != NULL && d->Use_StaticSeekTable == FALSE)
    1529         free(d->SeekTable);
    1530 
    1531     if (d->Use_SeekTable) {
    1532         if (d->Use_StaticSeekTable == FALSE) {
    1533             if (d->Max_SeekTable_Size == 0) {
    1534                 seekTableSize = si->frames;
    1535             } else {
    1536                 seekTableSize = min(si->frames, d->Max_SeekTable_Size / sizeof(mpc_uint32_t));
    1537 }
    1538             d->SeekTable = (mpc_uint32_t*) calloc( sizeof(mpc_uint32_t), seekTableSize);
    1539             d->SeekTable_Step = si->frames / seekTableSize;
    1540             if (si->frames % seekTableSize)
    1541                 d->SeekTable_Step+=1;
    1542         } else {
    1543             seekTableSize = d->Max_SeekTable_Size / sizeof(mpc_uint32_t);
    1544             d->SeekTable_Step = si->frames / seekTableSize;
    1545             if (si->frames % seekTableSize)
    1546                 d->SeekTable_Step+=1;
    1547         }
    1548     }
    1549 
    15501210}
    15511211
    15521212mpc_bool_t mpc_decoder_initialize(mpc_decoder *d, mpc_streaminfo *si)
    15531213{
    1554     mpc_uint32_t bitPos;
    1555     mpc_uint32_t fpos;
    1556 
    15571214    mpc_decoder_set_streaminfo(d, si);
    15581215
    1559     // setting position to the beginning of the data-bitstream
    1560     bitPos = get_initial_fpos(d, d->StreamVersion);
    1561     fpos = bitPos >> 5;
    1562 
    1563     // fill buffer and initialize decoder
    1564     f_seek(d, fpos*4 + d->MPCHeaderPos);
     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
    15651227    f_read_dword(d, d->Speicher, MEMSIZE );
    1566     d->Ring = 0;
    1567     d->Zaehler = 0;
    1568     d->pos = bitPos & 31;
    1569     d->WordsRead = fpos;
    1570     d->dword = SWAP(d->Speicher[0]);
    1571     d->next = SWAP(d->Speicher[1]);
    1572     d->SeekTable_Step = 1;
     1228    d->dword = d->Speicher[d->Zaehler = 0];
    15731229
    15741230    return TRUE;
     
    15851241    f_seek(d, (bitpos >> 5) * 4 + d->MPCHeaderPos);
    15861242    f_read_dword(d, d->Speicher, 2);
    1587     d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
     1243    d->dword = d->Speicher[d->Zaehler = 0];
    15881244    d->pos = bitpos & 31;
    15891245}
     1246#endif
    15901247
    15911248static void
     
    15941251    f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
    15951252    f_read_dword(d, d->Speicher, MEMSIZE);
    1596     d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
     1253    d->dword = d->Speicher[d->Zaehler = 0];
    15971254    d->pos = bitpos & 31;
    15981255}
    15991256
     1257#if 0
    16001258static void
    16011259helper3(mpc_decoder *d, mpc_uint32_t bitpos, mpc_uint32_t* buffoffs)
     
    16081266        f_read_dword(d, d->Speicher, MEMSIZE );
    16091267    }
    1610     d->dword = SWAP(d->Speicher[d->Zaehler = bitpos - *buffoffs ]);
     1268    d->dword = d->Speicher[d->Zaehler = bitpos - *buffoffs ];
    16111269}
    16121270#endif
    1613 
    1614 // jumps over the current frame
    1615 mpc_uint32_t mpc_decoder_jump_frame(mpc_decoder *d) {
    1616 
    1617     mpc_uint32_t frameSize;
    1618 
    1619     // ensure the buffer is full
    1620     mpc_decoder_update_buffer(d);
    1621 
    1622     // bits in frame
    1623     frameSize = mpc_decoder_bitstream_read(d, 20);
    1624 
    1625     // jump forward
    1626     mpc_decoder_seek_forward(d, frameSize);
    1627 
    1628     return frameSize + 20;
    1629 
    1630 }
    16311271
    16321272static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion)
     
    16491289}
    16501290
    1651 void mpc_decoder_reset_state(mpc_decoder *d) {
    1652 
    1653     memset(d->Y_L             , 0, sizeof Y_L              );
    1654     memset(d->Y_R             , 0, sizeof Y_R              );
    1655 #ifdef SCF_HACK
    1656     memset(d->SCF_Index_L     , -128, sizeof d->SCF_Index_L   );
    1657     memset(d->SCF_Index_R     , -128, sizeof d->SCF_Index_R   );
    1658 #else
     1291mpc_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);
     1298
     1299    memset(d->Y_L          , 0, sizeof d->Y_L           );
     1300    memset(d->Y_R          , 0, sizeof d->Y_R           );
    16591301    memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
    16601302    memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
    1661 #endif
    16621303    memset(d->Res_L           , 0, sizeof d->Res_L            );
    16631304    memset(d->Res_R           , 0, sizeof d->Res_R            );
     
    16731314    memset(d->MS_Flag         , 0, sizeof d->MS_Flag          );
    16741315
    1675 }
    1676 
    1677 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample)
    1678 {
    1679     mpc_uint32_t fpos = 0;        // the bit to seek to
    1680     mpc_uint32_t seekFrame = 0;   // the frame to seek to
    1681     mpc_uint32_t lastFrame = 0;   // last frame to seek to before scanning scale factors
    1682     mpc_int32_t  delta = 0;       // direction of seek
    1683    
    1684     destsample += MPC_DECODER_SYNTH_DELAY;
    1685     seekFrame = (mpc_uint32_t) ((destsample) / MPC_FRAME_LENGTH);
    1686     d->samples_to_skip = (mpc_uint32_t)((destsample) % MPC_FRAME_LENGTH);
     1316    // resetting synthesis filter to avoid "clicks"
     1317    mpc_decoder_reset_synthesis(d);
    16871318
    16881319    // prevent from desired position out of allowed range
    1689     seekFrame = seekFrame < d->OverallFrames  ?  seekFrame  :  d->OverallFrames;
    1690 
    1691     // seek direction (note: avoids casting to int64)
    1692     delta = (d->DecodedFrames > seekFrame ? -(mpc_int32_t)(d->DecodedFrames - seekFrame) : (mpc_int32_t)(seekFrame - d->DecodedFrames));
    1693 
    1694     // update max decoded frames
    1695     if (d->DecodedFrames > d->MaxDecodedFrames)
    1696         d->MaxDecodedFrames = d->DecodedFrames;
    1697 
    1698     if (seekFrame > 33)
    1699         lastFrame = seekFrame - 33 + 1 - d->SeekTable_Step;
    1700 
    1701     if ((!d->Use_SeekTable && delta < 0) || d->MaxDecodedFrames == 0) {
    1702 
    1703         mpc_decoder_reset_state(d);
    1704 
    1705         // starts from the beginning since no frames have been decoded yet, or not using seek table
    1706         fpos = get_initial_fpos(d, d->StreamVersion);
    1707 
    1708         // seek to the first frame
    1709         mpc_decoder_seek_to(d, fpos);
     1320    fwd = fwd < d->OverallFrames  ?  fwd  :  d->OverallFrames;
    17101321
    17111322    // reset number of decoded frames
    17121323    d->DecodedFrames = 0;
    17131324
    1714         if (d->Use_SeekTable) {
    1715             // jump to the last frame, updating seek table
    1716             if (d->SeekTable_Step == 1) {
    1717                 d->SeekTable[0] = (mpc_uint32_t)fpos;
    1718                 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++)
    1719                     d->SeekTable[d->DecodedFrames+1] = mpc_decoder_jump_frame(d);
    1720             } else {
    1721                 d->SeekTableIndex = 0;
    1722                 d->SeekTableCounter = (mpc_uint32_t)fpos;
    1723                 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) {
    1724                     if (d->DecodedFrames % d->SeekTable_Step == 0) {
    1725                         d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter;
    1726                         d->SeekTableIndex += 1;
    1727                         d->SeekTableCounter = 0;
    1728     }
    1729                     d->SeekTableCounter += mpc_decoder_jump_frame(d);
    1730                 }
    1731             }
    1732         } else {
    1733             // just jump to the last frame
    1734             for (;d->DecodedFrames < lastFrame; d->DecodedFrames++)
    1735                 mpc_decoder_jump_frame(d);
    1736         }
    1737 
    1738     } else if (delta < 0) {
    1739 
    1740         mpc_decoder_reset_state(d);
    1741 
    1742         // jumps backwards using the seek table
    1743         fpos = d->SeekTable[0];
    1744         if (d->SeekTable_Step == 1) {
    1745             for (d->DecodedFrames = 0;d->DecodedFrames < lastFrame; d->DecodedFrames++)
    1746                 fpos += d->SeekTable[d->DecodedFrames+1];
    1747         } else {
    1748             d->SeekTableIndex = 0;
    1749             //d->SeekTableCounter = 0;
    1750             for (d->DecodedFrames = 0;d->DecodedFrames < lastFrame; d->DecodedFrames+=d->SeekTable_Step, d->SeekTableIndex++)
    1751                 fpos += d->SeekTable[d->SeekTableIndex+1];
    1752             d->SeekTableCounter = d->SeekTable[d->SeekTableIndex];
    1753         }
    1754         mpc_decoder_seek_to(d, fpos);
    1755 
    1756     } else if (delta > 33) {
    1757 
    1758         mpc_decoder_reset_state(d);
    1759 
    1760         // jumps forward from the current position
    1761         if (d->Use_SeekTable) {
    1762 
    1763             if (d->MaxDecodedFrames > lastFrame) { // REVIEW: Correct?? or (d->MaxDecodedFrames > d->DecodedFrames)
    1764                 // jump to the last usable position in the seek table
    1765                 if (d->SeekTable_Step == 1) {
    1766                     fpos = mpc_decoder_bits_read(d);
    1767                     for (; d->DecodedFrames < d->MaxDecodedFrames && d->DecodedFrames < lastFrame; d->DecodedFrames++)
    1768                         fpos += d->SeekTable[d->DecodedFrames+1];
    1769                 } else {
    1770                     // could test SeekTable offset and jump to next entry but this is easier for now...
    1771                     //d->SeekTableIndex = 0;
    1772                     //d->SeekTableCounter = 0;
    1773                     fpos = d->SeekTable[0];
    1774                     d->SeekTableIndex = 0;
    1775                     for (d->DecodedFrames = 0;d->DecodedFrames < d->MaxDecodedFrames && d->DecodedFrames < lastFrame; d->DecodedFrames+=d->SeekTable_Step, d->SeekTableIndex++)
    1776                         fpos += d->SeekTable[d->SeekTableIndex+1];
    1777                     d->SeekTableCounter = d->SeekTable[d->SeekTableIndex];
    1778                 }
    1779 
    1780                 mpc_decoder_seek_to(d, fpos);
    1781             }
    1782             if (d->SeekTable_Step == 1) {
    1783                 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++)
    1784                     d->SeekTable[d->DecodedFrames+1] = mpc_decoder_jump_frame(d);
    1785             } else {
    1786                 for (;d->DecodedFrames < lastFrame; d->DecodedFrames++) {
    1787                     if (d->DecodedFrames % d->SeekTable_Step == 0) {
    1788                         d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter;
    1789                         d->SeekTableIndex += 1;
    1790                         d->SeekTableCounter = 0;
    1791                     }
    1792                     d->SeekTableCounter += mpc_decoder_jump_frame(d);
    1793                 }
    1794             }
    1795         } else {
    1796             for (;d->DecodedFrames < lastFrame; d->DecodedFrames++)
    1797                 mpc_decoder_jump_frame(d);
    1798         }
    1799 
    1800     }
    1801 
    1802     // REVIEW: Needed?
    1803     mpc_decoder_update_buffer(d);
    1804 
    1805     for (;d->DecodedFrames < seekFrame; d->DecodedFrames++) {
    1806 
     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++ ) {
    18071334        mpc_uint32_t   FrameBitCnt;
    1808 
     1335        mpc_uint32_t   RING;
     1336        RING         = d->Zaehler;
    18091337        d->FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);    // read jump-info
    18101338        d->ActDecodePos = (d->Zaehler << 5) + d->pos;
    1811         FrameBitCnt  = mpc_decoder_bits_read(d); 
    1812         // scanning the scalefactors (and check for validity of frame)
     1339        FrameBitCnt  = mpc_decoder_bits_read(d);  // scanning the scalefactors and check for validity of frame
    18131340        if (d->StreamVersion >= 7)  {
    1814             mpc_decoder_read_bitstream_sv7(d, d->Use_FastSeek && (d->DecodedFrames < seekFrame - 1));
     1341            mpc_decoder_read_bitstream_sv7(d);
    18151342        }
    18161343        else {
     
    18211348#endif
    18221349        }
    1823 
    1824         FrameBitCnt = mpc_decoder_bits_read(d) - FrameBitCnt;
    1825 
    1826         if (d->Use_FastSeek && d->FwdJumpInfo > FrameBitCnt)
    1827             mpc_decoder_seek_forward(d, d->FwdJumpInfo - FrameBitCnt);
    1828         else if (FrameBitCnt != d->FwdJumpInfo )
    1829             // Bug in perform_jump;
     1350        if (mpc_decoder_bits_read(d) - FrameBitCnt != d->FwdJumpInfo ) {
     1351            // Box ("Bug in perform_jump");
    18301352            return FALSE;
    1831 
    1832         // REVIEW: Only if decodedFrames < maxDecodedFrames??
    1833         if (d->Use_SeekTable) {
    1834             if (d->SeekTable_Step == 1) {
    1835                 // check that the frame length corresponds with any data already in the seek table
    1836                 if (d->SeekTable[d->DecodedFrames+1] != 0 && d->SeekTable[d->DecodedFrames+1] != d->FwdJumpInfo + 20)
    1837                     return FALSE;
    1838                 d->SeekTable [d->DecodedFrames+1] = d->FwdJumpInfo + 20;
    1839             } else {
    1840                 if (d->DecodedFrames % d->SeekTable_Step == 0) {
    1841                     if (d->SeekTable[d->SeekTableIndex] != 0 && d->SeekTable[d->SeekTableIndex] != d->SeekTableCounter)
    1842                         return FALSE;
    1843                     d->SeekTable[d->SeekTableIndex] = d->SeekTableCounter;
    1844                     d->SeekTableIndex += 1;
    1845                     d->SeekTableCounter = 0;
    1846         }
    1847                 d->SeekTableCounter += d->FwdJumpInfo + 20;
    1848             }
    1849         }
    1850 
     1353        }
    18511354        // update buffer
    1852         mpc_decoder_update_buffer(d);
    1853 
    1854         if (d->DecodedFrames == seekFrame - 1) {
    1855            
    1856             // initialize the synth correctly for perfect decoding
    1857             mpc_decoder_requantisierung(d, d->Max_Band);
    1858             mpc_decoder_synthese_filter_float(d, NULL);
    1859 
    1860         }
    1861        
    1862     }
     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;
    18631362
    18641363    return TRUE;
    18651364}
    18661365
    1867 
    1868 void mpc_decoder_fill_buffer(mpc_decoder *d) {
    1869 
    1870     f_read_dword(d, d->Speicher, MEMSIZE);
    1871     d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
    1872     d->next = SWAP(d->Speicher[1]);
    1873     d->Ring = 0;
    1874 
    1875 }
    1876 
    1877 
    1878 void mpc_decoder_update_buffer(mpc_decoder *d)
    1879 {
    1880     if ((d->Ring ^ d->Zaehler) & MEMSIZE2) {
     1366void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)
     1367{
     1368    if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
    18811369        // update buffer
    1882         f_read_dword(d, d->Speicher + (d->Ring & MEMSIZE2), MEMSIZE2);
    1883         d->Ring = d->Zaehler;
    1884     }
    1885 }
    1886 
    1887 
    1888 void mpc_decoder_seek_to(mpc_decoder *d, mpc_uint32_t bitPos) {
    1889 
    1890     // required dword
    1891     mpc_uint32_t fpos = (bitPos >> 5);
    1892     mpc_uint32_t bufferStart = d->WordsRead - d->Zaehler;
    1893     if ((d->Zaehler & MEMSIZE2) != FALSE)
    1894         bufferStart += MEMSIZE2;
    1895 
    1896     if (fpos >= bufferStart && fpos < bufferStart + MEMSIZE) {
    1897 
    1898         // required position is within the buffer, no need to seek
    1899         d->Zaehler = (fpos - bufferStart + ((d->Zaehler & MEMSIZE2) != FALSE ? MEMSIZE2 : 0)) & MEMMASK;
    1900         d->pos = bitPos & 31;
    1901         d->WordsRead = fpos;
    1902         d->dword = SWAP(d->Speicher[d->Zaehler]);
    1903         d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]);
    1904 
    1905         mpc_decoder_update_buffer(d);
    1906        
    1907 
    1908     } else {
    1909 
    1910         // DWORD aligned
    1911         f_seek(d, fpos*4 + d->MPCHeaderPos);
    1912         d->Zaehler = 0;
    1913         d->pos = bitPos & 31;
    1914         d->WordsRead = fpos;
    1915 
    1916         mpc_decoder_fill_buffer(d);
    1917 
    1918     }
    1919    
    1920 }
    1921 
    1922 
    1923 void mpc_decoder_seek_forward(mpc_decoder *d, mpc_uint32_t bits) {
    1924 
    1925     bits += d->pos;
    1926     d->pos = bits & 31;
    1927     bits = bits >> 5; // to DWORDs
    1928     d->Zaehler = (d->Zaehler + bits) & MEMMASK;
    1929     d->dword = SWAP(d->Speicher[d->Zaehler]);
    1930     d->next = SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]);
    1931     d->WordsRead += bits;
    1932 
    1933 }
    1934 
    1935 
    1936 void mpc_decoder_set_seek_table(mpc_decoder *d, mpc_uint32_t *seek_table, mpc_uint32_t max_table_size) {
    1937 
    1938     d->Use_StaticSeekTable = TRUE;
    1939     d->SeekTable = seek_table;
    1940     d->Max_SeekTable_Size = max_table_size;
    1941 
    1942 }
     1370        f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
     1371    }
     1372}
     1373
     1374
Note: See TracChangeset for help on using the changeset viewer.