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

📄 lame.c

📁 MP3编码程序和资料
💻 C
📖 第 1 页 / 共 4 页
字号:
/* *	LAME MP3 encoding engine * *	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. */#include <assert.h>#include "gtkanal.h"#include "lame.h"#include "util.h"#include "timestatus.h"#include "psymodel.h"#include "newmdct.h"#include "quantize.h"#include "quantize-pvt.h"#include "bitstream.h"#include "version.h"#include "VbrTag.h"#include "id3tag.h"#include "tables.h"#include "brhist.h"#include "get_audio.h"#ifdef __riscos__#include "asmstuff.h"#endif/******************************************************************** *   initialize internal params based on data in gf *   (globalflags struct filled in by calling program) * ********************************************************************/int lame_init_params(lame_global_flags *gfp){  int i;  lame_internal_flags *gfc=gfp->internal_flags;  gfc->lame_init_params_init=1;  memset(&gfc->bs, 0, sizeof(Bit_stream_struc));  memset(&gfc->l3_side,0x00,sizeof(III_side_info_t));  memset((char *) gfc->mfbuf, 0, sizeof(short)*2*MFSIZE);  /* The reason for   *       int mf_samples_to_encode = ENCDELAY + 288;   * ENCDELAY = internal encoder delay.  And then we have to add 288   * because of the 50% MDCT overlap.  A 576 MDCT granule decodes to   * 1152 samples.  To synthesize the 576 samples centered under this granule   * we need the previous granule for the first 288 samples (no problem), and   * the next granule for the next 288 samples (not possible if this is last   * granule).  So we need to pad with 288 samples to make sure we can   * encode the 576 samples we are interested in.   */  gfc->mf_samples_to_encode = ENCDELAY+288;  gfc->mf_size=ENCDELAY-MDCTDELAY;  /* we pad input with this many 0's */  gfp->frameNum=0;  if (gfp->num_channels==1) {    gfp->mode = MPG_MD_MONO;  }  gfc->stereo=2;  if (gfp->mode == MPG_MD_MONO) gfc->stereo=1;  if (gfp->silent) {   gfp->brhist_disp=0;  /* turn of VBR historgram */  }  if (gfp->VBR==vbr_off) {    gfp->brhist_disp=0;  /* turn of VBR historgram */  }  if (gfp->VBR!=vbr_off) {    gfp->free_format=0;  /* VBR cant mix with free format */  }  if (gfp->VBR==vbr_off && gfp->brate==0) {    /* no bitrate or compression ratio specified, use 11 */    if (gfp->compression_ratio==0) gfp->compression_ratio=11;  }  /* find bitrate if user specify a compression ratio */  if (gfp->VBR==vbr_off && gfp->compression_ratio > 0) {        if (gfp->out_samplerate==0)       gfp->out_samplerate=validSamplerate(gfp->in_samplerate);           /* choose a bitrate for the output samplerate which achieves     * specifed compression ratio      */    gfp->brate =       gfp->out_samplerate*16*gfc->stereo/(1000.0*gfp->compression_ratio);    /* we need the version for the bitrate table look up */    gfc->samplerate_index = SmpFrqIndex((long)gfp->out_samplerate, &gfp->version);    /* find the nearest allowed bitrate */    if (!gfp->free_format)      gfp->brate = FindNearestBitrate(gfp->brate,gfp->version,gfp->out_samplerate);  }  if (gfp->brate >= 320) gfp->VBR=vbr_off;  /* dont bother with VBR at 320kbs */  /* set the output sampling rate, and resample options if necessary     samplerate = input sample rate     resamplerate = ouput sample rate  */  if (gfp->out_samplerate==0) {    /* user did not specify output sample rate */    gfp->out_samplerate=gfp->in_samplerate;   /* default */    /* if resamplerate is not valid, find a valid value */    gfp->out_samplerate = validSamplerate(gfp->out_samplerate);    if (gfp->VBR==vbr_off && gfp->brate>0) {      /* check if user specified bitrate requires downsampling */      gfp->compression_ratio = gfp->out_samplerate*16*gfc->stereo/(1000.0*gfp->brate);      if (gfp->compression_ratio > 13 ) {	/* automatic downsample, if possible */	gfp->out_samplerate = validSamplerate((10*1000L*gfp->brate)/(16*gfc->stereo));      }    }    if (gfp->VBR==vbr_abr) {      /* check if user specified bitrate requires downsampling */      gfp->compression_ratio = gfp->out_samplerate*16*gfc->stereo/(1000.0*gfp->VBR_mean_bitrate_kbps);      if (gfp->compression_ratio > 13 ) {	/* automatic downsample, if possible */	gfp->out_samplerate = validSamplerate((10*1000L*gfp->VBR_mean_bitrate_kbps)/(16*gfc->stereo));      }    }  }  gfc->mode_gr = (gfp->out_samplerate <= 24000) ? 1 : 2;  /* mode_gr = 2 */  gfp->encoder_delay = ENCDELAY;  gfp->framesize = gfc->mode_gr*576;  if (gfp->ogg) gfp->framesize = 1024;  gfc->resample_ratio=1;  if (gfp->out_samplerate != gfp->in_samplerate)         gfc->resample_ratio = (FLOAT)gfp->in_samplerate/(FLOAT)gfp->out_samplerate;  /* estimate total frames.  must be done after setting sampling rate so   * we know the framesize.  */  gfp->totalframes=0;  gfp->totalframes = 2+ gfp->num_samples/(gfc->resample_ratio*gfp->framesize);  /* 44.1kHz at 56kbs/channel: compression factor of 12.6     44.1kHz at 64kbs/channel: compression factor of 11.025     44.1kHz at 80kbs/channel: compression factor of 8.82     22.05kHz at 24kbs:  14.7     22.05kHz at 32kbs:  11.025     22.05kHz at 40kbs:  8.82     16kHz at 16kbs:  16.0     16kHz at 24kbs:  10.7     compression_ratio        11                                .70?        12                   sox resample .66        14.7                 sox resample .45  */  /* for VBR, take a guess at the compression_ratio. for example: */  /* VBR_q           compression       like                      4.4             320kbs/41khz     0-1              5.5             256kbs/41khz     2                7.3             192kbs/41khz     4                8.8             160kbs/41khz     6                11              128kbs/41khz     9                14.7             96kbs     for lower bitrates, downsample with --resample  */  if (gfp->VBR==vbr_mt || gfp->VBR==vbr_rh) {    gfp->compression_ratio = 5.0 + gfp->VBR_q;  }else  if (gfp->VBR==vbr_abr) {    gfp->compression_ratio = gfp->out_samplerate*16*gfc->stereo/(1000.0*gfp->VBR_mean_bitrate_kbps);  }else{    gfp->compression_ratio = gfp->out_samplerate*16*gfc->stereo/(1000.0*gfp->brate);  }  /* At higher quality (lower compression) use STEREO instead of JSTEREO.   * (unless the user explicitly specified a mode ) */  if ( (!gfp->mode_fixed) && (gfp->mode !=MPG_MD_MONO)) {    if (gfp->compression_ratio < 9 ) {      gfp->mode = MPG_MD_STEREO;    }  }  /****************************************************************/  /* if a filter has not been enabled, see if we should add one: */  /****************************************************************/  if (gfp->lowpassfreq == 0) {    /* If the user has not selected their own filter, add a lowpass     * filter based on the compression ratio.  Formula based on          44.1   /160    4.4x          44.1   /128    5.5x      keep all bands          44.1   /96kbs  7.3x      keep band 28          44.1   /80kbs  8.8x      keep band 25          44.1khz/64kbs  11x       keep band 21  22?	  16khz/24kbs  10.7x       keep band 21	  22kHz/32kbs  11x         keep band ?	  22kHz/24kbs  14.7x       keep band 16          16    16     16x         keep band 14    */    /* Should we use some lowpass filters? */    int band = 1+floor(.5 + 14-18*log(gfp->compression_ratio/16.0));    if (gfc->resample_ratio != 1) {      /* resampling.  if we are resampling, add lowpass at least 90% */      band = Min(band,29);    }    if (band < 31) {      gfc->lowpass1 = band/31.0;      gfc->lowpass2 = band/31.0;    }  }  /****************************************************************/  /* apply user driven filters*/  /****************************************************************/  if ( gfp->highpassfreq > 0 ) {    gfc->highpass1 = 2.0*gfp->highpassfreq/gfp->out_samplerate; /* will always be >=0 */    if ( gfp->highpasswidth >= 0 ) {      gfc->highpass2 = 2.0*(gfp->highpassfreq+gfp->highpasswidth)/gfp->out_samplerate;    } else {      /* 15% above on default */      /* gfc->highpass2 = 1.15*2.0*gfp->highpassfreq/gfp->out_samplerate;  */      gfc->highpass2 = 1.00*2.0*gfp->highpassfreq/gfp->out_samplerate;     }  }  if ( gfp->lowpassfreq > 0 ) {    gfc->lowpass2 = 2.0*gfp->lowpassfreq/gfp->out_samplerate; /* will always be >=0 */    if ( gfp->lowpasswidth >= 0 ) {      gfc->lowpass1 = 2.0*(gfp->lowpassfreq-gfp->lowpasswidth)/gfp->out_samplerate;      if ( gfc->lowpass1 < 0 ) { /* has to be >= 0 */	gfc->lowpass1 = 0;      }    } else {      /* 15% below on default */      /* gfc->lowpass1 = 0.85*2.0*gfp->lowpassfreq/gfp->out_samplerate;  */      gfc->lowpass1 = 1.00*2.0*gfp->lowpassfreq/gfp->out_samplerate;    }  }  /***************************************************************/  /* compute info needed for polyphase filter (filter type==0, default)   */  /***************************************************************/  {    int band,maxband,minband;    FLOAT8 freq;    if (gfc->lowpass1 > 0) {      minband=999;      maxband=-1;      for (band=0;  band <=31 ; ++band) { 	freq = band/31.0;	gfc->amp_lowpass[band] = 1;	/* this band and above will be zeroed: */	if (freq >= gfc->lowpass2) {	  gfc->lowpass_band= Min(gfc->lowpass_band,band);	  gfc->amp_lowpass[band]=0;	}	if (gfc->lowpass1 < freq && freq < gfc->lowpass2) {          minband = Min(minband,band);          maxband = Max(maxband,band);	  gfc->amp_lowpass[band] = cos((PI/2)*(gfc->lowpass1-freq)/(gfc->lowpass2-gfc->lowpass1));	}	/*	DEBUGF("lowpass band=%i  amp=%f \n",band,gfc->amp_lowpass[band]);	*/      }      /* compute the *actual* transition band implemented by the polyphase filter */      if (minband==999) gfc->lowpass1 = (gfc->lowpass_band-.75)/31.0;      else gfc->lowpass1 = (minband-.75)/31.0;      gfc->lowpass2 = gfc->lowpass_band/31.0;            gfc->lowpass_start_band = minband;      gfc->lowpass_end_band   = maxband;            /* as the lowpass may have changed above       * calculate the amplification here again       */      for (band=minband;  band <=maxband; ++band) { 	freq = band/31.0;	gfc->amp_lowpass[band] = cos((PI/2)*(gfc->lowpass1-freq)/(gfc->lowpass2-gfc->lowpass1));      }    } else {      gfc->lowpass_start_band = 0;      gfc->lowpass_end_band   = -1;  /* do not to run into for-loops */    }    /* make sure highpass filter is within 90% of what the effective highpass     * frequency will be */    if (gfc->highpass2 > 0)       if (gfc->highpass2 <  .9*(.75/31.0) ) {	gfc->highpass1=0; gfc->highpass2=0;	MSGF("Warning: highpass filter disabled.  highpass frequency to small\n");      }        if (gfc->highpass2 > 0) {      minband=999;      maxband=-1;      for (band=0;  band <=31; ++band) { 	freq = band/31.0;	gfc->amp_highpass[band] = 1;	/* this band and below will be zereod */	if (freq <= gfc->highpass1) {	  gfc->highpass_band = Max(gfc->highpass_band,band);	  gfc->amp_highpass[band]=0;	}	if (gfc->highpass1 < freq && freq < gfc->highpass2) {          minband = Min(minband,band);          maxband = Max(maxband,band);	  gfc->amp_highpass[band] = cos((PI/2)*(gfc->highpass2-freq)/(gfc->highpass2-gfc->highpass1));	}	/*		DEBUGF("highpass band=%i  amp=%f \n",band,gfc->amp_highpass[band]);	*/      }      /* compute the *actual* transition band implemented by the polyphase filter */      gfc->highpass1 = gfc->highpass_band/31.0;      if (maxband==-1) gfc->highpass2 = (gfc->highpass_band+.75)/31.0;      else gfc->highpass2 = (maxband+.75)/31.0;            gfc->highpass_start_band = minband;      gfc->highpass_end_band   = maxband;      /* as the highpass may have changed above       * calculate the amplification here again       */      for (band=minband;  band <=maxband; ++band) { 	freq = band/31.0;	gfc->amp_highpass[band] = cos((PI/2)*(gfc->highpass2-freq)/(gfc->highpass2-gfc->highpass1));      }    } else {      gfc->highpass_start_band = 0;      gfc->highpass_end_band   = -1;  /* do not to run into for-loops */    }    /*    DEBUGF("lowpass band with amp=0:  %i \n",gfc->lowpass_band);    DEBUGF("highpass band with amp=0:  %i \n",gfc->highpass_band);    DEBUGF("lowpass band start:  %i \n",gfc->lowpass_start_band);    DEBUGF("lowpass band end:    %i \n",gfc->lowpass_end_band);    DEBUGF("highpass band start:  %i \n",gfc->highpass_start_band);    DEBUGF("highpass band end:    %i \n",gfc->highpass_end_band);    */  }  /***************************************************************/  /* compute info needed for FIR filter (filter_type==1) */  /***************************************************************/  gfc->mode_ext=MPG_MD_LR_LR;  gfc->stereo = (gfp->mode == MPG_MD_MONO) ? 1 : 2;

⌨️ 快捷键说明

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