1
2// Klemm settings
3#define OTHER_SEEK
4
5#include <stdio.h>
6#include <math.h>
7#include <string.h>
8#include <malloc.h>
9#include "minimax.h"
10#include "mpc_dec.h"
11#include "requant.h"
12#include "huffsv46.h"
13#include "huffsv7.h"
14#include "synth_filter.h"
15#include "bitstream.h"
16#include "in_mpc.h"
17
18#ifndef M_PI
19#	define M_PI             3.141592653589793238462643383276
20#endif
21
22//#include "..\cnv_mpc.h"
23
24void
25MPC_decoder::RESET_Synthesis ( void )
26{
27    Reset_V ();
28}
29
30void
31MPC_decoder::RESET_Y ( void )
32{
33    memset ( Y_L, 0, sizeof Y_L );
34    memset ( Y_R, 0, sizeof Y_R );
35}
36
37void
38MPC_decoder::RESET_Globals ( void )
39{
40    Reset_BitstreamDecode ();
41
42    DecodedFrames  = 0;
43    LastFrame      = 0;
44    LastBitsRead   = 0;
45    StreamVersion  = 0;
46    MS_used        = 0;
47    clips          = 0;
48    SectionBitrate = 0;
49    NumberOfConsecutiveBrokenFrames = 0;
50
51    memset ( Y_L             , 0, sizeof Y_L              );
52    memset ( Y_R             , 0, sizeof Y_R              );
53    memset ( SCF_Index_L     , 0, sizeof SCF_Index_L      );
54    memset ( SCF_Index_R     , 0, sizeof SCF_Index_R      );
55    memset ( Res_L           , 0, sizeof Res_L            );
56    memset ( Res_R           , 0, sizeof Res_R            );
57    memset ( SCFI_L          , 0, sizeof SCFI_L           );
58    memset ( SCFI_R          , 0, sizeof SCFI_R           );
59    memset ( DSCF_Flag_L     , 0, sizeof DSCF_Flag_L      );
60    memset ( DSCF_Flag_R     , 0, sizeof DSCF_Flag_R      );
61    memset ( DSCF_Reference_L, 0, sizeof DSCF_Reference_L );
62    memset ( DSCF_Reference_R, 0, sizeof DSCF_Reference_R );
63    memset ( Q               , 0, sizeof Q                );
64    memset ( MS_Flag         , 0, sizeof MS_Flag          );
65}
66
67void
68MPC_decoder::perform_EQ ( void )
69{
70#if 0
71    /*static*/ float  SAVE_L     [DELAY] [32];                      // buffer for ...
72    /*static*/ float  SAVE_R     [DELAY] [32];                      // ... upper subbands
73    /*static*/ float  FirSave_L  [FIR_BANDS] [EQ_TAP];              // buffer for ...
74    /*static*/ float  FirSave_R  [FIR_BANDS] [EQ_TAP];              // ... lowest subbands
75#endif
76    float         lowestSB_L [FIR_BANDS] [36];
77    float         lowestSB_R [FIR_BANDS] [36];
78    float         SWAP       [DELAY] [32];
79    int           n;
80    int           k;
81    int           i;
82    float         tmp;
83
84    for ( i = 0; i < FIR_BANDS; i++ )                           // L: delay subbands (synchronize FIR-filtered and gained subbands-samples)
85        for ( k = 0; k < 36; k++ )
86            lowestSB_L [i] [k] = Y_L [k] [i];
87
88    memcpy  ( SWAP,       SAVE_L,       DELAY        * 32 * sizeof(float) );
89    memcpy  ( SAVE_L,     Y_L+36-DELAY, DELAY        * 32 * sizeof(float) );
90    memmove ( Y_L+DELAY,  Y_L,          (36 - DELAY) * 32 * sizeof(float) );
91    memcpy  ( Y_L,        SWAP,         DELAY        * 32 * sizeof(float) );
92
93    for ( i = 0; i < FIR_BANDS; i++ )                           // R: delay subbands (synchronize FIR-filtered and gained subbands-samples)
94        for ( k = 0; k < 36; k++ )
95            lowestSB_R [i] [k] = Y_R [k] [i];
96
97    memcpy  ( SWAP,       SAVE_R,       DELAY        * 32 * sizeof(float) );
98    memcpy  ( SAVE_R,     Y_R+36-DELAY, DELAY        * 32 * sizeof(float) );
99    memmove ( Y_R+DELAY,  Y_R,          (36 - DELAY) * 32 * sizeof(float) );
100    memcpy  ( Y_R,        SWAP,         DELAY        * 32 * sizeof(float) );
101
102    for ( k = 0; k < 36; k++ ) {                                // apply global EQ to upper subbands
103        for ( n = FIR_BANDS; n <= Max_Band; n++ ) {
104            Y_L [k] [n] *= EQ_gain [n-FIR_BANDS];
105            Y_R [k] [n] *= EQ_gain [n-FIR_BANDS];
106        }
107    }
108
109    for ( i = 0; i < FIR_BANDS; i++ ) {                         // apply FIR to lower subbands for each channel
110        for ( k = 0; k < EQ_TAP; k++ ) {                        // L: perform filter for lowest subbands
111            tmp = 0.;
112            for ( n = 0; n < EQ_TAP - k; n++ )
113                tmp += FirSave_L  [i] [k+n]        * EQ_Filter [i] [n];
114            for ( ; n < EQ_TAP; n++ )
115                tmp += lowestSB_L [i] [k+n-EQ_TAP] * EQ_Filter [i] [n];
116            Y_L [k] [i] = tmp;
117        }
118        for ( ; k < 36; k++ ) {                                 // L: perform filter for lowest subbands
119            tmp = 0.;
120            for ( n = 0; n < EQ_TAP; n++ )
121                tmp += lowestSB_L [i] [k+n-EQ_TAP] * EQ_Filter [i] [n];
122            Y_L [k] [i] = tmp;
123        }
124        for ( n = 0; n < EQ_TAP; n++ )
125            FirSave_L [i] [n] = lowestSB_L [i] [36-EQ_TAP+n];
126
127        for ( k = 0; k < EQ_TAP; k++ ) {                        // R: perform filter for lowest subbands
128            tmp = 0.;
129            for ( n = 0; n < EQ_TAP - k; n++ )
130                tmp += FirSave_R  [i] [k+n]        * EQ_Filter [i] [n];
131            for ( ; n < EQ_TAP; n++ )
132                tmp += lowestSB_R [i] [k+n-EQ_TAP] * EQ_Filter [i] [n];
133            Y_R [k] [i] = tmp;
134        }
135        for ( ; k < 36; k++ ) {                                 // R: perform filter for lowest subbands
136            tmp = 0.;
137            for ( n = 0; n < EQ_TAP; n++ )
138                tmp += lowestSB_R [i] [k+n-EQ_TAP] * EQ_Filter [i] [n];
139            Y_R [k] [i] = tmp;
140        }
141        for ( n = 0; n < EQ_TAP; n++ )
142            FirSave_R [i] [n] = lowestSB_R [i] [36-EQ_TAP+n];
143    }
144
145    return;
146}
147
148int
149MPC_decoder::DECODE ( MPC_SAMPLE_FORMAT *buffer )
150{
151    unsigned int  FrameBitCnt = 0;
152
153    if ( DecodedFrames >= OverallFrames )
154        return 0;                           // end of file -> abort decoding
155
156    // read jump-info for validity check of frame
157    FwdJumpInfo  = Bitstream_read (20);
158    SeekTable [DecodedFrames] = 20 + FwdJumpInfo;       // ...
159
160    ActDecodePos = (Zaehler << 5) + pos;
161
162    // decode data and check for validity of frame
163    FrameBitCnt = BitsRead ();
164    switch ( StreamVersion ) {
165    case 0x04:
166    case 0x05:
167    case 0x06:
168        Lese_Bitstrom_SV6 ();
169        break;
170    case 0x07:
171    case 0x17:
172        Lese_Bitstrom_SV7 ();
173        break;
174    default:
175        return 0;
176    }
177    FrameWasValid = BitsRead() - FrameBitCnt == FwdJumpInfo;
178
179    // synthesize signal
180    Requantisierung ( Max_Band );
181    //if ( EQ_activated && PluginSettings.EQbyMPC )
182    //    perform_EQ ();
183#if   MPC_DECODE == TO_FLOAT
184
185    Synthese_Filter_float ( buffer );
186
187#elif MPC_DECODE == TO_PCM
188
189#if 0
190    if ( cfg_dither /*PluginSettings.DitherUsed*/ )
191        Synthese_Filter_dithered ( (short*)buffer );
192    else
193        Synthese_Filter_opt ( (short*)buffer );
194#endif
195    Synthese_Filter_dithered ( buffer );
196
197#else
198#	error Define MPC_DECODE to either TO_FLOAT or TO_PCM
199#endif
200    // (PluginSettings.DitherUsed  ?  this->Synthese_Filter_dithered  :  this->Synthese_Filter_opt) ( (short*)buffer );
201
202    DecodedFrames += 1;
203
204    // cut off first SYNTH_DELAY zero-samples
205    if ( DecodedFrames == 0 + 1 ) {
206        //memmove ( buffer, buffer + SYNTH_DELAY * 4, (FRAMELEN - SYNTH_DELAY) * 4 );
207        //return (FRAMELEN - 481) * 4;
208
209        //memmove ( buffer, buffer + SYNTH_DELAY * 2, (FRAMELEN - SYNTH_DELAY) * 4*2 );
210        memmove ( buffer, buffer + SYNTH_DELAY * 2, (FRAMELEN - SYNTH_DELAY) * 2 * sizeof (MPC_SAMPLE_FORMAT) );
211        return (FRAMELEN - 481) * 2;
212    }
213    else if ( DecodedFrames == OverallFrames  &&  StreamVersion >= 6 )
214    {        // reconstruct exact filelength
215        int  mod_block   = Bitstream_read (11);
216        int  FilterDecay;
217
218        if (mod_block == 0) mod_block = 1152;                    // Encoder bugfix
219        FilterDecay = (mod_block + SYNTH_DELAY) % FRAMELEN;
220
221        // additional FilterDecay samples are needed for decay of synthesis filter
222        if ( SYNTH_DELAY + mod_block >= FRAMELEN ) {
223
224            // **********************************************************************
225            // Rhoades 4/16/2002
226            // Commented out are blocks of code which cause gapless playback to fail.
227            // Temporary fix...
228            // **********************************************************************
229
230            if ( ! TrueGaplessPresent ) {
231                RESET_Y ();
232            } else {
233                //if ( FRAMELEN != LastValidSamples ) {
234                    Bitstream_read (20);
235                    Lese_Bitstrom_SV7 ();
236                    Requantisierung ( Max_Band );
237                    //FilterDecay = LastValidSamples;
238                //}
239                //else {
240                    //FilterDecay = 0;
241                //}
242            }
243            //if ( EQ_activated && PluginSettings.EQbyMPC )
244            //    perform_EQ ();
245#if   MPC_DECODE == TO_FLOAT
246
247            Synthese_Filter_float ( buffer + 2304 );
248
249#elif MPC_DECODE == TO_PCM
250
251#if 0
252            if ( cfg_dither /*PluginSettings.DitherUsed*/ )
253                Synthese_Filter_dithered ( (short*)buffer + 2304 );
254            else
255                Synthese_Filter_opt ( (short*)buffer + 2304 );
256#endif
257            Synthese_Filter_dithered ( buffer + 2304 );
258
259#endif
260            // (PluginSettings.DitherUsed  ?  this->Synthese_Filter_dithered  :  this->Synthese_Filter_opt) ( (short*)buffer + 2304);
261            // return (FRAMELEN + FilterDecay) * 4;
262            return (FRAMELEN + FilterDecay) * 2;
263        }
264        else {                              // there are only FilterDecay samples needed for this frame
265            // return FilterDecay * 4;
266            return FilterDecay * 2;
267        }
268    }
269    else {                  // full frame
270        // return FRAMELEN * 4;
271        return FRAMELEN * 2;
272    }
273}
274
275void
276MPC_decoder::Requantisierung ( const int Last_Band )
277{
278    int     Band;
279    int     n;
280    float   facL;
281    float   facR;
282    float   templ;
283    float   tempr;
284    float*  YL;
285    float*  YR;
286    int*    L;
287    int*    R;
288
289    // requantization and scaling of subband-samples
290    for ( Band = 0; Band <= Last_Band; Band++ ) {   // setting pointers
291        YL = Y_L [0] + Band;
292        YR = Y_R [0] + Band;
293        L  = Q [Band].L;
294        R  = Q [Band].R;
295        /************************** MS-coded **************************/
296        if ( MS_Flag [Band] ) {
297            if ( Res_L [Band] ) {
298                if ( Res_R [Band] ) {    // M!=0, S!=0
299                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][0]];
300                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][0]];
301                    for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
302                        *YL   = (templ = *L++ * facL)+(tempr = *R++ * facR);
303                        *YR   = templ - tempr;
304                    }
305                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][1]];
306                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][1]];
307                    for ( ; n < 24; n++, YL += 32, YR += 32 ) {
308                        *YL   = (templ = *L++ * facL)+(tempr = *R++ * facR);
309                        *YR   = templ - tempr;
310                    }
311                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][2]];
312                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][2]];
313                    for ( ; n < 36; n++, YL += 32, YR += 32 ) {
314                        *YL   = (templ = *L++ * facL)+(tempr = *R++ * facR);
315                        *YR   = templ - tempr;
316                    }
317                } else {    // M!=0, S==0
318                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][0]];
319                    for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
320                        *YR = *YL = *L++ * facL;
321                    }
322                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][1]];
323                    for ( ; n < 24; n++, YL += 32, YR += 32 ) {
324                        *YR = *YL = *L++ * facL;
325                    }
326                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][2]];
327                    for ( ; n < 36; n++, YL += 32, YR += 32 ) {
328                        *YR = *YL = *L++ * facL;
329                    }
330                }
331            } else {
332                if (Res_R[Band])    // M==0, S!=0
333                {
334                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][0]];
335                    for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
336                        *YR = - (*YL = *(R++) * facR);
337                    }
338                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][1]];
339                    for ( ; n < 24; n++, YL += 32, YR += 32 ) {
340                        *YR = - (*YL = *(R++) * facR);
341                    }
342                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][2]];
343                    for ( ; n < 36; n++, YL += 32, YR += 32 ) {
344                        *YR = - (*YL = *(R++) * facR);
345                    }
346                } else {    // M==0, S==0
347                    for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
348                        *YR = *YL = 0.f;
349                    }
350                }
351            }
352        }
353        /************************** LR-coded **************************/
354        else {
355            if ( Res_L [Band] ) {
356                if ( Res_R [Band] ) {    // L!=0, R!=0
357                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][0]];
358                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][0]];
359                    for (n = 0; n < 12; n++, YL += 32, YR += 32 ) {
360                        *YL = *L++ * facL;
361                        *YR = *R++ * facR;
362                    }
363                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][1]];
364                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][1]];
365                    for (; n < 24; n++, YL += 32, YR += 32 ) {
366                        *YL = *L++ * facL;
367                        *YR = *R++ * facR;
368                    }
369                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][2]];
370                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][2]];
371                    for (; n < 36; n++, YL += 32, YR += 32 ) {
372                        *YL = *L++ * facL;
373                        *YR = *R++ * facR;
374                    }
375                } else {     // L!=0, R==0
376                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][0]];
377                    for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
378                        *YL = *L++ * facL;
379                        *YR = 0.f;
380                    }
381                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][1]];
382                    for ( ; n < 24; n++, YL += 32, YR += 32 ) {
383                        *YL = *L++ * facL;
384                        *YR = 0.f;
385                    }
386                    facL = Cc[Res_L[Band]] * SCF[(unsigned char)SCF_Index_L[Band][2]];
387                    for ( ; n < 36; n++, YL += 32, YR += 32 ) {
388                        *YL = *L++ * facL;
389                        *YR = 0.f;
390                    }
391                }
392            }
393            else {
394                if ( Res_R [Band] ) {    // L==0, R!=0
395                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][0]];
396                    for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
397                        *YL = 0.f;
398                        *YR = *R++ * facR;
399                    }
400                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][1]];
401                    for ( ; n < 24; n++, YL += 32, YR += 32 ) {
402                        *YL = 0.f;
403                        *YR = *R++ * facR;
404                    }
405                    facR = Cc[Res_R[Band]] * SCF[(unsigned char)SCF_Index_R[Band][2]];
406                    for ( ; n < 36; n++, YL += 32, YR += 32 ) {
407                        *YL = 0.f;
408                        *YR = *R++ * facR;
409                    }
410                } else {    // L==0, R==0
411                    for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
412                        *YR = *YL = 0.f;
413                    }
414                }
415            }
416        }
417    }
418}
419
420/****************************************** SV 6 ******************************************/
421void
422MPC_decoder::Lese_Bitstrom_SV6 ( void )
423{
424    int n,k;
425    int Max_used_Band=0;
426    HuffmanTyp *Table;
427    const HuffmanTyp *x1;
428    const HuffmanTyp *x2;
429    int *L;
430    int *R;
431    int *ResL = Res_L;
432    int *ResR = Res_R;
433
434    /************************ HEADER **************************/
435    ResL = Res_L;
436    ResR = Res_R;
437    for (n=0; n<=Max_Band; ++n, ++ResL, ++ResR)
438    {
439        if      (n<11)           Table = Region_A;
440        else if (n>=11 && n<=22) Table = Region_B;
441        else /*if (n>=23)*/      Table = Region_C;
442
443        *ResL = Q_res[n][Huffman_Decode(Table)];
444        if (MS_used)      MS_Flag[n] = Bitstream_read(1);
445        *ResR = Q_res[n][Huffman_Decode(Table)];
446
447        // only perform the following procedure up to the maximum non-zero subband
448        if (*ResL || *ResR) Max_used_Band = n;
449    }
450
451    /************************* SCFI-Bundle *****************************/
452    ResL = Res_L;
453    ResR = Res_R;
454    for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR) {
455        if (*ResL) SCFI_Bundle_read(SCFI_Bundle, &SCFI_L[n], &DSCF_Flag_L[n]);
456        if (*ResR) SCFI_Bundle_read(SCFI_Bundle, &SCFI_R[n], &DSCF_Flag_R[n]);
457    }
458
459    /***************************** SCFI ********************************/
460    ResL = Res_L;
461    ResR = Res_R;
462    L    = SCF_Index_L[0];
463    R    = SCF_Index_R[0];
464    for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3)
465    {
466        if (*ResL)
467        {
468            /*********** DSCF ************/
469            if (DSCF_Flag_L[n]==1)
470            {
471                L[2] = DSCF_Reference_L[n];
472                switch (SCFI_L[n])
473                {
474                case 3:
475                    L[0] = L[2] + Huffman_Decode_fast(DSCF_Entropie);
476                    L[1] = L[0];
477                    L[2] = L[1];
478                    break;
479                case 1:
480                    L[0] = L[2] + Huffman_Decode_fast(DSCF_Entropie);
481                    L[1] = L[0] + Huffman_Decode_fast(DSCF_Entropie);
482                    L[2] = L[1];
483                    break;
484                case 2:
485                    L[0] = L[2] + Huffman_Decode_fast(DSCF_Entropie);
486                    L[1] = L[0];
487                    L[2] = L[1] + Huffman_Decode_fast(DSCF_Entropie);
488                    break;
489                case 0:
490                    L[0] = L[2] + Huffman_Decode_fast(DSCF_Entropie);
491                    L[1] = L[0] + Huffman_Decode_fast(DSCF_Entropie);
492                    L[2] = L[1] + Huffman_Decode_fast(DSCF_Entropie);
493                    break;
494                default:
495                    return;
496                    break;
497                }
498            }
499            /************ SCF ************/
500            else
501            {
502                switch (SCFI_L[n])
503                {
504                case 3:
505                    L[0] = Bitstream_read(6);
506                    L[1] = L[0];
507                    L[2] = L[1];
508                    break;
509                case 1:
510                    L[0] = Bitstream_read(6);
511                    L[1] = Bitstream_read(6);
512                    L[2] = L[1];
513                    break;
514                case 2:
515                    L[0] = Bitstream_read(6);
516                    L[1] = L[0];
517                    L[2] = Bitstream_read(6);
518                    break;
519                case 0:
520                    L[0] = Bitstream_read(6);
521                    L[1] = Bitstream_read(6);
522                    L[2] = Bitstream_read(6);
523                    break;
524                default:
525                    return;
526                    break;
527                }
528            }
529            // update Reference for DSCF
530            DSCF_Reference_L[n] = L[2];
531        }
532        if (*ResR)
533        {
534            R[2] = DSCF_Reference_R[n];
535            /*********** DSCF ************/
536            if (DSCF_Flag_R[n]==1)
537            {
538                switch (SCFI_R[n])
539                {
540                case 3:
541                    R[0] = R[2] + Huffman_Decode_fast(DSCF_Entropie);
542                    R[1] = R[0];
543                    R[2] = R[1];
544                    break;
545                case 1:
546                    R[0] = R[2] + Huffman_Decode_fast(DSCF_Entropie);
547                    R[1] = R[0] + Huffman_Decode_fast(DSCF_Entropie);
548                    R[2] = R[1];
549                    break;
550                case 2:
551                    R[0] = R[2] + Huffman_Decode_fast(DSCF_Entropie);
552                    R[1] = R[0];
553                    R[2] = R[1] + Huffman_Decode_fast(DSCF_Entropie);
554                    break;
555                case 0:
556                    R[0] = R[2] + Huffman_Decode_fast(DSCF_Entropie);
557                    R[1] = R[0] + Huffman_Decode_fast(DSCF_Entropie);
558                    R[2] = R[1] + Huffman_Decode_fast(DSCF_Entropie);
559                    break;
560                default:
561                    return;
562                    break;
563                }
564            }
565            /************ SCF ************/
566            else
567            {
568                switch (SCFI_R[n])
569                {
570                case 3:
571                    R[0] = Bitstream_read(6);
572                    R[1] = R[0];
573                    R[2] = R[1];
574                    break;
575                case 1:
576                    R[0] = Bitstream_read(6);
577                    R[1] = Bitstream_read(6);
578                    R[2] = R[1];
579                    break;
580                case 2:
581                    R[0] = Bitstream_read(6);
582                    R[1] = R[0];
583                    R[2] = Bitstream_read(6);
584                    break;
585                case 0:
586                    R[0] = Bitstream_read(6);
587                    R[1] = Bitstream_read(6);
588                    R[2] = Bitstream_read(6);
589                    break;
590                default:
591                    return;
592                    break;
593                }
594            }
595            // update Reference for DSCF
596            DSCF_Reference_R[n] = R[2];
597        }
598    }
599
600    /**************************** Samples ****************************/
601    ResL = Res_L;
602    ResR = Res_R;
603    for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR)
604    {
605        // setting pointers
606        x1 = SampleHuff[*ResL];
607        x2 = SampleHuff[*ResR];
608        L = Q[n].L;
609        R = Q[n].R;
610
611        if (x1!=NULL || x2!=NULL)
612            for (k=0; k<36; ++k)
613            {
614                if (x1 != NULL) *L++ = Huffman_Decode_fast (x1);
615                if (x2 != NULL) *R++ = Huffman_Decode_fast (x2);
616            }
617
618        if (*ResL>7 || *ResR>7)
619            for (k=0; k<36; ++k)
620            {
621                if (*ResL>7) *L++ = (int)Bitstream_read(Res_bit[*ResL]) - Dc[*ResL];
622                if (*ResR>7) *R++ = (int)Bitstream_read(Res_bit[*ResR]) - Dc[*ResR];
623            }
624    }
625}
626
627/****************************************** SV 7 ******************************************/
628void
629MPC_decoder::Lese_Bitstrom_SV7 ( void )
630{
631    // these arrays hold decoding results for bundled quantizers (3- and 5-step)
632    /*static*/ int idx30[] = { -1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1};
633    /*static*/ int idx31[] = { -1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1};
634    /*static*/ int idx32[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
635    /*static*/ int idx50[] = { -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
636    /*static*/ int 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};
637
638    int n,k;
639    int Max_used_Band=0;
640    const HuffmanTyp *Table;
641    int idx;
642    int *L   ,*R;
643    int *ResL,*ResR;
644    unsigned int tmp;
645
646    /***************************** Header *****************************/
647    ResL  = Res_L;
648    ResR  = Res_R;
649
650    // first subband
651    *ResL = Bitstream_read(4);
652    *ResR = Bitstream_read(4);
653    if (MS_used && !(*ResL==0 && *ResR==0)) MS_Flag[0] = Bitstream_read(1);
654
655    // consecutive subbands
656    ++ResL; ++ResR; // increase pointers
657    for (n=1; n<=Max_Band; ++n, ++ResL, ++ResR)
658    {
659        idx   = Huffman_Decode_fast(HuffHdr);
660        *ResL = (idx!=4) ? *(ResL-1) + idx : Bitstream_read(4);
661
662        idx   = Huffman_Decode_fast(HuffHdr);
663        *ResR = (idx!=4) ? *(ResR-1) + idx : Bitstream_read(4);
664
665        if (MS_used && !(*ResL==0 && *ResR==0)) MS_Flag[n] = Bitstream_read(1);
666
667        // only perform following procedures up to the maximum non-zero subband
668        if (*ResL!=0 || *ResR!=0) Max_used_Band = n;
669    }
670    /****************************** SCFI ******************************/
671    L     = SCFI_L;
672    R     = SCFI_R;
673    ResL  = Res_L;
674    ResR  = Res_R;
675    for (n=0; n<=Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) {
676        if (*ResL) *L = Huffman_Decode_faster(HuffSCFI);
677        if (*ResR) *R = Huffman_Decode_faster(HuffSCFI);
678    }
679
680    /**************************** SCF/DSCF ****************************/
681    ResL  = Res_L;
682    ResR  = Res_R;
683    L     = SCF_Index_L[0];
684    R     = SCF_Index_R[0];
685    for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3) {
686        if (*ResL)
687        {
688            L[2] = DSCF_Reference_L[n];
689            switch (SCFI_L[n])
690            {
691                case 1:
692                    idx  = Huffman_Decode_fast(HuffDSCF);
693                    L[0] = (idx!=8) ? L[2] + idx : Bitstream_read(6);
694                    idx  = Huffman_Decode_fast(HuffDSCF);
695                    L[1] = (idx!=8) ? L[0] + idx : Bitstream_read(6);
696                    L[2] = L[1];
697                    break;
698                case 3:
699                    idx  = Huffman_Decode_fast(HuffDSCF);
700                    L[0] = (idx!=8) ? L[2] + idx : Bitstream_read(6);
701                    L[1] = L[0];
702                    L[2] = L[1];
703                    break;
704                case 2:
705                    idx  = Huffman_Decode_fast(HuffDSCF);
706                    L[0] = (idx!=8) ? L[2] + idx : Bitstream_read(6);
707                    L[1] = L[0];
708                    idx  = Huffman_Decode_fast(HuffDSCF);
709                    L[2] = (idx!=8) ? L[1] + idx : Bitstream_read(6);
710                    break;
711                case 0:
712                    idx  = Huffman_Decode_fast(HuffDSCF);
713                    L[0] = (idx!=8) ? L[2] + idx : Bitstream_read(6);
714                    idx  = Huffman_Decode_fast(HuffDSCF);
715                    L[1] = (idx!=8) ? L[0] + idx : Bitstream_read(6);
716                    idx  = Huffman_Decode_fast(HuffDSCF);
717                    L[2] = (idx!=8) ? L[1] + idx : Bitstream_read(6);
718                    break;
719                default:
720                    return;
721                    break;
722            }
723            // update Reference for DSCF
724            DSCF_Reference_L[n] = L[2];
725        }
726        if (*ResR)
727        {
728            R[2] = DSCF_Reference_R[n];
729            switch (SCFI_R[n])
730            {
731                case 1:
732                    idx  = Huffman_Decode_fast(HuffDSCF);
733                    R[0] = (idx!=8) ? R[2] + idx : Bitstream_read(6);
734                    idx  = Huffman_Decode_fast(HuffDSCF);
735                    R[1] = (idx!=8) ? R[0] + idx : Bitstream_read(6);
736                    R[2] = R[1];
737                    break;
738                case 3:
739                    idx  = Huffman_Decode_fast(HuffDSCF);
740                    R[0] = (idx!=8) ? R[2] + idx : Bitstream_read(6);
741                    R[1] = R[0];
742                    R[2] = R[1];
743                    break;
744                case 2:
745                    idx  = Huffman_Decode_fast(HuffDSCF);
746                    R[0] = (idx!=8) ? R[2] + idx : Bitstream_read(6);
747                    R[1] = R[0];
748                    idx  = Huffman_Decode_fast(HuffDSCF);
749                    R[2] = (idx!=8) ? R[1] + idx : Bitstream_read(6);
750                    break;
751                case 0:
752                    idx  = Huffman_Decode_fast(HuffDSCF);
753                    R[0] = (idx!=8) ? R[2] + idx : Bitstream_read(6);
754                    idx  = Huffman_Decode_fast(HuffDSCF);
755                    R[1] = (idx!=8) ? R[0] + idx : Bitstream_read(6);
756                    idx  = Huffman_Decode_fast(HuffDSCF);
757                    R[2] = (idx!=8) ? R[1] + idx : Bitstream_read(6);
758                    break;
759                default:
760                    return;
761                    break;
762            }
763            // update Reference for DSCF
764            DSCF_Reference_R[n] = R[2];
765        }
766    }
767    /***************************** Samples ****************************/
768    ResL = Res_L;
769    ResR = Res_R;
770    L    = Q[0].L;
771    R    = Q[0].R;
772    for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=36, R+=36)
773    {
774        /************** links **************/
775        switch (*ResL)
776        {
777            case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
778            case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
779                L += 36;
780                break;
781            case -1:
782                for (k=0; k<36; k++ ) {
783                    tmp  = random_int ();
784                    *L++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
785                }
786                break;
787            case 0:
788                L += 36;// increase pointer
789                break;
790            case 1:
791                Table = HuffQ[Bitstream_read(1)][1];
792                for (k=0; k<12; ++k)
793                {
794                    idx = Huffman_Decode_fast(Table);
795                    *L++ = idx30[idx];
796                    *L++ = idx31[idx];
797                    *L++ = idx32[idx];
798                }
799                break;
800            case 2:
801                Table = HuffQ[Bitstream_read(1)][2];
802                for (k=0; k<18; ++k)
803                {
804                    idx = Huffman_Decode_fast(Table);
805                    *L++ = idx50[idx];
806                    *L++ = idx51[idx];
807                }
808                break;
809            case 3:
810            case 4:
811                Table = HuffQ[Bitstream_read(1)][*ResL];
812                for (k=0; k<36; ++k)
813                    *L++ = Huffman_Decode_faster(Table);
814                break;
815            case 5:
816                Table = HuffQ[Bitstream_read(1)][*ResL];
817                for (k=0; k<36; ++k)
818                    *L++ = Huffman_Decode_fast(Table);
819                break;
820            case 6:
821            case 7:
822                Table = HuffQ[Bitstream_read(1)][*ResL];
823                for (k=0; k<36; ++k)
824                    *L++ = Huffman_Decode(Table);
825                break;
826            case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
827                tmp = Dc[*ResL];
828                for (k=0; k<36; ++k)
829                    *L++ = (int)Bitstream_read(Res_bit[*ResL]) - tmp;
830                break;
831            default:
832                return;
833        }
834        /************** rechts **************/
835        switch (*ResR)
836        {
837            case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
838            case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
839                R += 36;
840                break;
841            case -1:
842                for (k=0; k<36; k++ ) {
843                    tmp  = random_int ();
844                    *R++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
845                }
846                break;
847            case 0:
848                R += 36;// increase pointer
849                break;
850            case 1:
851                Table = HuffQ[Bitstream_read(1)][1];
852                for (k=0; k<12; ++k)
853                {
854                    idx = Huffman_Decode_fast(Table);
855                    *R++ = idx30[idx];
856                    *R++ = idx31[idx];
857                    *R++ = idx32[idx];
858                }
859                break;
860            case 2:
861                Table = HuffQ[Bitstream_read(1)][2];
862                for (k=0; k<18; ++k)
863                {
864                    idx = Huffman_Decode_fast(Table);
865                    *R++ = idx50[idx];
866                    *R++ = idx51[idx];
867                }
868                break;
869            case 3:
870            case 4:
871                Table = HuffQ[Bitstream_read(1)][*ResR];
872                for (k=0; k<36; ++k)
873                    *R++ = Huffman_Decode_faster(Table);
874                break;
875            case 5:
876                Table = HuffQ[Bitstream_read(1)][*ResR];
877                for (k=0; k<36; ++k)
878                    *R++ = Huffman_Decode_fast(Table);
879                break;
880            case 6:
881            case 7:
882                Table = HuffQ[Bitstream_read(1)][*ResR];
883                for (k=0; k<36; ++k)
884                    *R++ = Huffman_Decode(Table);
885                break;
886            case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
887                tmp = Dc[*ResR];
888                for (k=0; k<36; ++k)
889                    *R++ = (int)Bitstream_read(Res_bit[*ResR]) - tmp;
890                break;
891            default:
892                return;
893        }
894    }
895}
896
897MPC_decoder::~MPC_decoder ()
898{
899  free ( SeekTable );
900  //if ( m_reader ) delete m_reader;
901	// ^^ breaks wa3
902}
903
904MPC_decoder::MPC_decoder(BPositionIO *file)
905{
906  m_reader = file;
907
908  HuffQ[0][0] = 0;
909  HuffQ[1][0] = 0;
910  HuffQ[0][1] = HuffQ1[0];
911  HuffQ[1][1] = HuffQ1[1];
912  HuffQ[0][2] = HuffQ2[0];
913  HuffQ[1][2] = HuffQ2[1];
914  HuffQ[0][3] = HuffQ3[0];
915  HuffQ[1][3] = HuffQ3[1];
916  HuffQ[0][4] = HuffQ4[0];
917  HuffQ[1][4] = HuffQ4[1];
918  HuffQ[0][5] = HuffQ5[0];
919  HuffQ[1][5] = HuffQ5[1];
920  HuffQ[0][6] = HuffQ6[0];
921  HuffQ[1][6] = HuffQ6[1];
922  HuffQ[0][7] = HuffQ7[0];
923  HuffQ[1][7] = HuffQ7[1];
924
925  SampleHuff[0] = NULL;
926  SampleHuff[1] = Entropie_1;
927  SampleHuff[2] = Entropie_2;
928  SampleHuff[3] = Entropie_3;
929  SampleHuff[4] = Entropie_4;
930  SampleHuff[5] = Entropie_5;
931  SampleHuff[6] = Entropie_6;
932  SampleHuff[7] = Entropie_7;
933  SampleHuff[8] = NULL;
934  SampleHuff[9] = NULL;
935  SampleHuff[10] = NULL;
936  SampleHuff[11] = NULL;
937  SampleHuff[12] = NULL;
938  SampleHuff[13] = NULL;
939  SampleHuff[14] = NULL;
940  SampleHuff[15] = NULL;
941  SampleHuff[16] = NULL;
942  SampleHuff[17] = NULL;
943
944  SeekTable = NULL;
945  EQ_activated = 0;
946  MPCHeaderPos = 0;
947  StreamVersion = 0;
948  MS_used = 0;
949  FwdJumpInfo = 0;
950  ActDecodePos = 0;
951  FrameWasValid = 0;
952  OverallFrames = 0;
953  DecodedFrames = 0;
954  LastFrame = 0;
955  LastBitsRead = 0;
956  SectionBitrate = 0;
957  LastValidSamples = 0;
958  TrueGaplessPresent = 0;
959  NumberOfConsecutiveBrokenFrames = 0;
960  WordsRead = 0;
961  Max_Band = 0;
962  SampleRate = 0;
963  clips = 0;
964  __r1 = 1;
965  __r2 = 1;
966
967  dword = 0;
968  pos = 0;
969  Zaehler = 0;
970  WordsRead = 0;
971  Max_Band = 0;
972
973  memset ( SAVE_L, 0, sizeof (SAVE_L) );
974  memset ( SAVE_R, 0, sizeof (SAVE_R) );
975  memset ( FirSave_L, 0, sizeof (FirSave_L) );
976  memset ( FirSave_R, 0, sizeof (FirSave_R) );
977
978  initialisiere_Quantisierungstabellen ();
979  Huffman_SV6_Decoder ();
980  Huffman_SV7_Decoder ();
981}
982
983
984void
985MPC_decoder::SetStreamInfo(StreamInfo *si)
986{
987	fInfo = si;
988		// this is used in MusePackDecoder::NegotiateOutputFormat()
989
990  StreamVersion      = si->simple.StreamVersion;
991  MS_used            = si->simple.MS;
992  Max_Band           = si->simple.MaxBand;
993  OverallFrames      = si->simple.Frames;
994  MPCHeaderPos       = si->simple.HeaderPosition;
995  LastValidSamples   = si->simple.LastFrameSamples;
996  TrueGaplessPresent = si->simple.IsTrueGapless;
997  SectionBitrate     = (int)si->simple.AverageBitrate;
998  SampleRate         = (int)si->simple.SampleFreq;
999
1000  if ( SeekTable != NULL ) free ( SeekTable );
1001  SeekTable = (unsigned short *)calloc ( sizeof(unsigned short), OverallFrames+64 );
1002}
1003
1004int MPC_decoder::FileInit ()
1005{
1006  // AB: setting position to the beginning of the data-bitstream
1007  switch ( StreamVersion ) {
1008  case 0x04: f_seek ( 4 + MPCHeaderPos, SEEK_SET); pos = 16; break;  // Geht auch �ber eine der Helperfunktionen
1009  case 0x05:
1010  case 0x06: f_seek ( 8 + MPCHeaderPos, SEEK_SET); pos =  0; break;
1011  case 0x07:
1012  case 0x17: f_seek ( 24 + MPCHeaderPos, SEEK_SET); pos =  8; break;
1013  default: return 0;
1014  }
1015
1016  // AB: fill buffer and initialize decoder
1017  f_read ( Speicher, 4*MEMSIZE );
1018  dword = Speicher [Zaehler = 0];
1019
1020  return 1;
1021}
1022
1023//---------------------------------------------------------------
1024// will seek from the beginning of the file to the desired
1025// position in ms (given by seek_needed)
1026//---------------------------------------------------------------
1027
1028void
1029MPC_decoder::Helper1 ( unsigned long bitpos )
1030{
1031    f_seek ( (bitpos>>5) * 4 + MPCHeaderPos, SEEK_SET );
1032    f_read ( Speicher, sizeof(int)*2 );
1033    dword = Speicher [ Zaehler = 0];
1034    pos   = bitpos & 31;
1035}
1036
1037void
1038MPC_decoder::Helper2 ( unsigned long bitpos )
1039{
1040    f_seek ( (bitpos>>5) * 4 + MPCHeaderPos, SEEK_SET );
1041    f_read ( Speicher, sizeof(int) * MEMSIZE );
1042    dword = Speicher [ Zaehler = 0];
1043    pos   = bitpos & 31;
1044}
1045
1046void
1047MPC_decoder::Helper3 ( unsigned long bitpos, long* buffoffs )
1048{
1049    pos      = bitpos & 31;
1050    bitpos >>= 5;
1051    if ( (unsigned long)(bitpos - *buffoffs) >= MEMSIZE-2 ) {
1052        *buffoffs = bitpos;
1053        f_seek ( bitpos * 4L + MPCHeaderPos, SEEK_SET );
1054        f_read ( Speicher, sizeof(int)*MEMSIZE );
1055    }
1056    dword = Speicher [ Zaehler = bitpos - *buffoffs ];
1057}
1058
1059int
1060MPC_decoder::perform_jump ( int seek_needed )
1061{
1062    unsigned long  fpos;
1063    unsigned int   FrameBitCnt;
1064    unsigned int   RING;
1065    int            fwd;
1066    unsigned int   decframes;
1067    unsigned long  buffoffs = 0x80000000;
1068
1069    switch ( StreamVersion ) {                                                  // setting position to the beginning of the data-bitstream
1070    case  0x04: fpos =  48; break;
1071    case  0x05:
1072    case  0x06: fpos =  64; break;
1073    case  0x07:
1074    case  0x17: fpos = 200; break;
1075    default   : return 0;                                                       // was gibt diese Funktion im Falle eines Fehlers zur�ck ???
1076    }
1077
1078    fwd           = (int) ( seek_needed * (float)SampleRate * 0.001 / FRAMELEN + .5f ); // no of frames to skip
1079    fwd           = fwd < (int)OverallFrames  ?  fwd  :  (int)OverallFrames;    // prevent from desired position out of allowed range
1080    decframes     = DecodedFrames;
1081    DecodedFrames = 0;                                                          // reset number of decoded frames
1082
1083    if ( fwd > 32 && decframes > 128 ) {                                        // only do proceed, if desired position is not in between the first 32 frames
1084        for ( ; (int)DecodedFrames + 32 < fwd; DecodedFrames++ ) {              // proceed until 32 frames before (!!) desired position (allows to scan the scalefactors)
1085            if ( SeekTable [DecodedFrames] == 0 ) {
1086#ifdef OTHER_SEEK
1087                Helper3 ( fpos, (long*)&buffoffs );
1088#else
1089                Helper1 ( fpos );
1090#endif
1091                fpos += SeekTable [DecodedFrames] = 20 + Bitstream_read (20);   // calculate desired pos (in Bits)
1092            } else {
1093                fpos += SeekTable [DecodedFrames];
1094            }
1095        }
1096    }
1097    Helper2 ( fpos );
1098
1099    for ( ; (int)DecodedFrames < fwd; DecodedFrames++ ) {                       // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping)
1100        RING         = Zaehler;
1101        FwdJumpInfo  = Bitstream_read (20);                                     // read jump-info
1102        ActDecodePos = (Zaehler << 5) + pos;
1103        FrameBitCnt  = BitsRead ();                                             // scanning the scalefactors and check for validity of frame
1104        if (StreamVersion >= 7)  Lese_Bitstrom_SV7 ();
1105        else Lese_Bitstrom_SV6 ();
1106        if ( BitsRead() - FrameBitCnt != FwdJumpInfo ) {
1107//            Box ("Bug in perform_jump");
1108            return 0;
1109        }
1110        if ( (RING ^ Zaehler) & MEMSIZE2 )                                      // update buffer
1111            f_read ( Speicher + (RING & MEMSIZE2), 4 * MEMSIZE2 );
1112    }
1113
1114    RESET_Synthesis ();                                                         // resetting synthesis filter to avoid "clicks"
1115    LastBitsRead = BitsRead ();
1116    LastFrame = DecodedFrames;
1117
1118    return 1;
1119}
1120
1121void MPC_decoder::UpdateBuffer ( unsigned int RING )
1122{
1123    if ( (RING ^ Zaehler) & MEMSIZE2 )
1124        f_read ( Speicher + (RING & MEMSIZE2), 4 * MEMSIZE2 );      // update buffer
1125}
1126
1127#if 0
1128void
1129MPC_decoder::EQSet ( int on, char data[10], int preamp )
1130{
1131    /*static*/ const int  specline [10] = { 1, 4, 7, 14, 23, 70, 139, 279, 325, 372 };
1132    /*static*/ const int  sym           = (EQ_TAP - 1) / 2;
1133    float       set [512];
1134    float       x   [512];
1135    float       mid  [32];
1136    float       power[10];
1137    float       win;
1138    int         i;
1139    int         n;
1140    int         k;
1141    int         idx;
1142
1143    /*
1144    The implemented EQ utilizes the given frequencies and interpolates the power level.
1145    All subbands except the lowest FIR_BANDS subbands will be attenuated by the calculated
1146    average gain. The first FIR_BANDS subbands will be processed by a EQ_TAP-order FIR-filter,
1147    because the frequency resolution of the EQ is much higher than the subbands bandwidth.
1148    */
1149    EQ_activated = 0;
1150    if ( on ) {
1151
1152        // calculate desired attenuation
1153        for ( n = 0; n < 10; n++ ) {
1154            power[n]  = (31-(int)data[n]) * PluginSettings.EQdB/32.f;
1155            power[n] += (31-      preamp) * PluginSettings.EQdB/32.f;
1156        }
1157
1158        // calculate desired attenuation for each bin
1159        set[0] = power[0];
1160        for ( k =  1; k <  4; k++ ) set [k]= (power[0]*(4  -k) + power[1]*(k-  1)) /  3.f;
1161        for ( k =  4; k <  7; k++ ) set [k]= (power[1]*(7  -k) + power[2]*(k-  4)) /  3.f;
1162        for ( k =  7; k < 14; k++ ) set [k]= (power[2]*(14 -k) + power[3]*(k-  7)) /  7.f;
1163        for ( k = 14; k < 23; k++ ) set [k]= (power[3]*(23 -k) + power[4]*(k- 14)) /  9.f;
1164        for ( k = 23; k < 70; k++ ) set [k]= (power[4]*(70 -k) + power[5]*(k- 23)) / 47.f;
1165        for ( k = 70; k <139; k++ ) set [k]= (power[5]*(139-k) + power[6]*(k- 70)) / 69.f;
1166        for ( k =139; k <279; k++ ) set [k]= (power[6]*(279-k) + power[7]*(k-139)) /140.f;
1167        for ( k =279; k <325; k++ ) set [k]= (power[7]*(325-k) + power[8]*(k-279)) / 46.f;
1168        for ( k =325; k <372; k++ ) set [k]= (power[8]*(372-k) + power[9]*(k-325)) / 47.f;
1169        for ( k =372; k <512; k++ ) set [k]=  power[9];
1170
1171        // transform from level to power
1172        for ( k = 0; k < 512; k++ )
1173            set[k] = (float) pow ( 10., 0.1*set[k] );
1174
1175        /************************** gain for upper subbands ****************************/
1176        // transform to attenuation (gain) per subband
1177        memset ( mid, 0, sizeof(mid) );
1178        for ( k = 16 * FIR_BANDS; k < 512; k++ )
1179            mid [k >> 4] += set [k];                                // summarize power
1180        for (n=FIR_BANDS   ; n<32 ; ++n)
1181            EQ_gain [n - FIR_BANDS] = (float) sqrt (mid[n] / 16.);  // average gain
1182
1183        /************************** FIR for lowest subbands ****************************/
1184        for ( i = 0; i < FIR_BANDS; i++ ) {  // calculate impulse response of FIR via IDFT
1185            for ( n = 0; n < DELAY; n++ ) {
1186                x[n] = 0;
1187                for ( k = 0; k < 16; k++ ) {
1188                    idx   = i & 1  ?  (i<<4)+15-k  :  (i<<4)+k;   // frequency inversion of every second subband
1189                    x[n] += (float) ( sqrt (set[idx]) * cos (2*M_PI*n*k/32) );
1190                }
1191                x[n] /= 16.;
1192            }
1193            // calculate a symmetric EQ_TAP-tap FIR-filter
1194            for ( n = 0; n < EQ_TAP; n++ ) {
1195                win             = (float) sin ( (n+1) * M_PI / (EQ_TAP+1) );
1196                win            *= win;   // Portable, falls win = sin� () die Absicht sein sollte
1197                EQ_Filter[i][n] =  n <= sym  ?  x[sym-n]*win  :  x[n-sym]*win;
1198            }
1199        }
1200        EQ_activated = 1;
1201    }
1202
1203    return;
1204}
1205#endif
1206
1207//---------------------------------------------------------------
1208// calculates the factor that must be applied to match the
1209// chosen ReplayGain-mode
1210//---------------------------------------------------------------
1211#if 0
1212float
1213MPC_decoder::ProcessReplayGain ( int mode, StreamInfo *info )
1214{
1215    unsigned char modetab [7] = { 0,4,5,6,7,2,3 };
1216    char                  message    [800];
1217    float                 factor_gain;
1218    float                 factor_clip;
1219    float                 factor_preamp;
1220    int                   Gain     = 0;
1221    int                   Peak     = 0;
1222    int                   Headroom = 0;
1223    char*                 p        = message;
1224
1225    mode = modetab [mode];
1226
1227    if ( (mode & 1) == 0 )
1228        Gain = info->simple.GainTitle, Peak = info->simple.PeakTitle;
1229    else
1230        Gain = info->simple.GainAlbum, Peak = info->simple.PeakAlbum;
1231
1232    /*
1233    if ( PluginSettings.DebugUsed ) {
1234        p += sprintf ( p, "UsedPeak: %u     UsedGain: %+5.2f\n", Peak, 0.01*Gain );
1235    }
1236    */
1237
1238    if ( (mode & 2) == 0 )
1239        Gain = 0;
1240
1241    if ( (mode & 4) == 0 )
1242        Peak = 0;
1243
1244    if ( mode == 0 )
1245        Headroom = 0;
1246    else
1247        Headroom = cfg_headroom /*PluginSettings.ReplayGainHeadroom*/ - 14;   // K-2...26, 2 => -12, 26 => +12
1248
1249    // calculate multipliers for ReplayGain and ClippingPrevention
1250    factor_preamp = (float) pow (10., -0.05 * Headroom);
1251    factor_gain   = (float) pow (10., 0.0005 * Gain);
1252
1253    if ( cfg_clipprotect == FALSE ) {
1254        factor_clip = 1.f;
1255    } else {
1256        factor_clip   = (float) (32767./( Peak + 1 ) );
1257
1258        if ( factor_preamp * factor_gain < factor_clip )
1259            factor_clip  = 1.f;
1260        else
1261            factor_clip /= factor_preamp * factor_gain;
1262    }
1263
1264    return factor_preamp * factor_gain * factor_clip;
1265}
1266#endif
1267