Ignore:
Timestamp:
09/16/06 16:55:23 (18 years ago)
Author:
zorg
Message:

Merge remaining interesting bits from rockbox svn including seeking code

File:
1 edited

Legend:

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

    r18 r21  
    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
     78mpc_uint8_t     LUT1_0  [1<< 6];
     79mpc_uint8_t     LUT1_1  [1<< 9];            //  576 Bytes
     80mpc_uint8_t     LUT2_0  [1<< 7];
     81mpc_uint8_t     LUT2_1  [1<<10];            // 1152 Bytes
     82mpc_uint8_t     LUT3_0  [1<< 4];
     83mpc_uint8_t     LUT3_1  [1<< 5];            //   48 Bytes
     84mpc_uint8_t     LUT4_0  [1<< 4];
     85mpc_uint8_t     LUT4_1  [1<< 5];            //   48 Bytes
     86mpc_uint8_t     LUT5_0  [1<< 6];
     87mpc_uint8_t     LUT5_1  [1<< 8];            //  320 Bytes
     88mpc_uint8_t     LUT6_0  [1<< 7];
     89mpc_uint8_t     LUT6_1  [1<< 7];            //  256 Bytes
     90mpc_uint8_t     LUT7_0  [1<< 8];mpc_uint8_t     LUT7_1  [1<< 8];            //  512 Bytes
     91mpc_uint8_t     LUTDSCF [1<< 6];            //   64 Bytes = 2976 Bytes
     92
    6193//------------------------------------------------------------------------------
    6294// types
     
    76108//------------------------------------------------------------------------------
    77109void mpc_decoder_read_bitstream_sv6(mpc_decoder *d);
    78 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d);
    79 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING);
     110void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t fastSeeking);
     111void mpc_decoder_update_buffer(mpc_decoder *d);
    80112mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample);
    81113void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
     114void mpc_decoder_seek_to(mpc_decoder *d, mpc_uint32_t bitPos);
     115void mpc_decoder_seek_forward(mpc_decoder *d, mpc_uint32_t bits);
     116mpc_uint32_t mpc_decoder_jump_frame(mpc_decoder *d);
     117void mpc_decoder_fill_buffer(mpc_decoder *d);
     118void mpc_decoder_reset_state(mpc_decoder *d);
     119static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion);
     120static __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);
     121static void mpc_move_next(mpc_decoder *d);
     122
     123mpc_uint32_t  Speicher[MPC_DECODER_MEMSIZE];
     124MPC_SAMPLE_FORMAT Y_L[36][32];
     125MPC_SAMPLE_FORMAT Y_R[36][32];
    82126
    83127//------------------------------------------------------------------------------
     
    129173{
    130174    d->dword = 0;
     175    d->next = 0;
    131176    d->pos = 0;
    132177    d->Zaehler = 0;
     
    141186}
    142187
     188static 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
    143196// read desired number of bits out of the bitstream
    144 static mpc_uint32_t
     197static __inline mpc_uint32_t
    145198mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits)
    146199{
     
    153206    }
    154207    else {
    155         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
    156         d->pos -= 32;
     208        mpc_move_next(d);
    157209        if (d->pos) {
    158210            out <<= d->pos;
    159211            out |= d->dword >> (32 - d->pos);
    160212        }
    161         ++(d->WordsRead);
    162213    }
    163214
    164215    return out & mask[bits];
     216}
     217
     218static void
     219mpc_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;
    165234}
    166235
     
    173242    // load preview and decode
    174243    mpc_uint32_t code  = d->dword << d->pos;
     244
    175245    if (d->pos > 26) {
    176         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
     246        code |= d->next >> (32 - d->pos);
    177247    }
    178248    while (code < Table->Code) {
     
    182252    // set the new position within bitstream without performing a dummy-read
    183253    if ((d->pos += Table->Length) >= 32) {
    184         d->pos -= 32;
    185         d->dword = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK];
    186         ++(d->WordsRead);
     254        mpc_move_next(d);
    187255    }
    188256
     
    198266    // load preview and decode
    199267    mpc_uint32_t code = d->dword << d->pos;
     268   
    200269    if (d->pos > 18) {
    201         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
     270        code |= d->next >> (32 - d->pos);
    202271    }
    203272    while (code < Table->Code) {
     
    207276    // set the new position within bitstream without performing a dummy-read
    208277    if ((d->pos += Table->Length) >= 32) {
    209         d->pos -= 32;
    210         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
    211         ++(d->WordsRead);
     278        mpc_move_next(d);
    212279    }
    213280
     
    222289    // load preview and decode
    223290    mpc_uint32_t code  = d->dword << d->pos;
     291   
    224292    if (d->pos > 22) {
    225         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
     293        code |= d->next >> (32 - d->pos);
    226294    }
    227295    while (code < Table->Code) {
     
    231299    // set the new position within bitstream without performing a dummy-read
    232300    if ((d->pos += Table->Length) >= 32) {
    233         d->pos -= 32;
    234         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
    235         ++(d->WordsRead);
     301        mpc_move_next(d);
    236302    }
    237303
     
    246312    // load preview and decode
    247313    mpc_uint32_t code  = d->dword << d->pos;
     314   
    248315    if (d->pos > 27) {
    249         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
     316        code |= d->next >> (32 - d->pos);
    250317    }
    251318    while (code < Table->Code) {
     
    255322    // set the new position within bitstream without performing a dummy-read
    256323    if ((d->pos += Table->Length) >= 32) {
    257         d->pos -= 32;
    258         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
    259         ++(d->WordsRead);
     324        mpc_move_next(d);
     325    }
     326
     327    return Table->Value;
     328}
     329
     330/* partial lookup table decode */
     331static mpc_int32_t
     332mpc_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 */
     356static __inline mpc_int32_t
     357mpc_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);
    260371    }
    261372
     
    279390mpc_decoder_reset_y(mpc_decoder *d)
    280391{
    281     memset(d->Y_L, 0, sizeof d->Y_L);
    282     memset(d->Y_R, 0, sizeof d->Y_R);
     392    memset(d->Y_L, 0, sizeof Y_L);
     393    memset(d->Y_R, 0, sizeof Y_R);
    283394}
    284395
     
    289400
    290401    d->DecodedFrames  = 0;
     402    d->SeekTableIndex = 0;
     403    d->MaxDecodedFrames = 0;
    291404    d->StreamVersion  = 0;
    292405    d->MS_used        = 0;
    293406
    294     memset(d->Y_L          , 0, sizeof d->Y_L           );
    295     memset(d->Y_R          , 0, sizeof d->Y_R           );
     407    memset(d->Y_L          , 0, sizeof Y_L           );
     408    memset(d->Y_R          , 0, sizeof Y_R           );
    296409    memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
    297410    memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
     
    314427                         mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer)
    315428{
    316   unsigned int i;
    317429  mpc_decoder_reset_bitstream_decode(d);
    318   if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher);
     430  if (in_len > sizeof(Speicher)) in_len = sizeof(Speicher);
    319431  memcpy(d->Speicher, in_buffer, in_len);
    320 #ifdef MPC_LITTLE_ENDIAN
    321   for (i = 0; i < (in_len + 3) / 4; i++)
    322     d->Speicher[i] = mpc_swap32(d->Speicher[i]);
    323 #endif
    324   d->dword = d->Speicher[0];
     432  d->dword = SWAP(d->Speicher[0]);
     433  d->next = SWAP(d->Speicher[1]);
    325434  switch (d->StreamVersion) {
    326435#ifdef MPC_SUPPORT_SV456
     
    333442    case 0x07:
    334443    case 0x17:
    335         mpc_decoder_read_bitstream_sv7(d);
     444        mpc_decoder_read_bitstream_sv7(d, FALSE);
    336445        break;
    337446    default:
     
    350459    mpc_uint32_t  FrameBitCnt = 0;
    351460
     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   
    352476    if (d->DecodedFrames >= d->OverallFrames) {
    353477        return (mpc_uint32_t)(-1);                           // end of file -> abort decoding
    354478    }
     479
     480    if (d->DecodedFrames == 0 && d->Use_SeekTable)
     481        d->SeekTable[0] = mpc_decoder_bits_read(d);
    355482
    356483    // read jump-info for validity check of frame
     
    371498    case 0x07:
    372499    case 0x17:
    373         mpc_decoder_read_bitstream_sv7(d);
     500        mpc_decoder_read_bitstream_sv7(d, FALSE);
    374501        break;
    375502    default:
     
    378505    d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;
    379506
     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
    380522    // synthesize signal
    381523    mpc_decoder_requantisierung(d, d->Max_Band);
     
    385527
    386528    mpc_decoder_synthese_filter_float(d, buffer);
    387 
    388     d->DecodedFrames++;
    389529
    390530    // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
     
    402542        // additional FilterDecay samples are needed for decay of synthesis filter
    403543        if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
    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         }
     544            // this variable will be checked for at the top of the function
     545            d->last_block_samples = FilterDecay;
     546            }
    416547        else {                              // there are only FilterDecay samples needed for this frame
    417548            output_frame_length = FilterDecay;
     
    428559            memmove(
    429560                buffer,
    430                 buffer + d->samples_to_skip * 2,
    431                 output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT));
     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));
    432567            d->samples_to_skip = 0;
    433568        }
     
    472607
    473608        }
    474         mpc_decoder_update_buffer(d, RING);
     609        mpc_decoder_update_buffer(d);
    475610
    476611        if (valid_samples > 0) {
     
    683818    const HuffmanTyp *x1;
    684819    const HuffmanTyp *x2;
    685     mpc_int8_t  *L;
    686     mpc_int8_t  *R;
     820    mpc_int8_t *L;
     821    mpc_int8_t *R;
    687822    mpc_int16_t *QL;
    688823    mpc_int16_t *QR;
     
    8871022/****************************************** SV 7 ******************************************/
    8881023void
    889 mpc_decoder_read_bitstream_sv7(mpc_decoder *d)
     1024mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t fastSeeking)
    8901025{
    8911026    // these arrays hold decoding results for bundled quantizers (3- and 5-step)
     
    8961031    /*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};
    8971032
    898     mpc_int32_t n, k;
     1033    mpc_int32_t n,k;
    8991034    mpc_int32_t Max_used_Band=0;
    9001035    const HuffmanTyp *Table;
    901     mpc_int32_t  idx;
    902     mpc_int8_t   *L, *R;
    903     mpc_int16_t  *LQ, *RQ;
    904     mpc_int8_t   *ResL, *ResR;
     1036    mpc_int32_t idx;
     1037    mpc_int8_t *L   ,*R;
     1038    mpc_int16_t *LQ   ,*RQ;
     1039    mpc_int8_t *ResL,*ResR;
    9051040    mpc_uint32_t tmp;
     1041    mpc_uint8_t *LUT;
     1042    mpc_uint8_t max_length;
    9061043
    9071044    /***************************** Header *****************************/
     
    9141051    if (d->MS_used && !(*ResL==0 && *ResR==0)) {
    9151052        d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1);
     1053    } else {
     1054        d->MS_Flag[0] = 0;
    9161055    }
    9171056
     
    9331072        if (*ResL!=0 || *ResR!=0) {
    9341073            Max_used_Band = n;
     1074        } else {
     1075            d->MS_Flag[n] = 0;
    9351076        }
    9361077    }
     
    9571098            {
    9581099            case 1:
    959                 idx  = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);
    960                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
    961                 idx  = mpc_decoder_huffman_decode_fast(d, mpc_table_HuffDSCF);
    962                 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    9631104                L[2] = L[1];
    9641105                break;
    9651106            case 3:
    966                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    967                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     1107                idx = Decode_DSCF ();
     1108                L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    9681109                L[1] = L[0];
    9691110                L[2] = L[1];
    9701111                break;
    9711112            case 2:
    972                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    973                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     1113                idx = Decode_DSCF ();
     1114                L[0] = (idx!=8) ? SCF_DIFF(L[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    9741115                L[1] = L[0];
    975                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    976                 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     1116                idx = Decode_DSCF ();
     1117                L[2] = (idx!=8) ? SCF_DIFF(L[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    9771118                break;
    9781119            case 0:
    979                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    980                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
    981                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    982                 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
    983                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    984                 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    9851126                break;
    9861127            default:
     
    9881129                break;
    9891130            }
    990             // update Reference for DSCF
    991             //d->DSCF_Reference_L[n] = L[2];
    9921131        }
    9931132        if (*ResR)
    9941133        {
    995             //R[2] = d->DSCF_Reference_R[n];
    9961134            switch (d->SCFI_R[n])
    9971135            {
    9981136            case 1:
    999                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1000                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
    1001                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1002                 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    10031141                R[2] = R[1];
    10041142                break;
    10051143            case 3:
    1006                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1007                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     1144                idx = Decode_DSCF ();
     1145                R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    10081146                R[1] = R[0];
    10091147                R[2] = R[1];
    10101148                break;
    10111149            case 2:
    1012                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1013                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     1150                idx = Decode_DSCF ();
     1151                R[0] = (idx!=8) ? SCF_DIFF(R[2], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    10141152                R[1] = R[0];
    1015                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1016                 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     1153                idx = Decode_DSCF ();
     1154                R[2] = (idx!=8) ? SCF_DIFF(R[1], idx) : (int) mpc_decoder_bitstream_read(d, 6);
    10171155                break;
    10181156            case 0:
    1019                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1020                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
    1021                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1022                 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
    1023                 idx  = mpc_decoder_huffman_decode_fast(d,  mpc_table_HuffDSCF);
    1024                 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
     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);
    10251163                break;
    10261164            default:
     
    10281166                break;
    10291167            }
    1030             // update Reference for DSCF
    1031             //d->DSCF_Reference_R[n] = R[2];
    1032         }
    1033     }
     1168        }
     1169    }
     1170
     1171    if (fastSeeking)
     1172        return;
     1173
    10341174    /***************************** Samples ****************************/
    10351175    ResL = d->Res_L;
    10361176    ResR = d->Res_R;
    1037     LQ   = d->Q[0].L;
    1038     RQ   = d->Q[0].R;
     1177    LQ    = d->Q[0].L;
     1178    RQ    = d->Q[0].R;
    10391179    for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, LQ+=36, RQ+=36)
    10401180    {
     
    10561196            break;
    10571197        case 1:
    1058             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][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            }
    10591207            for (k=0; k<12; ++k)
    10601208            {
    1061                 idx = mpc_decoder_huffman_decode_fast(d,  Table);
     1209                idx  = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    10621210                *LQ++ = idx30[idx];
    10631211                *LQ++ = idx31[idx];
     
    10661214            break;
    10671215        case 2:
    1068             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][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            }
    10691225            for (k=0; k<18; ++k)
    10701226            {
    1071                 idx = mpc_decoder_huffman_decode_fast(d,  Table);
     1227                idx  = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    10721228                *LQ++ = idx50[idx];
    10731229                *LQ++ = idx51[idx];
     
    10751231            break;
    10761232        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            }
     1242            for (k=0; k<36; ++k)
     1243                *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1244            break;
    10771245        case 4:
    1078             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
     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            }
    10791255            for (k=0; k<36; ++k)
    1080                 *LQ++ = mpc_decoder_huffman_decode_faster(d, Table);
     1256                *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    10811257            break;
    10821258        case 5:
    1083             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
     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            }
    10841268            for (k=0; k<36; ++k)
    1085                 *LQ++ = mpc_decoder_huffman_decode_fast(d, Table);
     1269                *LQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    10861270            break;
    10871271        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;
    10881286        case 7:
    1089             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
     1287            if (mpc_decoder_bitstream_read(d, 1)) {
     1288                Table = mpc_table_HuffQ[1][7];
     1289                LUT = LUT7_1;
     1290                max_length = 8;
    10901291            for (k=0; k<36; ++k)
    1091                 *LQ++ = mpc_decoder_huffman_decode(d, Table);
     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            }           
    10921300            break;
    10931301        case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
    10941302            tmp = Dc[*ResL];
    10951303            for (k=0; k<36; ++k)
    1096                 *LQ++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
     1304                *LQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
    10971305            break;
    10981306        default:
     
    11161324                break;
    11171325            case 1:
    1118                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][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                }
    11191335                for (k=0; k<12; ++k)
    11201336                {
    1121                     idx = mpc_decoder_huffman_decode_fast(d, Table);
     1337                    idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    11221338                    *RQ++ = idx30[idx];
    11231339                    *RQ++ = idx31[idx];
     
    11261342                break;
    11271343            case 2:
    1128                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][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                }
    11291353                for (k=0; k<18; ++k)
    11301354                {
    1131                     idx = mpc_decoder_huffman_decode_fast(d, Table);
     1355                    idx = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    11321356                    *RQ++ = idx50[idx];
    11331357                    *RQ++ = idx51[idx];
     
    11351359                break;
    11361360            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                }
     1370                for (k=0; k<36; ++k)
     1371                    *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
     1372                break;
    11371373            case 4:
    1138                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
     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                }
    11391383                for (k=0; k<36; ++k)
    1140                     *RQ++ = mpc_decoder_huffman_decode_faster(d, Table);
     1384                    *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    11411385                break;
    11421386            case 5:
    1143                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
     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                }
    11441396                for (k=0; k<36; ++k)
    1145                     *RQ++ = mpc_decoder_huffman_decode_fast(d, Table);
     1397                    *RQ++ = HUFFMAN_DECODE_FASTEST ( d, Table, LUT, max_length );
    11461398                break;
    11471399            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;
    11481414            case 7:
    1149                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
     1415            if (mpc_decoder_bitstream_read(d, 1)) {
     1416                Table = mpc_table_HuffQ[1][7];
     1417                LUT = LUT7_1;
     1418                max_length = 8;
    11501419                for (k=0; k<36; ++k)
    1151                     *RQ++ = mpc_decoder_huffman_decode(d, Table);
     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            }           
    11521428                break;
    11531429            case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
    11541430                tmp = Dc[*ResR];
    11551431                for (k=0; k<36; ++k)
    1156                     *RQ++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
     1432                    *RQ++ = (mpc_int16_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
    11571433                break;
    11581434            default:
     
    11741450  d->OverallFrames = 0;
    11751451  d->DecodedFrames = 0;
     1452  d->MaxDecodedFrames = 0;
    11761453  d->TrueGaplessPresent = 0;
     1454  d->last_block_samples = 0;
    11771455  d->WordsRead = 0;
    11781456  d->Max_Band = 0;
     
    11841462  d->pos = 0;
    11851463  d->Zaehler = 0;
     1464  d->Ring = 0;
    11861465  d->WordsRead = 0;
    11871466  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;
    11881475
    11891476  mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f);
     
    11921479  mpc_decoder_init_huffman_sv7(d);
    11931480#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
     1504void mpc_decoder_destroy(mpc_decoder *d) {
     1505
     1506    if (d->SeekTable != NULL && d->Use_StaticSeekTable == FALSE)
     1507        free(d->SeekTable);
     1508
    11941509}
    11951510
    11961511void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
    11971512{
     1513    mpc_uint16_t seekTableSize;
     1514
    11981515    mpc_decoder_reset_synthesis(d);
    11991516    mpc_decoder_reset_globals(d);
     
    12081525
    12091526    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
    12101550}
    12111551
    12121552mpc_bool_t mpc_decoder_initialize(mpc_decoder *d, mpc_streaminfo *si)
    12131553{
     1554    mpc_uint32_t bitPos;
     1555    mpc_uint32_t fpos;
     1556
    12141557    mpc_decoder_set_streaminfo(d, si);
    12151558
    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
     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);
    12271565    f_read_dword(d, d->Speicher, MEMSIZE );
    1228     d->dword = d->Speicher[d->Zaehler = 0];
     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;
    12291573
    12301574    return TRUE;
     
    12411585    f_seek(d, (bitpos >> 5) * 4 + d->MPCHeaderPos);
    12421586    f_read_dword(d, d->Speicher, 2);
    1243     d->dword = d->Speicher[d->Zaehler = 0];
     1587    d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
    12441588    d->pos = bitpos & 31;
    12451589}
    1246 #endif
    12471590
    12481591static void
     
    12511594    f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
    12521595    f_read_dword(d, d->Speicher, MEMSIZE);
    1253     d->dword = d->Speicher[d->Zaehler = 0];
     1596    d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
    12541597    d->pos = bitpos & 31;
    12551598}
    12561599
    1257 #if 0
    12581600static void
    12591601helper3(mpc_decoder *d, mpc_uint32_t bitpos, mpc_uint32_t* buffoffs)
     
    12661608        f_read_dword(d, d->Speicher, MEMSIZE );
    12671609    }
    1268     d->dword = d->Speicher[d->Zaehler = bitpos - *buffoffs ];
     1610    d->dword = SWAP(d->Speicher[d->Zaehler = bitpos - *buffoffs ]);
    12691611}
    12701612#endif
     1613
     1614// jumps over the current frame
     1615mpc_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}
    12711631
    12721632static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion)
     
    12891649}
    12901650
    1291 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample)
    1292 {
    1293     mpc_uint32_t fpos;
    1294     mpc_uint32_t fwd;
    1295 
    1296     fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH);
    1297     d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH);
    1298 
    1299     memset(d->Y_L          , 0, sizeof d->Y_L           );
    1300     memset(d->Y_R          , 0, sizeof d->Y_R           );
     1651void 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
    13011659    memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
    13021660    memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
     1661#endif
    13031662    memset(d->Res_L           , 0, sizeof d->Res_L            );
    13041663    memset(d->Res_R           , 0, sizeof d->Res_R            );
     
    13141673    memset(d->MS_Flag         , 0, sizeof d->MS_Flag          );
    13151674
    1316     // resetting synthesis filter to avoid "clicks"
    1317     mpc_decoder_reset_synthesis(d);
     1675}
     1676
     1677mpc_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);
    13181687
    13191688    // prevent from desired position out of allowed range
    1320     fwd = fwd < d->OverallFrames  ?  fwd  :  d->OverallFrames;
     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);
    13211710
    13221711    // reset number of decoded frames
    13231712    d->DecodedFrames = 0;
    13241713
    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++ ) {
     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
    13341807        mpc_uint32_t   FrameBitCnt;
    1335         mpc_uint32_t   RING;
    1336         RING         = d->Zaehler;
     1808
    13371809        d->FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);    // read jump-info
    13381810        d->ActDecodePos = (d->Zaehler << 5) + d->pos;
    1339         FrameBitCnt  = mpc_decoder_bits_read(d);  // scanning the scalefactors and check for validity of frame
     1811        FrameBitCnt  = mpc_decoder_bits_read(d); 
     1812        // scanning the scalefactors (and check for validity of frame)
    13401813        if (d->StreamVersion >= 7)  {
    1341             mpc_decoder_read_bitstream_sv7(d);
     1814            mpc_decoder_read_bitstream_sv7(d, d->Use_FastSeek && (d->DecodedFrames < seekFrame - 1));
    13421815        }
    13431816        else {
     
    13481821#endif
    13491822        }
    1350         if (mpc_decoder_bits_read(d) - FrameBitCnt != d->FwdJumpInfo ) {
    1351             // Box ("Bug in perform_jump");
     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;
    13521830            return FALSE;
    1353         }
     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
    13541851        // update buffer
    1355         if ((RING ^ d->Zaehler) & MEMSIZE2) {
    1356             f_read_dword(d, d->Speicher + (RING & MEMSIZE2),  MEMSIZE2);
    1357         }
    1358     }
    1359 
    1360     // LastBitsRead = BitsRead ();
    1361     // LastFrame = d->DecodedFrames;
     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    }
    13621863
    13631864    return TRUE;
    13641865}
    13651866
    1366 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)
    1367 {
    1368     if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
     1867
     1868void 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
     1878void mpc_decoder_update_buffer(mpc_decoder *d)
     1879{
     1880    if ((d->Ring ^ d->Zaehler) & MEMSIZE2) {
    13691881        // update buffer
    1370         f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
    1371     }
    1372 }
    1373 
    1374 
     1882        f_read_dword(d, d->Speicher + (d->Ring & MEMSIZE2), MEMSIZE2);
     1883        d->Ring = d->Zaehler;
     1884    }
     1885}
     1886
     1887
     1888void 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
     1923void 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
     1936void 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}
Note: See TracChangeset for help on using the changeset viewer.