quantize_pvt.c

Go to the documentation of this file.
00001 /*
00002  *      quantize_pvt source file
00003  *
00004  *      Copyright (c) 1999-2002 Takehiro Tominaga
00005  *      Copyright (c) 2000-2002 Robert Hegemann
00006  *      Copyright (c) 2001 Naoki Shibata
00007  *      Copyright (c) 2002-2005 Gabriel Bouvigne
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  * Boston, MA 02111-1307, USA.
00023  */
00024 
00025 /* $Id: quantize_pvt.c,v 1.146 2007/07/24 17:46:10 bouvigne Exp $ */
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029 
00030 
00031 #include "lame.h"
00032 #include "machine.h"
00033 #include "encoder.h"
00034 #include "util.h"
00035 #include "quantize_pvt.h"
00036 #include "lame_global_flags.h"
00037 #include "reservoir.h"
00038 #include "lame-analysis.h"
00039 #include <float.h>
00040 
00041 
00042 #define NSATHSCALE 100  /* Assuming dynamic range=96dB, this value should be 92 */
00043 
00044 /*
00045   The following table is used to implement the scalefactor
00046   partitioning for MPEG2 as described in section
00047   2.4.3.2 of the IS. The indexing corresponds to the
00048   way the tables are presented in the IS:
00049 
00050   [table_number][row_in_table][column of nr_of_sfb]
00051 */
00052 const int nr_of_sfb_block[6][3][4] = {
00053     {
00054      {6, 5, 5, 5},
00055      {9, 9, 9, 9},
00056      {6, 9, 9, 9}
00057      },
00058     {
00059      {6, 5, 7, 3},
00060      {9, 9, 12, 6},
00061      {6, 9, 12, 6}
00062      },
00063     {
00064      {11, 10, 0, 0},
00065      {18, 18, 0, 0},
00066      {15, 18, 0, 0}
00067      },
00068     {
00069      {7, 7, 7, 0},
00070      {12, 12, 12, 0},
00071      {6, 15, 12, 0}
00072      },
00073     {
00074      {6, 6, 6, 3},
00075      {12, 9, 9, 6},
00076      {6, 12, 9, 6}
00077      },
00078     {
00079      {8, 8, 5, 0},
00080      {15, 12, 9, 0},
00081      {6, 18, 9, 0}
00082      }
00083 };
00084 
00085 
00086 /* Table B.6: layer3 preemphasis */
00087 const int pretab[SBMAX_l] = {
00088     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00089     1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
00090 };
00091 
00092 /*
00093   Here are MPEG1 Table B.8 and MPEG2 Table B.1
00094   -- Layer III scalefactor bands. 
00095   Index into this using a method such as:
00096     idx  = fr_ps->header->sampling_frequency
00097            + (fr_ps->header->version * 3)
00098 */
00099 
00100 
00101 const scalefac_struct sfBandIndex[9] = {
00102     {                   /* Table B.2.b: 22.05 kHz */
00103      {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
00104       522, 576},
00105      {0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192}
00106      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00107      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00108      },
00109     {                   /* Table B.2.c: 24 kHz *//* docs: 332. mpg123(broken): 330 */
00110      {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464,
00111       540, 576},
00112      {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192}
00113      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00114      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00115      },
00116     {                   /* Table B.2.a: 16 kHz */
00117      {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
00118       522, 576},
00119      {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192}
00120      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00121      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00122      },
00123     {                   /* Table B.8.b: 44.1 kHz */
00124      {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418,
00125       576},
00126      {0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192}
00127      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00128      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00129      },
00130     {                   /* Table B.8.c: 48 kHz */
00131      {0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384,
00132       576},
00133      {0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192}
00134      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00135      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00136      },
00137     {                   /* Table B.8.a: 32 kHz */
00138      {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550,
00139       576},
00140      {0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192}
00141      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00142      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00143      },
00144     {                   /* MPEG-2.5 11.025 kHz */
00145      {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
00146       522, 576},
00147      {0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
00148       402 / 3, 522 / 3, 576 / 3}
00149      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00150      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00151      },
00152     {                   /* MPEG-2.5 12 kHz */
00153      {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
00154       522, 576},
00155      {0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
00156       402 / 3, 522 / 3, 576 / 3}
00157      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00158      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00159      },
00160     {                   /* MPEG-2.5 8 kHz */
00161      {0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570,
00162       572, 574, 576},
00163      {0 / 3, 24 / 3, 48 / 3, 72 / 3, 108 / 3, 156 / 3, 216 / 3, 288 / 3, 372 / 3, 480 / 3, 486 / 3,
00164       492 / 3, 498 / 3, 576 / 3}
00165      , {0, 0, 0, 0, 0, 0, 0} //  sfb21 pseudo sub bands
00166      , {0, 0, 0, 0, 0, 0, 0} //  sfb12 pseudo sub bands
00167      }
00168 };
00169 
00170 
00171 
00172 FLOAT   pow20[Q_MAX + Q_MAX2 + 1];
00173 FLOAT   ipow20[Q_MAX];
00174 FLOAT   pow43[PRECALC_SIZE];
00175 /* initialized in first call to iteration_init */
00176 #ifdef TAKEHIRO_IEEE754_HACK
00177 FLOAT   adj43asm[PRECALC_SIZE];
00178 #else
00179 FLOAT   adj43[PRECALC_SIZE];
00180 #endif
00181 
00182 /* 
00183 compute the ATH for each scalefactor band 
00184 cd range:  0..96db
00185 
00186 Input:  3.3kHz signal  32767 amplitude  (3.3kHz is where ATH is smallest = -5db)
00187 longblocks:  sfb=12   en0/bw=-11db    max_en0 = 1.3db
00188 shortblocks: sfb=5           -9db              0db
00189 
00190 Input:  1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
00191 longblocks:  amp=1      sfb=12   en0/bw=-103 db      max_en0 = -92db
00192             amp=32767   sfb=12           -12 db                 -1.4db 
00193 
00194 Input:  1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
00195 shortblocks: amp=1      sfb=5   en0/bw= -99                    -86 
00196             amp=32767   sfb=5           -9  db                  4db 
00197 
00198 
00199 MAX energy of largest wave at 3.3kHz = 1db
00200 AVE energy of largest wave at 3.3kHz = -11db
00201 Let's take AVE:  -11db = maximum signal in sfb=12.  
00202 Dynamic range of CD: 96db.  Therefor energy of smallest audible wave 
00203 in sfb=12  = -11  - 96 = -107db = ATH at 3.3kHz.  
00204 
00205 ATH formula for this wave: -5db.  To adjust to LAME scaling, we need
00206 ATH = ATH_formula  - 103  (db)
00207 ATH = ATH * 2.5e-10      (ener)
00208 
00209 */
00210 
00211 static FLOAT
00212 ATHmdct(lame_global_flags const* gfp, FLOAT f)
00213 {
00214     FLOAT   ath;
00215 
00216     ath = ATHformula(f, gfp);
00217 
00218     if (gfp->psymodel == PSY_NSPSYTUNE) {
00219         ath -= NSATHSCALE;
00220     }
00221     else {
00222         ath -= 114;
00223     }
00224 
00225     /* modify the MDCT scaling for the ATH and convert to energy */
00226     ath = pow(10.0, ath / 10.0 + gfp->ATHlower);
00227     return ath;
00228 }
00229 
00230 static void
00231 compute_ath(lame_global_flags * gfp)
00232 {
00233     FLOAT  *const ATH_l = gfp->internal_flags->ATH->l;
00234     FLOAT  *const ATH_psfb21 = gfp->internal_flags->ATH->psfb21;
00235     FLOAT  *const ATH_s = gfp->internal_flags->ATH->s;
00236     FLOAT  *const ATH_psfb12 = gfp->internal_flags->ATH->psfb12;
00237     lame_internal_flags const *const gfc = gfp->internal_flags;
00238     int     sfb, i, start, end;
00239     FLOAT   ATH_f;
00240     FLOAT const samp_freq = gfp->out_samplerate;
00241 
00242     for (sfb = 0; sfb < SBMAX_l; sfb++) {
00243         start = gfc->scalefac_band.l[sfb];
00244         end = gfc->scalefac_band.l[sfb + 1];
00245         ATH_l[sfb] = FLOAT_MAX;
00246         for (i = start; i < end; i++) {
00247             FLOAT const freq = i * samp_freq / (2 * 576);
00248             ATH_f = ATHmdct(gfp, freq); /* freq in kHz */
00249             ATH_l[sfb] = Min(ATH_l[sfb], ATH_f);
00250         }
00251         if (gfp->psymodel == PSY_GPSYCHO)
00252             ATH_l[sfb] *= (gfc->scalefac_band.l[sfb + 1] - gfc->scalefac_band.l[sfb]);
00253     }
00254 
00255     for (sfb = 0; sfb < PSFB21; sfb++) {
00256         start = gfc->scalefac_band.psfb21[sfb];
00257         end = gfc->scalefac_band.psfb21[sfb + 1];
00258         ATH_psfb21[sfb] = FLOAT_MAX;
00259         for (i = start; i < end; i++) {
00260             FLOAT const freq = i * samp_freq / (2 * 576);
00261             ATH_f = ATHmdct(gfp, freq); /* freq in kHz */
00262             ATH_psfb21[sfb] = Min(ATH_psfb21[sfb], ATH_f);
00263         }
00264     }
00265 
00266     for (sfb = 0; sfb < SBMAX_s; sfb++) {
00267         start = gfc->scalefac_band.s[sfb];
00268         end = gfc->scalefac_band.s[sfb + 1];
00269         ATH_s[sfb] = FLOAT_MAX;
00270         for (i = start; i < end; i++) {
00271             FLOAT const freq = i * samp_freq / (2 * 192);
00272             ATH_f = ATHmdct(gfp, freq); /* freq in kHz */
00273             ATH_s[sfb] = Min(ATH_s[sfb], ATH_f);
00274         }
00275         ATH_s[sfb] *= (gfc->scalefac_band.s[sfb + 1] - gfc->scalefac_band.s[sfb]);
00276     }
00277 
00278     for (sfb = 0; sfb < PSFB12; sfb++) {
00279         start = gfc->scalefac_band.psfb12[sfb];
00280         end = gfc->scalefac_band.psfb12[sfb + 1];
00281         ATH_psfb12[sfb] = FLOAT_MAX;
00282         for (i = start; i < end; i++) {
00283             FLOAT const freq = i * samp_freq / (2 * 192);
00284             ATH_f = ATHmdct(gfp, freq); /* freq in kHz */
00285             ATH_psfb12[sfb] = Min(ATH_psfb12[sfb], ATH_f);
00286         }
00287         /*not sure about the following */
00288         ATH_psfb12[sfb] *= (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]);
00289     }
00290 
00291 
00292     /*  no-ATH mode:
00293      *  reduce ATH to -200 dB
00294      */
00295 
00296     if (gfp->noATH) {
00297         for (sfb = 0; sfb < SBMAX_l; sfb++) {
00298             ATH_l[sfb] = 1E-37;
00299         }
00300         for (sfb = 0; sfb < PSFB21; sfb++) {
00301             ATH_psfb21[sfb] = 1E-37;
00302         }
00303         for (sfb = 0; sfb < SBMAX_s; sfb++) {
00304             ATH_s[sfb] = 1E-37;
00305         }
00306         for (sfb = 0; sfb < PSFB12; sfb++) {
00307             ATH_psfb12[sfb] = 1E-37;
00308         }
00309     }
00310 
00311     /*  work in progress, don't rely on it too much
00312      */
00313     gfc->ATH->floor = 10. * log10(ATHmdct(gfp, -1.));
00314 
00315     /*
00316        {   FLOAT g=10000, t=1e30, x;
00317        for ( f = 100; f < 10000; f++ ) {
00318        x = ATHmdct( gfp, f );
00319        if ( t > x ) t = x, g = f;
00320        }
00321        printf("min=%g\n", g);
00322        } */
00323 }
00324 
00325 
00326 
00327 
00328 /************************************************************************/
00329 /*  initialization for iteration_loop */
00330 /************************************************************************/
00331 void
00332 iteration_init(lame_global_flags * gfp)
00333 {
00334     lame_internal_flags *const gfc = gfp->internal_flags;
00335     III_side_info_t *const l3_side = &gfc->l3_side;
00336     int     i;
00337 
00338     if (gfc->iteration_init_init == 0) {
00339         gfc->iteration_init_init = 1;
00340 
00341         l3_side->main_data_begin = 0;
00342         compute_ath(gfp);
00343 
00344         pow43[0] = 0.0;
00345         for (i = 1; i < PRECALC_SIZE; i++)
00346             pow43[i] = pow((FLOAT) i, 4.0 / 3.0);
00347 
00348 #ifdef TAKEHIRO_IEEE754_HACK
00349         adj43asm[0] = 0.0;
00350         for (i = 1; i < PRECALC_SIZE; i++)
00351             adj43asm[i] = i - 0.5 - pow(0.5 * (pow43[i - 1] + pow43[i]), 0.75);
00352 #else
00353         for (i = 0; i < PRECALC_SIZE - 1; i++)
00354             adj43[i] = (i + 1) - pow(0.5 * (pow43[i] + pow43[i + 1]), 0.75);
00355         adj43[i] = 0.5;
00356 #endif
00357         for (i = 0; i < Q_MAX; i++)
00358             ipow20[i] = pow(2.0, (double) (i - 210) * -0.1875);
00359         for (i = 0; i <= Q_MAX + Q_MAX2; i++)
00360             pow20[i] = pow(2.0, (double) (i - 210 - Q_MAX2) * 0.25);
00361 
00362         huffman_init(gfc);
00363         quantize_init(gfc);
00364         init_xrpow_core_init(gfc);
00365 
00366         if (gfp->psymodel == PSY_NSPSYTUNE) {
00367             FLOAT   bass, alto, treble, sfb21;
00368 
00369             i = (gfp->exp_nspsytune >> 2) & 63;
00370             if (i >= 32)
00371                 i -= 64;
00372             bass = pow(10, i / 4.0 / 10.0);
00373 
00374             i = (gfp->exp_nspsytune >> 8) & 63;
00375             if (i >= 32)
00376                 i -= 64;
00377             alto = pow(10, i / 4.0 / 10.0);
00378 
00379             i = (gfp->exp_nspsytune >> 14) & 63;
00380             if (i >= 32)
00381                 i -= 64;
00382             treble = pow(10, i / 4.0 / 10.0);
00383 
00384             /*  to be compatible with Naoki's original code, the next 6 bits
00385              *  define only the amount of changing treble for sfb21 */
00386             i = (gfp->exp_nspsytune >> 20) & 63;
00387             if (i >= 32)
00388                 i -= 64;
00389             sfb21 = treble * pow(10, i / 4.0 / 10.0);
00390             for (i = 0; i < SBMAX_l; i++) {
00391                 FLOAT   f;
00392                 if (i <= 6)
00393                     f = bass;
00394                 else if (i <= 13)
00395                     f = alto;
00396                 else if (i <= 20)
00397                     f = treble;
00398                 else
00399                     f = sfb21;
00400 
00401                 gfc->nsPsy.longfact[i] = f;
00402             }
00403             for (i = 0; i < SBMAX_s; i++) {
00404                 FLOAT   f;
00405                 if (i <= 5)
00406                     f = bass;
00407                 else if (i <= 10)
00408                     f = alto;
00409                 else if (i <= 11)
00410                     f = treble;
00411                 else
00412                     f = sfb21;
00413 
00414                 gfc->nsPsy.shortfact[i] = f;
00415             }
00416         }
00417         else {
00418             for (i = 0; i < SBMAX_l; i++)
00419                 gfc->nsPsy.longfact[i] = 1.0;
00420             for (i = 0; i < SBMAX_s; i++)
00421                 gfc->nsPsy.shortfact[i] = 1.0;
00422         }
00423     }
00424 }
00425 
00426 
00427 
00428 
00429 
00430 /************************************************************************
00431  * allocate bits among 2 channels based on PE
00432  * mt 6/99
00433  * bugfixes rh 8/01: often allocated more than the allowed 4095 bits
00434  ************************************************************************/
00435 int
00436 on_pe(lame_global_flags const *gfp, FLOAT const pe[][2], III_side_info_t const *l3_side,
00437       int targ_bits[2], int mean_bits, int gr, int cbr)
00438 {
00439     lame_internal_flags const *const gfc = gfp->internal_flags;
00440     gr_info const *cod_info;
00441     int     extra_bits, tbits, bits;
00442     int     add_bits[2];
00443     int     max_bits;        /* maximum allowed bits for this granule */
00444     int     ch;
00445 
00446     /* allocate targ_bits for granule */
00447     ResvMaxBits(gfp, mean_bits, &tbits, &extra_bits, cbr);
00448     max_bits = tbits + extra_bits;
00449     if (max_bits > MAX_BITS_PER_GRANULE) /* hard limit per granule */
00450         max_bits = MAX_BITS_PER_GRANULE;
00451 
00452     for (bits = 0, ch = 0; ch < gfc->channels_out; ++ch) {
00453         /******************************************************************
00454          * allocate bits for each channel 
00455          ******************************************************************/
00456         cod_info = &l3_side->tt[gr][ch];
00457 
00458         targ_bits[ch] = Min(MAX_BITS_PER_CHANNEL, tbits / gfc->channels_out);
00459 
00460         if (gfp->psymodel == PSY_NSPSYTUNE) {
00461             add_bits[ch] = targ_bits[ch] * pe[gr][ch] / 700.0 - targ_bits[ch];
00462         }
00463         else {
00464             add_bits[ch] = (pe[gr][ch] - 750) / 1.4;
00465             /* short blocks us a little extra, no matter what the pe */
00466             if (cod_info->block_type == SHORT_TYPE) {
00467                 if (add_bits[ch] < mean_bits / 4)
00468                     add_bits[ch] = mean_bits / 4;
00469             }
00470         }
00471 
00472         /* at most increase bits by 1.5*average */
00473         if (add_bits[ch] > mean_bits * 3 / 4)
00474             add_bits[ch] = mean_bits * 3 / 4;
00475         if (add_bits[ch] < 0)
00476             add_bits[ch] = 0;
00477 
00478         if (add_bits[ch] + targ_bits[ch] > MAX_BITS_PER_CHANNEL)
00479             add_bits[ch] = Max(0, MAX_BITS_PER_CHANNEL - targ_bits[ch]);
00480 
00481         bits += add_bits[ch];
00482     }
00483     if (bits > extra_bits) {
00484         for (ch = 0; ch < gfc->channels_out; ++ch) {
00485             add_bits[ch] = extra_bits * add_bits[ch] / bits;
00486         }
00487     }
00488 
00489     for (ch = 0; ch < gfc->channels_out; ++ch) {
00490         targ_bits[ch] += add_bits[ch];
00491         extra_bits -= add_bits[ch];
00492     }
00493 
00494     for (bits = 0, ch = 0; ch < gfc->channels_out; ++ch) {
00495         bits += targ_bits[ch];
00496     }
00497     if (bits > MAX_BITS_PER_GRANULE) {
00498         int sum = 0;
00499         for (ch = 0; ch < gfc->channels_out; ++ch) {
00500             targ_bits[ch] *= MAX_BITS_PER_GRANULE;
00501             targ_bits[ch] /= bits;
00502             sum += targ_bits[ch];
00503         }
00504         assert( sum <= MAX_BITS_PER_GRANULE );
00505     }
00506 
00507     return max_bits;
00508 }
00509 
00510 
00511 
00512 
00513 void
00514 reduce_side(int targ_bits[2], FLOAT ms_ener_ratio, int mean_bits, int max_bits)
00515 {
00516     int     move_bits;
00517     FLOAT   fac;
00518 
00519     assert(max_bits <= MAX_BITS_PER_GRANULE);
00520     assert(targ_bits[0]+targ_bits[1] <= MAX_BITS_PER_GRANULE);
00521 
00522     /*  ms_ener_ratio = 0:  allocate 66/33  mid/side  fac=.33  
00523      *  ms_ener_ratio =.5:  allocate 50/50 mid/side   fac= 0 */
00524     /* 75/25 split is fac=.5 */
00525     /* float fac = .50*(.5-ms_ener_ratio[gr])/.5; */
00526     fac = .33 * (.5 - ms_ener_ratio) / .5;
00527     if (fac < 0)
00528         fac = 0;
00529     if (fac > .5)
00530         fac = .5;
00531 
00532     /* number of bits to move from side channel to mid channel */
00533     /*    move_bits = fac*targ_bits[1];  */
00534     move_bits = fac * .5 * (targ_bits[0] + targ_bits[1]);
00535 
00536     if (move_bits > MAX_BITS_PER_CHANNEL - targ_bits[0]) {
00537         move_bits = MAX_BITS_PER_CHANNEL - targ_bits[0];
00538     }
00539     if (move_bits < 0)
00540         move_bits = 0;
00541 
00542     if (targ_bits[1] >= 125) {
00543         /* dont reduce side channel below 125 bits */
00544         if (targ_bits[1] - move_bits > 125) {
00545 
00546             /* if mid channel already has 2x more than average, dont bother */
00547             /* mean_bits = bits per granule (for both channels) */
00548             if (targ_bits[0] < mean_bits)
00549                 targ_bits[0] += move_bits;
00550             targ_bits[1] -= move_bits;
00551         }
00552         else {
00553             targ_bits[0] += targ_bits[1] - 125;
00554             targ_bits[1] = 125;
00555         }
00556     }
00557 
00558     move_bits = targ_bits[0] + targ_bits[1];
00559     if (move_bits > max_bits) {
00560         targ_bits[0] = (max_bits * targ_bits[0]) / move_bits;
00561         targ_bits[1] = (max_bits * targ_bits[1]) / move_bits;
00562     }
00563     assert(targ_bits[0] <= MAX_BITS_PER_CHANNEL);
00564     assert(targ_bits[1] <= MAX_BITS_PER_CHANNEL);
00565     assert(targ_bits[0]+targ_bits[1] <= MAX_BITS_PER_GRANULE);
00566 }
00567 
00568 
00575 FLOAT
00576 athAdjust(FLOAT a, FLOAT x, FLOAT athFloor)
00577 {
00578     /*  work in progress
00579      */
00580     FLOAT const o = 90.30873362;
00581     FLOAT const p = 94.82444863;
00582     FLOAT   u = FAST_LOG10_X(x, 10.0);
00583     FLOAT const v = a * a;
00584     FLOAT   w = 0.0;
00585     u -= athFloor;      /* undo scaling */
00586     if (v > 1E-20)
00587         w = 1. + FAST_LOG10_X(v, 10.0 / o);
00588     if (w < 0)
00589         w = 0.;
00590     u *= w;
00591     u += athFloor + o - p; /* redo scaling */
00592 
00593     return pow(10., 0.1 * u);
00594 }
00595 
00596 
00597 
00598 /*************************************************************************/
00599 /*            calc_xmin                                                  */
00600 /*************************************************************************/
00601 
00602 /*
00603   Calculate the allowed distortion for each scalefactor band,
00604   as determined by the psychoacoustic model.
00605   xmin(sb) = ratio(sb) * en(sb) / bw(sb)
00606 
00607   returns number of sfb's with energy > ATH
00608 */
00609 int
00610 calc_xmin(lame_global_flags const *gfp,
00611           III_psy_ratio const *const ratio, gr_info * const cod_info, FLOAT * pxmin)
00612 {
00613     lame_internal_flags const *const gfc = gfp->internal_flags;
00614     int     sfb, gsfb, j = 0, ath_over = 0, k;
00615     ATH_t const *const ATH = gfc->ATH;
00616     const FLOAT *const xr = cod_info->xr;
00617     int     max_nonzero;
00618     int const enable_athaa_fix = (gfp->VBR == vbr_mtrh) ? 1 : 0;
00619 
00620     for (gsfb = 0; gsfb < cod_info->psy_lmax; gsfb++) {
00621         FLOAT   en0, xmin;
00622         FLOAT   rh1, rh2;
00623         int     width, l;
00624 
00625         if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh)
00626             xmin = athAdjust(ATH->adjust, ATH->l[gsfb], ATH->floor);
00627         else
00628             xmin = ATH->adjust * ATH->l[gsfb];
00629 
00630         width = cod_info->width[gsfb];
00631         rh1 = xmin/width;
00632 #ifdef DBL_EPSILON
00633         rh2 = DBL_EPSILON;
00634 #else
00635         rh2 = 2.2204460492503131e-016;
00636 #endif
00637         l = width >> 1;
00638         en0 = 0.0;
00639         do {
00640             FLOAT xa, xb;
00641             xa = xr[j] * xr[j];
00642             en0 += xa;
00643             rh2 += ( xa < rh1 ) ? xa : rh1;
00644             j++;
00645             xb = xr[j] * xr[j];
00646             en0 += xb;
00647             rh2 += ( xb < rh1 ) ? xb : rh1;
00648             j++;
00649         } while (--l > 0);
00650         if (en0 > xmin)
00651             ath_over++;
00652 
00653         if (gsfb == SBPSY_l) {
00654             FLOAT x = xmin*gfc->nsPsy.longfact[gsfb];
00655             if (rh2 < x) {
00656                 rh2 = x;
00657             }
00658         }
00659         if (enable_athaa_fix) {
00660             xmin = rh2;
00661         }
00662         if (!gfp->ATHonly) {
00663             FLOAT const e = ratio->en.l[gsfb];
00664             if (e > 0.0f) {
00665                 FLOAT   x;
00666                 x = en0 * ratio->thm.l[gsfb] * gfc->masking_lower / e;
00667                 if (enable_athaa_fix)
00668                     x *= gfc->nsPsy.longfact[gsfb];
00669                 if (xmin < x)
00670                     xmin = x;
00671             }
00672         }
00673         if (enable_athaa_fix)
00674             *pxmin++ = xmin;
00675         else 
00676             *pxmin++ = xmin * gfc->nsPsy.longfact[gsfb];
00677     }                   /* end of long block loop */
00678 
00679 
00680 
00681 
00682     /*use this function to determine the highest non-zero coeff */
00683     max_nonzero = 575;
00684     if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT */
00685         k = 576;
00686         while (k-- && !xr[k]) {
00687             max_nonzero = k;
00688         }
00689     }
00690     cod_info->max_nonzero_coeff = max_nonzero;
00691 
00692 
00693 
00694     for (sfb = cod_info->sfb_smin; gsfb < cod_info->psymax; sfb++, gsfb += 3) {
00695         int     width, b;
00696         FLOAT   tmpATH;
00697         if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh)
00698             tmpATH = athAdjust(ATH->adjust, ATH->s[sfb], ATH->floor);
00699         else
00700             tmpATH = ATH->adjust * ATH->s[sfb];
00701 
00702         width = cod_info->width[gsfb];
00703         for (b = 0; b < 3; b++) {
00704             FLOAT   en0 = 0.0, xmin;
00705             FLOAT   rh1, rh2;
00706             int     l = width >> 1;
00707 
00708             rh1 = tmpATH/width;
00709 #ifdef DBL_EPSILON
00710             rh2 = DBL_EPSILON;
00711 #else
00712             rh2 = 2.2204460492503131e-016;
00713 #endif
00714             do {
00715                 FLOAT xa, xb;
00716                 xa = xr[j] * xr[j];
00717                 en0 += xa;
00718                 rh2 += ( xa < rh1 ) ? xa : rh1;
00719                 j++;
00720                 xb = xr[j] * xr[j];
00721                 en0 += xb;
00722                 rh2 += ( xb < rh1 ) ? xb : rh1;
00723                 j++;
00724             } while (--l > 0);
00725             if (en0 > tmpATH)
00726                 ath_over++;
00727             if (sfb == SBPSY_s) {
00728                 FLOAT x = tmpATH*gfc->nsPsy.shortfact[sfb];
00729                 if (rh2 < x) {
00730                     rh2 = x;
00731                 }
00732             }
00733             if (enable_athaa_fix)
00734                 xmin = rh2;
00735             else
00736                 xmin = tmpATH;
00737 
00738             if (!gfp->ATHonly && !gfp->ATHshort) {
00739                 FLOAT const e = ratio->en.s[sfb][b];
00740                 if (e > 0.0f) {
00741                     FLOAT   x;
00742                     x = en0 * ratio->thm.s[sfb][b] * gfc->masking_lower / e;
00743                     if (enable_athaa_fix)
00744                         x *= gfc->nsPsy.shortfact[sfb];
00745                     if (xmin < x)
00746                         xmin = x;
00747                 }
00748             }
00749             if (enable_athaa_fix)
00750                 *pxmin++ = xmin;
00751             else
00752                 *pxmin++ = xmin * gfc->nsPsy.shortfact[sfb];
00753         }               /* b */
00754         if (gfp->useTemporal) {
00755             if (pxmin[-3] > pxmin[-3 + 1])
00756                 pxmin[-3 + 1] += (pxmin[-3] - pxmin[-3 + 1]) * gfc->decay;
00757             if (pxmin[-3 + 1] > pxmin[-3 + 2])
00758                 pxmin[-3 + 2] += (pxmin[-3 + 1] - pxmin[-3 + 2]) * gfc->decay;
00759         }
00760     }                   /* end of short block sfb loop */
00761 
00762     return ath_over;
00763 }
00764 
00765 
00766 FLOAT
00767 calc_noise_core_c(const gr_info * const cod_info, int *startline, int l, FLOAT step)
00768 {
00769     FLOAT   noise = 0;
00770     int     j = *startline;
00771     const int *const ix = cod_info->l3_enc;
00772 
00773     if (j > cod_info->count1) {
00774         while (l--) {
00775             FLOAT   temp;
00776             temp = cod_info->xr[j];
00777             j++;
00778             noise += temp * temp;
00779             temp = cod_info->xr[j];
00780             j++;
00781             noise += temp * temp;
00782         }
00783     }
00784     else if (j > cod_info->big_values) {
00785         FLOAT   ix01[2];
00786         ix01[0] = 0;
00787         ix01[1] = step;
00788         while (l--) {
00789             FLOAT   temp;
00790             temp = fabs(cod_info->xr[j]) - ix01[ix[j]];
00791             j++;
00792             noise += temp * temp;
00793             temp = fabs(cod_info->xr[j]) - ix01[ix[j]];
00794             j++;
00795             noise += temp * temp;
00796         }
00797     }
00798     else {
00799         while (l--) {
00800             FLOAT   temp;
00801             temp = fabs(cod_info->xr[j]) - pow43[ix[j]] * step;
00802             j++;
00803             noise += temp * temp;
00804             temp = fabs(cod_info->xr[j]) - pow43[ix[j]] * step;
00805             j++;
00806             noise += temp * temp;
00807         }
00808     }
00809 
00810     *startline = j;
00811     return noise;
00812 }
00813 
00814 
00815 /*************************************************************************/
00816 /*            calc_noise                                                 */
00817 /*************************************************************************/
00818 
00819 /* -oo dB  =>  -1.00 */
00820 /* - 6 dB  =>  -0.97 */
00821 /* - 3 dB  =>  -0.80 */
00822 /* - 2 dB  =>  -0.64 */
00823 /* - 1 dB  =>  -0.38 */
00824 /*   0 dB  =>   0.00 */
00825 /* + 1 dB  =>  +0.49 */
00826 /* + 2 dB  =>  +1.06 */
00827 /* + 3 dB  =>  +1.68 */
00828 /* + 6 dB  =>  +3.69 */
00829 /* +10 dB  =>  +6.45 */
00830 
00831 int
00832 calc_noise(gr_info const *const cod_info,
00833            FLOAT const *l3_xmin,
00834            FLOAT * distort, calc_noise_result * const res, calc_noise_data * prev_noise)
00835 {
00836     int     sfb, l, over = 0;
00837     FLOAT   over_noise_db = 0;
00838     FLOAT   tot_noise_db = 0; /*    0 dB relative to masking */
00839     FLOAT   max_noise = -20.0; /* -200 dB relative to masking */
00840     int     j = 0;
00841     const int *scalefac = cod_info->scalefac;
00842 
00843     res->over_SSD = 0;
00844 
00845 
00846     for (sfb = 0; sfb < cod_info->psymax; sfb++) {
00847         int const s =
00848             cod_info->global_gain - (((*scalefac++) + (cod_info->preflag ? pretab[sfb] : 0))
00849                                      << (cod_info->scalefac_scale + 1))
00850             - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
00851         FLOAT   noise = 0.0;
00852 
00853         if (prev_noise && (prev_noise->step[sfb] == s)) {
00854 
00855             /* use previously computed values */
00856             noise = prev_noise->noise[sfb];
00857             j += cod_info->width[sfb];
00858             *distort++ = noise / *l3_xmin++;
00859 
00860             noise = prev_noise->noise_log[sfb];
00861 
00862         }
00863         else {
00864             FLOAT const step = POW20(s);
00865             l = cod_info->width[sfb] >> 1;
00866 
00867             if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
00868                 int     usefullsize;
00869                 usefullsize = cod_info->max_nonzero_coeff - j + 1;
00870 
00871                 if (usefullsize > 0)
00872                     l = usefullsize >> 1;
00873                 else
00874                     l = 0;
00875             }
00876 
00877             noise = calc_noise_core_c(cod_info, &j, l, step);
00878 
00879 
00880             if (prev_noise) {
00881                 /* save noise values */
00882                 prev_noise->step[sfb] = s;
00883                 prev_noise->noise[sfb] = noise;
00884             }
00885 
00886             noise = *distort++ = noise / *l3_xmin++;
00887 
00888             /* multiplying here is adding in dB, but can overflow */
00889             noise = FAST_LOG10(Max(noise, 1E-20));
00890 
00891             if (prev_noise) {
00892                 /* save noise values */
00893                 prev_noise->noise_log[sfb] = noise;
00894             }
00895         }
00896 
00897         if (prev_noise) {
00898             /* save noise values */
00899             prev_noise->global_gain = cod_info->global_gain;;
00900         }
00901 
00902 
00903         /*tot_noise *= Max(noise, 1E-20); */
00904         tot_noise_db += noise;
00905 
00906         if (noise > 0.0) {
00907             int     tmp;
00908 
00909             tmp = Max((int) (noise * 10 + .5), 1);
00910             res->over_SSD += tmp * tmp;
00911 
00912             over++;
00913             /* multiplying here is adding in dB -but can overflow */
00914             /*over_noise *= noise; */
00915             over_noise_db += noise;
00916         }
00917         max_noise = Max(max_noise, noise);
00918 
00919     }
00920 
00921     res->over_count = over;
00922     res->tot_noise = tot_noise_db;
00923     res->over_noise = over_noise_db;
00924     res->max_noise = max_noise;
00925 
00926     return over;
00927 }
00928 
00929 
00930 
00931 
00932 
00933 
00934 
00935 
00936 /************************************************************************
00937  *
00938  *  set_pinfo()
00939  *
00940  *  updates plotting data    
00941  *
00942  *  Mark Taylor 2000-??-??                
00943  *
00944  *  Robert Hegemann: moved noise/distortion calc into it
00945  *
00946  ************************************************************************/
00947 
00948 static
00949     void
00950 set_pinfo(lame_global_flags const *gfp,
00951           gr_info * const cod_info, const III_psy_ratio * const ratio, const int gr, const int ch)
00952 {
00953     lame_internal_flags const *const gfc = gfp->internal_flags;
00954     int     sfb, sfb2;
00955     int     j, i, l, start, end, bw;
00956     FLOAT   en0, en1;
00957     FLOAT const ifqstep = (cod_info->scalefac_scale == 0) ? .5 : 1.0;
00958     int const *const scalefac = cod_info->scalefac;
00959 
00960     FLOAT   l3_xmin[SFBMAX], xfsf[SFBMAX];
00961     calc_noise_result noise;
00962 
00963     (void) calc_xmin(gfp, ratio, cod_info, l3_xmin);
00964     (void) calc_noise(cod_info, l3_xmin, xfsf, &noise, 0);
00965 
00966     j = 0;
00967     sfb2 = cod_info->sfb_lmax;
00968     if (cod_info->block_type != SHORT_TYPE && !cod_info->mixed_block_flag)
00969         sfb2 = 22;
00970     for (sfb = 0; sfb < sfb2; sfb++) {
00971         start = gfc->scalefac_band.l[sfb];
00972         end = gfc->scalefac_band.l[sfb + 1];
00973         bw = end - start;
00974         for (en0 = 0.0; j < end; j++)
00975             en0 += cod_info->xr[j] * cod_info->xr[j];
00976         en0 /= bw;
00977         /* convert to MDCT units */
00978         en1 = 1e15;     /* scaling so it shows up on FFT plot */
00979         gfc->pinfo->en[gr][ch][sfb] = en1 * en0;
00980         gfc->pinfo->xfsf[gr][ch][sfb] = en1 * l3_xmin[sfb] * xfsf[sfb] / bw;
00981 
00982         if (ratio->en.l[sfb] > 0 && !gfp->ATHonly)
00983             en0 = en0 / ratio->en.l[sfb];
00984         else
00985             en0 = 0.0;
00986 
00987         gfc->pinfo->thr[gr][ch][sfb] = en1 * Max(en0 * ratio->thm.l[sfb], gfc->ATH->l[sfb]);
00988 
00989         /* there is no scalefactor bands >= SBPSY_l */
00990         gfc->pinfo->LAMEsfb[gr][ch][sfb] = 0;
00991         if (cod_info->preflag && sfb >= 11)
00992             gfc->pinfo->LAMEsfb[gr][ch][sfb] = -ifqstep * pretab[sfb];
00993 
00994         if (sfb < SBPSY_l) {
00995             assert(scalefac[sfb] >= 0); /* scfsi should be decoded by caller side */
00996             gfc->pinfo->LAMEsfb[gr][ch][sfb] -= ifqstep * scalefac[sfb];
00997         }
00998     }                   /* for sfb */
00999 
01000     if (cod_info->block_type == SHORT_TYPE) {
01001         sfb2 = sfb;
01002         for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
01003             start = gfc->scalefac_band.s[sfb];
01004             end = gfc->scalefac_band.s[sfb + 1];
01005             bw = end - start;
01006             for (i = 0; i < 3; i++) {
01007                 for (en0 = 0.0, l = start; l < end; l++) {
01008                     en0 += cod_info->xr[j] * cod_info->xr[j];
01009                     j++;
01010                 }
01011                 en0 = Max(en0 / bw, 1e-20);
01012                 /* convert to MDCT units */
01013                 en1 = 1e15; /* scaling so it shows up on FFT plot */
01014 
01015                 gfc->pinfo->en_s[gr][ch][3 * sfb + i] = en1 * en0;
01016                 gfc->pinfo->xfsf_s[gr][ch][3 * sfb + i] = en1 * l3_xmin[sfb2] * xfsf[sfb2] / bw;
01017                 if (ratio->en.s[sfb][i] > 0)
01018                     en0 = en0 / ratio->en.s[sfb][i];
01019                 else
01020                     en0 = 0.0;
01021                 if (gfp->ATHonly || gfp->ATHshort)
01022                     en0 = 0;
01023 
01024                 gfc->pinfo->thr_s[gr][ch][3 * sfb + i] =
01025                     en1 * Max(en0 * ratio->thm.s[sfb][i], gfc->ATH->s[sfb]);
01026 
01027                 /* there is no scalefactor bands >= SBPSY_s */
01028                 gfc->pinfo->LAMEsfb_s[gr][ch][3 * sfb + i]
01029                     = -2.0 * cod_info->subblock_gain[i];
01030                 if (sfb < SBPSY_s) {
01031                     gfc->pinfo->LAMEsfb_s[gr][ch][3 * sfb + i] -= ifqstep * scalefac[sfb2];
01032                 }
01033                 sfb2++;
01034             }
01035         }
01036     }                   /* block type short */
01037     gfc->pinfo->LAMEqss[gr][ch] = cod_info->global_gain;
01038     gfc->pinfo->LAMEmainbits[gr][ch] = cod_info->part2_3_length + cod_info->part2_length;
01039     gfc->pinfo->LAMEsfbits[gr][ch] = cod_info->part2_length;
01040 
01041     gfc->pinfo->over[gr][ch] = noise.over_count;
01042     gfc->pinfo->max_noise[gr][ch] = noise.max_noise * 10.0;
01043     gfc->pinfo->over_noise[gr][ch] = noise.over_noise * 10.0;
01044     gfc->pinfo->tot_noise[gr][ch] = noise.tot_noise * 10.0;
01045     gfc->pinfo->over_SSD[gr][ch] = noise.over_SSD;
01046 }
01047 
01048 
01049 /************************************************************************
01050  *
01051  *  set_frame_pinfo()
01052  *
01053  *  updates plotting data for a whole frame  
01054  *
01055  *  Robert Hegemann 2000-10-21                          
01056  *
01057  ************************************************************************/
01058 
01059 void
01060 set_frame_pinfo(lame_global_flags const *gfp, III_psy_ratio const ratio[2][2])
01061 {
01062     lame_internal_flags *const gfc = gfp->internal_flags;
01063     int     ch;
01064     int     gr;
01065 
01066     gfc->masking_lower = 1.0;
01067 
01068     /* for every granule and channel patch l3_enc and set info
01069      */
01070     for (gr = 0; gr < gfc->mode_gr; gr++) {
01071         for (ch = 0; ch < gfc->channels_out; ch++) {
01072             gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
01073             int     scalefac_sav[SFBMAX];
01074             memcpy(scalefac_sav, cod_info->scalefac, sizeof(scalefac_sav));
01075 
01076             /* reconstruct the scalefactors in case SCFSI was used 
01077              */
01078             if (gr == 1) {
01079                 int     sfb;
01080                 for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
01081                     if (cod_info->scalefac[sfb] < 0) /* scfsi */
01082                         cod_info->scalefac[sfb] = gfc->l3_side.tt[0][ch].scalefac[sfb];
01083                 }
01084             }
01085 
01086             set_pinfo(gfp, cod_info, &ratio[gr][ch], gr, ch);
01087             memcpy(cod_info->scalefac, scalefac_sav, sizeof(scalefac_sav));
01088         }               /* for ch */
01089     }                   /* for gr */
01090 }

Generated on Sun Dec 2 11:34:20 2007 for LAME by  doxygen 1.5.2