1 /*
2  * MP3 quantization
3  *
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
8  *
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.
13  *
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.
18  *
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.
23  */
24 
25 /* $Id: quantize.c,v 1.219 2017/08/02 19:48:05 robert Exp $ */
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 #include "lame.h"
32 #include "machine.h"
33 #include "encoder.h"
34 #include "util.h"
35 #include "quantize_pvt.h"
36 #include "reservoir.h"
37 #include "bitstream.h"
38 #include "vbrquantize.h"
39 #include "quantize.h"
40 #ifdef HAVE_XMMINTRIN_H
41 #include "vector/lame_intrin.h"
42 #endif
43 
44 
45 
46 
47 /* convert from L/R <-> Mid/Side */
48 static void
ms_convert(III_side_info_t * l3_side,int gr)49 ms_convert(III_side_info_t * l3_side, int gr)
50 {
51     int     i;
52     for (i = 0; i < 576; ++i) {
53         FLOAT   l, r;
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);
58     }
59 }
60 
61 /************************************************************************
62  *
63  *      init_outer_loop()
64  *  mt 6/99
65  *
66  *  initializes cod_info, scalefac and xrpow
67  *
68  *  returns 0 if all energies in xr are zero, else 1
69  *
70  ************************************************************************/
71 
72 static void
init_xrpow_core_c(gr_info * const cod_info,FLOAT xrpow[576],int upper,FLOAT * sum)73 init_xrpow_core_c(gr_info * const cod_info, FLOAT xrpow[576], int upper, FLOAT * sum)
74 {
75     int     i;
76     FLOAT   tmp;
77     *sum = 0;
78     for (i = 0; i <= upper; ++i) {
79         tmp = fabs(cod_info->xr[i]);
80         *sum += tmp;
81         xrpow[i] = sqrt(tmp * sqrt(tmp));
82 
83         if (xrpow[i] > cod_info->xrpow_max)
84             cod_info->xrpow_max = xrpow[i];
85     }
86 }
87 
88 
89 
90 
91 
92 void
init_xrpow_core_init(lame_internal_flags * const gfc)93 init_xrpow_core_init(lame_internal_flags * const gfc)
94 {
95     gfc->init_xrpow_core = init_xrpow_core_c;
96 
97 #if defined(HAVE_XMMINTRIN_H)
98     if (gfc->CPU_features.SSE)
99         gfc->init_xrpow_core = init_xrpow_core_sse;
100 #endif
101 #ifndef HAVE_NASM
102 #ifdef MIN_ARCH_SSE
103     gfc->init_xrpow_core = init_xrpow_core_sse;
104 #endif
105 #endif
106 }
107 
108 
109 
110 static int
init_xrpow(lame_internal_flags * gfc,gr_info * const cod_info,FLOAT xrpow[576])111 init_xrpow(lame_internal_flags * gfc, gr_info * const cod_info, FLOAT xrpow[576])
112 {
113     FLOAT   sum = 0;
114     int     i;
115     int const upper = cod_info->max_nonzero_coeff;
116 
117     assert(xrpow != NULL);
118     cod_info->xrpow_max = 0;
119 
120     /*  check if there is some energy we have to quantize
121      *  and calculate xrpow matching our fresh scalefactors
122      */
123     assert(0 <= upper && upper <= 575);
124     memset(&(xrpow[upper]), 0, (576 - upper) * sizeof(xrpow[0]));
125 
126 
127     gfc->init_xrpow_core(cod_info, xrpow, upper, &sum);
128 
129     /*  return 1 if we have something to quantize, else 0
130      */
131     if (sum > (FLOAT) 1E-20) {
132         int     j = 0;
133         if (gfc->sv_qnt.substep_shaping & 2)
134             j = 1;
135 
136         for (i = 0; i < cod_info->psymax; i++)
137             gfc->sv_qnt.pseudohalf[i] = j;
138 
139         return 1;
140     }
141 
142     memset(&cod_info->l3_enc[0], 0, sizeof(int) * 576);
143     return 0;
144 }
145 
146 
147 
148 
149 
150 /*
151 Gabriel Bouvigne feb/apr 2003
152 Analog silence detection in partitionned sfb21
153 or sfb12 for short blocks
154 
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.
158 */
159 static void
psfb21_analogsilence(lame_internal_flags const * gfc,gr_info * const cod_info)160 psfb21_analogsilence(lame_internal_flags const *gfc, gr_info * const cod_info)
161 {
162     ATH_t const *const ATH = gfc->ATH;
163     FLOAT  *const xr = cod_info->xr;
164 
165     if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT blocks */
166         int     gsfb;
167         int     stop = 0;
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];
171             int     j;
172             FLOAT   ath21;
173             ath21 = athAdjust(ATH->adjust_factor, ATH->psfb21[gsfb], ATH->floor, 0);
174 
175             if (gfc->sv_qnt.longfact[21] > 1e-12f)
176                 ath21 *= gfc->sv_qnt.longfact[21];
177 
178             for (j = end - 1; j >= start; j--) {
179                 if (fabs(xr[j]) < ath21)
180                     xr[j] = 0;
181                 else {
182                     stop = 1;
183                     break;
184                 }
185             }
186         }
187     }
188     else {
189         /*note: short blocks coeffs are reordered */
190         int     block;
191         for (block = 0; block < 3; block++) {
192 
193             int     gsfb;
194             int     stop = 0;
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]);
199                 int const end =
200                     start + (gfc->scalefac_band.psfb12[gsfb + 1] - gfc->scalefac_band.psfb12[gsfb]);
201                 int     j;
202                 FLOAT   ath12;
203                 ath12 = athAdjust(ATH->adjust_factor, ATH->psfb12[gsfb], ATH->floor, 0);
204 
205                 if (gfc->sv_qnt.shortfact[12] > 1e-12f)
206                     ath12 *= gfc->sv_qnt.shortfact[12];
207 
208                 for (j = end - 1; j >= start; j--) {
209                     if (fabs(xr[j]) < ath12)
210                         xr[j] = 0;
211                     else {
212                         stop = 1;
213                         break;
214                     }
215                 }
216             }
217         }
218     }
219 
220 }
221 
222 
223 
224 
225 
226 static void
init_outer_loop(lame_internal_flags const * gfc,gr_info * const cod_info)227 init_outer_loop(lame_internal_flags const *gfc, gr_info * const cod_info)
228 {
229     SessionConfig_t const *const cfg = &gfc->cfg;
230     int     sfb, j;
231     /*  initialize fresh cod_info
232      */
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     if (cfg->samplerate_out <= 8000) {
253       cod_info->sfb_lmax = 17;
254       cod_info->sfb_smin = 9;
255       cod_info->psy_lmax = 17;
256     }
257     else {
258       cod_info->sfb_lmax = SBPSY_l;
259       cod_info->sfb_smin = SBPSY_s;
260       cod_info->psy_lmax = gfc->sv_qnt.sfb21_extra ? SBMAX_l : SBPSY_l;
261     }
262     cod_info->psymax = cod_info->psy_lmax;
263     cod_info->sfbmax = cod_info->sfb_lmax;
264     cod_info->sfbdivide = 11;
265     for (sfb = 0; sfb < SBMAX_l; sfb++) {
266         cod_info->width[sfb]
267             = gfc->scalefac_band.l[sfb + 1] - gfc->scalefac_band.l[sfb];
268         cod_info->window[sfb] = 3; /* which is always 0. */
269     }
270     if (cod_info->block_type == SHORT_TYPE) {
271         FLOAT   ixwork[576];
272         FLOAT  *ix;
273 
274         cod_info->sfb_smin = 0;
275         cod_info->sfb_lmax = 0;
276         if (cod_info->mixed_block_flag) {
277             /*
278              *  MPEG-1:      sfbs 0-7 long block, 3-12 short blocks
279              *  MPEG-2(.5):  sfbs 0-5 long block, 3-12 short blocks
280              */
281             cod_info->sfb_smin = 3;
282             cod_info->sfb_lmax = cfg->mode_gr * 2 + 4;
283         }
284         if (cfg->samplerate_out <= 8000) {
285             cod_info->psymax
286                 = cod_info->sfb_lmax
287                 + 3 * (9 - cod_info->sfb_smin);
288             cod_info->sfbmax = cod_info->sfb_lmax + 3 * (9 - cod_info->sfb_smin);
289         }
290         else {
291             cod_info->psymax
292                 = cod_info->sfb_lmax
293                 + 3 * ((gfc->sv_qnt.sfb21_extra ? SBMAX_s : SBPSY_s) - cod_info->sfb_smin);
294             cod_info->sfbmax = cod_info->sfb_lmax + 3 * (SBPSY_s - cod_info->sfb_smin);
295         }
296         cod_info->sfbdivide = cod_info->sfbmax - 18;
297         cod_info->psy_lmax = cod_info->sfb_lmax;
298         /* re-order the short blocks, for more efficient encoding below */
299         /* By Takehiro TOMINAGA */
300         /*
301            Within each scalefactor band, data is given for successive
302            time windows, beginning with window 0 and ending with window 2.
303            Within each window, the quantized values are then arranged in
304            order of increasing frequency...
305          */
306         ix = &cod_info->xr[gfc->scalefac_band.l[cod_info->sfb_lmax]];
307         memcpy(ixwork, cod_info->xr, 576 * sizeof(FLOAT));
308         for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
309             int const start = gfc->scalefac_band.s[sfb];
310             int const end = gfc->scalefac_band.s[sfb + 1];
311             int     window, l;
312             for (window = 0; window < 3; window++) {
313                 for (l = start; l < end; l++) {
314                     *ix++ = ixwork[3 * l + window];
315                 }
316             }
317         }
318 
319         j = cod_info->sfb_lmax;
320         for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
321             cod_info->width[j] = cod_info->width[j + 1] = cod_info->width[j + 2]
322                 = gfc->scalefac_band.s[sfb + 1] - gfc->scalefac_band.s[sfb];
323             cod_info->window[j] = 0;
324             cod_info->window[j + 1] = 1;
325             cod_info->window[j + 2] = 2;
326             j += 3;
327         }
328     }
329 
330     cod_info->count1bits = 0;
331     cod_info->sfb_partition_table = nr_of_sfb_block[0][0];
332     cod_info->slen[0] = 0;
333     cod_info->slen[1] = 0;
334     cod_info->slen[2] = 0;
335     cod_info->slen[3] = 0;
336 
337     cod_info->max_nonzero_coeff = 575;
338 
339     /*  fresh scalefactors are all zero
340      */
341     memset(cod_info->scalefac, 0, sizeof(cod_info->scalefac));
342 
343     if (cfg->vbr != vbr_mt && cfg->vbr != vbr_mtrh && cfg->vbr != vbr_abr && cfg->vbr != vbr_off) {
344         psfb21_analogsilence(gfc, cod_info);
345     }
346 }
347 
348 
349 
350 /************************************************************************
351  *
352  *      bin_search_StepSize()
353  *
354  *  author/date??
355  *
356  *  binary step size search
357  *  used by outer_loop to get a quantizer step size to start with
358  *
359  ************************************************************************/
360 
361 typedef enum {
362     BINSEARCH_NONE,
363     BINSEARCH_UP,
364     BINSEARCH_DOWN
365 } binsearchDirection_t;
366 
367 static int
bin_search_StepSize(lame_internal_flags * const gfc,gr_info * const cod_info,int desired_rate,const int ch,const FLOAT xrpow[576])368 bin_search_StepSize(lame_internal_flags * const gfc, gr_info * const cod_info,
369                     int desired_rate, const int ch, const FLOAT xrpow[576])
370 {
371     int     nBits;
372     int     CurrentStep = gfc->sv_qnt.CurrentStep[ch];
373     int     flag_GoneOver = 0;
374     int const start = gfc->sv_qnt.OldValue[ch];
375     binsearchDirection_t Direction = BINSEARCH_NONE;
376     cod_info->global_gain = start;
377     desired_rate -= cod_info->part2_length;
378 
379     assert(CurrentStep);
380     for (;;) {
381         int     step;
382         nBits = count_bits(gfc, xrpow, cod_info, 0);
383 
384         if (CurrentStep == 1 || nBits == desired_rate)
385             break;      /* nothing to adjust anymore */
386 
387         if (nBits > desired_rate) {
388             /* increase Quantize_StepSize */
389             if (Direction == BINSEARCH_DOWN)
390                 flag_GoneOver = 1;
391 
392             if (flag_GoneOver)
393                 CurrentStep /= 2;
394             Direction = BINSEARCH_UP;
395             step = CurrentStep;
396         }
397         else {
398             /* decrease Quantize_StepSize */
399             if (Direction == BINSEARCH_UP)
400                 flag_GoneOver = 1;
401 
402             if (flag_GoneOver)
403                 CurrentStep /= 2;
404             Direction = BINSEARCH_DOWN;
405             step = -CurrentStep;
406         }
407         cod_info->global_gain += step;
408         if (cod_info->global_gain < 0) {
409             cod_info->global_gain = 0;
410             flag_GoneOver = 1;
411         }
412         if (cod_info->global_gain > 255) {
413             cod_info->global_gain = 255;
414             flag_GoneOver = 1;
415         }
416     }
417 
418     assert(cod_info->global_gain >= 0);
419     assert(cod_info->global_gain < 256);
420 
421     while (nBits > desired_rate && cod_info->global_gain < 255) {
422         cod_info->global_gain++;
423         nBits = count_bits(gfc, xrpow, cod_info, 0);
424     }
425     gfc->sv_qnt.CurrentStep[ch] = (start - cod_info->global_gain >= 4) ? 4 : 2;
426     gfc->sv_qnt.OldValue[ch] = cod_info->global_gain;
427     cod_info->part2_3_length = nBits;
428     return nBits;
429 }
430 
431 
432 
433 
434 /************************************************************************
435  *
436  *      trancate_smallspectrums()
437  *
438  *  Takehiro TOMINAGA 2002-07-21
439  *
440  *  trancate smaller nubmers into 0 as long as the noise threshold is allowed.
441  *
442  ************************************************************************/
443 static int
floatcompare(const void * v1,const void * v2)444 floatcompare(const void *v1, const void *v2)
445 {
446     const FLOAT *const a = v1, *const b = v2;
447     if (*a > *b)
448         return 1;
449     if (*a < *b)
450         return -1;
451     return 0;
452 }
453 
454 static void
trancate_smallspectrums(lame_internal_flags const * gfc,gr_info * const gi,const FLOAT * const l3_xmin,FLOAT * const work)455 trancate_smallspectrums(lame_internal_flags const *gfc,
456                         gr_info * const gi, const FLOAT * const l3_xmin, FLOAT * const work)
457 {
458     int     sfb, j, width;
459     FLOAT   distort[SFBMAX];
460     calc_noise_result dummy;
461 
462     if ((!(gfc->sv_qnt.substep_shaping & 4) && gi->block_type == SHORT_TYPE)
463         || gfc->sv_qnt.substep_shaping & 0x80)
464         return;
465     (void) calc_noise(gi, l3_xmin, distort, &dummy, 0);
466     for (j = 0; j < 576; j++) {
467         FLOAT   xr = 0.0;
468         if (gi->l3_enc[j] != 0)
469             xr = fabs(gi->xr[j]);
470         work[j] = xr;
471     }
472 
473     j = 0;
474     sfb = 8;
475     if (gi->block_type == SHORT_TYPE)
476         sfb = 6;
477     do {
478         FLOAT   allowedNoise, trancateThreshold;
479         int     nsame, start;
480 
481         width = gi->width[sfb];
482         j += width;
483         if (distort[sfb] >= 1.0)
484             continue;
485 
486         qsort(&work[j - width], width, sizeof(FLOAT), floatcompare);
487         if (EQ(work[j - 1], 0.0))
488             continue;   /* all zero sfb */
489 
490         allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];
491         trancateThreshold = 0.0;
492         start = 0;
493         do {
494             FLOAT   noise;
495             for (nsame = 1; start + nsame < width; nsame++)
496                 if (NEQ(work[start + j - width], work[start + j + nsame - width]))
497                     break;
498 
499             noise = work[start + j - width] * work[start + j - width] * nsame;
500             if (allowedNoise < noise) {
501                 if (start != 0)
502                     trancateThreshold = work[start + j - width - 1];
503                 break;
504             }
505             allowedNoise -= noise;
506             start += nsame;
507         } while (start < width);
508         if (EQ(trancateThreshold, 0.0))
509             continue;
510 
511 /*      printf("%e %e %e\n", */
512 /*             trancateThreshold/l3_xmin[sfb], */
513 /*             trancateThreshold/(l3_xmin[sfb]*start), */
514 /*             trancateThreshold/(l3_xmin[sfb]*(start+width)) */
515 /*          ); */
516 /*      if (trancateThreshold > 1000*l3_xmin[sfb]*start) */
517 /*          trancateThreshold = 1000*l3_xmin[sfb]*start; */
518 
519         do {
520             if (fabs(gi->xr[j - width]) <= trancateThreshold)
521                 gi->l3_enc[j - width] = 0;
522         } while (--width > 0);
523     } while (++sfb < gi->psymax);
524 
525     gi->part2_3_length = noquant_count_bits(gfc, gi, 0);
526 }
527 
528 
529 /*************************************************************************
530  *
531  *      loop_break()
532  *
533  *  author/date??
534  *
535  *  Function: Returns zero if there is a scalefac which has not been
536  *            amplified. Otherwise it returns one.
537  *
538  *************************************************************************/
539 
540 inline static int
loop_break(const gr_info * const cod_info)541 loop_break(const gr_info * const cod_info)
542 {
543     int     sfb;
544 
545     for (sfb = 0; sfb < cod_info->sfbmax; sfb++)
546         if (cod_info->scalefac[sfb]
547             + cod_info->subblock_gain[cod_info->window[sfb]] == 0)
548             return 0;
549 
550     return 1;
551 }
552 
553 
554 
555 
556 /*  mt 5/99:  Function: Improved calc_noise for a single channel   */
557 
558 /*************************************************************************
559  *
560  *      quant_compare()
561  *
562  *  author/date??
563  *
564  *  several different codes to decide which quantization is better
565  *
566  *************************************************************************/
567 
568 static double
penalties(double noise)569 penalties(double noise)
570 {
571     return FAST_LOG10(0.368 + 0.632 * noise * noise * noise);
572 }
573 
574 static double
get_klemm_noise(const FLOAT * distort,const gr_info * const gi)575 get_klemm_noise(const FLOAT * distort, const gr_info * const gi)
576 {
577     int     sfb;
578     double  klemm_noise = 1E-37;
579     for (sfb = 0; sfb < gi->psymax; sfb++)
580         klemm_noise += penalties(distort[sfb]);
581 
582     return Max(1e-20, klemm_noise);
583 }
584 
585 inline static int
quant_compare(const int quant_comp,const calc_noise_result * const best,calc_noise_result * const calc,const gr_info * const gi,const FLOAT * distort)586 quant_compare(const int quant_comp,
587               const calc_noise_result * const best,
588               calc_noise_result * const calc, const gr_info * const gi, const FLOAT * distort)
589 {
590     /*
591        noise is given in decibels (dB) relative to masking thesholds.
592 
593        over_noise:  ??? (the previous comment is fully wrong)
594        tot_noise:   ??? (the previous comment is fully wrong)
595        max_noise:   max quantization noise
596 
597      */
598     int     better;
599 
600     switch (quant_comp) {
601     default:
602     case 9:{
603             if (best->over_count > 0) {
604                 /* there are distorted sfb */
605                 better = calc->over_SSD <= best->over_SSD;
606                 if (calc->over_SSD == best->over_SSD)
607                     better = calc->bits < best->bits;
608             }
609             else {
610                 /* no distorted sfb */
611                 better = ((calc->max_noise < 0) &&
612                           ((calc->max_noise * 10 + calc->bits) <=
613                            (best->max_noise * 10 + best->bits)));
614             }
615             break;
616         }
617 
618     case 0:
619         better = calc->over_count < best->over_count
620             || (calc->over_count == best->over_count && calc->over_noise < best->over_noise)
621             || (calc->over_count == best->over_count &&
622                 EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
623         break;
624 
625     case 8:
626         calc->max_noise = get_klemm_noise(distort, gi);
627         /*lint --fallthrough */
628     case 1:
629         better = calc->max_noise < best->max_noise;
630         break;
631     case 2:
632         better = calc->tot_noise < best->tot_noise;
633         break;
634     case 3:
635         better = (calc->tot_noise < best->tot_noise)
636             && (calc->max_noise < best->max_noise);
637         break;
638     case 4:
639         better = (calc->max_noise <= 0.0 && best->max_noise > 0.2)
640             || (calc->max_noise <= 0.0 &&
641                 best->max_noise < 0.0 &&
642                 best->max_noise > calc->max_noise - 0.2 && calc->tot_noise < best->tot_noise)
643             || (calc->max_noise <= 0.0 &&
644                 best->max_noise > 0.0 &&
645                 best->max_noise > calc->max_noise - 0.2 &&
646                 calc->tot_noise < best->tot_noise + best->over_noise)
647             || (calc->max_noise > 0.0 &&
648                 best->max_noise > -0.05 &&
649                 best->max_noise > calc->max_noise - 0.1 &&
650                 calc->tot_noise + calc->over_noise < best->tot_noise + best->over_noise)
651             || (calc->max_noise > 0.0 &&
652                 best->max_noise > -0.1 &&
653                 best->max_noise > calc->max_noise - 0.15 &&
654                 calc->tot_noise + calc->over_noise + calc->over_noise <
655                 best->tot_noise + best->over_noise + best->over_noise);
656         break;
657     case 5:
658         better = calc->over_noise < best->over_noise
659             || (EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
660         break;
661     case 6:
662         better = calc->over_noise < best->over_noise
663             || (EQ(calc->over_noise, best->over_noise) &&
664                 (calc->max_noise < best->max_noise
665                  || (EQ(calc->max_noise, best->max_noise) && calc->tot_noise <= best->tot_noise)
666                 ));
667         break;
668     case 7:
669         better = calc->over_count < best->over_count || calc->over_noise < best->over_noise;
670         break;
671     }
672 
673 
674     if (best->over_count == 0) {
675         /*
676            If no distorted bands, only use this quantization
677            if it is better, and if it uses less bits.
678            Unfortunately, part2_3_length is sometimes a poor
679            estimator of the final size at low bitrates.
680          */
681         better = better && calc->bits < best->bits;
682     }
683 
684 
685     return better;
686 }
687 
688 
689 
690 /*************************************************************************
691  *
692  *          amp_scalefac_bands()
693  *
694  *  author/date??
695  *
696  *  Amplify the scalefactor bands that violate the masking threshold.
697  *  See ISO 11172-3 Section C.1.5.4.3.5
698  *
699  *  distort[] = noise/masking
700  *  distort[] > 1   ==> noise is not masked
701  *  distort[] < 1   ==> noise is masked
702  *  max_dist = maximum value of distort[]
703  *
704  *  Three algorithms:
705  *  noise_shaping_amp
706  *        0             Amplify all bands with distort[]>1.
707  *
708  *        1             Amplify all bands with distort[] >= max_dist^(.5);
709  *                     ( 50% in the db scale)
710  *
711  *        2             Amplify first band with distort[] >= max_dist;
712  *
713  *
714  *  For algorithms 0 and 1, if max_dist < 1, then amplify all bands
715  *  with distort[] >= .95*max_dist.  This is to make sure we always
716  *  amplify at least one band.
717  *
718  *
719  *************************************************************************/
720 static void
amp_scalefac_bands(lame_internal_flags * gfc,gr_info * const cod_info,FLOAT const * distort,FLOAT xrpow[576],int bRefine)721 amp_scalefac_bands(lame_internal_flags * gfc,
722                    gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
723 {
724     SessionConfig_t const *const cfg = &gfc->cfg;
725     int     j, sfb;
726     FLOAT   ifqstep34, trigger;
727     int     noise_shaping_amp;
728 
729     if (cod_info->scalefac_scale == 0) {
730         ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5) */
731     }
732     else {
733         ifqstep34 = 1.68179283050742922612; /* 2**(.75*1) */
734     }
735 
736     /* compute maximum value of distort[]  */
737     trigger = 0;
738     for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
739         if (trigger < distort[sfb])
740             trigger = distort[sfb];
741     }
742 
743     noise_shaping_amp = cfg->noise_shaping_amp;
744     if (noise_shaping_amp == 3) {
745         if (bRefine == 1)
746             noise_shaping_amp = 2;
747         else
748             noise_shaping_amp = 1;
749     }
750     switch (noise_shaping_amp) {
751     case 2:
752         /* amplify exactly 1 band */
753         break;
754 
755     case 1:
756         /* amplify bands within 50% of max (on db scale) */
757         if (trigger > 1.0)
758             trigger = pow(trigger, .5);
759         else
760             trigger *= .95;
761         break;
762 
763     case 0:
764     default:
765         /* ISO algorithm.  amplify all bands with distort>1 */
766         if (trigger > 1.0)
767             trigger = 1.0;
768         else
769             trigger *= .95;
770         break;
771     }
772 
773     j = 0;
774     for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
775         int const width = cod_info->width[sfb];
776         int     l;
777         j += width;
778         if (distort[sfb] < trigger)
779             continue;
780 
781         if (gfc->sv_qnt.substep_shaping & 2) {
782             gfc->sv_qnt.pseudohalf[sfb] = !gfc->sv_qnt.pseudohalf[sfb];
783             if (!gfc->sv_qnt.pseudohalf[sfb] && cfg->noise_shaping_amp == 2)
784                 return;
785         }
786         cod_info->scalefac[sfb]++;
787         for (l = -width; l < 0; l++) {
788             xrpow[j + l] *= ifqstep34;
789             if (xrpow[j + l] > cod_info->xrpow_max)
790                 cod_info->xrpow_max = xrpow[j + l];
791         }
792 
793         if (cfg->noise_shaping_amp == 2)
794             return;
795     }
796 }
797 
798 /*************************************************************************
799  *
800  *      inc_scalefac_scale()
801  *
802  *  Takehiro Tominaga 2000-xx-xx
803  *
804  *  turns on scalefac scale and adjusts scalefactors
805  *
806  *************************************************************************/
807 
808 static void
inc_scalefac_scale(gr_info * const cod_info,FLOAT xrpow[576])809 inc_scalefac_scale(gr_info * const cod_info, FLOAT xrpow[576])
810 {
811     int     l, j, sfb;
812     const FLOAT ifqstep34 = 1.29683955465100964055;
813 
814     j = 0;
815     for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
816         int const width = cod_info->width[sfb];
817         int     s = cod_info->scalefac[sfb];
818         if (cod_info->preflag)
819             s += pretab[sfb];
820         j += width;
821         if (s & 1) {
822             s++;
823             for (l = -width; l < 0; l++) {
824                 xrpow[j + l] *= ifqstep34;
825                 if (xrpow[j + l] > cod_info->xrpow_max)
826                     cod_info->xrpow_max = xrpow[j + l];
827             }
828         }
829         cod_info->scalefac[sfb] = s >> 1;
830     }
831     cod_info->preflag = 0;
832     cod_info->scalefac_scale = 1;
833 }
834 
835 
836 
837 /*************************************************************************
838  *
839  *      inc_subblock_gain()
840  *
841  *  Takehiro Tominaga 2000-xx-xx
842  *
843  *  increases the subblock gain and adjusts scalefactors
844  *
845  *************************************************************************/
846 
847 static int
inc_subblock_gain(const lame_internal_flags * const gfc,gr_info * const cod_info,FLOAT xrpow[576])848 inc_subblock_gain(const lame_internal_flags * const gfc, gr_info * const cod_info, FLOAT xrpow[576])
849 {
850     int     sfb, window;
851     int    *const scalefac = cod_info->scalefac;
852 
853     /* subbloc_gain can't do anything in the long block region */
854     for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
855         if (scalefac[sfb] >= 16)
856             return 1;
857     }
858 
859     for (window = 0; window < 3; window++) {
860         int     s1, s2, l, j;
861         s1 = s2 = 0;
862 
863         for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbdivide; sfb += 3) {
864             if (s1 < scalefac[sfb])
865                 s1 = scalefac[sfb];
866         }
867         for (; sfb < cod_info->sfbmax; sfb += 3) {
868             if (s2 < scalefac[sfb])
869                 s2 = scalefac[sfb];
870         }
871 
872         if (s1 < 16 && s2 < 8)
873             continue;
874 
875         if (cod_info->subblock_gain[window] >= 7)
876             return 1;
877 
878         /* even though there is no scalefactor for sfb12
879          * subblock gain affects upper frequencies too, that's why
880          * we have to go up to SBMAX_s
881          */
882         cod_info->subblock_gain[window]++;
883         j = gfc->scalefac_band.l[cod_info->sfb_lmax];
884         for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbmax; sfb += 3) {
885             FLOAT   amp;
886             int const width = cod_info->width[sfb];
887             int     s = scalefac[sfb];
888             assert(s >= 0);
889             s = s - (4 >> cod_info->scalefac_scale);
890             if (s >= 0) {
891                 scalefac[sfb] = s;
892                 j += width * 3;
893                 continue;
894             }
895 
896             scalefac[sfb] = 0;
897             {
898                 int const gain = 210 + (s << (cod_info->scalefac_scale + 1));
899                 amp = IPOW20(gain);
900             }
901             j += width * (window + 1);
902             for (l = -width; l < 0; l++) {
903                 xrpow[j + l] *= amp;
904                 if (xrpow[j + l] > cod_info->xrpow_max)
905                     cod_info->xrpow_max = xrpow[j + l];
906             }
907             j += width * (3 - window - 1);
908         }
909 
910         {
911             FLOAT const amp = IPOW20(202);
912             j += cod_info->width[sfb] * (window + 1);
913             for (l = -cod_info->width[sfb]; l < 0; l++) {
914                 xrpow[j + l] *= amp;
915                 if (xrpow[j + l] > cod_info->xrpow_max)
916                     cod_info->xrpow_max = xrpow[j + l];
917             }
918         }
919     }
920     return 0;
921 }
922 
923 
924 
925 /********************************************************************
926  *
927  *      balance_noise()
928  *
929  *  Takehiro Tominaga /date??
930  *  Robert Hegemann 2000-09-06: made a function of it
931  *
932  *  amplifies scalefactor bands,
933  *   - if all are already amplified returns 0
934  *   - if some bands are amplified too much:
935  *      * try to increase scalefac_scale
936  *      * if already scalefac_scale was set
937  *          try on short blocks to increase subblock gain
938  *
939  ********************************************************************/
940 inline static int
balance_noise(lame_internal_flags * gfc,gr_info * const cod_info,FLOAT const * distort,FLOAT xrpow[576],int bRefine)941 balance_noise(lame_internal_flags * gfc,
942               gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
943 {
944     SessionConfig_t const *const cfg = &gfc->cfg;
945     int     status;
946 
947     amp_scalefac_bands(gfc, cod_info, distort, xrpow, bRefine);
948 
949     /* check to make sure we have not amplified too much
950      * loop_break returns 0 if there is an unamplified scalefac
951      * scale_bitcount returns 0 if no scalefactors are too large
952      */
953 
954     status = loop_break(cod_info);
955 
956     if (status)
957         return 0;       /* all bands amplified */
958 
959     /* not all scalefactors have been amplified.  so these
960      * scalefacs are possibly valid.  encode them:
961      */
962     status = scale_bitcount(gfc, cod_info);
963 
964     if (!status)
965         return 1;       /* amplified some bands not exceeding limits */
966 
967     /*  some scalefactors are too large.
968      *  lets try setting scalefac_scale=1
969      */
970     if (cfg->noise_shaping > 1) {
971         memset(&gfc->sv_qnt.pseudohalf[0], 0, sizeof(gfc->sv_qnt.pseudohalf));
972         if (!cod_info->scalefac_scale) {
973             inc_scalefac_scale(cod_info, xrpow);
974             status = 0;
975         }
976         else {
977             if (cod_info->block_type == SHORT_TYPE && cfg->subblock_gain > 0) {
978                 status = inc_subblock_gain(gfc, cod_info, xrpow)
979                     || loop_break(cod_info);
980             }
981         }
982     }
983 
984     if (!status) {
985         status = scale_bitcount(gfc, cod_info);
986     }
987     return !status;
988 }
989 
990 
991 
992 /************************************************************************
993  *
994  *  outer_loop ()
995  *
996  *  Function: The outer iteration loop controls the masking conditions
997  *  of all scalefactorbands. It computes the best scalefac and
998  *  global gain. This module calls the inner iteration loop
999  *
1000  *  mt 5/99 completely rewritten to allow for bit reservoir control,
1001  *  mid/side channels with L/R or mid/side masking thresholds,
1002  *  and chooses best quantization instead of last quantization when
1003  *  no distortion free quantization can be found.
1004  *
1005  *  added VBR support mt 5/99
1006  *
1007  *  some code shuffle rh 9/00
1008  ************************************************************************/
1009 
1010 static int
outer_loop(lame_internal_flags * gfc,gr_info * const cod_info,const FLOAT * const l3_xmin,FLOAT xrpow[576],const int ch,const int targ_bits)1011 outer_loop(lame_internal_flags * gfc, gr_info * const cod_info, const FLOAT * const l3_xmin, /* allowed distortion */
1012            FLOAT xrpow[576], /* coloured magnitudes of spectral */
1013            const int ch, const int targ_bits)
1014 {                       /* maximum allowed bits */
1015     SessionConfig_t const *const cfg = &gfc->cfg;
1016     gr_info cod_info_w;
1017     FLOAT   save_xrpow[576];
1018     FLOAT   distort[SFBMAX];
1019     calc_noise_result best_noise_info;
1020     int     huff_bits;
1021     int     better;
1022     int     age;
1023     calc_noise_data prev_noise;
1024     int     best_part2_3_length = 9999999;
1025     int     bEndOfSearch = 0;
1026     int     bRefine = 0;
1027     int     best_ggain_pass1 = 0;
1028 
1029     (void) bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);
1030 
1031     if (!cfg->noise_shaping)
1032         /* fast mode, no noise shaping, we are ready */
1033         return 100;     /* default noise_info.over_count */
1034 
1035     memset(&prev_noise, 0, sizeof(calc_noise_data));
1036 
1037 
1038     /* compute the distortion in this quantization */
1039     /* coefficients and thresholds both l/r (or both mid/side) */
1040     (void) calc_noise(cod_info, l3_xmin, distort, &best_noise_info, &prev_noise);
1041     best_noise_info.bits = cod_info->part2_3_length;
1042 
1043     cod_info_w = *cod_info;
1044     age = 0;
1045     /* if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */
1046     memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
1047 
1048     while (!bEndOfSearch) {
1049         /* BEGIN MAIN LOOP */
1050         do {
1051             calc_noise_result noise_info;
1052             int     search_limit;
1053             int     maxggain = 255;
1054 
1055             /* When quantization with no distorted bands is found,
1056              * allow up to X new unsuccesful tries in serial. This
1057              * gives us more possibilities for different quant_compare modes.
1058              * Much more than 3 makes not a big difference, it is only slower.
1059              */
1060 
1061             if (gfc->sv_qnt.substep_shaping & 2) {
1062                 search_limit = 20;
1063             }
1064             else {
1065                 search_limit = 3;
1066             }
1067 
1068 
1069 
1070             /* Check if the last scalefactor band is distorted.
1071              * in VBR mode we can't get rid of the distortion, so quit now
1072              * and VBR mode will try again with more bits.
1073              * (makes a 10% speed increase, the files I tested were
1074              * binary identical, 2000/05/20 Robert Hegemann)
1075              * distort[] > 1 means noise > allowed noise
1076              */
1077             if (gfc->sv_qnt.sfb21_extra) {
1078                 if (distort[cod_info_w.sfbmax] > 1.0)
1079                     break;
1080                 if (cod_info_w.block_type == SHORT_TYPE
1081                     && (distort[cod_info_w.sfbmax + 1] > 1.0
1082                         || distort[cod_info_w.sfbmax + 2] > 1.0))
1083                     break;
1084             }
1085 
1086             /* try a new scalefactor conbination on cod_info_w */
1087             if (balance_noise(gfc, &cod_info_w, distort, xrpow, bRefine) == 0)
1088                 break;
1089             if (cod_info_w.scalefac_scale)
1090                 maxggain = 254;
1091 
1092             /* inner_loop starts with the initial quantization step computed above
1093              * and slowly increases until the bits < huff_bits.
1094              * Thus it is important not to start with too large of an inital
1095              * quantization step.  Too small is ok, but inner_loop will take longer
1096              */
1097             huff_bits = targ_bits - cod_info_w.part2_length;
1098             if (huff_bits <= 0)
1099                 break;
1100 
1101             /*  increase quantizer stepsize until needed bits are below maximum
1102              */
1103             while ((cod_info_w.part2_3_length
1104                     = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > huff_bits
1105                    && cod_info_w.global_gain <= maxggain)
1106                 cod_info_w.global_gain++;
1107 
1108             if (cod_info_w.global_gain > maxggain)
1109                 break;
1110 
1111             if (best_noise_info.over_count == 0) {
1112 
1113                 while ((cod_info_w.part2_3_length
1114                         = count_bits(gfc, xrpow, &cod_info_w, &prev_noise)) > best_part2_3_length
1115                        && cod_info_w.global_gain <= maxggain)
1116                     cod_info_w.global_gain++;
1117 
1118                 if (cod_info_w.global_gain > maxggain)
1119                     break;
1120             }
1121 
1122             /* compute the distortion in this quantization */
1123             (void) calc_noise(&cod_info_w, l3_xmin, distort, &noise_info, &prev_noise);
1124             noise_info.bits = cod_info_w.part2_3_length;
1125 
1126             /* check if this quantization is better
1127              * than our saved quantization */
1128             if (cod_info->block_type != SHORT_TYPE) /* NORM, START or STOP type */
1129                 better = cfg->quant_comp;
1130             else
1131                 better = cfg->quant_comp_short;
1132 
1133 
1134             better = quant_compare(better, &best_noise_info, &noise_info, &cod_info_w, distort);
1135 
1136 
1137             /* save data so we can restore this quantization later */
1138             if (better) {
1139                 best_part2_3_length = cod_info->part2_3_length;
1140                 best_noise_info = noise_info;
1141                 *cod_info = cod_info_w;
1142                 age = 0;
1143                 /* save data so we can restore this quantization later */
1144                 /*if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */  {
1145                     /* store for later reuse */
1146                     memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
1147                 }
1148             }
1149             else {
1150                 /* early stop? */
1151                 if (cfg->full_outer_loop == 0) {
1152                     if (++age > search_limit && best_noise_info.over_count == 0)
1153                         break;
1154                     if ((cfg->noise_shaping_amp == 3) && bRefine && age > 30)
1155                         break;
1156                     if ((cfg->noise_shaping_amp == 3) && bRefine &&
1157                         (cod_info_w.global_gain - best_ggain_pass1) > 15)
1158                         break;
1159                 }
1160             }
1161         }
1162         while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);
1163 
1164         if (cfg->noise_shaping_amp == 3) {
1165             if (!bRefine) {
1166                 /* refine search */
1167                 cod_info_w = *cod_info;
1168                 memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
1169                 age = 0;
1170                 best_ggain_pass1 = cod_info_w.global_gain;
1171 
1172                 bRefine = 1;
1173             }
1174             else {
1175                 /* search already refined, stop */
1176                 bEndOfSearch = 1;
1177             }
1178 
1179         }
1180         else {
1181             bEndOfSearch = 1;
1182         }
1183     }
1184 
1185     assert((cod_info->global_gain + cod_info->scalefac_scale) <= 255);
1186     /*  finish up
1187      */
1188     if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh || cfg->vbr == vbr_mt)
1189         /* restore for reuse on next try */
1190         memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
1191     /*  do the 'substep shaping'
1192      */
1193     else if (gfc->sv_qnt.substep_shaping & 1)
1194         trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);
1195 
1196     return best_noise_info.over_count;
1197 }
1198 
1199 
1200 
1201 
1202 
1203 /************************************************************************
1204  *
1205  *      iteration_finish_one()
1206  *
1207  *  Robert Hegemann 2000-09-06
1208  *
1209  *  update reservoir status after FINAL quantization/bitrate
1210  *
1211  ************************************************************************/
1212 
1213 static void
iteration_finish_one(lame_internal_flags * gfc,int gr,int ch)1214 iteration_finish_one(lame_internal_flags * gfc, int gr, int ch)
1215 {
1216     SessionConfig_t const *const cfg = &gfc->cfg;
1217     III_side_info_t *const l3_side = &gfc->l3_side;
1218     gr_info *const cod_info = &l3_side->tt[gr][ch];
1219 
1220     /*  try some better scalefac storage
1221      */
1222     best_scalefac_store(gfc, gr, ch, l3_side);
1223 
1224     /*  best huffman_divide may save some bits too
1225      */
1226     if (cfg->use_best_huffman == 1)
1227         best_huffman_divide(gfc, cod_info);
1228 
1229     /*  update reservoir status after FINAL quantization/bitrate
1230      */
1231     ResvAdjust(gfc, cod_info);
1232 }
1233 
1234 
1235 
1236 /*********************************************************************
1237  *
1238  *      VBR_encode_granule()
1239  *
1240  *  2000-09-04 Robert Hegemann
1241  *
1242  *********************************************************************/
1243 
1244 static void
VBR_encode_granule(lame_internal_flags * gfc,gr_info * const cod_info,const FLOAT * const l3_xmin,FLOAT xrpow[576],const int ch,int min_bits,int max_bits)1245 VBR_encode_granule(lame_internal_flags * gfc, gr_info * const cod_info, const FLOAT * const l3_xmin, /* allowed distortion of the scalefactor */
1246                    FLOAT xrpow[576], /* coloured magnitudes of spectral values */
1247                    const int ch, int min_bits, int max_bits)
1248 {
1249     gr_info bst_cod_info;
1250     FLOAT   bst_xrpow[576];
1251     int const Max_bits = max_bits;
1252     int     real_bits = max_bits + 1;
1253     int     this_bits = (max_bits + min_bits) / 2;
1254     int     dbits, over, found = 0;
1255     int const sfb21_extra = gfc->sv_qnt.sfb21_extra;
1256 
1257     assert(Max_bits <= MAX_BITS_PER_CHANNEL);
1258     memset(bst_cod_info.l3_enc, 0, sizeof(bst_cod_info.l3_enc));
1259 
1260     /*  search within round about 40 bits of optimal
1261      */
1262     do {
1263         assert(this_bits >= min_bits);
1264         assert(this_bits <= max_bits);
1265         assert(min_bits <= max_bits);
1266 
1267         if (this_bits > Max_bits - 42)
1268             gfc->sv_qnt.sfb21_extra = 0;
1269         else
1270             gfc->sv_qnt.sfb21_extra = sfb21_extra;
1271 
1272         over = outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, this_bits);
1273 
1274         /*  is quantization as good as we are looking for ?
1275          *  in this case: is no scalefactor band distorted?
1276          */
1277         if (over <= 0) {
1278             found = 1;
1279             /*  now we know it can be done with "real_bits"
1280              *  and maybe we can skip some iterations
1281              */
1282             real_bits = cod_info->part2_3_length;
1283 
1284             /*  store best quantization so far
1285              */
1286             bst_cod_info = *cod_info;
1287             memcpy(bst_xrpow, xrpow, sizeof(FLOAT) * 576);
1288 
1289             /*  try with fewer bits
1290              */
1291             max_bits = real_bits - 32;
1292             dbits = max_bits - min_bits;
1293             this_bits = (max_bits + min_bits) / 2;
1294         }
1295         else {
1296             /*  try with more bits
1297              */
1298             min_bits = this_bits + 32;
1299             dbits = max_bits - min_bits;
1300             this_bits = (max_bits + min_bits) / 2;
1301 
1302             if (found) {
1303                 found = 2;
1304                 /*  start again with best quantization so far
1305                  */
1306                 *cod_info = bst_cod_info;
1307                 memcpy(xrpow, bst_xrpow, sizeof(FLOAT) * 576);
1308             }
1309         }
1310     } while (dbits > 12);
1311 
1312     gfc->sv_qnt.sfb21_extra = sfb21_extra;
1313 
1314     /*  found=0 => nothing found, use last one
1315      *  found=1 => we just found the best and left the loop
1316      *  found=2 => we restored a good one and have now l3_enc to restore too
1317      */
1318     if (found == 2) {
1319         memcpy(cod_info->l3_enc, bst_cod_info.l3_enc, sizeof(int) * 576);
1320     }
1321     assert(cod_info->part2_3_length <= Max_bits);
1322 
1323 }
1324 
1325 
1326 
1327 /************************************************************************
1328  *
1329  *      get_framebits()
1330  *
1331  *  Robert Hegemann 2000-09-05
1332  *
1333  *  calculates
1334  *  * how many bits are available for analog silent granules
1335  *  * how many bits to use for the lowest allowed bitrate
1336  *  * how many bits each bitrate would provide
1337  *
1338  ************************************************************************/
1339 
1340 static void
get_framebits(lame_internal_flags * gfc,int frameBits[15])1341 get_framebits(lame_internal_flags * gfc, int frameBits[15])
1342 {
1343     SessionConfig_t const *const cfg = &gfc->cfg;
1344     EncResult_t *const eov = &gfc->ov_enc;
1345     int     bitsPerFrame, i;
1346 
1347     /*  always use at least this many bits per granule per channel
1348      *  unless we detect analog silence, see below
1349      */
1350     eov->bitrate_index = cfg->vbr_min_bitrate_index;
1351     bitsPerFrame = getframebits(gfc);
1352 
1353     /*  bits for analog silence
1354      */
1355     eov->bitrate_index = 1;
1356     bitsPerFrame = getframebits(gfc);
1357 
1358     for (i = 1; i <= cfg->vbr_max_bitrate_index; i++) {
1359         eov->bitrate_index = i;
1360         frameBits[i] = ResvFrameBegin(gfc, &bitsPerFrame);
1361     }
1362 }
1363 
1364 
1365 
1366 /*********************************************************************
1367  *
1368  *      VBR_prepare()
1369  *
1370  *  2000-09-04 Robert Hegemann
1371  *
1372  *  * converts LR to MS coding when necessary
1373  *  * calculates allowed/adjusted quantization noise amounts
1374  *  * detects analog silent frames
1375  *
1376  *  some remarks:
1377  *  - lower masking depending on Quality setting
1378  *  - quality control together with adjusted ATH MDCT scaling
1379  *    on lower quality setting allocate more noise from
1380  *    ATH masking, and on higher quality setting allocate
1381  *    less noise from ATH masking.
1382  *  - experiments show that going more than 2dB over GPSYCHO's
1383  *    limits ends up in very annoying artefacts
1384  *
1385  *********************************************************************/
1386 
1387 /* RH: this one needs to be overhauled sometime */
1388 
1389 static int
VBR_old_prepare(lame_internal_flags * gfc,const FLOAT pe[2][2],FLOAT const ms_ener_ratio[2],const III_psy_ratio ratio[2][2],FLOAT l3_xmin[2][2][SFBMAX],int frameBits[16],int min_bits[2][2],int max_bits[2][2],int bands[2][2])1390 VBR_old_prepare(lame_internal_flags * gfc,
1391                 const FLOAT pe[2][2], FLOAT const ms_ener_ratio[2],
1392                 const III_psy_ratio ratio[2][2],
1393                 FLOAT l3_xmin[2][2][SFBMAX],
1394                 int frameBits[16], int min_bits[2][2], int max_bits[2][2], int bands[2][2])
1395 {
1396     SessionConfig_t const *const cfg = &gfc->cfg;
1397     EncResult_t *const eov = &gfc->ov_enc;
1398 
1399     FLOAT   masking_lower_db, adjust = 0.0;
1400     int     gr, ch;
1401     int     analog_silence = 1;
1402     int     avg, mxb, bits = 0;
1403 
1404     eov->bitrate_index = cfg->vbr_max_bitrate_index;
1405     avg = ResvFrameBegin(gfc, &avg) / cfg->mode_gr;
1406 
1407     get_framebits(gfc, frameBits);
1408 
1409     for (gr = 0; gr < cfg->mode_gr; gr++) {
1410         mxb = on_pe(gfc, pe, max_bits[gr], avg, gr, 0);
1411         if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1412             ms_convert(&gfc->l3_side, gr);
1413             reduce_side(max_bits[gr], ms_ener_ratio[gr], avg, mxb);
1414         }
1415         for (ch = 0; ch < cfg->channels_out; ++ch) {
1416             gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
1417 
1418             if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
1419                 adjust = 1.28 / (1 + exp(3.5 - pe[gr][ch] / 300.)) - 0.05;
1420                 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
1421             }
1422             else {
1423                 adjust = 2.56 / (1 + exp(3.5 - pe[gr][ch] / 300.)) - 0.14;
1424                 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
1425             }
1426             gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
1427 
1428             init_outer_loop(gfc, cod_info);
1429             bands[gr][ch] = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]);
1430             if (bands[gr][ch])
1431                 analog_silence = 0;
1432 
1433             min_bits[gr][ch] = 126;
1434 
1435             bits += max_bits[gr][ch];
1436         }
1437     }
1438     for (gr = 0; gr < cfg->mode_gr; gr++) {
1439         for (ch = 0; ch < cfg->channels_out; ch++) {
1440             if (bits > frameBits[cfg->vbr_max_bitrate_index] && bits > 0) {
1441                 max_bits[gr][ch] *= frameBits[cfg->vbr_max_bitrate_index];
1442                 max_bits[gr][ch] /= bits;
1443             }
1444             if (min_bits[gr][ch] > max_bits[gr][ch])
1445                 min_bits[gr][ch] = max_bits[gr][ch];
1446 
1447         }               /* for ch */
1448     }                   /* for gr */
1449 
1450     return analog_silence;
1451 }
1452 
1453 static void
bitpressure_strategy(lame_internal_flags const * gfc,FLOAT l3_xmin[2][2][SFBMAX],const int min_bits[2][2],int max_bits[2][2])1454 bitpressure_strategy(lame_internal_flags const *gfc,
1455                      FLOAT l3_xmin[2][2][SFBMAX], const int min_bits[2][2], int max_bits[2][2])
1456 {
1457     SessionConfig_t const *const cfg = &gfc->cfg;
1458     int     gr, ch, sfb;
1459     for (gr = 0; gr < cfg->mode_gr; gr++) {
1460         for (ch = 0; ch < cfg->channels_out; ch++) {
1461             gr_info const *const gi = &gfc->l3_side.tt[gr][ch];
1462             FLOAT  *pxmin = l3_xmin[gr][ch];
1463             for (sfb = 0; sfb < gi->psy_lmax; sfb++)
1464                 *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_l / SBMAX_l;
1465 
1466             if (gi->block_type == SHORT_TYPE) {
1467                 for (sfb = gi->sfb_smin; sfb < SBMAX_s; sfb++) {
1468                     *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
1469                     *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
1470                     *pxmin++ *= 1. + .029 * sfb * sfb / SBMAX_s / SBMAX_s;
1471                 }
1472             }
1473             max_bits[gr][ch] = Max(min_bits[gr][ch], 0.9 * max_bits[gr][ch]);
1474         }
1475     }
1476 }
1477 
1478 /************************************************************************
1479  *
1480  *      VBR_iteration_loop()
1481  *
1482  *  tries to find out how many bits are needed for each granule and channel
1483  *  to get an acceptable quantization. An appropriate bitrate will then be
1484  *  choosed for quantization.  rh 8/99
1485  *
1486  *  Robert Hegemann 2000-09-06 rewrite
1487  *
1488  ************************************************************************/
1489 
1490 void
VBR_old_iteration_loop(lame_internal_flags * gfc,const FLOAT pe[2][2],const FLOAT ms_ener_ratio[2],const III_psy_ratio ratio[2][2])1491 VBR_old_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1492                        const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1493 {
1494     SessionConfig_t const *const cfg = &gfc->cfg;
1495     EncResult_t *const eov = &gfc->ov_enc;
1496     FLOAT   l3_xmin[2][2][SFBMAX];
1497 
1498     FLOAT   xrpow[576];
1499     int     bands[2][2];
1500     int     frameBits[15];
1501     int     used_bits;
1502     int     bits;
1503     int     min_bits[2][2], max_bits[2][2];
1504     int     mean_bits;
1505     int     ch, gr, analog_silence;
1506     III_side_info_t *const l3_side = &gfc->l3_side;
1507 
1508     analog_silence = VBR_old_prepare(gfc, pe, ms_ener_ratio, ratio,
1509                                      l3_xmin, frameBits, min_bits, max_bits, bands);
1510 
1511     /*---------------------------------*/
1512     for (;;) {
1513 
1514         /*  quantize granules with lowest possible number of bits
1515          */
1516 
1517         used_bits = 0;
1518 
1519         for (gr = 0; gr < cfg->mode_gr; gr++) {
1520             for (ch = 0; ch < cfg->channels_out; ch++) {
1521                 int     ret;
1522                 gr_info *const cod_info = &l3_side->tt[gr][ch];
1523 
1524                 /*  init_outer_loop sets up cod_info, scalefac and xrpow
1525                  */
1526                 ret = init_xrpow(gfc, cod_info, xrpow);
1527                 if (ret == 0 || max_bits[gr][ch] == 0) {
1528                     /*  xr contains no energy
1529                      *  l3_enc, our encoding data, will be quantized to zero
1530                      */
1531                     continue; /* with next channel */
1532                 }
1533 
1534                 VBR_encode_granule(gfc, cod_info, l3_xmin[gr][ch], xrpow,
1535                                    ch, min_bits[gr][ch], max_bits[gr][ch]);
1536 
1537                 /*  do the 'substep shaping'
1538                  */
1539                 if (gfc->sv_qnt.substep_shaping & 1) {
1540                     trancate_smallspectrums(gfc, &l3_side->tt[gr][ch], l3_xmin[gr][ch], xrpow);
1541                 }
1542 
1543                 ret = cod_info->part2_3_length + cod_info->part2_length;
1544                 used_bits += ret;
1545             }           /* for ch */
1546         }               /* for gr */
1547 
1548         /*  find lowest bitrate able to hold used bits
1549          */
1550         if (analog_silence && !cfg->enforce_min_bitrate)
1551             /*  we detected analog silence and the user did not specify
1552              *  any hard framesize limit, so start with smallest possible frame
1553              */
1554             eov->bitrate_index = 1;
1555         else
1556             eov->bitrate_index = cfg->vbr_min_bitrate_index;
1557 
1558         for (; eov->bitrate_index < cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
1559             if (used_bits <= frameBits[eov->bitrate_index])
1560                 break;
1561         }
1562         bits = ResvFrameBegin(gfc, &mean_bits);
1563 
1564         if (used_bits <= bits)
1565             break;
1566 
1567         bitpressure_strategy(gfc, l3_xmin, (const int (*)[2])min_bits, max_bits);
1568 
1569     }                   /* breaks adjusted */
1570     /*--------------------------------------*/
1571 
1572     for (gr = 0; gr < cfg->mode_gr; gr++) {
1573         for (ch = 0; ch < cfg->channels_out; ch++) {
1574             iteration_finish_one(gfc, gr, ch);
1575         }               /* for ch */
1576     }                   /* for gr */
1577     ResvFrameEnd(gfc, mean_bits);
1578 }
1579 
1580 
1581 
1582 static int
VBR_new_prepare(lame_internal_flags * gfc,const FLOAT pe[2][2],const III_psy_ratio ratio[2][2],FLOAT l3_xmin[2][2][SFBMAX],int frameBits[16],int max_bits[2][2],int * max_resv)1583 VBR_new_prepare(lame_internal_flags * gfc,
1584                 const FLOAT pe[2][2], const III_psy_ratio ratio[2][2],
1585                 FLOAT l3_xmin[2][2][SFBMAX], int frameBits[16], int max_bits[2][2],
1586                 int* max_resv)
1587 {
1588     SessionConfig_t const *const cfg = &gfc->cfg;
1589     EncResult_t *const eov = &gfc->ov_enc;
1590 
1591     int     gr, ch;
1592     int     analog_silence = 1;
1593     int     avg, bits = 0;
1594     int     maximum_framebits;
1595 
1596     if (!cfg->free_format) {
1597         eov->bitrate_index = cfg->vbr_max_bitrate_index;
1598         (void) ResvFrameBegin(gfc, &avg);
1599         *max_resv = gfc->sv_enc.ResvMax;
1600 
1601         get_framebits(gfc, frameBits);
1602         maximum_framebits = frameBits[cfg->vbr_max_bitrate_index];
1603     }
1604     else {
1605         eov->bitrate_index = 0;
1606         maximum_framebits = ResvFrameBegin(gfc, &avg);
1607         frameBits[0] = maximum_framebits;
1608         *max_resv = gfc->sv_enc.ResvMax;
1609     }
1610 
1611     for (gr = 0; gr < cfg->mode_gr; gr++) {
1612         (void) on_pe(gfc, pe, max_bits[gr], avg, gr, 0);
1613         if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1614             ms_convert(&gfc->l3_side, gr);
1615         }
1616         for (ch = 0; ch < cfg->channels_out; ++ch) {
1617             gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
1618 
1619             gfc->sv_qnt.masking_lower = pow(10.0, gfc->sv_qnt.mask_adjust * 0.1);
1620 
1621             init_outer_loop(gfc, cod_info);
1622             if (0 != calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]))
1623                 analog_silence = 0;
1624 
1625             bits += max_bits[gr][ch];
1626         }
1627     }
1628     for (gr = 0; gr < cfg->mode_gr; gr++) {
1629         for (ch = 0; ch < cfg->channels_out; ch++) {
1630             if (bits > maximum_framebits && bits > 0) {
1631                 max_bits[gr][ch] *= maximum_framebits;
1632                 max_bits[gr][ch] /= bits;
1633             }
1634 
1635         }               /* for ch */
1636     }                   /* for gr */
1637     if (analog_silence) {
1638         *max_resv = 0;
1639     }
1640     return analog_silence;
1641 }
1642 
1643 
1644 
1645 void
VBR_new_iteration_loop(lame_internal_flags * gfc,const FLOAT pe[2][2],const FLOAT ms_ener_ratio[2],const III_psy_ratio ratio[2][2])1646 VBR_new_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1647                        const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1648 {
1649     SessionConfig_t const *const cfg = &gfc->cfg;
1650     EncResult_t *const eov = &gfc->ov_enc;
1651     FLOAT   l3_xmin[2][2][SFBMAX];
1652 
1653     FLOAT   xrpow[2][2][576];
1654     int     frameBits[15];
1655     int     used_bits;
1656     int     max_bits[2][2];
1657     int     ch, gr, analog_silence, pad;
1658     III_side_info_t *const l3_side = &gfc->l3_side;
1659 
1660     const FLOAT (*const_l3_xmin)[2][SFBMAX] = (const FLOAT (*)[2][SFBMAX])l3_xmin;
1661     const FLOAT (*const_xrpow)[2][576] = (const FLOAT (*)[2][576])xrpow;
1662     const int (*const_max_bits)[2] = (const int (*)[2])max_bits;
1663 
1664     (void) ms_ener_ratio; /* not used */
1665 
1666     memset(xrpow, 0, sizeof(xrpow));
1667 
1668     analog_silence = VBR_new_prepare(gfc, pe, ratio, l3_xmin, frameBits, max_bits, &pad);
1669 
1670     for (gr = 0; gr < cfg->mode_gr; gr++) {
1671         for (ch = 0; ch < cfg->channels_out; ch++) {
1672             gr_info *const cod_info = &l3_side->tt[gr][ch];
1673 
1674             /*  init_outer_loop sets up cod_info, scalefac and xrpow
1675              */
1676             if (0 == init_xrpow(gfc, cod_info, xrpow[gr][ch])) {
1677                 max_bits[gr][ch] = 0; /* silent granule needs no bits */
1678             }
1679         }               /* for ch */
1680     }                   /* for gr */
1681 
1682     /*  quantize granules with lowest possible number of bits
1683      */
1684 
1685     used_bits = VBR_encode_frame(gfc, const_xrpow, const_l3_xmin, const_max_bits);
1686 
1687     if (!cfg->free_format) {
1688         int     i, j;
1689 
1690         /*  find lowest bitrate able to hold used bits
1691          */
1692         if (analog_silence && !cfg->enforce_min_bitrate) {
1693             /*  we detected analog silence and the user did not specify
1694              *  any hard framesize limit, so start with smallest possible frame
1695              */
1696             i = 1;
1697         }
1698         else {
1699             i = cfg->vbr_min_bitrate_index;
1700         }
1701 
1702         for (; i < cfg->vbr_max_bitrate_index; i++) {
1703             if (used_bits <= frameBits[i])
1704                 break;
1705         }
1706         if (i > cfg->vbr_max_bitrate_index) {
1707             i = cfg->vbr_max_bitrate_index;
1708         }
1709         if (pad > 0) {
1710             for (j = cfg->vbr_max_bitrate_index; j > i; --j) {
1711                 int const unused = frameBits[j] - used_bits;
1712                 if (unused <= pad)
1713                     break;
1714             }
1715             eov->bitrate_index = j;
1716         }
1717         else {
1718             eov->bitrate_index = i;
1719         }
1720     }
1721     else {
1722 #if 0
1723         static int mmm = 0;
1724         int     fff = getFramesize_kbps(gfc, used_bits);
1725         int     hhh = getFramesize_kbps(gfc, MAX_BITS_PER_GRANULE * cfg->mode_gr);
1726         if (mmm < fff)
1727             mmm = fff;
1728         printf("demand=%3d kbps  max=%3d kbps   limit=%3d kbps\n", fff, mmm, hhh);
1729 #endif
1730         eov->bitrate_index = 0;
1731     }
1732     if (used_bits <= frameBits[eov->bitrate_index]) {
1733         /* update Reservoire status */
1734         int     mean_bits, fullframebits;
1735         fullframebits = ResvFrameBegin(gfc, &mean_bits);
1736         assert(used_bits <= fullframebits);
1737         for (gr = 0; gr < cfg->mode_gr; gr++) {
1738             for (ch = 0; ch < cfg->channels_out; ch++) {
1739                 gr_info const *const cod_info = &l3_side->tt[gr][ch];
1740                 ResvAdjust(gfc, cod_info);
1741             }
1742         }
1743         ResvFrameEnd(gfc, mean_bits);
1744     }
1745     else {
1746         /* SHOULD NOT HAPPEN INTERNAL ERROR
1747          */
1748         ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE, please send bug report\n");
1749         exit(-1);
1750     }
1751 }
1752 
1753 
1754 
1755 
1756 
1757 /********************************************************************
1758  *
1759  *  calc_target_bits()
1760  *
1761  *  calculates target bits for ABR encoding
1762  *
1763  *  mt 2000/05/31
1764  *
1765  ********************************************************************/
1766 
1767 static void
calc_target_bits(lame_internal_flags * gfc,const FLOAT pe[2][2],FLOAT const ms_ener_ratio[2],int targ_bits[2][2],int * analog_silence_bits,int * max_frame_bits)1768 calc_target_bits(lame_internal_flags * gfc,
1769                  const FLOAT pe[2][2],
1770                  FLOAT const ms_ener_ratio[2],
1771                  int targ_bits[2][2], int *analog_silence_bits, int *max_frame_bits)
1772 {
1773     SessionConfig_t const *const cfg = &gfc->cfg;
1774     EncResult_t *const eov = &gfc->ov_enc;
1775     III_side_info_t const *const l3_side = &gfc->l3_side;
1776     FLOAT   res_factor;
1777     int     gr, ch, totbits, mean_bits;
1778     int     framesize = 576 * cfg->mode_gr;
1779 
1780     eov->bitrate_index = cfg->vbr_max_bitrate_index;
1781     *max_frame_bits = ResvFrameBegin(gfc, &mean_bits);
1782 
1783     eov->bitrate_index = 1;
1784     mean_bits = getframebits(gfc) - cfg->sideinfo_len * 8;
1785     *analog_silence_bits = mean_bits / (cfg->mode_gr * cfg->channels_out);
1786 
1787     mean_bits = cfg->vbr_avg_bitrate_kbps * framesize * 1000;
1788     if (gfc->sv_qnt.substep_shaping & 1)
1789         mean_bits *= 1.09;
1790     mean_bits /= cfg->samplerate_out;
1791     mean_bits -= cfg->sideinfo_len * 8;
1792     mean_bits /= (cfg->mode_gr * cfg->channels_out);
1793 
1794     /*
1795        res_factor is the percentage of the target bitrate that should
1796        be used on average.  the remaining bits are added to the
1797        bitreservoir and used for difficult to encode frames.
1798 
1799        Since we are tracking the average bitrate, we should adjust
1800        res_factor "on the fly", increasing it if the average bitrate
1801        is greater than the requested bitrate, and decreasing it
1802        otherwise.  Reasonable ranges are from .9 to 1.0
1803 
1804        Until we get the above suggestion working, we use the following
1805        tuning:
1806        compression ratio    res_factor
1807        5.5  (256kbps)         1.0      no need for bitreservoir
1808        11   (128kbps)         .93      7% held for reservoir
1809 
1810        with linear interpolation for other values.
1811 
1812      */
1813     res_factor = .93 + .07 * (11.0 - cfg->compression_ratio) / (11.0 - 5.5);
1814     if (res_factor < .90)
1815         res_factor = .90;
1816     if (res_factor > 1.00)
1817         res_factor = 1.00;
1818 
1819     for (gr = 0; gr < cfg->mode_gr; gr++) {
1820         int     sum = 0;
1821         for (ch = 0; ch < cfg->channels_out; ch++) {
1822             targ_bits[gr][ch] = res_factor * mean_bits;
1823 
1824             if (pe[gr][ch] > 700) {
1825                 int     add_bits = (pe[gr][ch] - 700) / 1.4;
1826 
1827                 gr_info const *const cod_info = &l3_side->tt[gr][ch];
1828                 targ_bits[gr][ch] = res_factor * mean_bits;
1829 
1830                 /* short blocks use a little extra, no matter what the pe */
1831                 if (cod_info->block_type == SHORT_TYPE) {
1832                     if (add_bits < mean_bits / 2)
1833                         add_bits = mean_bits / 2;
1834                 }
1835                 /* at most increase bits by 1.5*average */
1836                 if (add_bits > mean_bits * 3 / 2)
1837                     add_bits = mean_bits * 3 / 2;
1838                 else if (add_bits < 0)
1839                     add_bits = 0;
1840 
1841                 targ_bits[gr][ch] += add_bits;
1842             }
1843             if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL) {
1844                 targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
1845             }
1846             sum += targ_bits[gr][ch];
1847         }               /* for ch */
1848         if (sum > MAX_BITS_PER_GRANULE) {
1849             for (ch = 0; ch < cfg->channels_out; ++ch) {
1850                 targ_bits[gr][ch] *= MAX_BITS_PER_GRANULE;
1851                 targ_bits[gr][ch] /= sum;
1852             }
1853         }
1854     }                   /* for gr */
1855 
1856     if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR)
1857         for (gr = 0; gr < cfg->mode_gr; gr++) {
1858             reduce_side(targ_bits[gr], ms_ener_ratio[gr], mean_bits * cfg->channels_out,
1859                         MAX_BITS_PER_GRANULE);
1860         }
1861 
1862     /*  sum target bits
1863      */
1864     totbits = 0;
1865     for (gr = 0; gr < cfg->mode_gr; gr++) {
1866         for (ch = 0; ch < cfg->channels_out; ch++) {
1867             if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL)
1868                 targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
1869             totbits += targ_bits[gr][ch];
1870         }
1871     }
1872 
1873     /*  repartion target bits if needed
1874      */
1875     if (totbits > *max_frame_bits && totbits > 0) {
1876         for (gr = 0; gr < cfg->mode_gr; gr++) {
1877             for (ch = 0; ch < cfg->channels_out; ch++) {
1878                 targ_bits[gr][ch] *= *max_frame_bits;
1879                 targ_bits[gr][ch] /= totbits;
1880             }
1881         }
1882     }
1883 }
1884 
1885 
1886 
1887 
1888 
1889 
1890 /********************************************************************
1891  *
1892  *  ABR_iteration_loop()
1893  *
1894  *  encode a frame with a disired average bitrate
1895  *
1896  *  mt 2000/05/31
1897  *
1898  ********************************************************************/
1899 
1900 void
ABR_iteration_loop(lame_internal_flags * gfc,const FLOAT pe[2][2],const FLOAT ms_ener_ratio[2],const III_psy_ratio ratio[2][2])1901 ABR_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1902                    const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1903 {
1904     SessionConfig_t const *const cfg = &gfc->cfg;
1905     EncResult_t *const eov = &gfc->ov_enc;
1906     FLOAT   l3_xmin[SFBMAX];
1907     FLOAT   xrpow[576];
1908     int     targ_bits[2][2];
1909     int     mean_bits, max_frame_bits;
1910     int     ch, gr, ath_over;
1911     int     analog_silence_bits;
1912     gr_info *cod_info;
1913     III_side_info_t *const l3_side = &gfc->l3_side;
1914 
1915     mean_bits = 0;
1916 
1917     calc_target_bits(gfc, pe, ms_ener_ratio, targ_bits, &analog_silence_bits, &max_frame_bits);
1918 
1919     /*  encode granules
1920      */
1921     for (gr = 0; gr < cfg->mode_gr; gr++) {
1922 
1923         if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1924             ms_convert(&gfc->l3_side, gr);
1925         }
1926         for (ch = 0; ch < cfg->channels_out; ch++) {
1927             FLOAT   adjust, masking_lower_db;
1928             cod_info = &l3_side->tt[gr][ch];
1929 
1930             if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
1931                 /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05; */
1932                 adjust = 0;
1933                 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
1934             }
1935             else {
1936                 /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
1937                 adjust = 0;
1938                 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
1939             }
1940             gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
1941 
1942 
1943             /*  cod_info, scalefac and xrpow get initialized in init_outer_loop
1944              */
1945             init_outer_loop(gfc, cod_info);
1946             if (init_xrpow(gfc, cod_info, xrpow)) {
1947                 /*  xr contains energy we will have to encode
1948                  *  calculate the masking abilities
1949                  *  find some good quantization in outer_loop
1950                  */
1951                 ath_over = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin);
1952                 if (0 == ath_over) /* analog silence */
1953                     targ_bits[gr][ch] = analog_silence_bits;
1954 
1955                 (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[gr][ch]);
1956             }
1957             iteration_finish_one(gfc, gr, ch);
1958         }               /* ch */
1959     }                   /* gr */
1960 
1961     /*  find a bitrate which can refill the resevoir to positive size.
1962      */
1963     for (eov->bitrate_index = cfg->vbr_min_bitrate_index;
1964          eov->bitrate_index <= cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
1965         if (ResvFrameBegin(gfc, &mean_bits) >= 0)
1966             break;
1967     }
1968     assert(eov->bitrate_index <= cfg->vbr_max_bitrate_index);
1969 
1970     ResvFrameEnd(gfc, mean_bits);
1971 }
1972 
1973 
1974 
1975 
1976 
1977 
1978 /************************************************************************
1979  *
1980  *      CBR_iteration_loop()
1981  *
1982  *  author/date??
1983  *
1984  *  encodes one frame of MP3 data with constant bitrate
1985  *
1986  ************************************************************************/
1987 
1988 void
CBR_iteration_loop(lame_internal_flags * gfc,const FLOAT pe[2][2],const FLOAT ms_ener_ratio[2],const III_psy_ratio ratio[2][2])1989 CBR_iteration_loop(lame_internal_flags * gfc, const FLOAT pe[2][2],
1990                    const FLOAT ms_ener_ratio[2], const III_psy_ratio ratio[2][2])
1991 {
1992     SessionConfig_t const *const cfg = &gfc->cfg;
1993     FLOAT   l3_xmin[SFBMAX];
1994     FLOAT   xrpow[576];
1995     int     targ_bits[2];
1996     int     mean_bits, max_bits;
1997     int     gr, ch;
1998     III_side_info_t *const l3_side = &gfc->l3_side;
1999     gr_info *cod_info;
2000 
2001     (void) ResvFrameBegin(gfc, &mean_bits);
2002 
2003     /* quantize! */
2004     for (gr = 0; gr < cfg->mode_gr; gr++) {
2005 
2006         /*  calculate needed bits
2007          */
2008         max_bits = on_pe(gfc, pe, targ_bits, mean_bits, gr, gr);
2009 
2010         if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
2011             ms_convert(&gfc->l3_side, gr);
2012             reduce_side(targ_bits, ms_ener_ratio[gr], mean_bits, max_bits);
2013         }
2014 
2015         for (ch = 0; ch < cfg->channels_out; ch++) {
2016             FLOAT   adjust, masking_lower_db;
2017             cod_info = &l3_side->tt[gr][ch];
2018 
2019             if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type */
2020                 /* adjust = 1.28/(1+exp(3.5-pe[gr][ch]/300.))-0.05; */
2021                 adjust = 0;
2022                 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
2023             }
2024             else {
2025                 /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
2026                 adjust = 0;
2027                 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
2028             }
2029             gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
2030 
2031             /*  init_outer_loop sets up cod_info, scalefac and xrpow
2032              */
2033             init_outer_loop(gfc, cod_info);
2034             if (init_xrpow(gfc, cod_info, xrpow)) {
2035                 /*  xr contains energy we will have to encode
2036                  *  calculate the masking abilities
2037                  *  find some good quantization in outer_loop
2038                  */
2039                 (void) calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin);
2040                 (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[ch]);
2041             }
2042 
2043             iteration_finish_one(gfc, gr, ch);
2044             assert(cod_info->part2_3_length <= MAX_BITS_PER_CHANNEL);
2045             assert(cod_info->part2_3_length <= targ_bits[ch]);
2046         }               /* for ch */
2047     }                   /* for gr */
2048 
2049     ResvFrameEnd(gfc, mean_bits);
2050 }
2051