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

📄 quantize.c

📁 MP3编码程序和资料
💻 C
📖 第 1 页 / 共 3 页
字号:
   *******************************************************************/  analog_silence=1;  for (gr = 0; gr < gfc->mode_gr; gr++) {    int num_chan=gfc->stereo;    /* determine quality based on mid channel only */    if (reduce_s_ch) num_chan=1;      /* copy data to be quantized into xr */    if (gfc->mode_ext==MPG_MD_MS_LR) 	ms_convert(xr[gr],xr[gr]);    for (ch = 0; ch < num_chan; ch++) {       int real_bits;            /******************************************************************       * find smallest number of bits for an allowable quantization       ******************************************************************/      cod_info = &l3_side->gr[gr].ch[ch].tt;      min_bits = Max(125,min_mean_bits);      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));        save_bits[gr][ch] = 0;	if (gfp->gtkflag) {	  memset(xfsf,0,sizeof(xfsf));	  set_pinfo(gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch], xr[gr][ch], xfsf, noise, gr, ch);	}	continue; /* with next channel */      }            memcpy( &clean_cod_info, cod_info, sizeof(gr_info) );            /* disable analog_silence if *any* of the granules != silence */      /* if energy < ATH, set min_bits = 125 */      i = calc_xmin(gfp,xr[gr][ch], &ratio[gr][ch], cod_info, &l3_xmin);      if (i) {	analog_silence=0;      }else{	if (!gfp->VBR_hard_min) min_bits=125;      }      if (cod_info->block_type==SHORT_TYPE) {	  min_bits += Max(1000,pe[gr][ch]);	  min_bits=Min(min_bits,1800);      }      max_bits = 1200 + frameBits[gfc->VBR_max_bitrate]/(gfc->stereo*gfc->mode_gr);      max_bits=Min(max_bits,2500);      max_bits=Max(max_bits,min_bits);      dbits = (max_bits-min_bits)/4;      this_bits = (max_bits+min_bits)/2;      real_bits = max_bits+1;      /* bin search to within +/- 10 bits of optimal */      do {	  assert(this_bits>=min_bits);	  assert(this_bits<=max_bits);	  if( this_bits >= real_bits ){	      /* 	       * we already found a quantization with fewer bits	       * so we can skip this try	       */	      this_bits -= dbits;	      dbits /= 2;	      continue; /* skips the rest of this do-while loop */	  }	  /*	   *  OK, start with a fresh setting	   *  - scalefac  will be set up by outer_loop	   *  - l3_enc    will be set up by outer_loop	   *  + cod_info  we will restore our initialized one, see below	   */	  memcpy( cod_info, &clean_cod_info, sizeof(gr_info) );	  outer_loop( gfp,xr[gr][ch], this_bits, noise, &l3_xmin,                      l3_enc[gr][ch], &scalefac[gr][ch], cod_info, xfsf, ch);	  /* is quantization as good as we are looking for ?           */	  if (noise[0] <= 0) {	      /* 	       * we now know it can be done with "real_bits"	       * and maybe we can skip some iterations	       */	      real_bits = cod_info->part2_3_length;	      /*	       * save best quantization so far	       */              memcpy( &bst_scalefac, &scalefac[gr][ch], sizeof(III_scalefac_t)  );              memcpy(  bst_l3_enc,    l3_enc  [gr][ch], sizeof(int)*576         );              memcpy( &bst_cod_info,  cod_info,         sizeof(gr_info)         );              if (gfp->gtkflag) {	        set_pinfo(gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch], xr[gr][ch], xfsf, noise, gr, ch);                memcpy( &bst_pinfo, gfc->pinfo, sizeof(plotting_data) );	      }	      /*	       * try with fewer bits	       */	      this_bits -= dbits;	  } else {	      /*	       * try with more bits	       */	      this_bits += dbits;	  }	  dbits /= 2;      } while (dbits>10) ;            if (real_bits <= max_bits)      {        /* restore best quantization found */        memcpy(  cod_info,         &bst_cod_info, sizeof(gr_info)        );        memcpy( &scalefac[gr][ch], &bst_scalefac, sizeof(III_scalefac_t) );        memcpy(  l3_enc  [gr][ch],  bst_l3_enc,   sizeof(int)*576        );        if (gfp->gtkflag) {          memcpy( gfc->pinfo, &bst_pinfo, sizeof(plotting_data) );	}      }       else      { /* we didn't find any satisfying quantization above         * the only thing we still need to set is the gtk info field         */        if (gfp->gtkflag)	  set_pinfo(gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch], xr[gr][ch], xfsf, noise, gr, ch);      }      assert((int)cod_info->part2_3_length <= max_bits);      assert((int)cod_info->part2_3_length < 4096);      save_bits[gr][ch] = cod_info->part2_3_length;      used_bits += save_bits[gr][ch];          } /* for ch */  } /* for gr */  if (reduce_s_ch) {    /* number of bits needed was found for MID channel above.  Use formula     * (fixed bitrate code) to set the side channel bits */    for (gr = 0; gr < gfc->mode_gr; gr++) {      FLOAT8 fac = .33*(.5-ms_ener_ratio[gr])/.5;      save_bits[gr][1]=((1-fac)/(1+fac))*save_bits[gr][0];      save_bits[gr][1]=Max(125,save_bits[gr][1]);      used_bits += save_bits[gr][1];    }  }  /******************************************************************   * find lowest bitrate able to hold used bits   ******************************************************************/  for( gfc->bitrate_index =   ((analog_silence && !gfp->VBR_hard_min) ? 1 : gfc->VBR_min_bitrate );       gfc->bitrate_index < gfc->VBR_max_bitrate;       gfc->bitrate_index++    ) {    if( used_bits <= frameBits[gfc->bitrate_index] ) break;  }  /*******************************************************************   * calculate quantization for this bitrate   *******************************************************************/    getframebits (gfp,&bitsPerFrame, &mean_bits);  bits=ResvFrameBegin (gfp,l3_side, mean_bits, bitsPerFrame);  /* repartion available bits in same proportion */  if (used_bits > bits ) {    reparted = 1;    for( gr = 0; gr < gfc->mode_gr; gr++) {      for(ch = 0; ch < gfc->stereo; ch++) {	save_bits[gr][ch]=(save_bits[gr][ch]*frameBits[gfc->bitrate_index])/used_bits;      }    }    used_bits=0;    for( gr = 0; gr < gfc->mode_gr; gr++) {      for(ch = 0; ch < gfc->stereo; ch++) {	used_bits += save_bits[gr][ch];      }    }  }  assert(used_bits <= bits);  for(gr = 0; gr < gfc->mode_gr; gr++) {    for(ch = 0; ch < gfc->stereo; ch++) {      if (reparted || (reduce_s_ch && ch == 1))      {        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	{          calc_xmin(gfp,xr[gr][ch], &ratio[gr][ch], cod_info, &l3_xmin);	          outer_loop( gfp,xr[gr][ch], save_bits[gr][ch], noise,	 	      &l3_xmin, l3_enc[gr][ch], 		      &scalefac[gr][ch], cod_info, xfsf, ch);	}	if (gfp->gtkflag) 	  set_pinfo(gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch], xr[gr][ch], xfsf, noise, gr, ch);      }    }  }  /*******************************************************************   * 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 (gr = 0; gr < gfc->mode_gr; gr++)    for (ch = 0; ch < gfc->stereo; ch++) {      for ( i = 0; i < 576; i++) {        if (xr[gr][ch][i] < 0) l3_enc[gr][ch][i] *= -1;      }    }  ResvFrameEnd (gfp,l3_side, mean_bits);}/************************************************************************//*  init_outer_loop  mt 6/99                                            *//*  returns 0 if all energies in xr are zero, else 1                    *//************************************************************************/int init_outer_loop(lame_global_flags *gfp,    FLOAT8 xr[576],        /*  could be L/R OR MID/SIDE */    gr_info *cod_info){  int i;  for ( i = 0; i < 4; i++ )    cod_info->slen[i] = 0;  cod_info->sfb_partition_table = &nr_of_sfb_block[0][0][0];  cod_info->part2_3_length    = 0;  cod_info->big_values        = 0;  cod_info->count1            = 0;  cod_info->scalefac_compress = 0;  cod_info->table_select[0]   = 0;  cod_info->table_select[1]   = 0;  cod_info->table_select[2]   = 0;  cod_info->subblock_gain[0]  = 0;  cod_info->subblock_gain[1]  = 0;  cod_info->subblock_gain[2]  = 0;  cod_info->region0_count     = 0;  cod_info->region1_count     = 0;  cod_info->part2_length      = 0;  cod_info->preflag           = 0;  cod_info->scalefac_scale    = 0;  cod_info->global_gain       = 210;  cod_info->count1table_select= 0;  cod_info->count1bits        = 0;    /*   *  check if there is some energy we have to quantize   *  if so, then return 1 else 0   */  for (i=0; i<576; i++)     if ( 1e-99 < fabs (xr[i]) )      return 1;    return 0;}/************************************************************************//*  outer_loop                                                         *//************************************************************************//*  Function: The outer iteration loop controls the masking conditions  *//*  of all scalefactorbands. It computes the best scalefac and          *//*  global gain. This module calls the inner iteration loop              *  *  mt 5/99 completely rewritten to allow for bit reservoir control,    *  mid/side channels with L/R or mid/side masking thresholds,  *  and chooses best quantization instead of last quantization when  *  no distortion free quantization can be found.   *   *  added VBR support mt 5/99 ************************************************************************/void outer_loop(    lame_global_flags *gfp,    FLOAT8 xr[576],            int targ_bits,    FLOAT8 best_noise[4],    III_psy_xmin *l3_xmin,   /* the allowed distortion of the scalefactor */    int l3_enc[576],         /* vector of quantized values ix(0..575) */    III_scalefac_t *scalefac, /* scalefactors */    gr_info *cod_info,    FLOAT8 xfsf[4][SBMAX_l],    int ch){  lame_internal_flags *gfc=gfp->internal_flags;  III_scalefac_t scalefac_w;  gr_info save_cod_info;  int l3_enc_w[576];   int i, iteration;  int status,bits_found=0;  int huff_bits;  FLOAT8 xrpow[576],temp;  int better;  int over=0;  calc_noise_result noise_info;  calc_noise_result best_noise_info;  FLOAT8 xfsf_w[4][SBMAX_l];  FLOAT8 distort[4][SBMAX_l];  int notdone=1;  noise_info.over_count = 100;  noise_info.tot_count = 100;  noise_info.max_noise = 0;  noise_info.tot_noise = 0;  noise_info.over_noise = 0;  noise_info.tot_avg_noise = 0;  noise_info.over_avg_noise = 0;  best_noise_info = noise_info;  /* reset of iteration variables */  memset(&scalefac_w, 0, sizeof(III_scalefac_t));  for (i=0;i<576;i++) {    temp=fabs(xr[i]);    xrpow[i]=sqrt(sqrt(temp)*temp);  }    bits_found=bin_search_StepSize2(gfp,targ_bits,gfc->OldValue[ch],				  l3_enc_w,xrpow,cod_info);  gfc->OldValue[ch] = cod_info->global_gain;  /* BEGIN MAIN LOOP */  iteration = 0;  while ( notdone  ) {    ++iteration;    /* inner_loop starts with the initial quantization step computed above     * and slowly increases until the bits < huff_bits.     * Thus it is important not to start with too large of an inital     * quantization step.  Too small is ok, but inner_loop will take longer      */    huff_bits = targ_bits - cod_info->part2_length;    if (huff_bits < 0) {

⌨️ 快捷键说明

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