4 * Copyright (c) 1999-2000 Mark Taylor
5 * Copyright (c) 1999-2003 Takehiro Tominaga
6 * Copyright (c) 2000-2011 Robert Hegemann
7 * Copyright (c) 2001-2005 Gabriel Bouvigne
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 /* $Id: quantize.c,v 1.216 2011/05/07 16:05:17 rbrito Exp $ */
35 #include "quantize_pvt.h"
36 #include "reservoir.h"
37 #include "bitstream.h"
38 #include "vbrquantize.h"
40 #ifdef HAVE_XMMINTRIN_H
41 #include "vector/lame_intrin.h"
47 /* convert from L/R <-> Mid/Side */
49 ms_convert(III_side_info_t * l3_side, int gr)
52 for (i = 0; i < 576; ++i) {
54 l = l3_side->tt[gr][0].xr[i];
55 r = l3_side->tt[gr][1].xr[i];
56 l3_side->tt[gr][0].xr[i] = (l + r) * (FLOAT) (SQRT2 * 0.5);
57 l3_side->tt[gr][1].xr[i] = (l - r) * (FLOAT) (SQRT2 * 0.5);
61 /************************************************************************
66 * initializes cod_info, scalefac and xrpow
68 * returns 0 if all energies in xr are zero, else 1
70 ************************************************************************/
73 init_xrpow_core_c(gr_info * const cod_info, FLOAT xrpow[576], int upper, FLOAT * sum)
78 for (i = 0; i <= upper; ++i) {
79 tmp = fabs(cod_info->xr[i]);
81 xrpow[i] = sqrt(tmp * sqrt(tmp));
83 if (xrpow[i] > cod_info->xrpow_max)
84 cod_info->xrpow_max = xrpow[i];
93 init_xrpow_core_init(lame_internal_flags * const gfc)
95 gfc->init_xrpow_core = init_xrpow_core_c;
97 #if defined(HAVE_XMMINTRIN_H)
98 if (gfc->CPU_features.SSE)
99 gfc->init_xrpow_core = init_xrpow_core_sse;
103 gfc->init_xrpow_core = init_xrpow_core_sse;
111 init_xrpow(lame_internal_flags * gfc, gr_info * const cod_info, FLOAT xrpow[576])
115 int const upper = cod_info->max_nonzero_coeff;
117 assert(xrpow != NULL);
118 cod_info->xrpow_max = 0;
120 /* check if there is some energy we have to quantize
121 * and calculate xrpow matching our fresh scalefactors
123 assert(0 <= upper && upper <= 575);
124 memset(&(xrpow[upper]), 0, (576 - upper) * sizeof(xrpow[0]));
127 gfc->init_xrpow_core(cod_info, xrpow, upper, &sum);
129 /* return 1 if we have something to quantize, else 0
131 if (sum > (FLOAT) 1E-20) {
133 if (gfc->sv_qnt.substep_shaping & 2)
136 for (i = 0; i < cod_info->psymax; i++)
137 gfc->sv_qnt.pseudohalf[i] = j;
142 memset(&cod_info->l3_enc[0], 0, sizeof(int) * 576);
151 Gabriel Bouvigne feb/apr 2003
152 Analog silence detection in partitionned sfb21
153 or sfb12 for short blocks
155 From top to bottom of sfb, changes to 0
156 coeffs which are below ath. It stops on the first
157 coeff higher than ath.
160 psfb21_analogsilence(lame_internal_flags const *gfc, gr_info * const cod_info)
162 ATH_t const *const ATH = gfc->ATH;
163 FLOAT *const xr = cod_info->xr;
165 if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT blocks */
168 for (gsfb = PSFB21 - 1; gsfb >= 0 && !stop; gsfb--) {
169 int const start = gfc->scalefac_band.psfb21[gsfb];
170 int const end = gfc->scalefac_band.psfb21[gsfb + 1];
173 ath21 = athAdjust(ATH->adjust_factor, ATH->psfb21[gsfb], ATH->floor, 0);
175 if (gfc->sv_qnt.longfact[21] > 1e-12f)
176 ath21 *= gfc->sv_qnt.longfact[21];
178 for (j = end - 1; j >= start; j--) {
179 if (fabs(xr[j]) < ath21)
189 /*note: short blocks coeffs are reordered */
191 for (block = 0; block < 3; block++) {
195 for (gsfb = PSFB12 - 1; gsfb >= 0 && !stop; gsfb--) {
196 int const start = gfc->scalefac_band.s[12] * 3 +
197 (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]) * block +
198 (gfc->scalefac_band.psfb12[gsfb] - gfc->scalefac_band.psfb12[0]);
200 start + (gfc->scalefac_band.psfb12[gsfb + 1] - gfc->scalefac_band.psfb12[gsfb]);
203 ath12 = athAdjust(ATH->adjust_factor, ATH->psfb12[gsfb], ATH->floor, 0);
205 if (gfc->sv_qnt.shortfact[12] > 1e-12f)
206 ath12 *= gfc->sv_qnt.shortfact[12];
208 for (j = end - 1; j >= start; j--) {
209 if (fabs(xr[j]) < ath12)
227 init_outer_loop(lame_internal_flags const *gfc, gr_info * const cod_info)
229 SessionConfig_t const *const cfg = &gfc->cfg;
231 /* initialize fresh cod_info
233 cod_info->part2_3_length = 0;
234 cod_info->big_values = 0;
235 cod_info->count1 = 0;
236 cod_info->global_gain = 210;
237 cod_info->scalefac_compress = 0;
238 /* mixed_block_flag, block_type was set in psymodel.c */
239 cod_info->table_select[0] = 0;
240 cod_info->table_select[1] = 0;
241 cod_info->table_select[2] = 0;
242 cod_info->subblock_gain[0] = 0;
243 cod_info->subblock_gain[1] = 0;
244 cod_info->subblock_gain[2] = 0;
245 cod_info->subblock_gain[3] = 0; /* this one is always 0 */
246 cod_info->region0_count = 0;
247 cod_info->region1_count = 0;
248 cod_info->preflag = 0;
249 cod_info->scalefac_scale = 0;
250 cod_info->count1table_select = 0;
251 cod_info->part2_length = 0;
252 cod_info->sfb_lmax = SBPSY_l;
253 cod_info->sfb_smin = SBPSY_s;
254 cod_info->psy_lmax = gfc->sv_qnt.sfb21_extra ? SBMAX_l : SBPSY_l;
255 cod_info->psymax = cod_info->psy_lmax;
256 cod_info->sfbmax = cod_info->sfb_lmax;
257 cod_info->sfbdivide = 11;
258 for (sfb = 0; sfb < SBMAX_l; sfb++) {
260 = gfc->scalefac_band.l[sfb + 1] - gfc->scalefac_band.l[sfb];
261 cod_info->window[sfb] = 3; /* which is always 0. */
263 if (cod_info->block_type == SHORT_TYPE) {
267 cod_info->sfb_smin = 0;
268 cod_info->sfb_lmax = 0;
269 if (cod_info->mixed_block_flag) {
271 * MPEG-1: sfbs 0-7 long block, 3-12 short blocks
272 * MPEG-2(.5): sfbs 0-5 long block, 3-12 short blocks
274 cod_info->sfb_smin = 3;
275 cod_info->sfb_lmax = cfg->mode_gr * 2 + 4;
279 + 3 * ((gfc->sv_qnt.sfb21_extra ? SBMAX_s : SBPSY_s) - cod_info->sfb_smin);
280 cod_info->sfbmax = cod_info->sfb_lmax + 3 * (SBPSY_s - cod_info->sfb_smin);
281 cod_info->sfbdivide = cod_info->sfbmax - 18;
282 cod_info->psy_lmax = cod_info->sfb_lmax;
283 /* re-order the short blocks, for more efficient encoding below */
284 /* By Takehiro TOMINAGA */
286 Within each scalefactor band, data is given for successive
287 time windows, beginning with window 0 and ending with window 2.
288 Within each window, the quantized values are then arranged in
289 order of increasing frequency...
291 ix = &cod_info->xr[gfc->scalefac_band.l[cod_info->sfb_lmax]];
292 memcpy(ixwork, cod_info->xr, 576 * sizeof(FLOAT));
293 for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
294 int const start = gfc->scalefac_band.s[sfb];
295 int const end = gfc->scalefac_band.s[sfb + 1];
297 for (window = 0; window < 3; window++) {
298 for (l = start; l < end; l++) {
299 *ix++ = ixwork[3 * l + window];
304 j = cod_info->sfb_lmax;
305 for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
306 cod_info->width[j] = cod_info->width[j + 1] = cod_info->width[j + 2]
307 = gfc->scalefac_band.s[sfb + 1] - gfc->scalefac_band.s[sfb];
308 cod_info->window[j] = 0;
309 cod_info->window[j + 1] = 1;
310 cod_info->window[j + 2] = 2;
315 cod_info->count1bits = 0;
316 cod_info->sfb_partition_table = nr_of_sfb_block[0][0];
317 cod_info->slen[0] = 0;
318 cod_info->slen[1] = 0;
319 cod_info->slen[2] = 0;
320 cod_info->slen[3] = 0;
322 cod_info->max_nonzero_coeff = 575;
324 /* fresh scalefactors are all zero
326 memset(cod_info->scalefac, 0, sizeof(cod_info->scalefac));
328 if (cfg->vbr != vbr_mt && cfg->vbr != vbr_mtrh && cfg->vbr != vbr_abr && cfg->vbr != vbr_off) {
329 psfb21_analogsilence(gfc, cod_info);
335 /************************************************************************
337 * bin_search_StepSize()
341 * binary step size search
342 * used by outer_loop to get a quantizer step size to start with
344 ************************************************************************/
350 } binsearchDirection_t;
353 bin_search_StepSize(lame_internal_flags * const gfc, gr_info * const cod_info,
354 int desired_rate, const int ch, const FLOAT xrpow[576])
357 int CurrentStep = gfc->sv_qnt.CurrentStep[ch];
358 int flag_GoneOver = 0;
359 int const start = gfc->sv_qnt.OldValue[ch];
360 binsearchDirection_t Direction = BINSEARCH_NONE;
361 cod_info->global_gain = start;
362 desired_rate -= cod_info->part2_length;
367 nBits = count_bits(gfc, xrpow, cod_info, 0);
369 if (CurrentStep == 1 || nBits == desired_rate)
370 break; /* nothing to adjust anymore */
372 if (nBits > desired_rate) {
373 /* increase Quantize_StepSize */
374 if (Direction == BINSEARCH_DOWN)
379 Direction = BINSEARCH_UP;
383 /* decrease Quantize_StepSize */
384 if (Direction == BINSEARCH_UP)
389 Direction = BINSEARCH_DOWN;
392 cod_info->global_gain += step;
393 if (cod_info->global_gain < 0) {
394 cod_info->global_gain = 0;
397 if (cod_info->global_gain > 255) {
398 cod_info->global_gain = 255;
403 assert(cod_info->global_gain >= 0);
404 assert(cod_info->global_gain < 256);
406 while (nBits > desired_rate && cod_info->global_gain < 255) {
407 cod_info->global_gain++;
408 nBits = count_bits(gfc, xrpow, cod_info, 0);
410 gfc->sv_qnt.CurrentStep[ch] = (start - cod_info->global_gain >= 4) ? 4 : 2;
411 gfc->sv_qnt.OldValue[ch] = cod_info->global_gain;
412 cod_info->part2_3_length = nBits;
419 /************************************************************************
421 * trancate_smallspectrums()
423 * Takehiro TOMINAGA 2002-07-21
425 * trancate smaller nubmers into 0 as long as the noise threshold is allowed.
427 ************************************************************************/
429 floatcompare(const void *v1, const void *v2)
431 const FLOAT *const a = v1, *const b = v2;
440 trancate_smallspectrums(lame_internal_flags const *gfc,
441 gr_info * const gi, const FLOAT * const l3_xmin, FLOAT * const work)
444 FLOAT distort[SFBMAX];
445 calc_noise_result dummy;
447 if ((!(gfc->sv_qnt.substep_shaping & 4) && gi->block_type == SHORT_TYPE)
448 || gfc->sv_qnt.substep_shaping & 0x80)
450 (void) calc_noise(gi, l3_xmin, distort, &dummy, 0);
451 for (j = 0; j < 576; j++) {
453 if (gi->l3_enc[j] != 0)
454 xr = fabs(gi->xr[j]);
460 if (gi->block_type == SHORT_TYPE)
463 FLOAT allowedNoise, trancateThreshold;
466 width = gi->width[sfb];
468 if (distort[sfb] >= 1.0)
471 qsort(&work[j - width], width, sizeof(FLOAT), floatcompare);
472 if (EQ(work[j - 1], 0.0))
473 continue; /* all zero sfb */
475 allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];
476 trancateThreshold = 0.0;
480 for (nsame = 1; start + nsame < width; nsame++)
481 if (NEQ(work[start + j - width], work[start + j + nsame - width]))
484 noise = work[start + j - width] * work[start + j - width] * nsame;
485 if (allowedNoise < noise) {
487 trancateThreshold = work[start + j - width - 1];
490 allowedNoise -= noise;
492 } while (start < width);
493 if (EQ(trancateThreshold, 0.0))
496 /* printf("%e %e %e\n", */
497 /* trancateThreshold/l3_xmin[sfb], */
498 /* trancateThreshold/(l3_xmin[sfb]*start), */
499 /* trancateThreshold/(l3_xmin[sfb]*(start+width)) */
501 /* if (trancateThreshold > 1000*l3_xmin[sfb]*start) */
502 /* trancateThreshold = 1000*l3_xmin[sfb]*start; */
505 if (fabs(gi->xr[j - width]) <= trancateThreshold)
506 gi->l3_enc[j - width] = 0;
507 } while (--width > 0);
508 } while (++sfb < gi->psymax);
510 gi->part2_3_length = noquant_count_bits(gfc, gi, 0);
514 /*************************************************************************
520 * Function: Returns zero if there is a scalefac which has not been
521 * amplified. Otherwise it returns one.
523 *************************************************************************/
526 loop_break(const gr_info * const cod_info)
530 for (sfb = 0; sfb < cod_info->sfbmax; sfb++)
531 if (cod_info->scalefac[sfb]
532 + cod_info->subblock_gain[cod_info->window[sfb]] == 0)
541 /* mt 5/99: Function: Improved calc_noise for a single channel */
543 /*************************************************************************
549 * several different codes to decide which quantization is better
551 *************************************************************************/
554 penalties(double noise)
556 return FAST_LOG10(0.368 + 0.632 * noise * noise * noise);
560 get_klemm_noise(const FLOAT * distort, const gr_info * const gi)
563 double klemm_noise = 1E-37;
564 for (sfb = 0; sfb < gi->psymax; sfb++)
565 klemm_noise += penalties(distort[sfb]);
567 return Max(1e-20, klemm_noise);
571 quant_compare(const int quant_comp,
572 const calc_noise_result * const best,
573 calc_noise_result * const calc, const gr_info * const gi, const FLOAT * distort)
576 noise is given in decibels (dB) relative to masking thesholds.
578 over_noise: ??? (the previous comment is fully wrong)
579 tot_noise: ??? (the previous comment is fully wrong)
580 max_noise: max quantization noise
585 switch (quant_comp) {
588 if (best->over_count > 0) {
589 /* there are distorted sfb */
590 better = calc->over_SSD <= best->over_SSD;
591 if (calc->over_SSD == best->over_SSD)
592 better = calc->bits < best->bits;
595 /* no distorted sfb */
596 better = ((calc->max_noise < 0) &&
597 ((calc->max_noise * 10 + calc->bits) <=
598 (best->max_noise * 10 + best->bits)));
604 better = calc->over_count < best->over_count
605 || (calc->over_count == best->over_count && calc->over_noise < best->over_noise)
606 || (calc->over_count == best->over_count &&
607 EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
611 calc->max_noise = get_klemm_noise(distort, gi);
612 /*lint --fallthrough */
614 better = calc->max_noise < best->max_noise;
617 better = calc->tot_noise < best->tot_noise;
620 better = (calc->tot_noise < best->tot_noise)
621 && (calc->max_noise < best->max_noise);
624 better = (calc->max_noise <= 0.0 && best->max_noise > 0.2)
625 || (calc->max_noise <= 0.0 &&
626 best->max_noise < 0.0 &&
627 best->max_noise > calc->max_noise - 0.2 && calc->tot_noise < best->tot_noise)
628 || (calc->max_noise <= 0.0 &&
629 best->max_noise > 0.0 &&
630 best->max_noise > calc->max_noise - 0.2 &&
631 calc->tot_noise < best->tot_noise + best->over_noise)
632 || (calc->max_noise > 0.0 &&
633 best->max_noise > -0.05 &&
634 best->max_noise > calc->max_noise - 0.1 &&
635 calc->tot_noise + calc->over_noise < best->tot_noise + best->over_noise)
636 || (calc->max_noise > 0.0 &&
637 best->max_noise > -0.1 &&
638 best->max_noise > calc->max_noise - 0.15 &&
639 calc->tot_noise + calc->over_noise + calc->over_noise <
640 best->tot_noise + best->over_noise + best->over_noise);
643 better = calc->over_noise < best->over_noise
644 || (EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
647 better = calc->over_noise < best->over_noise
648 || (EQ(calc->over_noise, best->over_noise) &&
649 (calc->max_noise < best->max_noise
650 || (EQ(calc->max_noise, best->max_noise) && calc->tot_noise <= best->tot_noise)
654 better = calc->over_count < best->over_count || calc->over_noise < best->over_noise;
659 if (best->over_count == 0) {
661 If no distorted bands, only use this quantization
662 if it is better, and if it uses less bits.
663 Unfortunately, part2_3_length is sometimes a poor
664 estimator of the final size at low bitrates.
666 better = better && calc->bits < best->bits;
675 /*************************************************************************
677 * amp_scalefac_bands()
681 * Amplify the scalefactor bands that violate the masking threshold.
682 * See ISO 11172-3 Section C.1.5.4.3.5
684 * distort[] = noise/masking
685 * distort[] > 1 ==> noise is not masked
686 * distort[] < 1 ==> noise is masked
687 * max_dist = maximum value of distort[]
691 * 0 Amplify all bands with distort[]>1.
693 * 1 Amplify all bands with distort[] >= max_dist^(.5);
694 * ( 50% in the db scale)
696 * 2 Amplify first band with distort[] >= max_dist;
699 * For algorithms 0 and 1, if max_dist < 1, then amplify all bands
700 * with distort[] >= .95*max_dist. This is to make sure we always
701 * amplify at least one band.
704 *************************************************************************/
706 amp_scalefac_bands(lame_internal_flags * gfc,
707 gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
709 SessionConfig_t const *const cfg = &gfc->cfg;
711 FLOAT ifqstep34, trigger;
712 int noise_shaping_amp;
714 if (cod_info->scalefac_scale == 0) {
715 ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5) */
718 ifqstep34 = 1.68179283050742922612; /* 2**(.75*1) */
721 /* compute maximum value of distort[] */
723 for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
724 if (trigger < distort[sfb])
725 trigger = distort[sfb];
728 noise_shaping_amp = cfg->noise_shaping_amp;
729 if (noise_shaping_amp == 3) {
731 noise_shaping_amp = 2;
733 noise_shaping_amp = 1;
735 switch (noise_shaping_amp) {
737 /* amplify exactly 1 band */
741 /* amplify bands within 50% of max (on db scale) */
743 trigger = pow(trigger, .5);
750 /* ISO algorithm. amplify all bands with distort>1 */
759 for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
760 int const width = cod_info->width[sfb];
763 if (distort[sfb] < trigger)
766 if (gfc->sv_qnt.substep_shaping & 2) {
767 gfc->sv_qnt.pseudohalf[sfb] = !gfc->sv_qnt.pseudohalf[sfb];
768 if (!gfc->sv_qnt.pseudohalf[sfb] && cfg->noise_shaping_amp == 2)
771 cod_info->scalefac[sfb]++;
772 for (l = -width; l < 0; l++) {
773 xrpow[j + l] *= ifqstep34;
774 if (xrpow[j + l] > cod_info->xrpow_max)
775 cod_info->xrpow_max = xrpow[j + l];
778 if (cfg->noise_shaping_amp == 2)
783 /*************************************************************************
785 * inc_scalefac_scale()
787 * Takehiro Tominaga 2000-xx-xx
789 * turns on scalefac scale and adjusts scalefactors
791 *************************************************************************/
794 inc_scalefac_scale(gr_info * const cod_info, FLOAT xrpow[576])
797 const FLOAT ifqstep34 = 1.29683955465100964055;
800 for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
801 int const width = cod_info->width[sfb];
802 int s = cod_info->scalefac[sfb];
803 if (cod_info->preflag)
808 for (l = -width; l < 0; l++) {
809 xrpow[j + l] *= ifqstep34;
810 if (xrpow[j + l] > cod_info->xrpow_max)
811 cod_info->xrpow_max = xrpow[j + l];
814 cod_info->scalefac[sfb] = s >> 1;
816 cod_info->preflag = 0;
817 cod_info->scalefac_scale = 1;
822 /*************************************************************************
824 * inc_subblock_gain()
826 * Takehiro Tominaga 2000-xx-xx
828 * increases the subblock gain and adjusts scalefactors
830 *************************************************************************/
833 inc_subblock_gain(const lame_internal_flags * const gfc, gr_info * const cod_info, FLOAT xrpow[576])
836 int *const scalefac = cod_info->scalefac;
838 /* subbloc_gain can't do anything in the long block region */
839 for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
840 if (scalefac[sfb] >= 16)
844 for (window = 0; window < 3; window++) {
848 for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbdivide; sfb += 3) {
849 if (s1 < scalefac[sfb])
852 for (; sfb < cod_info->sfbmax; sfb += 3) {
853 if (s2 < scalefac[sfb])
857 if (s1 < 16 && s2 < 8)
860 if (cod_info->subblock_gain[window] >= 7)
863 /* even though there is no scalefactor for sfb12
864 * subblock gain affects upper frequencies too, that's why
865 * we have to go up to SBMAX_s
867 cod_info->subblock_gain[window]++;
868 j = gfc->scalefac_band.l[cod_info->sfb_lmax];
869 for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbmax; sfb += 3) {
871 int const width = cod_info->width[sfb];
872 int s = scalefac[sfb];
874 s = s - (4 >> cod_info->scalefac_scale);
883 int const gain = 210 + (s << (cod_info->scalefac_scale + 1));
886 j += width * (window + 1);
887 for (l = -width; l < 0; l++) {
889 if (xrpow[j + l] > cod_info->xrpow_max)
890 cod_info->xrpow_max = xrpow[j + l];
892 j += width * (3 - window - 1);
896 FLOAT const amp = IPOW20(202);
897 j += cod_info->width[sfb] * (window + 1);
898 for (l = -cod_info->width[sfb]; l < 0; l++) {
900 if (xrpow[j + l] > cod_info->xrpow_max)
901 cod_info->xrpow_max = xrpow[j + l];
910 /********************************************************************
914 * Takehiro Tominaga /date??
915 * Robert Hegemann 2000-09-06: made a function of it
917 * amplifies scalefactor bands,
918 * - if all are already amplified returns 0
919 * - if some bands are amplified too much:
920 * * try to increase scalefac_scale
921 * * if already scalefac_scale was set
922 * try on short blocks to increase subblock gain
924 ********************************************************************/
926 balance_noise(lame_internal_flags * gfc,
927 gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
929 SessionConfig_t const *const cfg = &gfc->cfg;
932 amp_scalefac_bands(gfc, cod_info, distort, xrpow, bRefine);
934 /* check to make sure we have not amplified too much
935 * loop_break returns 0 if there is an unamplified scalefac
936 * scale_bitcount returns 0 if no scalefactors are too large
939 status = loop_break(cod_info);
942 return 0; /* all bands amplified */
944 /* not all scalefactors have been amplified. so these
945 * scalefacs are possibly valid. encode them:
947 status = scale_bitcount(gfc, cod_info);
950 return 1; /* amplified some bands not exceeding limits */
952 /* some scalefactors are too large.
953 * lets try setting scalefac_scale=1
955 if (cfg->noise_shaping > 1) {
956 memset(&gfc->sv_qnt.pseudohalf[0], 0, sizeof(gfc->sv_qnt.pseudohalf));
957 if (!cod_info->scalefac_scale) {
958 inc_scalefac_scale(cod_info, xrpow);
962 if (cod_info->block_type == SHORT_TYPE && cfg->subblock_gain > 0) {
963 status = inc_subblock_gain(gfc, cod_info, xrpow)
964 || loop_break(cod_info);
970 status = scale_bitcount(gfc, cod_info);
977 /************************************************************************
981 * Function: The outer iteration loop controls the masking conditions
982 * of all scalefactorbands. It computes the best scalefac and
983 * global gain. This module calls the inner iteration loop
985 * mt 5/99 completely rewritten to allow for bit reservoir control,
986 * mid/side channels with L/R or mid/side masking thresholds,
987 * and chooses best quantization instead of last quantization when
988 * no distortion free quantization can be found.
990 * added VBR support mt 5/99
992 * some code shuffle rh 9/00
993 ************************************************************************/
996 outer_loop(lame_internal_flags * gfc, gr_info * const cod_info, const FLOAT * const l3_xmin, /* allowed distortion */
997 FLOAT xrpow[576], /* coloured magnitudes of spectral */
998 const int ch, const int targ_bits)
999 { /* maximum allowed bits */
1000 SessionConfig_t const *const cfg = &gfc->cfg;
1002 FLOAT save_xrpow[576];
1003 FLOAT distort[SFBMAX];
1004 calc_noise_result best_noise_info;
1008 calc_noise_data prev_noise;
1009 int best_part2_3_length = 9999999;
1010 int bEndOfSearch = 0;
1012 int best_ggain_pass1 = 0;
1014 (void) bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);
1016 if (!cfg->noise_shaping)
1017 /* fast mode, no noise shaping, we are ready */
1018 return 100; /* default noise_info.over_count */
1020 memset(&prev_noise, 0, sizeof(calc_noise_data));
1023 /* compute the distortion in this quantization */
1024 /* coefficients and thresholds both l/r (or both mid/side) */
1025 (void) calc_noise(cod_info, l3_xmin, distort, &best_noise_info, &prev_noise);
1026 best_noise_info.bits = cod_info->part2_3_length;
1028 cod_info_w = *cod_info;
1030 /* if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */
1031 memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
1033 while (!bEndOfSearch) {
1034 /* BEGIN MAIN LOOP */
1036 calc_noise_result noise_info;
1040 /* When quantization with no distorted bands is found,
1041 * allow up to X new unsuccesful tries in serial. This
1042 * gives us more possibilities for different quant_compare modes.
1043 * Much more than 3 makes not a big difference, it is only slower.
1046 if (gfc->sv_qnt.substep_shaping & 2) {
1055 /* Check if the last scalefactor band is distorted.
1056 * in VBR mode we can't get rid of the distortion, so quit now
1057 * and VBR mode will try again with more bits.
1058 * (makes a 10% speed increase, the files I tested were
1059 * binary identical, 2000/05/20 Robert Hegemann)
1060 * distort[] > 1 means noise > allowed noise
1062 if (gfc->sv_qnt.sfb21_extra) {
1063 if (distort[cod_info_w.sfbmax] > 1.0)
1065 if (cod_info_w.block_type == SHORT_TYPE
1066 && (distort[cod_info_w.sfbmax + 1] > 1.0
1067 || distort[cod_info_w.sfbmax + 2] > 1.0))
1071 /* try a new scalefactor conbination on cod_info_w */
1072 if (balance_noise(gfc, &cod_info_w, distort, xrpow, bRefine) == 0)
1074 if (cod_info_w.scalefac_scale)
1077 /* inner_loop starts with the initial quantization step computed above
1078 * and slowly increases until the bits < huff_bits.
1079 * Thus it is important not to start with too large of an inital
1080 * quantization step. Too small is ok, but inner_loop will take longer
1082 huff_bits = targ_bits - cod_info_w.part2_length;
1086 /* increase quantizer stepsize until needed bits are below maximum
1088 while ((cod_info_w.part2_3_length
1089 = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > huff_bits
1090 && cod_info_w.global_gain <= maxggain)
1091 cod_info_w.global_gain++;
1093 if (cod_info_w.global_gain > maxggain)
1096 if (best_noise_info.over_count == 0) {
1098 while ((cod_info_w.part2_3_length
1099 = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > best_part2_3_length
1100 && cod_info_w.global_gain <= maxggain)
1101 cod_info_w.global_gain++;
1103 if (cod_info_w.global_gain > maxggain)
1107 /* compute the distortion in this quantization */
1108 (void) calc_noise(&cod_info_w, l3_xmin, distort, &noise_info, &prev_noise);
1109 noise_info.bits = cod_info_w.part2_3_length;
1111 /* check if this quantization is better
1112 * than our saved quantization */
1113 if (cod_info->block_type != SHORT_TYPE) /* NORM, START or STOP type */
1114 better = cfg->quant_comp;
1116 better = cfg->quant_comp_short;
1119 better = quant_compare(better, &best_noise_info, &noise_info, &cod_info_w, distort);
1122 /* save data so we can restore this quantization later */
1124 best_part2_3_length = cod_info->part2_3_length;
1125 best_noise_info = noise_info;
1126 *cod_info = cod_info_w;
1128 /* save data so we can restore this quantization later */
1129 /*if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */ {
1130 /* store for later reuse */
1131 memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
1136 if (cfg->full_outer_loop == 0) {
1137 if (++age > search_limit && best_noise_info.over_count == 0)
1139 if ((cfg->noise_shaping_amp == 3) && bRefine && age > 30)
1141 if ((cfg->noise_shaping_amp == 3) && bRefine &&
1142 (cod_info_w.global_gain - best_ggain_pass1) > 15)
1147 while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);
1149 if (cfg->noise_shaping_amp == 3) {
1152 cod_info_w = *cod_info;
1153 memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
1155 best_ggain_pass1 = cod_info_w.global_gain;
1160 /* search already refined, stop */
1170 assert((cod_info->global_gain + cod_info->scalefac_scale) <= 255);
1173 if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh || cfg->vbr == vbr_mt)
1174 /* restore for reuse on next try */
1175 memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
1176 /* do the 'substep shaping'
1178 else if (gfc->sv_qnt.substep_shaping & 1)
1179 trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);
1181 return best_noise_info.over_count;
1188 /************************************************************************
1190 * iteration_finish_one()
1192 * Robert Hegemann 2000-09-06
1194 * update reservoir status after FINAL quantization/bitrate
1196 ************************************************************************/
1199 iteration_finish_one(lame_internal_flags * gfc, int gr, int ch)
1201 SessionConfig_t const *const cfg = &gfc->cfg;
1202 III_side_info_t *const l3_side = &gfc->l3_side;
1203 gr_info *const cod_info = &l3_side->tt[gr][ch];
1205 /* try some better scalefac storage
1207 best_scalefac_store(gfc, gr, ch, l3_side);
1209 /* best huffman_divide may save some bits too
1211 if (cfg->use_best_huffman == 1)
1212 best_huffman_divide(gfc, cod_info);
1214 /* update reservoir status after FINAL quantization/bitrate
1216 ResvAdjust(gfc, cod_info);
1221 /*********************************************************************
1223 * VBR_encode_granule()
1225 * 2000-09-04 Robert Hegemann
1227 *********************************************************************/
1230 VBR_encode_granule(lame_internal_flags * gfc, gr_info * const cod_info, const FLOAT * const l3_xmin, /* allowed distortion of the scalefactor */
1231 FLOAT xrpow[576], /* coloured magnitudes of spectral values */
1232 const int ch, int min_bits, int max_bits)
1234 gr_info bst_cod_info;
1235 FLOAT bst_xrpow[576];
1236 int const Max_bits = max_bits;
1237 int real_bits = max_bits + 1;
1238 int this_bits = (max_bits + min_bits) / 2;
1239 int dbits, over, found = 0;
1240 int const sfb21_extra = gfc->sv_qnt.sfb21_extra;
1242 assert(Max_bits <= MAX_BITS_PER_CHANNEL);
1243 memset(bst_cod_info.l3_enc, 0, sizeof(bst_cod_info.l3_enc));
1245 /* search within round about 40 bits of optimal
1248 assert(this_bits >= min_bits);
1249 assert(this_bits <= max_bits);
1250 assert(min_bits <= max_bits);
1252 if (this_bits > Max_bits - 42)
1253 gfc->sv_qnt.sfb21_extra = 0;
1255 gfc->sv_qnt.sfb21_extra = sfb21_extra;
1257 over = outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, this_bits);
1259 /* is quantization as good as we are looking for ?
1260 * in this case: is no scalefactor band distorted?
1264 /* now we know it can be done with "real_bits"
1265 * and maybe we can skip some iterations
1267 real_bits = cod_info->part2_3_length;
1269 /* store best quantization so far
1271 bst_cod_info = *cod_info;
1272 memcpy(bst_xrpow, xrpow, sizeof(FLOAT) * 576);
1274 /* try with fewer bits
1276 max_bits = real_bits - 32;
1277 dbits = max_bits - min_bits;
1278 this_bits = (max_bits + min_bits) / 2;
1281 /* try with more bits
1283 min_bits = this_bits + 32;
1284 dbits = max_bits - min_bits;
1285 this_bits = (max_bits + min_bits) / 2;
1289 /* start again with best quantization so far
1291 *cod_info = bst_cod_info;
1292 memcpy(xrpow, bst_xrpow, sizeof(FLOAT) * 576);
1295 } while (dbits > 12);
1297 gfc->sv_qnt.sfb21_extra = sfb21_extra;
1299 /* found=0 => nothing found, use last one
1300 * found=1 => we just found the best and left the loop
1301 * found=2 => we restored a good one and have now l3_enc to restore too
1304 memcpy(cod_info->l3_enc, bst_cod_info.l3_enc, sizeof(int) * 576);
1306 assert(cod_info->part2_3_length <= Max_bits);
1312 /************************************************************************
1316 * Robert Hegemann 2000-09-05
1319 * * how many bits are available for analog silent granules
1320 * * how many bits to use for the lowest allowed bitrate
1321 * * how many bits each bitrate would provide
1323 ************************************************************************/
1326 get_framebits(lame_internal_flags * gfc, int frameBits[15])
1328 SessionConfig_t const *const cfg = &gfc->cfg;
1329 EncResult_t *const eov = &gfc->ov_enc;
1330 int bitsPerFrame, i;
1332 /* always use at least this many bits per granule per channel
1333 * unless we detect analog silence, see below
1335 eov->bitrate_index = cfg->vbr_min_bitrate_index;
1336 bitsPerFrame = getframebits(gfc);
1338 /* bits for analog silence
1340 eov->bitrate_index = 1;
1341 bitsPerFrame = getframebits(gfc);
1343 for (i = 1; i <= cfg->vbr_max_bitrate_index; i++) {
1344 eov->bitrate_index = i;
1345 frameBits[i] = ResvFrameBegin(gfc, &bitsPerFrame);
1351 /*********************************************************************
1355 * 2000-09-04 Robert Hegemann
1357 * * converts LR to MS coding when necessary
1358 * * calculates allowed/adjusted quantization noise amounts
1359 * * detects analog silent frames
1362 * - lower masking depending on Quality setting
1363 * - quality control together with adjusted ATH MDCT scaling
1364 * on lower quality setting allocate more noise from
1365 * ATH masking, and on higher quality setting allocate
1366 * less noise from ATH masking.
1367 * - experiments show that going more than 2dB over GPSYCHO's
1368 * limits ends up in very annoying artefacts
1370 *********************************************************************/
1372 /* RH: this one needs to be overhauled sometime */
1375 VBR_old_prepare(lame_internal_flags * gfc,
1376 const FLOAT pe[2][2], FLOAT const ms_ener_ratio[2],
1377 const III_psy_ratio ratio[2][2],
1378 FLOAT l3_xmin[2][2][SFBMAX],
1379 int frameBits[16], int min_bits[2][2], int max_bits[2][2], int bands[2][2])
1381 SessionConfig_t const *const cfg = &gfc->cfg;
1382 EncResult_t *const eov = &gfc->ov_enc;
1384 FLOAT masking_lower_db, adjust = 0.0;
1386 int analog_silence = 1;
1387 int avg, mxb, bits = 0;
1389 eov->bitrate_index = cfg->vbr_max_bitrate_index;
1390 avg = ResvFrameBegin(gfc, &avg) / cfg->mode_gr;
1392 get_framebits(gfc, frameBits);
1394 for (gr = 0; gr < cfg->mode_gr; gr++) {
1395 mxb = on_pe(gfc, pe, max_bits[gr], avg, gr, 0);
1396 if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1397 ms_convert(&gfc->l3_side, gr);
1398 reduce_side(max_bits[gr], ms_ener_ratio[gr], avg, mxb);
1400 for (ch = 0; ch < cfg->channels_out; ++ch) {
1401 gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
1403 if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
1404 adjust = 1.28 / (1 + exp(3.5 - pe[gr][ch] / 300.)) - 0.05;
1405 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
1408 adjust = 2.56 / (1 + exp(3.5 - pe[gr][ch] / 300.)) - 0.14;
1409 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
1411 gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
1413 init_outer_loop(gfc, cod_info);
1414 bands[gr][ch] = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]);
1418 min_bits[gr][ch] = 126;
1420 bits += max_bits[gr][ch];
1423 for (gr = 0; gr < cfg->mode_gr; gr++) {
1424 for (ch = 0; ch < cfg->channels_out; ch++) {
1425 if (bits > frameBits[cfg->vbr_max_bitrate_index] && bits > 0) {
1426 max_bits[gr][ch] *= frameBits[cfg->vbr_max_bitrate_index];
1427 max_bits[gr][ch] /= bits;
1429 if (min_bits[gr][ch] > max_bits[gr][ch])
1430 min_bits[gr][ch] = max_bits[gr][ch];
1435 return analog_silence;
1439 bitpressure_strategy(lame_internal_flags const *gfc,
1440 FLOAT l3_xmin[2][2][SFBMAX], const int min_bits[2][2], int max_bits[2][2])
1442 SessionConfig_t const *const cfg = &gfc->cfg;
1444 for (gr = 0; gr < cfg->mode_gr; gr++) {
1445 for (ch = 0; ch < cfg->channels_out; ch++) {
1446 gr_info const *const gi = &gfc->l3_side.tt[gr][ch];
1447 FLOAT *pxmin = l3_xmin[gr][ch];
1448 for (sfb = 0; sfb < gi->psy_lmax; sfb++)
1449 *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_l / SBMAX_l;
1451 if (gi->block_type == SHORT_TYPE) {
1452 for (sfb = gi->sfb_smin; sfb < SBMAX_s; sfb++) {
1453 *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
1454 *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
1455 *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
1458 max_bits[gr][ch] = Max(min_bits[gr][ch], 0.9 * max_bits[gr][ch]);
1463 /************************************************************************
1465 * VBR_iteration_loop()
1467 * tries to find out how many bits are needed for each granule and channel
1468 * to get an acceptable quantization. An appropriate bitrate will then be
1469 * choosed for quantization. rh 8/99
1471 * Robert Hegemann 2000-09-06 rewrite
1473 ************************************************************************/
1476 VBR_old_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1477 const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1479 SessionConfig_t const *const cfg = &gfc->cfg;
1480 EncResult_t *const eov = &gfc->ov_enc;
1481 FLOAT l3_xmin[2][2][SFBMAX];
1488 int min_bits[2][2], max_bits[2][2];
1490 int ch, gr, analog_silence;
1491 III_side_info_t *const l3_side = &gfc->l3_side;
1493 analog_silence = VBR_old_prepare(gfc, pe, ms_ener_ratio, ratio,
1494 l3_xmin, frameBits, min_bits, max_bits, bands);
1496 /*---------------------------------*/
1499 /* quantize granules with lowest possible number of bits
1504 for (gr = 0; gr < cfg->mode_gr; gr++) {
1505 for (ch = 0; ch < cfg->channels_out; ch++) {
1507 gr_info *const cod_info = &l3_side->tt[gr][ch];
1509 /* init_outer_loop sets up cod_info, scalefac and xrpow
1511 ret = init_xrpow(gfc, cod_info, xrpow);
1512 if (ret == 0 || max_bits[gr][ch] == 0) {
1513 /* xr contains no energy
1514 * l3_enc, our encoding data, will be quantized to zero
1516 continue; /* with next channel */
1519 VBR_encode_granule(gfc, cod_info, l3_xmin[gr][ch], xrpow,
1520 ch, min_bits[gr][ch], max_bits[gr][ch]);
1522 /* do the 'substep shaping'
1524 if (gfc->sv_qnt.substep_shaping & 1) {
1525 trancate_smallspectrums(gfc, &l3_side->tt[gr][ch], l3_xmin[gr][ch], xrpow);
1528 ret = cod_info->part2_3_length + cod_info->part2_length;
1533 /* find lowest bitrate able to hold used bits
1535 if (analog_silence && !cfg->enforce_min_bitrate)
1536 /* we detected analog silence and the user did not specify
1537 * any hard framesize limit, so start with smallest possible frame
1539 eov->bitrate_index = 1;
1541 eov->bitrate_index = cfg->vbr_min_bitrate_index;
1543 for (; eov->bitrate_index < cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
1544 if (used_bits <= frameBits[eov->bitrate_index])
1547 bits = ResvFrameBegin(gfc, &mean_bits);
1549 if (used_bits <= bits)
1552 bitpressure_strategy(gfc, l3_xmin, (const int (*)[2])min_bits, max_bits);
1554 } /* breaks adjusted */
1555 /*--------------------------------------*/
1557 for (gr = 0; gr < cfg->mode_gr; gr++) {
1558 for (ch = 0; ch < cfg->channels_out; ch++) {
1559 iteration_finish_one(gfc, gr, ch);
1562 ResvFrameEnd(gfc, mean_bits);
1568 VBR_new_prepare(lame_internal_flags * gfc,
1569 const FLOAT pe[2][2], const III_psy_ratio ratio[2][2],
1570 FLOAT l3_xmin[2][2][SFBMAX], int frameBits[16], int max_bits[2][2],
1573 SessionConfig_t const *const cfg = &gfc->cfg;
1574 EncResult_t *const eov = &gfc->ov_enc;
1577 int analog_silence = 1;
1579 int maximum_framebits;
1581 if (!cfg->free_format) {
1582 eov->bitrate_index = cfg->vbr_max_bitrate_index;
1583 (void) ResvFrameBegin(gfc, &avg);
1584 *max_resv = gfc->sv_enc.ResvMax;
1586 get_framebits(gfc, frameBits);
1587 maximum_framebits = frameBits[cfg->vbr_max_bitrate_index];
1590 eov->bitrate_index = 0;
1591 maximum_framebits = ResvFrameBegin(gfc, &avg);
1592 frameBits[0] = maximum_framebits;
1593 *max_resv = gfc->sv_enc.ResvMax;
1596 for (gr = 0; gr < cfg->mode_gr; gr++) {
1597 (void) on_pe(gfc, pe, max_bits[gr], avg, gr, 0);
1598 if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1599 ms_convert(&gfc->l3_side, gr);
1601 for (ch = 0; ch < cfg->channels_out; ++ch) {
1602 gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
1604 gfc->sv_qnt.masking_lower = pow(10.0, gfc->sv_qnt.mask_adjust * 0.1);
1606 init_outer_loop(gfc, cod_info);
1607 if (0 != calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]))
1610 bits += max_bits[gr][ch];
1613 for (gr = 0; gr < cfg->mode_gr; gr++) {
1614 for (ch = 0; ch < cfg->channels_out; ch++) {
1615 if (bits > maximum_framebits && bits > 0) {
1616 max_bits[gr][ch] *= maximum_framebits;
1617 max_bits[gr][ch] /= bits;
1622 if (analog_silence) {
1625 return analog_silence;
1631 VBR_new_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1632 const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1634 SessionConfig_t const *const cfg = &gfc->cfg;
1635 EncResult_t *const eov = &gfc->ov_enc;
1636 FLOAT l3_xmin[2][2][SFBMAX];
1638 FLOAT xrpow[2][2][576];
1642 int ch, gr, analog_silence, pad;
1643 III_side_info_t *const l3_side = &gfc->l3_side;
1645 const FLOAT (*const_l3_xmin)[2][SFBMAX] = (const FLOAT (*)[2][SFBMAX])l3_xmin;
1646 const FLOAT (*const_xrpow)[2][576] = (const FLOAT (*)[2][576])xrpow;
1647 const int (*const_max_bits)[2] = (const int (*)[2])max_bits;
1649 (void) ms_ener_ratio; /* not used */
1651 memset(xrpow, 0, sizeof(xrpow));
1653 analog_silence = VBR_new_prepare(gfc, pe, ratio, l3_xmin, frameBits, max_bits, &pad);
1655 for (gr = 0; gr < cfg->mode_gr; gr++) {
1656 for (ch = 0; ch < cfg->channels_out; ch++) {
1657 gr_info *const cod_info = &l3_side->tt[gr][ch];
1659 /* init_outer_loop sets up cod_info, scalefac and xrpow
1661 if (0 == init_xrpow(gfc, cod_info, xrpow[gr][ch])) {
1662 max_bits[gr][ch] = 0; /* silent granule needs no bits */
1667 /* quantize granules with lowest possible number of bits
1670 used_bits = VBR_encode_frame(gfc, const_xrpow, const_l3_xmin, const_max_bits);
1672 if (!cfg->free_format) {
1675 /* find lowest bitrate able to hold used bits
1677 if (analog_silence && !cfg->enforce_min_bitrate) {
1678 /* we detected analog silence and the user did not specify
1679 * any hard framesize limit, so start with smallest possible frame
1684 i = cfg->vbr_min_bitrate_index;
1687 for (; i < cfg->vbr_max_bitrate_index; i++) {
1688 if (used_bits <= frameBits[i])
1691 if (i > cfg->vbr_max_bitrate_index) {
1692 i = cfg->vbr_max_bitrate_index;
1695 for (j = cfg->vbr_max_bitrate_index; j > i; --j) {
1696 int const unused = frameBits[j] - used_bits;
1700 eov->bitrate_index = j;
1703 eov->bitrate_index = i;
1709 int fff = getFramesize_kbps(gfc, used_bits);
1710 int hhh = getFramesize_kbps(gfc, MAX_BITS_PER_GRANULE * cfg->mode_gr);
1713 printf("demand=%3d kbps max=%3d kbps limit=%3d kbps\n", fff, mmm, hhh);
1715 eov->bitrate_index = 0;
1717 if (used_bits <= frameBits[eov->bitrate_index]) {
1718 /* update Reservoire status */
1719 int mean_bits, fullframebits;
1720 fullframebits = ResvFrameBegin(gfc, &mean_bits);
1721 assert(used_bits <= fullframebits);
1722 for (gr = 0; gr < cfg->mode_gr; gr++) {
1723 for (ch = 0; ch < cfg->channels_out; ch++) {
1724 gr_info const *const cod_info = &l3_side->tt[gr][ch];
1725 ResvAdjust(gfc, cod_info);
1728 ResvFrameEnd(gfc, mean_bits);
1731 /* SHOULD NOT HAPPEN INTERNAL ERROR
1733 ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE, please send bug report\n");
1742 /********************************************************************
1744 * calc_target_bits()
1746 * calculates target bits for ABR encoding
1750 ********************************************************************/
1753 calc_target_bits(lame_internal_flags * gfc,
1754 const FLOAT pe[2][2],
1755 FLOAT const ms_ener_ratio[2],
1756 int targ_bits[2][2], int *analog_silence_bits, int *max_frame_bits)
1758 SessionConfig_t const *const cfg = &gfc->cfg;
1759 EncResult_t *const eov = &gfc->ov_enc;
1760 III_side_info_t const *const l3_side = &gfc->l3_side;
1762 int gr, ch, totbits, mean_bits;
1763 int framesize = 576 * cfg->mode_gr;
1765 eov->bitrate_index = cfg->vbr_max_bitrate_index;
1766 *max_frame_bits = ResvFrameBegin(gfc, &mean_bits);
1768 eov->bitrate_index = 1;
1769 mean_bits = getframebits(gfc) - cfg->sideinfo_len * 8;
1770 *analog_silence_bits = mean_bits / (cfg->mode_gr * cfg->channels_out);
1772 mean_bits = cfg->vbr_avg_bitrate_kbps * framesize * 1000;
1773 if (gfc->sv_qnt.substep_shaping & 1)
1775 mean_bits /= cfg->samplerate_out;
1776 mean_bits -= cfg->sideinfo_len * 8;
1777 mean_bits /= (cfg->mode_gr * cfg->channels_out);
1780 res_factor is the percentage of the target bitrate that should
1781 be used on average. the remaining bits are added to the
1782 bitreservoir and used for difficult to encode frames.
1784 Since we are tracking the average bitrate, we should adjust
1785 res_factor "on the fly", increasing it if the average bitrate
1786 is greater than the requested bitrate, and decreasing it
1787 otherwise. Reasonable ranges are from .9 to 1.0
1789 Until we get the above suggestion working, we use the following
1791 compression ratio res_factor
1792 5.5 (256kbps) 1.0 no need for bitreservoir
1793 11 (128kbps) .93 7% held for reservoir
1795 with linear interpolation for other values.
1798 res_factor = .93 + .07 * (11.0 - cfg->compression_ratio) / (11.0 - 5.5);
1799 if (res_factor < .90)
1801 if (res_factor > 1.00)
1804 for (gr = 0; gr < cfg->mode_gr; gr++) {
1806 for (ch = 0; ch < cfg->channels_out; ch++) {
1807 targ_bits[gr][ch] = res_factor * mean_bits;
1809 if (pe[gr][ch] > 700) {
1810 int add_bits = (pe[gr][ch] - 700) / 1.4;
1812 gr_info const *const cod_info = &l3_side->tt[gr][ch];
1813 targ_bits[gr][ch] = res_factor * mean_bits;
1815 /* short blocks use a little extra, no matter what the pe */
1816 if (cod_info->block_type == SHORT_TYPE) {
1817 if (add_bits < mean_bits / 2)
1818 add_bits = mean_bits / 2;
1820 /* at most increase bits by 1.5*average */
1821 if (add_bits > mean_bits * 3 / 2)
1822 add_bits = mean_bits * 3 / 2;
1823 else if (add_bits < 0)
1826 targ_bits[gr][ch] += add_bits;
1828 if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL) {
1829 targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
1831 sum += targ_bits[gr][ch];
1833 if (sum > MAX_BITS_PER_GRANULE) {
1834 for (ch = 0; ch < cfg->channels_out; ++ch) {
1835 targ_bits[gr][ch] *= MAX_BITS_PER_GRANULE;
1836 targ_bits[gr][ch] /= sum;
1841 if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR)
1842 for (gr = 0; gr < cfg->mode_gr; gr++) {
1843 reduce_side(targ_bits[gr], ms_ener_ratio[gr], mean_bits * cfg->channels_out,
1844 MAX_BITS_PER_GRANULE);
1850 for (gr = 0; gr < cfg->mode_gr; gr++) {
1851 for (ch = 0; ch < cfg->channels_out; ch++) {
1852 if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL)
1853 targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
1854 totbits += targ_bits[gr][ch];
1858 /* repartion target bits if needed
1860 if (totbits > *max_frame_bits && totbits > 0) {
1861 for (gr = 0; gr < cfg->mode_gr; gr++) {
1862 for (ch = 0; ch < cfg->channels_out; ch++) {
1863 targ_bits[gr][ch] *= *max_frame_bits;
1864 targ_bits[gr][ch] /= totbits;
1875 /********************************************************************
1877 * ABR_iteration_loop()
1879 * encode a frame with a disired average bitrate
1883 ********************************************************************/
1886 ABR_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1887 const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1889 SessionConfig_t const *const cfg = &gfc->cfg;
1890 EncResult_t *const eov = &gfc->ov_enc;
1891 FLOAT l3_xmin[SFBMAX];
1893 int targ_bits[2][2];
1894 int mean_bits, max_frame_bits;
1895 int ch, gr, ath_over;
1896 int analog_silence_bits;
1898 III_side_info_t *const l3_side = &gfc->l3_side;
1902 calc_target_bits(gfc, pe, ms_ener_ratio, targ_bits, &analog_silence_bits, &max_frame_bits);
1906 for (gr = 0; gr < cfg->mode_gr; gr++) {
1908 if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1909 ms_convert(&gfc->l3_side, gr);
1911 for (ch = 0; ch < cfg->channels_out; ch++) {
1912 FLOAT adjust, masking_lower_db;
1913 cod_info = &l3_side->tt[gr][ch];
1915 if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
1916 /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05; */
1918 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
1921 /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
1923 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
1925 gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
1928 /* cod_info, scalefac and xrpow get initialized in init_outer_loop
1930 init_outer_loop(gfc, cod_info);
1931 if (init_xrpow(gfc, cod_info, xrpow)) {
1932 /* xr contains energy we will have to encode
1933 * calculate the masking abilities
1934 * find some good quantization in outer_loop
1936 ath_over = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin);
1937 if (0 == ath_over) /* analog silence */
1938 targ_bits[gr][ch] = analog_silence_bits;
1940 (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[gr][ch]);
1942 iteration_finish_one(gfc, gr, ch);
1946 /* find a bitrate which can refill the resevoir to positive size.
1948 for (eov->bitrate_index = cfg->vbr_min_bitrate_index;
1949 eov->bitrate_index <= cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
1950 if (ResvFrameBegin(gfc, &mean_bits) >= 0)
1953 assert(eov->bitrate_index <= cfg->vbr_max_bitrate_index);
1955 ResvFrameEnd(gfc, mean_bits);
1963 /************************************************************************
1965 * CBR_iteration_loop()
1969 * encodes one frame of MP3 data with constant bitrate
1971 ************************************************************************/
1974 CBR_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1975 const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1977 SessionConfig_t const *const cfg = &gfc->cfg;
1978 FLOAT l3_xmin[SFBMAX];
1981 int mean_bits, max_bits;
1983 III_side_info_t *const l3_side = &gfc->l3_side;
1986 (void) ResvFrameBegin(gfc, &mean_bits);
1989 for (gr = 0; gr < cfg->mode_gr; gr++) {
1991 /* calculate needed bits
1993 max_bits = on_pe(gfc, pe, targ_bits, mean_bits, gr, gr);
1995 if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1996 ms_convert(&gfc->l3_side, gr);
1997 reduce_side(targ_bits, ms_ener_ratio[gr], mean_bits, max_bits);
2000 for (ch = 0; ch < cfg->channels_out; ch++) {
2001 FLOAT adjust, masking_lower_db;
2002 cod_info = &l3_side->tt[gr][ch];
2004 if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
2005 /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05; */
2007 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
2010 /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
2012 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
2014 gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
2016 /* init_outer_loop sets up cod_info, scalefac and xrpow
2018 init_outer_loop(gfc, cod_info);
2019 if (init_xrpow(gfc, cod_info, xrpow)) {
2020 /* xr contains energy we will have to encode
2021 * calculate the masking abilities
2022 * find some good quantization in outer_loop
2024 (void) calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin);
2025 (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[ch]);
2028 iteration_finish_one(gfc, gr, ch);
2029 assert(cod_info->part2_3_length <= MAX_BITS_PER_CHANNEL);
2030 assert(cod_info->part2_3_length <= targ_bits[ch]);
2034 ResvFrameEnd(gfc, mean_bits);