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

📄 quantize.c

📁 MP3编码程序和资料
💻 C
📖 第 1 页 / 共 3 页
字号:
#undef MAXNOISE/* *	MP3 quantization * *	Copyright (c) 1999 Mark Taylor * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING.  If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */#include <assert.h>#include "util.h"#include "l3side.h"#include "quantize.h"#include "reservoir.h"#include "quantize-pvt.h"#include "gtkanal.h"/************************************************************************//*  iteration_loop()                                                    *//************************************************************************/voiditeration_loop( lame_global_flags *gfp,                FLOAT8 pe[2][2], FLOAT8 ms_ener_ratio[2],		FLOAT8 xr[2][2][576], III_psy_ratio ratio[2][2],		int l3_enc[2][2][576],		III_scalefac_t scalefac[2][2]){  lame_internal_flags *gfc=gfp->internal_flags;  FLOAT8 xfsf[4][SBMAX_l];  FLOAT8 noise[4]; /* over,max_noise,over_noise,tot_noise; */  III_psy_xmin l3_xmin[2];  gr_info *cod_info;  int bitsPerFrame;  int mean_bits,max_bits;  int ch, gr, i, bit_rate;  III_side_info_t *l3_side;  l3_side = &gfc->l3_side;  iteration_init(gfp,l3_side,l3_enc);  bit_rate = bitrate_table[gfp->version][gfc->bitrate_index];  getframebits(gfp,&bitsPerFrame, &mean_bits);  ResvFrameBegin(gfp, l3_side, mean_bits, bitsPerFrame );  /* quantize! */  for ( gr = 0; gr < gfc->mode_gr; gr++ ) {    int targ_bits[2];    if (gfc->mode_ext==MPG_MD_MS_LR)       ms_convert(xr[gr], xr[gr]);        max_bits=on_pe(gfp,pe,l3_side,targ_bits,mean_bits, gr);    if (gfc->mode_ext==MPG_MD_MS_LR)       reduce_side(targ_bits,ms_ener_ratio[gr],mean_bits,max_bits);        for (ch=0 ; ch < gfc->stereo ; ch ++) {      cod_info = &l3_side->gr[gr].ch[ch].tt;	      if (!init_outer_loop(gfp,xr[gr][ch], cod_info))        {          /* xr contains no energy            * cod_info-> was initialized in init_outer_loop	   */          memset(&scalefac[gr][ch],0,sizeof(III_scalefac_t));          memset(l3_enc[gr][ch],0,576*sizeof(int));	  memset(xfsf,0,sizeof(xfsf));	  noise[0]=noise[1]=noise[2]=noise[3]=0;        }      else	{          calc_xmin(gfp,xr[gr][ch], &ratio[gr][ch], cod_info, &l3_xmin[ch]);	  outer_loop( gfp,xr[gr][ch], targ_bits[ch], noise,		      &l3_xmin[ch], l3_enc[gr][ch], 		      &scalefac[gr][ch], cod_info, xfsf, ch);        }      best_scalefac_store(gfp,gr, ch, l3_enc, l3_side, scalefac);      if (gfc->use_best_huffman==1 && cod_info->block_type != SHORT_TYPE) {	best_huffman_divide(gfc, gr, ch, cod_info, l3_enc[gr][ch]);      }      assert((int)cod_info->part2_3_length < 4096);      if (gfp->gtkflag)	set_pinfo (gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch], xr[gr][ch], xfsf, noise, gr, ch);/*#define NORES_TEST */#ifndef NORES_TEST      ResvAdjust(gfp,cod_info, l3_side, mean_bits );#endif      /* set the sign of l3_enc */      for ( i = 0; i < 576; i++) {	if (xr[gr][ch][i] < 0)	  l3_enc[gr][ch][i] *= -1;      }    } /* loop over ch */  } /* loop over gr */#ifdef NORES_TEST  /* replace ResvAdjust above with this code if you do not want     the second granule to use bits saved by the first granule.     when combined with --nores, this is usefull for testing only */  for ( gr = 0; gr < gfc->mode_gr; gr++ ) {    for ( ch =  0; ch < gfc->stereo; ch++ ) {	cod_info = &l3_side->gr[gr].ch[ch].tt;	ResvAdjust(gfp, cod_info, l3_side, mean_bits );    }  }#endif  ResvFrameEnd(gfp,l3_side, mean_bits );}/* *  ABR_iteration_loop() * *  encode a frame with a disired average bitrate * *  mt 2000/05/31 */voidABR_iteration_loop (lame_global_flags *gfp,                FLOAT8 pe[2][2], FLOAT8 ms_ener_ratio[2],                FLOAT8 xr[2][2][576], III_psy_ratio ratio[2][2],                int l3_enc[2][2][576],                III_scalefac_t scalefac[2][2]){  lame_internal_flags *gfc=gfp->internal_flags;    III_psy_xmin l3_xmin;  gr_info  *cod_info = NULL;  int       targ_bits[2][2];  FLOAT8    xfsf[4][SBMAX_l];  FLOAT8    noise[4];  int       bit_rate,bitsPerFrame, mean_bits,totbits,max_frame_bits;  int       i,ch, gr, ath_over;  int       analog_silence_bits;  III_side_info_t *l3_side;  l3_side = &gfc->l3_side;  iteration_init(gfp,l3_side,l3_enc);  gfc->bitrate_index = gfc->VBR_max_bitrate;  getframebits (gfp,&bitsPerFrame, &mean_bits);  max_frame_bits=ResvFrameBegin (gfp,l3_side, mean_bits, bitsPerFrame);  gfc->bitrate_index = 1;  getframebits (gfp,&bitsPerFrame, &mean_bits);  analog_silence_bits = mean_bits/gfc->stereo;  /* compute a target  mean_bits based on compression ratio    * which was set based on VBR_q     */  /*  bit_rate = gfp->out_samplerate*16*gfc->stereo/(1000.0*gfp->compression_ratio);   */  bit_rate = gfp->VBR_mean_bitrate_kbps;  bitsPerFrame = (bit_rate*gfp->framesize*1000)/gfp->out_samplerate;  mean_bits = (bitsPerFrame - 8*gfc->sideinfo_len) / gfc->mode_gr;  for(gr = 0; gr < gfc->mode_gr; gr++) {    for(ch = 0; ch < gfc->stereo; ch++) {      if (pe[gr][ch]<750) {	targ_bits[gr][ch]=(mean_bits/gfc->stereo)*Max(.5,(pe[gr][ch])/750.0);      }else{	int add_bits=(pe[gr][ch]-750)/1.4;		cod_info = &l3_side->gr[gr].ch[ch].tt;	targ_bits[gr][ch]=(mean_bits/gfc->stereo);		/* short blocks us a little extra, no matter what the pe */	if (cod_info->block_type==SHORT_TYPE) {	  if (add_bits<mean_bits/4) add_bits=mean_bits/4;	}	/* at most increase bits by 1.5*average */	if (add_bits > .75*mean_bits) add_bits=mean_bits*.75;	if (add_bits < 0) add_bits=0;		targ_bits[gr][ch] += add_bits;      }    }  }  if (gfc->mode_ext==MPG_MD_MS_LR)     for(gr = 0; gr < gfc->mode_gr; gr++)       reduce_side(targ_bits[gr],ms_ener_ratio[gr],mean_bits,4095);  totbits=0;  for(gr = 0; gr < gfc->mode_gr; gr++) {    for(ch = 0; ch < gfc->stereo; ch++) {      if (targ_bits[gr][ch] > 4095) targ_bits[gr][ch]=4095;      totbits += targ_bits[gr][ch];    }  }  if (totbits > max_frame_bits) {    for(gr = 0; gr < gfc->mode_gr; gr++)       for(ch = 0; ch < gfc->stereo; ch++) 	targ_bits[gr][ch] *= ((float)max_frame_bits/(float)totbits);   }  totbits=0;  for(gr = 0; gr < gfc->mode_gr; gr++) {    if (gfc->mode_ext==MPG_MD_MS_LR)       ms_convert(xr[gr], xr[gr]);    for(ch = 0; ch < gfc->stereo; ch++) {      cod_info = &l3_side->gr[gr].ch[ch].tt;      if (!init_outer_loop(gfp,xr[gr][ch], cod_info))        {          /* xr contains no energy            * cod_info was set in init_outer_loop above	   */          memset(&scalefac[gr][ch],0,sizeof(III_scalefac_t));          memset(l3_enc[gr][ch],0,576*sizeof(int));	  noise[0]=noise[1]=noise[2]=noise[3]=0;        }      else	{	  ath_over = calc_xmin(gfp,xr[gr][ch], &ratio[gr][ch], cod_info, &l3_xmin);	  if (0==ath_over) {	    /* analog silence */	    targ_bits[gr][ch]=analog_silence_bits;	  }	  outer_loop( gfp,xr[gr][ch], targ_bits[gr][ch], noise,		      &l3_xmin, l3_enc[gr][ch], 		      &scalefac[gr][ch], cod_info, xfsf, ch);	}      totbits += cod_info->part2_3_length;      if (gfp->gtkflag) 	set_pinfo(gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch], xr[gr][ch], xfsf, noise, gr, ch);    }  }  /*******************************************************************   * find a bitrate which can handle totbits    *******************************************************************/  for( gfc->bitrate_index =  gfc->VBR_min_bitrate ;       gfc->bitrate_index <= gfc->VBR_max_bitrate;       gfc->bitrate_index++    ) {    getframebits (gfp,&bitsPerFrame, &mean_bits);    max_frame_bits=ResvFrameBegin (gfp,l3_side, mean_bits, bitsPerFrame);    if( totbits <= max_frame_bits) break;  }  assert (gfc->bitrate_index <= gfc->VBR_max_bitrate);  /*******************************************************************   * update reservoir status after FINAL quantization/bitrate    *******************************************************************/  for (gr = 0; gr < gfc->mode_gr; gr++)    for (ch = 0; ch < gfc->stereo; ch++) {      cod_info = &l3_side->gr[gr].ch[ch].tt;      best_scalefac_store(gfp,gr, ch, l3_enc, l3_side, scalefac);      if (gfc->use_best_huffman==1 && cod_info->block_type != SHORT_TYPE) {	best_huffman_divide(gfc, gr, ch, cod_info, l3_enc[gr][ch]);      }      if (gfp->gtkflag) {	gfc->pinfo->LAMEmainbits[gr][ch]=cod_info->part2_3_length;      }      ResvAdjust (gfp,cod_info, l3_side, mean_bits);      /* set the sign of l3_enc from the sign of xr */      for ( i = 0; i < 576; i++) {	if (xr[gr][ch][i] < 0) l3_enc[gr][ch][i] *= -1;      }    }  ResvFrameEnd (gfp,l3_side, mean_bits);}/************************************************************************ * * VBR_iteration_loop()    * * tries to find out how many bits are needed for each granule and channel * to get an acceptable quantization. An appropriate bitrate will then be * choosed for quantization.  rh 8/99                                                 * ************************************************************************/voidVBR_iteration_loop (lame_global_flags *gfp,                FLOAT8 pe[2][2], FLOAT8 ms_ener_ratio[2],                FLOAT8 xr[2][2][576], III_psy_ratio ratio[2][2],                int l3_enc[2][2][576],                III_scalefac_t scalefac[2][2]){  plotting_data bst_pinfo;  lame_internal_flags *gfc=gfp->internal_flags;  gr_info         bst_cod_info, clean_cod_info;  III_scalefac_t  bst_scalefac;  int             bst_l3_enc[576];     III_psy_xmin l3_xmin;  gr_info  *cod_info = NULL;  int       save_bits[2][2];  FLOAT8    noise[4];          /* over,max_noise,over_noise,tot_noise; */  FLOAT8    xfsf[4][SBMAX_l];  int       this_bits, dbits;  int       used_bits=0;  int       min_bits,max_bits,min_mean_bits=0;  int       frameBits[15];  int       bitsPerFrame;  int       bits;  int       mean_bits;  int       i,ch, gr, analog_silence;  int	    reparted = 0;  int       reduce_s_ch=0;  III_side_info_t *l3_side;  l3_side = &gfc->l3_side;  iteration_init(gfp,l3_side,l3_enc);    /* my experiences are, that side channel reduction       * does more harm than good when VBR encoding     * (Robert.Hegemann@gmx.de 2000-02-18)  if (gfc->mode_ext==MPG_MD_MS_LR)     reduce_s_ch=1;     */  /*******************************************************************   * how many bits are available for each bitrate?   *******************************************************************/  for( gfc->bitrate_index = 1;       gfc->bitrate_index <= gfc->VBR_max_bitrate;       gfc->bitrate_index++    ) {    getframebits (gfp,&bitsPerFrame, &mean_bits);    if (gfc->bitrate_index == gfc->VBR_min_bitrate) {      /* always use at least this many bits per granule per channel */      /* unless we detect analog silence, see below */      min_mean_bits=mean_bits/gfc->stereo;    }    frameBits[gfc->bitrate_index]=      ResvFrameBegin (gfp,l3_side, mean_bits, bitsPerFrame);  }  gfc->bitrate_index=gfc->VBR_max_bitrate;    /*******************************************************************   * how many bits would we use of it?

⌨️ 快捷键说明

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