Ignore:
Timestamp:
10/07/06 02:49:28 (16 years ago)
Author:
r2d
Message:
  • fixed a LOT of bugs
  • fixed a bug that took me 2 days to find :)
  • remember to ALWAYS use -Wall with a C compiler (C++ is great !!!)
  • files can be played, but are not bit identical with the 1.15w encoder (the last digits of floats are not the same) - must be fixed.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • mppenc/branches/r2d/libmpcpsy/psy.c

    r66 r71  
    6060#include "minimax.h"
    6161
    62 
     62// psy_tab.c
    6363extern const float  iw        [PART_LONG];      // inverse partition-width for long
    6464extern const float  iw_short  [PART_SHORT];     // inverse partition-width for short
     
    6767extern const int    wh        [PART_LONG];      // w_high for long
    6868extern const int    wh_short  [PART_SHORT];     // w_high for short
    69 
     69extern float        MinVal   [PART_LONG];       // minimum quality that's adapted to the model, minval for long
     70extern float  Loudness [PART_LONG];               // weighting factors for loudness calculation
     71extern float  SPRD     [PART_LONG] [PART_LONG];   // tabulated spreading function
     72extern float  O_MAX;
     73extern float  O_MIN;
     74extern float  FAC1;
     75extern float  FAC2;                               // constants for offset calculation
     76extern float  partLtq  [PART_LONG];               // threshold in quiet (partitions)
     77extern float  invLtq   [PART_LONG];               // inverse threshold in quiet (partitions, long)
     78extern float  fftLtq   [512];                     // threshold in quiet (FFT)
     79
     80// ans.c
     81extern float         ANSspec_L [MAX_ANS_LINES];
     82extern float         ANSspec_R [MAX_ANS_LINES];         // L/R-masking threshold for ANS
     83extern float         ANSspec_M [MAX_ANS_LINES];
     84extern float         ANSspec_S [MAX_ANS_LINES];         // M/S-masking threshold for ANS
     85
     86void   Init_Psychoakustiktabellen ( PsyModel* );
     87int    CVD2048 ( PsyModel*, const float*, int* );
    7088
    7189// Antialiasing for calculation of the subband power
     
    7795/* V A R I A B L E S */
    7896
     97static float         a          [PART_LONG];
     98static float         b          [PART_LONG];
     99static float         c          [PART_LONG];
     100static float         d          [PART_LONG];           // Integrations for tmpMask
    79101static float  Xsave_L    [3 * 512];
    80102static float  Xsave_R    [3 * 512];             // FFT-Amplitudes L/R
    81103static float  Ysave_L    [3 * 512];
    82104static float  Ysave_R    [3 * 512];             // FFT-Phases L/R
     105static float         T_L        [PART_LONG];
     106static float         T_R        [PART_LONG];           // time-constants for tmpMask
     107static float         pre_erg_L[2][PART_SHORT];
     108static float         pre_erg_R[2][PART_SHORT];          // Preecho-control short
     109static float         PreThr_L   [PART_LONG];
     110static float         PreThr_R   [PART_LONG];           // for Pre-Echo-control L/R
     111static float         tmp_Mask_L [PART_LONG];
     112static float         tmp_Mask_R [PART_LONG];           // for Post-Masking L/R
     113static int           Vocal_L    [MAX_CVD_LINE + 4];
     114static int           Vocal_R    [MAX_CVD_LINE + 4];    // FFT-Line belongs to harmonic?
    83115
    84116/* F U N C T I O N S */
     
    102134        memset ( Ysave_L,   0, sizeof Ysave_L );
    103135        memset ( Ysave_R,   0, sizeof Ysave_R );
    104         memset ( m->a,         0, sizeof m->a       );
    105         memset ( m->b,         0, sizeof m->b       );
    106         memset ( m->c,         0, sizeof m->c       );
    107         memset ( m->d,         0, sizeof m->d       );
    108         memset ( m->T_L,       0, sizeof m->T_L     );
    109         memset ( m->T_R,       0, sizeof m->T_R     );
    110         memset ( m->Vocal_L,   0, sizeof m->Vocal_L );
    111         memset ( m->Vocal_R,   0, sizeof m->Vocal_R );
     136        memset ( a,         0, sizeof a       );
     137        memset ( b,         0, sizeof b       );
     138        memset ( c,         0, sizeof c       );
     139        memset ( d,         0, sizeof d       );
     140        memset ( T_L,       0, sizeof T_L     );
     141        memset ( T_R,       0, sizeof T_R     );
     142        memset ( Vocal_L,   0, sizeof Vocal_L );
     143        memset ( Vocal_R,   0, sizeof Vocal_R );
    112144
    113145        m->SampleFreq = 0.;
     
    115147        m->KBD1 = 2.;
    116148        m->KBD2 = -1.;
     149        m->Ltq_offset = 0;
     150        m->Ltq_max = 0;
     151        m->EarModelFlag = 0;
    117152
    118153    // generate FFT lookup-tables with largest FFT-size of 1024
    119154        Init_FFT (m);
    120155
     156        Init_Psychoakustiktabellen (m);
     157
    121158    // setting pre-echo variables to Ltq
    122159        for ( i = 0; i < PART_LONG; i++ ) {
    123                 m->pre_erg_L  [0][i/3] = m->pre_erg_R  [0][i/3] =
    124                                 m->pre_erg_L  [1][i/3] = m->pre_erg_R  [1][i/3] =
    125                                 m->tmp_Mask_L [i]   = m->tmp_Mask_R [i]   =
    126                                 m->PreThr_L   [i]   = m->PreThr_R   [i]   = m->partLtq [i];
     160                pre_erg_L  [0][i/3] = pre_erg_R  [0][i/3] =
     161                                pre_erg_L  [1][i/3] = pre_erg_R  [1][i/3] =
     162                                tmp_Mask_L [i]   = tmp_Mask_R [i]   =
     163                                PreThr_L   [i]   = PreThr_R   [i]   = partLtq [i];
    127164        }
    128165
     
    496533// SPRD describes the spreading function as calculated in psy_tab.c
    497534static void
    498 SpreadingSignal ( PsyModel* m, const float* erg, const float* werg, float* res,
     535SpreadingSignal ( const float* erg, const float* werg, float* res,
    499536                                  float* wres )
    500537{
     
    511548        start = maxi(k-5, 0);           // minimum affected partition
    512549        stop  = mini(k+7, PART_LONG-1); // maximum affected partition
    513         sprd  = m->SPRD[k] + start;         // load vector
     550        sprd  = SPRD[k] + start;         // load vector
    514551        e     = *erg;
    515552        ew    = *werg;
     
    527564// output: masking threshold *erg after applying the tonality-offset
    528565static void
    529 ApplyTonalityOffset ( PsyModel* m, float* erg0, float* erg1, const float* werg0, const float* werg1 )
     566ApplyTonalityOffset ( float* erg0, float* erg1, const float* werg0, const float* werg1 )
    530567{
    531568    int    n;
     
    537574    for ( n = 0; n < PART_LONG; n++ ) {
    538575        quot = *werg0++ / *erg0;
    539         if      (quot <= 0.05737540597f) Offset = m->O_MAX;
    540         else if (quot <  0.5871011603f ) Offset = m->FAC1 * POW (quot, m->FAC2);
    541         else                             Offset = m->O_MIN;
    542         *erg0++ *= iw[n] * minf(m->MinVal[n], Offset);
     576        if      (quot <= 0.05737540597f) Offset = O_MAX;
     577        else if (quot <  0.5871011603f ) Offset = FAC1 * POW (quot, FAC2);
     578        else                             Offset = O_MIN;
     579        *erg0++ *= iw[n] * minf(MinVal[n], Offset);
    543580
    544581        quot = *werg1++ / *erg1;
    545         if      (quot <= 0.05737540597f) Offset = m->O_MAX;
    546         else if (quot <  0.5871011603f ) Offset = m->FAC1 * POW (quot, m->FAC2);
    547         else                             Offset = m->O_MIN;
    548                 *erg1++ *= iw[n] * minf(m->MinVal[n], Offset);
     582        if      (quot <= 0.05737540597f) Offset = O_MAX;
     583        else if (quot <  0.5871011603f ) Offset = FAC1 * POW (quot, FAC2);
     584        else                             Offset = O_MIN;
     585                *erg1++ *= iw[n] * minf(MinVal[n], Offset);
    549586    }
    550587
     
    558595{
    559596    static float  loud   = 0.f;
    560         float*        weight = m->Loudness;
     597        float*        weight = Loudness;
    561598    float         sum    = 0.f;
    562599    int           n;
     
    579616// output: tracked Integrations *a and *b, time constant *tau
    580617static void
    581 CalcTemporalThreshold ( PsyModel* m, float* a, float* b, float* tau, float* frqthr, float* tmpthr )
     618CalcTemporalThreshold ( float* a, float* b, float* tau, float* frqthr, float* tmpthr )
    582619{
    583620    int    n;
     
    587624    for ( n = 0; n < PART_LONG; n++ ) {
    588625        // following calculations relative to threshold in quiet
    589         frqthr[n] *= m->invLtq[n];
    590                 tmpthr[n] *= m->invLtq[n];
     626        frqthr[n] *= invLtq[n];
     627                tmpthr[n] *= invLtq[n];
    591628
    592629        // new post-masking 'tmp' via time constant tau, if old post-masking  > Ltq (=1)
     
    601638
    602639        // use post-masking of (Re-Normalization)
    603                 tmpthr[n] = maxf (frqthr[n], tmp) * m->partLtq[n];
     640                tmpthr[n] = maxf (frqthr[n], tmp) * partLtq[n];
    604641    }
    605642
     
    772809// inline, because it's called 4x
    773810static void
    774 ApplyLtq ( PsyModel* m,
    775                    float*        thr0,
     811ApplyLtq ( float*        thr0,
    776812           float*        thr1,
    777813           const float*  partThr0,
     
    794830#else
    795831            // Applies a much more gentle ATH rolloff + 6 dB more dynamic
    796             ltq   = sqrt (ms * m->fftLtq [k]);
     832            ltq   = sqrt (ms * fftLtq [k]);
    797833            tmp   = sqrt (partThr0 [n]) + ltq;
    798834            *thr0 = tmp * tmp;
     
    10201056    // 'ClearVocalDetection'-Process
    10211057    if ( m->CVD_used ) {
    1022         memset ( m->Vocal_L, 0, sizeof m->Vocal_L );
    1023         memset ( m->Vocal_R, 0, sizeof m->Vocal_R );
     1058        memset ( Vocal_L, 0, sizeof Vocal_L );
     1059        memset ( Vocal_R, 0, sizeof Vocal_R );
    10241060
    10251061        // left channel
    10261062        PowSpec2048 ( &data->L[0], Xerg );
    1027         isvoc_L = CVD2048 ( Xerg, m->Vocal_L );
     1063        isvoc_L = CVD2048 ( m, Xerg, Vocal_L );
    10281064        // right channel
    10291065        PowSpec2048 ( &data->R[0], Xerg );
    1030         isvoc_R = CVD2048 ( Xerg, m->Vocal_R );
     1066        isvoc_R = CVD2048 ( m, Xerg, Vocal_R );
    10311067    }
    10321068
     
    10451081    memmove ( Xsave_L+512, Xsave_L, 1024*sizeof(float) );
    10461082    memmove ( Ysave_L+512, Ysave_L, 1024*sizeof(float) );
    1047     CalcUnpred ( m, MaxLine, erg0, phs0, isvoc_L ? m->Vocal_L : NULL, Xsave_L, Ysave_L, cw_L );
     1083    CalcUnpred ( m, MaxLine, erg0, phs0, isvoc_L ? Vocal_L : NULL, Xsave_L, Ysave_L, cw_L );
    10481084    // right
    10491085    memmove ( Xsave_R+512, Xsave_R, 1024*sizeof(float) );
    10501086    memmove ( Ysave_R+512, Ysave_R, 1024*sizeof(float) );
    1051     CalcUnpred ( m, MaxLine, erg1, phs1, isvoc_R ? m->Vocal_R : NULL, Xsave_R, Ysave_R, cw_R );
     1087    CalcUnpred ( m, MaxLine, erg1, phs1, isvoc_R ? Vocal_R : NULL, Xsave_R, Ysave_R, cw_R );
    10521088
    10531089    // calculation of the weighted acoustic pressures per each partition
     
    10581094    memset ( clow_L    , 0, sizeof clow_L );
    10591095    memset ( sim_Mask_L, 0, sizeof sim_Mask_L );
    1060     SpreadingSignal ( m, Ls_L, cLs_L, sim_Mask_L, clow_L );
     1096    SpreadingSignal ( Ls_L, cLs_L, sim_Mask_L, clow_L );
    10611097    // right
    10621098    memset ( clow_R    , 0, sizeof clow_R );
    10631099    memset ( sim_Mask_R, 0, sizeof sim_Mask_R );
    1064     SpreadingSignal ( m, Ls_R, cLs_R, sim_Mask_R, clow_R );
     1100    SpreadingSignal ( Ls_R, cLs_R, sim_Mask_R, clow_R );
    10651101
    10661102    // Offset depending on tonality
    1067     ApplyTonalityOffset ( m, sim_Mask_L, sim_Mask_R, clow_L, clow_R );
     1103    ApplyTonalityOffset ( sim_Mask_L, sim_Mask_R, clow_L, clow_R );
    10681104
    10691105    // handling of transient signals
     
    10741110    PowSpec256 ( &data->L[432+SHORTFFT_OFFSET], F_256[3] );
    10751111    // calculate short Threshold
    1076     CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_L, m->pre_erg_L, TransientL );
     1112    CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_L, pre_erg_L, TransientL );
    10771113
    10781114    // calculate four short FFTs (right)
     
    10821118    PowSpec256 ( &data->R[432+SHORTFFT_OFFSET], F_256[3] );
    10831119    // calculate short Threshold
    1084     CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_R, m->pre_erg_R, TransientR );
     1120    CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_R, pre_erg_R, TransientR );
    10851121
    10861122    // dynamic adjustment of the threshold in quiet to the loudness of the current sequence
     
    10901126    // utilization of the temporal post-masking
    10911127    if ( m->tmpMask_used ) {
    1092                 CalcTemporalThreshold (m, m->a, m->b, m->T_L, sim_Mask_L, m->tmp_Mask_L );
    1093                 CalcTemporalThreshold (m, m->c, m->d, m->T_R, sim_Mask_R, m->tmp_Mask_R );
    1094                 memcpy ( sim_Mask_L, m->tmp_Mask_L, sizeof sim_Mask_L );
    1095                 memcpy ( sim_Mask_R, m->tmp_Mask_R, sizeof sim_Mask_R );
     1128                CalcTemporalThreshold ( a, b, T_L, sim_Mask_L, tmp_Mask_L );
     1129                CalcTemporalThreshold ( c, d, T_R, sim_Mask_R, tmp_Mask_R );
     1130                memcpy ( sim_Mask_L, tmp_Mask_L, sizeof sim_Mask_L );
     1131                memcpy ( sim_Mask_R, tmp_Mask_R, sizeof sim_Mask_R );
    10961132    }
    10971133
     
    11111147
    11121148    // Pre-Echo control
    1113         PreechoControl ( PartThr_L, m->PreThr_L, sim_Mask_L, PartThr_R, m->PreThr_R, sim_Mask_R );
     1149        PreechoControl ( PartThr_L,PreThr_L, sim_Mask_L, PartThr_R, PreThr_R, sim_Mask_R );
    11141150
    11151151    // utilization of the threshold in quiet
    1116     ApplyLtq ( m, Thr_L, Thr_R, PartThr_L, PartThr_R, factorLTQ, 0 );
     1152    ApplyLtq ( Thr_L, Thr_R, PartThr_L, PartThr_R, factorLTQ, 0 );
    11171153
    11181154    // Consideration of aliasing between the subbands (noise is smeared)
     
    11401176        // calculate masking thresholds for M/S
    11411177        CalcMSThreshold ( m, Ls_L, Ls_R, Ls_M, Ls_S, PartThr_L, PartThr_R, PartThr_M, PartThr_S );
    1142         ApplyLtq ( m, Thr_M, Thr_S, PartThr_M, PartThr_S, factorLTQ, 1 );
     1178        ApplyLtq ( Thr_M, Thr_S, PartThr_M, PartThr_S, factorLTQ, 1 );
    11431179
    11441180        // Consideration of aliasing between the subbands (noise is smeared)
     
    11531189
    11541190        if ( m->NS_Order > 0 ) {       // providing the Noise Shaping thresholds
    1155                 memcpy ( m->ANSspec_L, Thr_L, sizeof m->ANSspec_L );
    1156                 memcpy ( m->ANSspec_R, Thr_R, sizeof m->ANSspec_R );
    1157                 memcpy ( m->ANSspec_M, Thr_M, sizeof m->ANSspec_M );
    1158                 memcpy ( m->ANSspec_S, Thr_S, sizeof m->ANSspec_S );
     1191                memcpy ( ANSspec_L, Thr_L, sizeof ANSspec_L );
     1192                memcpy ( ANSspec_R, Thr_R, sizeof ANSspec_R );
     1193                memcpy ( ANSspec_M, Thr_M, sizeof ANSspec_M );
     1194                memcpy ( ANSspec_S, Thr_S, sizeof ANSspec_S );
    11591195    }
    11601196    /***************************************************************************************/
     
    11781214    memmove ( Xsave_L+512, Xsave_L, 1024*sizeof(float) );
    11791215    memmove ( Ysave_L+512, Ysave_L, 1024*sizeof(float) );
    1180         CalcUnpred ( m, MaxLine, erg0, phs0, isvoc_L ? m->Vocal_L : NULL, Xsave_L, Ysave_L, cw_L );
     1216        CalcUnpred ( m, MaxLine, erg0, phs0, isvoc_L ? Vocal_L : NULL, Xsave_L, Ysave_L, cw_L );
    11811217    // right
    11821218    memmove ( Xsave_R+512, Xsave_R, 1024*sizeof(float) );
    11831219    memmove ( Ysave_R+512, Ysave_R, 1024*sizeof(float) );
    1184         CalcUnpred ( m, MaxLine, erg1, phs1, isvoc_R ? m->Vocal_R : NULL, Xsave_R, Ysave_R, cw_R );
     1220        CalcUnpred ( m, MaxLine, erg1, phs1, isvoc_R ? Vocal_R : NULL, Xsave_R, Ysave_R, cw_R );
    11851221
    11861222    // calculation of the weighted acoustic pressure per each partition
     
    11911227    memset ( clow_L    , 0, sizeof clow_L );
    11921228    memset ( sim_Mask_L, 0, sizeof sim_Mask_L );
    1193     SpreadingSignal ( m, Ls_L, cLs_L, sim_Mask_L, clow_L );
     1229    SpreadingSignal ( Ls_L, cLs_L, sim_Mask_L, clow_L );
    11941230    // right
    11951231    memset ( clow_R    , 0, sizeof clow_R );
    11961232    memset ( sim_Mask_R, 0, sizeof sim_Mask_R );
    1197     SpreadingSignal ( m, Ls_R, cLs_R, sim_Mask_R, clow_R );
     1233    SpreadingSignal ( Ls_R, cLs_R, sim_Mask_R, clow_R );
    11981234
    11991235    // Offset depending on tonality
    1200     ApplyTonalityOffset ( m, sim_Mask_L, sim_Mask_R, clow_L, clow_R );
     1236    ApplyTonalityOffset ( sim_Mask_L, sim_Mask_R, clow_L, clow_R );
    12011237
    12021238    // Handling of transient signals
     
    12071243    PowSpec256 ( &data->L[1008+SHORTFFT_OFFSET], F_256[3] );
    12081244    // calculate short Threshold
    1209         CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_L, m->pre_erg_L, TransientL );
     1245        CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_L, pre_erg_L, TransientL );
    12101246
    12111247    // calculate four short FFTs (right)
     
    12151251    PowSpec256 ( &data->R[1008+SHORTFFT_OFFSET], F_256[3] );
    12161252    // calculate short Threshold
    1217         CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_R, m->pre_erg_R, TransientR );
     1253        CalcShortThreshold ( m, F_256, m->ShortThr, shortThr_R, pre_erg_R, TransientR );
    12181254
    12191255    // dynamic adjustment of threshold in quiet to loudness of the current sequence
     
    12231259    // utilization of temporal post-masking
    12241260        if (m->tmpMask_used) {
    1225                 CalcTemporalThreshold ( m, m->a, m->b, m->T_L, sim_Mask_L, m->tmp_Mask_L );
    1226                 CalcTemporalThreshold ( m, m->c, m->d, m->T_R, sim_Mask_R, m->tmp_Mask_R );
    1227                 memcpy ( sim_Mask_L, m->tmp_Mask_L, sizeof sim_Mask_L );
    1228                 memcpy ( sim_Mask_R, m->tmp_Mask_R, sizeof sim_Mask_R );
     1261                CalcTemporalThreshold ( a, b, T_L, sim_Mask_L, tmp_Mask_L );
     1262                CalcTemporalThreshold ( c, d, T_R, sim_Mask_R, tmp_Mask_R );
     1263                memcpy ( sim_Mask_L, tmp_Mask_L, sizeof sim_Mask_L );
     1264                memcpy ( sim_Mask_R, tmp_Mask_R, sizeof sim_Mask_R );
    12291265    }
    12301266
     
    12441280
    12451281    // Pre-Echo control
    1246         PreechoControl ( PartThr_L, m->PreThr_L, sim_Mask_L, PartThr_R, m->PreThr_R, sim_Mask_R );
     1282        PreechoControl ( PartThr_L, PreThr_L, sim_Mask_L, PartThr_R, PreThr_R, sim_Mask_R );
    12471283
    12481284    // utilization of threshold in quiet
    1249     ApplyLtq ( m, Thr_L, Thr_R, PartThr_L, PartThr_R, factorLTQ, 0 );
     1285    ApplyLtq ( Thr_L, Thr_R, PartThr_L, PartThr_R, factorLTQ, 0 );
    12501286
    12511287    // Consideration of aliasing between the subbands (noise is smeared)
     
    12731309        // calculate masking thresholds for M/S
    12741310        CalcMSThreshold ( m, Ls_L, Ls_R, Ls_M, Ls_S, PartThr_L, PartThr_R, PartThr_M, PartThr_S );
    1275         ApplyLtq ( m, Thr_M, Thr_S, PartThr_M, PartThr_S, factorLTQ, 1 );
     1311        ApplyLtq ( Thr_M, Thr_S, PartThr_M, PartThr_S, factorLTQ, 1 );
    12761312
    12771313        // Consideration of aliasing between the subbands (noise is smeared)
     
    12891325        if ( m->NS_Order > 0 ) {
    12901326        for ( n = 0; n < MAX_ANS_LINES; n++ ) {                 // providing Noise Shaping thresholds
    1291                         m->ANSspec_L [n] = minf ( m->ANSspec_L [n], Thr_L [n] );
    1292                         m->ANSspec_R [n] = minf ( m->ANSspec_R [n], Thr_R [n] );
    1293                         m->ANSspec_M [n] = minf ( m->ANSspec_M [n], Thr_M [n] );
    1294                         m->ANSspec_S [n] = minf ( m->ANSspec_S [n], Thr_S [n] );
     1327                        ANSspec_L [n] = minf ( ANSspec_L [n], Thr_L [n] );
     1328                        ANSspec_R [n] = minf ( ANSspec_R [n], Thr_R [n] );
     1329                        ANSspec_M [n] = minf ( ANSspec_M [n], Thr_M [n] );
     1330                        ANSspec_S [n] = minf ( ANSspec_S [n], Thr_S [n] );
    12951331        }
    12961332    }
Note: See TracChangeset for help on using the changeset viewer.