rminnichinitial1fcdf7916 years agoHistory
2273 lines · 61.7 KB · C
RawDownload
/* *	psymodel.c * *	Copyright (c) 1999 Mark Taylor * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* $Id: psymodel.c,v 1.75 2001/03/25 21:37:00 shibatch Exp $ */  /*PSYCHO ACOUSTICS  This routine computes the psycho acoustics, delayed byone granule.   Input: buffer of PCM data (1024 samples).   This window should be centered over the 576 sample granule window.The routine will compute the psycho acoustics forthis granule, but return the psycho acoustics computedfor the *previous* granule.  This is because the blocktype of the previous granule can only be determinedafter we have computed the psycho acoustics for the followinggranule.   Output:  maskings and energies for each scalefactor band.block type, PE, and some correlation measures.  The PE is used by CBR modes to determine if extra bitsfrom the bit reservoir should be used.  The correlationmeasures are used to determine mid/side or regular stereo.  Notation: barks:  a non-linear frequency scale.  Mapping from frequency to        barks is given by freq2bark() scalefactor bands: The spectrum (frequencies) are broken into                    SBMAX "scalefactor bands".  Thes bands                   are determined by the MPEG ISO spec.  In                   the noise shaping/quantization code, we allocate                   bits among the partition bands to achieve the                   best possible quality partition bands:   The spectrum is also broken into about                   64 "partition bands".  Each partition                    band is about .34 barks wide.  There are about 2-5                   partition bands for each scalefactor band. LAME computes all psycho acoustic information for each partitionband.  Then at the end of the computations, this informationis mapped to scalefactor bands.  The energy in each scalefactorband is taken as the sum of the energy in all partition bandswhich overlap the scalefactor band.  The maskings can be computedin the same way (and thus represent the average masking in that band)or by taking the minmum value multiplied by the number ofpartition bands used (which represents a minimum masking in that band).  The general outline is as follows:  1. compute the energy in each partition band2. compute the tonality in each partition band3. compute the strength of each partion band "masker"4. compute the masking (via the spreading function applied to each masker)5. Modifications for mid/side masking.   Each partition band is considiered a "masker".  The strengthof the i'th masker in band j is given by:     s3(bark(i)-bark(j))*strength(i) The strength of the masker is a function of the energy and tonality.The more tonal, the less masking.  LAME uses a simple linear formula(controlled by NMT and TMN) which says the strength is given by theenergy divided by a linear function of the tonality.  s3() is the "spreading function".  It is given by a formuladetermined via listening tests.   The total masking in the j'th partition band is the sum overall maskings i.  It is thus given by the convolution ofthe strength with s3(), the "spreading function." masking(j) = sum_over_i  s3(i-j)*strength(i)  = s3 o strength where "o" = convolution operator.  s3 is given by a formula determinedvia listening tests.  It is normalized so that s3 o 1 = 1. Note: instead of a simple convolution, LAME also has theoption of using "additive masking" The most critical part is step 2, computing the tonality of eachpartition band.  LAME has two tonality estimators.  The firstis based on the ISO spec, and measures how predictiable thesignal is over time.  The more predictable, the more tonal.The second measure is based on looking at the spectrum ofa single granule.  The more peaky the spectrum, the moretonal.  By most indications, the latter approach is better. Finally, in step 5, the maskings for the mid and sidechannel are possibly increased.  Under certain circumstances,noise in the mid & side channels is assumed to alsobe masked by strong maskers in the L or R channels.  Other data computed by the psy-model: ms_ratio        side-channel / mid-channel masking ratio (for previous granule)ms_ratio_next   side-channel / mid-channel masking ratio for this granule percep_entropy[2]     L and R values (prev granule) of PE - A measure of how                       much pre-echo is in the previous granulepercep_entropy_MS[2]  mid and side channel values (prev granule) of percep_entropyenergy[4]             L,R,M,S energy in each channel, prev granuleblocktype_d[2]        block type to use for previous granule  */    #ifdef HAVE_CONFIG_H# include <config.h>#endif #include "util.h"#include "encoder.h"#include "psymodel.h"#include "l3side.h"#include <assert.h>#include "tables.h"#include "fft.h" #ifdef WITH_DMALLOC#include <dmalloc.h>#endif #define NSFIRLEN 21#define rpelev 2#define rpelev2 16 /* size of each partition band, in barks: */#define DELBARK .34  #if 1    /* AAC values, results in more masking over MP3 values */# define TMN 18# define NMT 6#else    /* MP3 values */# define TMN 29# define NMT 6#endif #define NBPSY_l  (SBMAX_l)#define NBPSY_s  (SBMAX_s)  #ifdef M_LN10#define		LN_TO_LOG10		(M_LN10/10)#else#define         LN_TO_LOG10             0.2302585093#endif     /*   L3psycho_anal.  Compute psycho acoustics.    Data returned to the calling program must be delayed by one    granule.     This is done in two places.     If we do not need to know the blocktype, the copying   can be done here at the top of the program: we copy the data for   the last granule (computed during the last call) before it is   overwritten with the new data.  It looks like this:     0. static psymodel_data    1. calling_program_data = psymodel_data   2. compute psymodel_data       For data which needs to know the blocktype, the copying must be   done at the end of this loop, and the old values must be saved:      0. static psymodel_data_old    1. compute psymodel_data   2. compute possible block type of this granule   3. compute final block type of previous granule based on #2.   4. calling_program_data = psymodel_data_old   5. psymodel_data_old = psymodel_data*/int L3psycho_anal( lame_global_flags * gfp,                    const sample_t *buffer[2], int gr_out,                     FLOAT8 *ms_ratio,                    FLOAT8 *ms_ratio_next,		    III_psy_ratio masking_ratio[2][2],		    III_psy_ratio masking_MS_ratio[2][2],		    FLOAT8 percep_entropy[2],FLOAT8 percep_MS_entropy[2],                     FLOAT8 energy[4],                    int blocktype_d[2]){/* to get a good cache performance, one has to think about * the sequence, in which the variables are used.   * (Note: these static variables have been moved to the gfc-> struct, * and their order in memory is layed out in util.h) */  lame_internal_flags *gfc=gfp->internal_flags;    /* fft and energy calculation   */  FLOAT (*wsamp_l)[BLKSIZE];  FLOAT (*wsamp_s)[3][BLKSIZE_s];   /* convolution   */  FLOAT8 eb[CBANDS];  FLOAT8 cb[CBANDS];  FLOAT8 thr[CBANDS];    /* ratios    */  FLOAT8 ms_ratio_l=0,ms_ratio_s=0;   /* block type  */  int blocktype[2],uselongblock[2];    /* usual variables like loop indices, etc..    */  int numchn, chn;  int   b, i, j, k;  int	sb,sblock;    if(gfc->psymodel_init==0) {      psymodel_init(gfp);      init_fft(gfc);      gfc->psymodel_init=1;  }          numchn = gfc->channels_out;  /* chn=2 and 3 = Mid and Side channels */  if (gfp->mode == JOINT_STEREO) numchn=4;   for (chn=0; chn<numchn; chn++) {      for (i=0; i<numchn; ++i) {	  energy[chn]=gfc->tot_ener[chn];      }  }    for (chn=0; chn<numchn; chn++) {       /* there is a one granule delay.  Copy maskings computed last call       * into masking_ratio to return to calling program.       */      if (chn < 2) {    	  /* LR maskings  */	  percep_entropy            [chn]       = gfc -> pe  [chn]; 	  masking_ratio    [gr_out] [chn]  .en  = gfc -> en  [chn];	  masking_ratio    [gr_out] [chn]  .thm = gfc -> thm [chn];      } else {	  /* MS maskings  */	  percep_MS_entropy         [chn-2]     = gfc -> pe  [chn]; 	  masking_MS_ratio [gr_out] [chn-2].en  = gfc -> en  [chn];	  masking_MS_ratio [gr_out] [chn-2].thm = gfc -> thm [chn];      }                 /**********************************************************************     *  compute FFTs     **********************************************************************/    wsamp_s = gfc->wsamp_S+(chn & 1);    wsamp_l = gfc->wsamp_L+(chn & 1);    if (chn<2) {          fft_long ( gfc, *wsamp_l, chn, buffer);      fft_short( gfc, *wsamp_s, chn, buffer);    }     /* FFT data for mid and side channel is derived from L & R */    if (chn == 2)      {        for (j = BLKSIZE-1; j >=0 ; --j)        {          FLOAT l = gfc->wsamp_L[0][j];          FLOAT r = gfc->wsamp_L[1][j];          gfc->wsamp_L[0][j] = (l+r)*(FLOAT)(SQRT2*0.5);          gfc->wsamp_L[1][j] = (l-r)*(FLOAT)(SQRT2*0.5);        }        for (b = 2; b >= 0; --b)        {          for (j = BLKSIZE_s-1; j >= 0 ; --j)          {            FLOAT l = gfc->wsamp_S[0][b][j];            FLOAT r = gfc->wsamp_S[1][b][j];            gfc->wsamp_S[0][b][j] = (l+r)*(FLOAT)(SQRT2*0.5);            gfc->wsamp_S[1][b][j] = (l-r)*(FLOAT)(SQRT2*0.5);          }        }      }      /**********************************************************************     *  compute energies     **********************************************************************/                gfc->energy[0]  = (*wsamp_l)[0];    gfc->energy[0] *= gfc->energy[0];        gfc->tot_ener[chn] = gfc->energy[0]; /* sum total energy at nearly no extra cost */        for (j=BLKSIZE/2-1; j >= 0; --j)    {      FLOAT re = (*wsamp_l)[BLKSIZE/2-j];      FLOAT im = (*wsamp_l)[BLKSIZE/2+j];      gfc->energy[BLKSIZE/2-j] = (re * re + im * im) * (FLOAT)0.5;       if (BLKSIZE/2-j > 10)	gfc->tot_ener[chn] += gfc->energy[BLKSIZE/2-j];    }    for (b = 2; b >= 0; --b)    {      gfc->energy_s[b][0]  = (*wsamp_s)[b][0];      gfc->energy_s[b][0] *=  gfc->energy_s [b][0];      for (j=BLKSIZE_s/2-1; j >= 0; --j)      {        FLOAT re = (*wsamp_s)[b][BLKSIZE_s/2-j];        FLOAT im = (*wsamp_s)[b][BLKSIZE_s/2+j];        gfc->energy_s[b][BLKSIZE_s/2-j] = (re * re + im * im) * (FLOAT)0.5;      }    }    if (gfp->analysis) {    for (j=0; j<HBLKSIZE ; j++) {      gfc->pinfo->energy[gr_out][chn][j]=gfc->energy_save[chn][j];      gfc->energy_save[chn][j]=gfc->energy[j];    }  }        /**********************************************************************     *    compute unpredicatability of first six spectral lines            *      **********************************************************************/    for ( j = 0; j < gfc->cw_lower_index; j++ )      {	 /* calculate unpredictability measure cw */	FLOAT an, a1, a2;	FLOAT bn, b1, b2;	FLOAT rn, r1, r2;	FLOAT numre, numim, den; 	a2 = gfc-> ax_sav[chn][1][j];	b2 = gfc-> bx_sav[chn][1][j];	r2 = gfc-> rx_sav[chn][1][j];	a1 = gfc-> ax_sav[chn][1][j] = gfc-> ax_sav[chn][0][j];	b1 = gfc-> bx_sav[chn][1][j] = gfc-> bx_sav[chn][0][j];	r1 = gfc-> rx_sav[chn][1][j] = gfc-> rx_sav[chn][0][j];	an = gfc-> ax_sav[chn][0][j] = (*wsamp_l)[j];	bn = gfc-> bx_sav[chn][0][j] = j==0 ? (*wsamp_l)[0] : (*wsamp_l)[BLKSIZE-j];  	rn = gfc-> rx_sav[chn][0][j] = sqrt(gfc->energy[j]); 	{ /* square (x1,y1) */	  if( r1 != 0 ) {	    numre = (a1*b1);	    numim = (a1*a1-b1*b1)*(FLOAT)0.5;	    den = r1*r1;	  } else {	    numre = 1;	    numim = 0;	    den = 1;	  }	}		{ /* multiply by (x2,-y2) */	  if( r2 != 0 ) {	    FLOAT tmp2 = (numim+numre)*(a2+b2)*(FLOAT)0.5;	    FLOAT tmp1 = -a2*numre+tmp2;	    numre =       -b2*numim+tmp2;	    numim = tmp1;	    den *= r2;	  } else {	    /* do nothing */	  }	}		{ /* r-prime factor */	  FLOAT tmp = (2*r1-r2)/den;	  numre *= tmp;	  numim *= tmp;	}	den=rn+fabs(2*r1-r2);	if( den != 0 ) {	  numre = (an+bn)*(FLOAT)0.5-numre;	  numim = (an-bn)*(FLOAT)0.5-numim;	  den = sqrt(numre*numre+numim*numim)/den;	}	gfc->cw[j] = den;      }       /**********************************************************************     *     compute unpredicatibility of next 200 spectral lines            *     **********************************************************************/     for ( j = gfc->cw_lower_index; j < gfc->cw_upper_index; j += 4 )      {/* calculate unpredictability measure cw */	FLOAT rn, r1, r2;	FLOAT numre, numim, den;		k = (j+2) / 4; 		{ /* square (x1,y1) */	  r1 = gfc->energy_s[0][k];	  if( r1 != 0 ) {	    FLOAT a1 = (*wsamp_s)[0][k]; 	    FLOAT b1 = (*wsamp_s)[0][BLKSIZE_s-k]; /* k is never 0 */	    numre = (a1*b1);	    numim = (a1*a1-b1*b1)*(FLOAT)0.5;	    den = r1;	    r1 = sqrt(r1);	  } else {	    numre = 1;	    numim = 0;	    den = 1;	  }	}			{ /* multiply by (x2,-y2) */	  r2 = gfc->energy_s[2][k];	  if( r2 != 0 ) {	    FLOAT a2 = (*wsamp_s)[2][k]; 	    FLOAT b2 = (*wsamp_s)[2][BLKSIZE_s-k];	    	    	    FLOAT tmp2 = (numim+numre)*(a2+b2)*(FLOAT)0.5;	    FLOAT tmp1 = -a2*numre+tmp2;	    numre =       -b2*numim+tmp2;	    numim = tmp1;	    	    r2 = sqrt(r2);	    den *= r2;	  } else {	    /* do nothing */	  }	}		{ /* r-prime factor */	  FLOAT tmp = (2*r1-r2)/den;	  numre *= tmp;	  numim *= tmp;	}		rn = sqrt(gfc->energy_s[1][k]);	den=rn+fabs(2*r1-r2);	if( den != 0 ) {	  FLOAT an = (*wsamp_s)[1][k]; 	  FLOAT bn = (*wsamp_s)[1][BLKSIZE_s-k];	  numre = (an+bn)*(FLOAT)0.5-numre;	  numim = (an-bn)*(FLOAT)0.5-numim;	  den = sqrt(numre*numre+numim*numim)/den;	}		gfc->cw[j+1] = gfc->cw[j+2] = gfc->cw[j+3] = gfc->cw[j] = den;      }    #if 0    for ( j = 14; j < HBLKSIZE-4; j += 4 )      {/* calculate energy from short ffts */	FLOAT8 tot,ave;	k = (j+2) / 4; 	for (tot=0, sblock=0; sblock < 3; sblock++)	  tot+=gfc->energy_s[sblock][k];	ave = gfc->energy[j+1]+ gfc->energy[j+2]+ gfc->energy[j+3]+ gfc->energy[j];	ave /= 4.;	gfc->energy[j+1] = gfc->energy[j+2] = gfc->energy[j+3] =  gfc->energy[j]=tot;

Lines 1–500 of 2273.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t