⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 音频编码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *	lame utility library source file * *	Copyright (c) 1999 Albert L Faber *	Copyright (c) 2000-2005 Alexander Leidinger * * 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: util.c,v 1.117.2.1 2005/11/20 14:08:25 bouvigne Exp $ */#ifdef HAVE_CONFIG_H# include <config.h>#endif#define PRECOMPUTE#include "util.h"#include <ctype.h>#include <assert.h>#include <stdarg.h>#if defined(__FreeBSD__) && !defined(__alpha__)# include <machine/floatingpoint.h>#endif#ifdef WITH_DMALLOC#include <dmalloc.h>#endif/*************************************************************************  Global Function Definitions************************************************************************//*empty and close mallocs in gfc */void  freegfc ( lame_internal_flags* const gfc )   /* bit stream structure */{    int  i;    for ( i = 0 ; i <= 2*BPC; i++ )        if ( gfc->blackfilt[i] != NULL ) {            free ( gfc->blackfilt[i] );	    gfc->blackfilt[i] = NULL;	}    if ( gfc->inbuf_old[0] ) {         free ( gfc->inbuf_old[0] );	gfc->inbuf_old[0] = NULL;    }    if ( gfc->inbuf_old[1] ) {         free ( gfc->inbuf_old[1] );	gfc->inbuf_old[1] = NULL;    }    if ( gfc->bs.buf != NULL ) {        free ( gfc->bs.buf );        gfc->bs.buf = NULL;    }    if ( gfc->VBR_seek_table.bag ) {        free ( gfc->VBR_seek_table.bag );        gfc->VBR_seek_table.bag=NULL;        gfc->VBR_seek_table.size=0;    }    if ( gfc->ATH ) {        free ( gfc->ATH );    }    if ( gfc->PSY ) {        free ( gfc->PSY );    }    if ( gfc->rgdata ) {        free ( gfc->rgdata );    }    if ( gfc->s3_ll ) {        /* XXX allocated in psymodel_init() */        free ( gfc->s3_ll );    }    if ( gfc->s3_ss ) {        /* XXX allocated in psymodel_init() */        free ( gfc->s3_ss );    }    if ( gfc->in_buffer_0 ) {        free ( gfc->in_buffer_0 );    }    if ( gfc->in_buffer_1 ) {        free ( gfc->in_buffer_1 );    }    free ( gfc );}/*those ATH formulas are returningtheir minimum value for input = -1*/FLOAT ATHformula_GB(FLOAT f, FLOAT value){  /* from Painter & Spanias    modified by Gabriel Bouvigne to better fit the reality  ath =    3.640 * pow(f,-0.8)         - 6.800 * exp(-0.6*pow(f-3.4,2.0))         + 6.000 * exp(-0.15*pow(f-8.7,2.0))         + 0.6* 0.001 * pow(f,4.0);  In the past LAME was using the Painter &Spanias formula.  But we had some recurrent problems with HF content.  We measured real ATH values, and found the older formula  to be inacurate in the higher part. So we made this new  formula and this solved most of HF problematic testcases.  The tradeoff is that in VBR mode it increases a lot the  bitrate.*//*this curve can be udjusted according to the VBR scale:it adjusts from something close to Painter & Spaniason V9 up to Bouvigne's formula for V0. This way the VBRbitrate is more balanced according to the -V value.*/  FLOAT ath;  if (f < -.3)      f=3410;  f /= 1000;  /* convert to khz */  f  = Max(0.01, f);/*  f  = Min(21.0, f);*/  ath =    3.640 * pow(f,-0.8)         - 6.800 * exp(-0.6*pow(f-3.4,2.0))         + 6.000 * exp(-0.15*pow(f-8.7,2.0))         + (0.6+0.04*value)* 0.001 * pow(f,4.0);  return ath;}FLOAT ATHformula(FLOAT f,lame_global_flags *gfp){  switch(gfp->ATHtype)    {    case 0:      return ATHformula_GB(f, 9);    case 1:        return ATHformula_GB(f, -1); /*over sensitive, should probably be removed*/    case 2:      return ATHformula_GB(f, 0);    case 3:      return ATHformula_GB(f, 1) + 6;     /*modification of GB formula by Roel*/    case 4:      return ATHformula_GB(f,gfp->ATHcurve);    }  return ATHformula_GB(f, 0);}/* see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7 */FLOAT freq2bark(FLOAT freq){  /* input: freq in hz  output: barks */    if (freq<0) freq=0;    freq = freq * 0.001;    return 13.0*atan(.76*freq) + 3.5*atan(freq*freq/(7.5*7.5));}/* see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7 */FLOAT freq2cbw(FLOAT freq){  /* input: freq in hz  output: critical band width */    freq = freq * 0.001;    return 25+75*pow(1+1.4*(freq*freq),0.69);}#define ABS(A) (((A)>0) ? (A) : -(A))int FindNearestBitrate(int bRate,        /* legal rates from 8 to 320 */int version)      /* MPEG-1 or MPEG-2 LSF */{    int  bitrate = 0;    int  i;      for ( i = 1; i <= 14; i++ )        if ( ABS (bitrate_table[version][i] - bRate) < ABS (bitrate - bRate) )            bitrate = bitrate_table [version] [i];	        return bitrate;}#ifndef Min#define         Min(A, B)       ((A) < (B) ? (A) : (B))#endif#ifndef Max#define         Max(A, B)       ((A) > (B) ? (A) : (B))#endif/* Used to find table index when * we need bitrate-based values * determined using tables * * bitrate in kbps * * Gabriel Bouvigne 2002-11-03 */int nearestBitrateFullIndex(const int bitrate){    /* borrowed from DM abr presets*/        int index; /* resolved range */    const int bitrate_table[] = {8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320};    int lower_range = 0, lower_range_kbps = 0,        upper_range = 0, upper_range_kbps = 0;        int b;    /* We assume specified bitrate will be 320kbps */    upper_range_kbps = bitrate_table[16];    upper_range = 16;    lower_range_kbps = bitrate_table[16];    lower_range = 16;     /* Determine which significant bitrates the value specified falls between,     * if loop ends without breaking then we were correct above that the value was 320     */    for (b = 0; b < 16; b++) {        if ((Max(bitrate, bitrate_table[b+1])) != bitrate) {              upper_range_kbps = bitrate_table[b+1];              upper_range = b+1;              lower_range_kbps = bitrate_table[b];              lower_range = (b);              break; /* We found upper range */        }    }    /* Determine which range the value specified is closer to */    if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps))        index = lower_range;    else        index = upper_range;    return index;}/* map frequency to a valid MP3 sample frequency * * Robert Hegemann 2000-07-01 */int map2MP3Frequency(int freq){    if (freq <=  8000) return  8000;    if (freq <= 11025) return 11025;    if (freq <= 12000) return 12000;    if (freq <= 16000) return 16000;    if (freq <= 22050) return 22050;    if (freq <= 24000) return 24000;    if (freq <= 32000) return 32000;    if (freq <= 44100) return 44100;        return 48000;}int BitrateIndex(int bRate,        /* legal rates from 32 to 448 kbps */int version,      /* MPEG-1 or MPEG-2/2.5 LSF */int samplerate)   /* convert bitrate in kbps to index */{    int  i;    for ( i = 0; i <= 14; i++)        if ( bitrate_table [version] [i] == bRate )            return i;	        return -1;}/* convert samp freq in Hz to index */int SmpFrqIndex ( int sample_freq, int* const version ){    switch ( sample_freq ) {    case 44100: *version = 1; return  0;    case 48000: *version = 1; return  1;    case 32000: *version = 1; return  2;    case 22050: *version = 0; return  0;    case 24000: *version = 0; return  1;    case 16000: *version = 0; return  2;    case 11025: *version = 0; return  0;    case 12000: *version = 0; return  1;    case  8000: *version = 0; return  2;    default:    *version = 0; return -1;    }}/*******************************************************************************  End of bit_stream.c package******************************************************************************//* resampling via FIR filter, blackman window */inline static FLOAT blackman(FLOAT x,FLOAT fcn,int l){  /* This algorithm from:SIGNAL PROCESSING ALGORITHMS IN FORTRAN AND CS.D. Stearns and R.A. David, Prentice-Hall, 1992  */  FLOAT bkwn,x2;  FLOAT wcn = (PI * fcn);    x /= l;  if (x<0) x=0;  if (x>1) x=1;  x2 = x - .5;  bkwn = 0.42 - 0.5*cos(2*x*PI)  + 0.08*cos(4*x*PI);  if (fabs(x2)<1e-9) return wcn/PI;  else     return  (  bkwn*sin(l*wcn*x2)  / (PI*l*x2)  );}/* gcd - greatest common divisor *//* Joint work of Euclid and M. Hendry */int gcd ( int i, int j ){/*    assert ( i > 0  &&  j > 0 ); */    return j ? gcd(j, i % j) : i;}/* copy in new samples from in_buffer into mfbuf, with resampling   if necessary.  n_in = number of samples from the input buffer that   were used.  n_out = number of samples copied into mfbuf  */void fill_buffer(lame_global_flags *gfp,		 sample_t *mfbuf[2],		 sample_t *in_buffer[2],		 int nsamples, int *n_in, int *n_out){    lame_internal_flags *gfc = gfp->internal_flags;    int ch,i;    /* copy in new samples into mfbuf, with resampling if necessary */    if ( (gfc->resample_ratio < .9999) || (gfc->resample_ratio > 1.0001) ){	for (ch = 0; ch < gfc->channels_out; ch++) {	    *n_out =		fill_buffer_resample(gfp, &mfbuf[ch][gfc->mf_size],				     gfp->framesize, in_buffer[ch],				     nsamples, n_in, ch);	}    }    else {	*n_out = Min(gfp->framesize, nsamples);	*n_in = *n_out;	for (i = 0; i < *n_out; ++i) {	    mfbuf[0][gfc->mf_size + i] = in_buffer[0][i];	    if (gfc->channels_out == 2)		mfbuf[1][gfc->mf_size + i] = in_buffer[1][i];	}    }}    int fill_buffer_resample(       lame_global_flags *gfp,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -