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

📄 quantize.c

📁 音频编码
💻 C
📖 第 1 页 / 共 5 页
字号:
            huff_bits = targ_bits - cod_info_w.part2_length;            if (huff_bits <= 0)                break;	        /*  increase quantizer stepsize until needed bits are below maximum	         */	        while ((cod_info_w.part2_3_length		        = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > huff_bits	               && cod_info_w.global_gain <= maxggain)	            cod_info_w.global_gain++;	        if (cod_info_w.global_gain > maxggain)	            break;            if (best_noise_info.over_count == 0) {	            while ((cod_info_w.part2_3_length		            = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > best_part2_3_length	                   && cod_info_w.global_gain <= maxggain)	                cod_info_w.global_gain++;	            if (cod_info_w.global_gain > maxggain)	                break;            }                                /* compute the distortion in this quantization */	        over = calc_noise (gfc, &cod_info_w, l3_xmin, distort, &noise_info, &prev_noise);            noise_info.bits = cod_info_w.part2_3_length;                /* check if this quantization is better                 * than our saved quantization */	        if (cod_info->block_type == NORM_TYPE)	            better = gfp->quant_comp;            else	            better = gfp->quant_comp_short;	        better = quant_compare(better, gfc, &best_noise_info, &noise_info,			               &cod_info_w, distort);            /* save data so we can restore this quantization later */	        if (better) {                best_part2_3_length = cod_info->part2_3_length;	            best_noise_info = noise_info;	            *cod_info = cod_info_w;	            age = 0;	            /* save data so we can restore this quantization later */	            /*if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh)*/{		            /* store for later reuse */		            memcpy(save_xrpow, xrpow, sizeof(FLOAT)*576);	            }            } else {                /* early stop? */                if (gfc->full_outer_loop == 0) {	                if (++age > search_limit && best_noise_info.over_count == 0)		                break;                    if ((gfc->noise_shaping_amp == 3) && bRefine &&                        age>30)                        break;                    if ((gfc->noise_shaping_amp == 3) && bRefine &&                        (cod_info_w.global_gain - best_ggain_pass1)>15)                        break;                }	        }        }        while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);        if (gfc->noise_shaping_amp == 3) {            if (!bRefine) {                /* refine search*/                cod_info_w = *cod_info;		        memcpy( xrpow, save_xrpow, sizeof(FLOAT)*576);                age = 0;                best_ggain_pass1 = cod_info_w.global_gain;                bRefine = 1;            } else {                /* search already refined, stop*/                bEndOfSearch = 1;            }        } else {            bEndOfSearch = 1;        }    }    assert ((cod_info->global_gain + cod_info->scalefac_scale) <= 255);    /*  finish up     */    if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh)	/* restore for reuse on next try */	memcpy(xrpow, save_xrpow, sizeof(FLOAT)*576);    /*  do the 'substep shaping'     */    else if (gfc->substep_shaping & 1)	trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);    return best_noise_info.over_count;}/************************************************************************ * *      iteration_finish_one() * *  Robert Hegemann 2000-09-06 * *  update reservoir status after FINAL quantization/bitrate  * ************************************************************************/static void iteration_finish_one (    lame_internal_flags *gfc,    int gr, int ch){    III_side_info_t *l3_side = &gfc->l3_side;    gr_info *cod_info = &l3_side->tt[gr][ch];    /*  try some better scalefac storage     */    best_scalefac_store (gfc, gr, ch, l3_side);    /*  best huffman_divide may save some bits too     */    if (gfc->use_best_huffman == 1) 	best_huffman_divide (gfc, cod_info);    /*  update reservoir status after FINAL quantization/bitrate     */    ResvAdjust (gfc, cod_info);}/********************************************************************* * *      VBR_encode_granule() * *  2000-09-04 Robert Hegemann * *********************************************************************/ static voidVBR_encode_granule (    lame_global_flags	*gfp,    gr_info		* const cod_info,    const FLOAT	* const l3_xmin,     /* allowed distortion of the scalefactor */          FLOAT                 xrpow[576],  /* coloured magnitudes of spectral values */    const int                    ch,           int                    min_bits,           int                    max_bits ){    lame_internal_flags *gfc=gfp->internal_flags;    gr_info         bst_cod_info;    FLOAT          bst_xrpow [576];     int Max_bits  = max_bits;    int real_bits = max_bits+1;    int this_bits = (max_bits+min_bits)/2;    int dbits, over, found = 0;    int sfb21_extra = gfc->sfb21_extra;    assert(Max_bits <= MAX_BITS);    /*  search within round about 40 bits of optimal     */    do {        assert(this_bits >= min_bits);        assert(this_bits <= max_bits);        assert(min_bits <= max_bits);        if (this_bits > Max_bits-42)             gfc->sfb21_extra = 0;        else            gfc->sfb21_extra = sfb21_extra;        over = outer_loop ( gfp, cod_info, l3_xmin, xrpow, ch, this_bits );        /*  is quantization as good as we are looking for ?         *  in this case: is no scalefactor band distorted?         */        if (over <= 0) {            found = 1;            /*  now we know it can be done with "real_bits"             *  and maybe we can skip some iterations             */            real_bits = cod_info->part2_3_length;            /*  store best quantization so far             */            bst_cod_info = *cod_info;            memcpy(bst_xrpow, xrpow, sizeof(FLOAT)*576);            /*  try with fewer bits             */            max_bits  = real_bits-32;            dbits     = max_bits-min_bits;            this_bits = (max_bits+min_bits)/2;        }         else {            /*  try with more bits             */            min_bits  = this_bits+32;            dbits     = max_bits-min_bits;            this_bits = (max_bits+min_bits)/2;                        if (found) {                found = 2;                /*  start again with best quantization so far                 */                *cod_info = bst_cod_info;                memcpy(xrpow, bst_xrpow, sizeof(FLOAT)*576);            }        }    } while (dbits>12);    gfc->sfb21_extra = sfb21_extra;    /*  found=0 => nothing found, use last one     *  found=1 => we just found the best and left the loop     *  found=2 => we restored a good one and have now l3_enc to restore too     */    if (found==2) {        memcpy(cod_info->l3_enc, bst_cod_info.l3_enc, sizeof(int)*576);    }    assert(cod_info->part2_3_length <= Max_bits);}/************************************************************************ * *      get_framebits()    * *  Robert Hegemann 2000-09-05 * *  calculates *  * how many bits are available for analog silent granules *  * how many bits to use for the lowest allowed bitrate *  * how many bits each bitrate would provide * ************************************************************************/static void get_framebits (    lame_global_flags *gfp,    int     * const analog_mean_bits,    int     * const min_mean_bits,    int             frameBits[15] ){    lame_internal_flags *gfc=gfp->internal_flags;    int bitsPerFrame, i;        /*  always use at least this many bits per granule per channel      *  unless we detect analog silence, see below      */    gfc->bitrate_index = gfc->VBR_min_bitrate;    bitsPerFrame = getframebits(gfp);    *min_mean_bits = (bitsPerFrame - gfc->sideinfo_len * 8) / (gfc->mode_gr*gfc->channels_out);    /*  bits for analog silence      */    gfc->bitrate_index = 1;    bitsPerFrame = getframebits(gfp);    *analog_mean_bits = (bitsPerFrame - gfc->sideinfo_len * 8) / (gfc->mode_gr*gfc->channels_out);    for (i = 1; i <= gfc->VBR_max_bitrate; i++) {        gfc->bitrate_index = i;        frameBits[i] = ResvFrameBegin (gfp, &bitsPerFrame);    }}/************************************************************************ * *      calc_min_bits()    * *  Robert Hegemann 2000-09-04 * *  determine minimal bit skeleton * ************************************************************************/inlinestatic int calc_min_bits (    lame_global_flags *gfp,    const gr_info * const cod_info,    const int             pe,    const FLOAT          ms_ener_ratio,     const int             bands,        const int             mch_bits,    const int             analog_mean_bits,    const int             min_mean_bits,    const int             analog_silence,    const int             ch ){    lame_internal_flags *gfc=gfp->internal_flags;    int min_bits, min_pe_bits;        if (gfp->psymodel == PSY_NSPSYTUNE) return 126;                    /*  changed minimum from 1 to 126 bits                     *  the iteration loops require a minimum of bits                     *  for each granule to start with; robert 2001-07-02 */    /*  base amount of minimum bits     */    min_bits = Max (126, min_mean_bits);    if (gfc->mode_ext == MPG_MD_MS_LR && ch == 1)          min_bits = Max (min_bits, mch_bits/5);    /*  bit skeleton based on PE     */    if (cod_info->block_type == SHORT_TYPE)         /*  if LAME switches to short blocks then pe is         *  >= 1000 on medium surge         *  >= 3000 on big surge         */        min_pe_bits = (pe-350) * bands/(cod_info->sfbmax+3);    else         min_pe_bits = (pe-350) * bands/(cod_info->sfbmax+1);        if (gfc->mode_ext == MPG_MD_MS_LR && ch == 1) {        /*  side channel will use a lower bit skeleton based on PE         */         FLOAT fac  = .33 * (.5 - ms_ener_ratio) / .5;        min_pe_bits = (int)(min_pe_bits * ((1-fac)/(1+fac)));    }    min_pe_bits = Min (min_pe_bits, (1820 * gfp->out_samplerate / 44100));    /*  determine final minimum bits     */    if (analog_silence && !gfp->VBR_hard_min)         min_bits = analog_mean_bits;    else         min_bits = Max (min_bits, min_pe_bits);        return min_bits;}/********************************************************************* * *      VBR_prepare() * *  2000-09-04 Robert Hegemann * *  * converts LR to MS coding when necessary  *  * calculates allowed/adjusted quantization noise amounts *  * detects analog silent frames * *  some remarks: *  - lower masking depending on Quality setting *  - quality control together with adjusted ATH MDCT scaling *    on lower quality setting allocate more noise from *    ATH masking, and on higher quality setting allocate *    less noise from ATH masking. *  - experiments show that going more than 2dB over GPSYCHO's *    limits ends up in very annoying artefacts * *********************************************************************//* RH: this one needs to be overhauled sometime */ static int VBR_prepare (          lame_global_flags *gfp,          FLOAT           pe            [2][2],          FLOAT           ms_ener_ratio [2],           III_psy_ratio   ratio         [2][2],           FLOAT	  l3_xmin       [2][2][SFBMAX],          int             frameBits     [16],          int            *analog_mean_bits,          int            *min_mean_bits,          int             min_bits      [2][2],          int             max_bits      [2][2],          int             bands         [2][2] ){    lame_internal_flags *gfc=gfp->internal_flags;            FLOAT  masking_lower_db, adjust = 0.0;    int     gr, ch;    int     analog_silence = 1;    int     avg, mxb, bits = 0;      gfc->bitrate_index = gfc->VBR_max_bitrate;    avg = ResvFrameBegin (gfp, &avg) / gfc->mode_gr;        get_framebits (gfp, analog_mean_bits, min_mean_bits, frameBits);    for (gr = 0; gr < gfc->mode_gr; gr++) {        mxb = on_pe (gfp, pe, &gfc->l3_side, max_bits[gr], avg, gr, 0);        if (gfc->mode_ext == MPG_MD_MS_LR) {            ms_convert (&gfc->l3_side, gr);            ms_sparsing( gfc, gr );            reduce_side (max_bits[gr], ms_ener_ratio[gr], avg, mxb);        }        for (ch = 0; ch < gfc->channels_out; ++ch) {            gr_info *cod_info = &gfc->l3_side.tt[gr][ch];                  if (cod_info->block_type == NORM_TYPE) {                adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05;                masking_lower_db   = gfc->PSY->mask_adjust - adjust;            } else {                 adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14;                masking_lower_db   = gfc->PSY->mask_adjust_short - adjust;             }            gfc->masking_lower = pow (10.0, masking_lower_db * 0.1);                  init_outer_loop(gfp, gfc, cod_info);	    bands[gr][ch] = calc_xmin (gfp, &ratio[gr][ch],                                        cod_info, l3_xmin[gr][ch]);            if (bands[gr][ch])                 analog_silence = 0;            min_bits[gr][ch] = calc_min_bits (gfp, cod_info, (int)pe[gr][ch],                                      ms_ener_ratio[gr], bands[gr][ch],                                      0, *analog_mean_bits,                                       *min_mean_bits, analog_silence, ch);                  bits += max_bits[gr][ch];        }    }

⌨️ 快捷键说明

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