]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/lame/quantize.c
wwww
[16.git] / src / lib / doslib / ext / lame / quantize.c
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.216 2011/05/07 16:05:17 rbrito 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
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
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
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
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
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
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     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++) {
259         cod_info->width[sfb]
260             = gfc->scalefac_band.l[sfb + 1] - gfc->scalefac_band.l[sfb];
261         cod_info->window[sfb] = 3; /* which is always 0. */
262     }
263     if (cod_info->block_type == SHORT_TYPE) {
264         FLOAT   ixwork[576];
265         FLOAT  *ix;
266
267         cod_info->sfb_smin = 0;
268         cod_info->sfb_lmax = 0;
269         if (cod_info->mixed_block_flag) {
270             /*
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
273              */
274             cod_info->sfb_smin = 3;
275             cod_info->sfb_lmax = cfg->mode_gr * 2 + 4;
276         }
277         cod_info->psymax
278             = cod_info->sfb_lmax
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 */
285         /*
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...
290          */
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];
296             int     window, l;
297             for (window = 0; window < 3; window++) {
298                 for (l = start; l < end; l++) {
299                     *ix++ = ixwork[3 * l + window];
300                 }
301             }
302         }
303
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;
311             j += 3;
312         }
313     }
314
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;
321
322     cod_info->max_nonzero_coeff = 575;
323
324     /*  fresh scalefactors are all zero
325      */
326     memset(cod_info->scalefac, 0, sizeof(cod_info->scalefac));
327
328     if (cfg->vbr != vbr_mt && cfg->vbr != vbr_mtrh && cfg->vbr != vbr_abr && cfg->vbr != vbr_off) {
329         psfb21_analogsilence(gfc, cod_info);
330     }
331 }
332
333
334
335 /************************************************************************
336  *
337  *      bin_search_StepSize()
338  *
339  *  author/date??
340  *
341  *  binary step size search
342  *  used by outer_loop to get a quantizer step size to start with
343  *
344  ************************************************************************/
345
346 typedef enum {
347     BINSEARCH_NONE,
348     BINSEARCH_UP,
349     BINSEARCH_DOWN
350 } binsearchDirection_t;
351
352 static int
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])
355 {
356     int     nBits;
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;
363
364     assert(CurrentStep);
365     for (;;) {
366         int     step;
367         nBits = count_bits(gfc, xrpow, cod_info, 0);
368
369         if (CurrentStep == 1 || nBits == desired_rate)
370             break;      /* nothing to adjust anymore */
371
372         if (nBits > desired_rate) {
373             /* increase Quantize_StepSize */
374             if (Direction == BINSEARCH_DOWN)
375                 flag_GoneOver = 1;
376
377             if (flag_GoneOver)
378                 CurrentStep /= 2;
379             Direction = BINSEARCH_UP;
380             step = CurrentStep;
381         }
382         else {
383             /* decrease Quantize_StepSize */
384             if (Direction == BINSEARCH_UP)
385                 flag_GoneOver = 1;
386
387             if (flag_GoneOver)
388                 CurrentStep /= 2;
389             Direction = BINSEARCH_DOWN;
390             step = -CurrentStep;
391         }
392         cod_info->global_gain += step;
393         if (cod_info->global_gain < 0) {
394             cod_info->global_gain = 0;
395             flag_GoneOver = 1;
396         }
397         if (cod_info->global_gain > 255) {
398             cod_info->global_gain = 255;
399             flag_GoneOver = 1;
400         }
401     }
402
403     assert(cod_info->global_gain >= 0);
404     assert(cod_info->global_gain < 256);
405
406     while (nBits > desired_rate && cod_info->global_gain < 255) {
407         cod_info->global_gain++;
408         nBits = count_bits(gfc, xrpow, cod_info, 0);
409     }
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;
413     return nBits;
414 }
415
416
417
418
419 /************************************************************************
420  *
421  *      trancate_smallspectrums()
422  *
423  *  Takehiro TOMINAGA 2002-07-21
424  *
425  *  trancate smaller nubmers into 0 as long as the noise threshold is allowed.
426  *
427  ************************************************************************/
428 static int
429 floatcompare(const void *v1, const void *v2)
430 {
431     const FLOAT *const a = v1, *const b = v2;
432     if (*a > *b)
433         return 1;
434     if (*a < *b)
435         return -1;
436     return 0;
437 }
438
439 static void
440 trancate_smallspectrums(lame_internal_flags const *gfc,
441                         gr_info * const gi, const FLOAT * const l3_xmin, FLOAT * const work)
442 {
443     int     sfb, j, width;
444     FLOAT   distort[SFBMAX];
445     calc_noise_result dummy;
446
447     if ((!(gfc->sv_qnt.substep_shaping & 4) && gi->block_type == SHORT_TYPE)
448         || gfc->sv_qnt.substep_shaping & 0x80)
449         return;
450     (void) calc_noise(gi, l3_xmin, distort, &dummy, 0);
451     for (j = 0; j < 576; j++) {
452         FLOAT   xr = 0.0;
453         if (gi->l3_enc[j] != 0)
454             xr = fabs(gi->xr[j]);
455         work[j] = xr;
456     }
457
458     j = 0;
459     sfb = 8;
460     if (gi->block_type == SHORT_TYPE)
461         sfb = 6;
462     do {
463         FLOAT   allowedNoise, trancateThreshold;
464         int     nsame, start;
465
466         width = gi->width[sfb];
467         j += width;
468         if (distort[sfb] >= 1.0)
469             continue;
470
471         qsort(&work[j - width], width, sizeof(FLOAT), floatcompare);
472         if (EQ(work[j - 1], 0.0))
473             continue;   /* all zero sfb */
474
475         allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];
476         trancateThreshold = 0.0;
477         start = 0;
478         do {
479             FLOAT   noise;
480             for (nsame = 1; start + nsame < width; nsame++)
481                 if (NEQ(work[start + j - width], work[start + j + nsame - width]))
482                     break;
483
484             noise = work[start + j - width] * work[start + j - width] * nsame;
485             if (allowedNoise < noise) {
486                 if (start != 0)
487                     trancateThreshold = work[start + j - width - 1];
488                 break;
489             }
490             allowedNoise -= noise;
491             start += nsame;
492         } while (start < width);
493         if (EQ(trancateThreshold, 0.0))
494             continue;
495
496 /*      printf("%e %e %e\n", */
497 /*             trancateThreshold/l3_xmin[sfb], */
498 /*             trancateThreshold/(l3_xmin[sfb]*start), */
499 /*             trancateThreshold/(l3_xmin[sfb]*(start+width)) */
500 /*          ); */
501 /*      if (trancateThreshold > 1000*l3_xmin[sfb]*start) */
502 /*          trancateThreshold = 1000*l3_xmin[sfb]*start; */
503
504         do {
505             if (fabs(gi->xr[j - width]) <= trancateThreshold)
506                 gi->l3_enc[j - width] = 0;
507         } while (--width > 0);
508     } while (++sfb < gi->psymax);
509
510     gi->part2_3_length = noquant_count_bits(gfc, gi, 0);
511 }
512
513
514 /*************************************************************************
515  *
516  *      loop_break()
517  *
518  *  author/date??
519  *
520  *  Function: Returns zero if there is a scalefac which has not been
521  *            amplified. Otherwise it returns one.
522  *
523  *************************************************************************/
524
525 inline static int
526 loop_break(const gr_info * const cod_info)
527 {
528     int     sfb;
529
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)
533             return 0;
534
535     return 1;
536 }
537
538
539
540
541 /*  mt 5/99:  Function: Improved calc_noise for a single channel   */
542
543 /*************************************************************************
544  *
545  *      quant_compare()
546  *
547  *  author/date??
548  *
549  *  several different codes to decide which quantization is better
550  *
551  *************************************************************************/
552
553 static double
554 penalties(double noise)
555 {
556     return FAST_LOG10(0.368 + 0.632 * noise * noise * noise);
557 }
558
559 static double
560 get_klemm_noise(const FLOAT * distort, const gr_info * const gi)
561 {
562     int     sfb;
563     double  klemm_noise = 1E-37;
564     for (sfb = 0; sfb < gi->psymax; sfb++)
565         klemm_noise += penalties(distort[sfb]);
566
567     return Max(1e-20, klemm_noise);
568 }
569
570 inline static int
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)
574 {
575     /*
576        noise is given in decibels (dB) relative to masking thesholds.
577
578        over_noise:  ??? (the previous comment is fully wrong)
579        tot_noise:   ??? (the previous comment is fully wrong)
580        max_noise:   max quantization noise
581
582      */
583     int     better;
584
585     switch (quant_comp) {
586     default:
587     case 9:{
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;
593             }
594             else {
595                 /* no distorted sfb */
596                 better = ((calc->max_noise < 0) &&
597                           ((calc->max_noise * 10 + calc->bits) <=
598                            (best->max_noise * 10 + best->bits)));
599             }
600             break;
601         }
602
603     case 0:
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);
608         break;
609
610     case 8:
611         calc->max_noise = get_klemm_noise(distort, gi);
612         /*lint --fallthrough */
613     case 1:
614         better = calc->max_noise < best->max_noise;
615         break;
616     case 2:
617         better = calc->tot_noise < best->tot_noise;
618         break;
619     case 3:
620         better = (calc->tot_noise < best->tot_noise)
621             && (calc->max_noise < best->max_noise);
622         break;
623     case 4:
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);
641         break;
642     case 5:
643         better = calc->over_noise < best->over_noise
644             || (EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
645         break;
646     case 6:
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)
651                 ));
652         break;
653     case 7:
654         better = calc->over_count < best->over_count || calc->over_noise < best->over_noise;
655         break;
656     }
657
658
659     if (best->over_count == 0) {
660         /*
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.
665          */
666         better = better && calc->bits < best->bits;
667     }
668
669
670     return better;
671 }
672
673
674
675 /*************************************************************************
676  *
677  *          amp_scalefac_bands()
678  *
679  *  author/date??
680  *
681  *  Amplify the scalefactor bands that violate the masking threshold.
682  *  See ISO 11172-3 Section C.1.5.4.3.5
683  *
684  *  distort[] = noise/masking
685  *  distort[] > 1   ==> noise is not masked
686  *  distort[] < 1   ==> noise is masked
687  *  max_dist = maximum value of distort[]
688  *
689  *  Three algorithms:
690  *  noise_shaping_amp
691  *        0             Amplify all bands with distort[]>1.
692  *
693  *        1             Amplify all bands with distort[] >= max_dist^(.5);
694  *                     ( 50% in the db scale)
695  *
696  *        2             Amplify first band with distort[] >= max_dist;
697  *
698  *
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.
702  *
703  *
704  *************************************************************************/
705 static void
706 amp_scalefac_bands(lame_internal_flags * gfc,
707                    gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
708 {
709     SessionConfig_t const *const cfg = &gfc->cfg;
710     int     j, sfb;
711     FLOAT   ifqstep34, trigger;
712     int     noise_shaping_amp;
713
714     if (cod_info->scalefac_scale == 0) {
715         ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5) */
716     }
717     else {
718         ifqstep34 = 1.68179283050742922612; /* 2**(.75*1) */
719     }
720
721     /* compute maximum value of distort[]  */
722     trigger = 0;
723     for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
724         if (trigger < distort[sfb])
725             trigger = distort[sfb];
726     }
727
728     noise_shaping_amp = cfg->noise_shaping_amp;
729     if (noise_shaping_amp == 3) {
730         if (bRefine == 1)
731             noise_shaping_amp = 2;
732         else
733             noise_shaping_amp = 1;
734     }
735     switch (noise_shaping_amp) {
736     case 2:
737         /* amplify exactly 1 band */
738         break;
739
740     case 1:
741         /* amplify bands within 50% of max (on db scale) */
742         if (trigger > 1.0)
743             trigger = pow(trigger, .5);
744         else
745             trigger *= .95;
746         break;
747
748     case 0:
749     default:
750         /* ISO algorithm.  amplify all bands with distort>1 */
751         if (trigger > 1.0)
752             trigger = 1.0;
753         else
754             trigger *= .95;
755         break;
756     }
757
758     j = 0;
759     for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
760         int const width = cod_info->width[sfb];
761         int     l;
762         j += width;
763         if (distort[sfb] < trigger)
764             continue;
765
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)
769                 return;
770         }
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];
776         }
777
778         if (cfg->noise_shaping_amp == 2)
779             return;
780     }
781 }
782
783 /*************************************************************************
784  *
785  *      inc_scalefac_scale()
786  *
787  *  Takehiro Tominaga 2000-xx-xx
788  *
789  *  turns on scalefac scale and adjusts scalefactors
790  *
791  *************************************************************************/
792
793 static void
794 inc_scalefac_scale(gr_info * const cod_info, FLOAT xrpow[576])
795 {
796     int     l, j, sfb;
797     const FLOAT ifqstep34 = 1.29683955465100964055;
798
799     j = 0;
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)
804             s += pretab[sfb];
805         j += width;
806         if (s & 1) {
807             s++;
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];
812             }
813         }
814         cod_info->scalefac[sfb] = s >> 1;
815     }
816     cod_info->preflag = 0;
817     cod_info->scalefac_scale = 1;
818 }
819
820
821
822 /*************************************************************************
823  *
824  *      inc_subblock_gain()
825  *
826  *  Takehiro Tominaga 2000-xx-xx
827  *
828  *  increases the subblock gain and adjusts scalefactors
829  *
830  *************************************************************************/
831
832 static int
833 inc_subblock_gain(const lame_internal_flags * const gfc, gr_info * const cod_info, FLOAT xrpow[576])
834 {
835     int     sfb, window;
836     int    *const scalefac = cod_info->scalefac;
837
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)
841             return 1;
842     }
843
844     for (window = 0; window < 3; window++) {
845         int     s1, s2, l, j;
846         s1 = s2 = 0;
847
848         for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbdivide; sfb += 3) {
849             if (s1 < scalefac[sfb])
850                 s1 = scalefac[sfb];
851         }
852         for (; sfb < cod_info->sfbmax; sfb += 3) {
853             if (s2 < scalefac[sfb])
854                 s2 = scalefac[sfb];
855         }
856
857         if (s1 < 16 && s2 < 8)
858             continue;
859
860         if (cod_info->subblock_gain[window] >= 7)
861             return 1;
862
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
866          */
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) {
870             FLOAT   amp;
871             int const width = cod_info->width[sfb];
872             int     s = scalefac[sfb];
873             assert(s >= 0);
874             s = s - (4 >> cod_info->scalefac_scale);
875             if (s >= 0) {
876                 scalefac[sfb] = s;
877                 j += width * 3;
878                 continue;
879             }
880
881             scalefac[sfb] = 0;
882             {
883                 int const gain = 210 + (s << (cod_info->scalefac_scale + 1));
884                 amp = IPOW20(gain);
885             }
886             j += width * (window + 1);
887             for (l = -width; l < 0; l++) {
888                 xrpow[j + l] *= amp;
889                 if (xrpow[j + l] > cod_info->xrpow_max)
890                     cod_info->xrpow_max = xrpow[j + l];
891             }
892             j += width * (3 - window - 1);
893         }
894
895         {
896             FLOAT const amp = IPOW20(202);
897             j += cod_info->width[sfb] * (window + 1);
898             for (l = -cod_info->width[sfb]; l < 0; l++) {
899                 xrpow[j + l] *= amp;
900                 if (xrpow[j + l] > cod_info->xrpow_max)
901                     cod_info->xrpow_max = xrpow[j + l];
902             }
903         }
904     }
905     return 0;
906 }
907
908
909
910 /********************************************************************
911  *
912  *      balance_noise()
913  *
914  *  Takehiro Tominaga /date??
915  *  Robert Hegemann 2000-09-06: made a function of it
916  *
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
923  *
924  ********************************************************************/
925 inline static int
926 balance_noise(lame_internal_flags * gfc,
927               gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
928 {
929     SessionConfig_t const *const cfg = &gfc->cfg;
930     int     status;
931
932     amp_scalefac_bands(gfc, cod_info, distort, xrpow, bRefine);
933
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
937      */
938
939     status = loop_break(cod_info);
940
941     if (status)
942         return 0;       /* all bands amplified */
943
944     /* not all scalefactors have been amplified.  so these
945      * scalefacs are possibly valid.  encode them:
946      */
947     status = scale_bitcount(gfc, cod_info);
948
949     if (!status)
950         return 1;       /* amplified some bands not exceeding limits */
951
952     /*  some scalefactors are too large.
953      *  lets try setting scalefac_scale=1
954      */
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);
959             status = 0;
960         }
961         else {
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);
965             }
966         }
967     }
968
969     if (!status) {
970         status = scale_bitcount(gfc, cod_info);
971     }
972     return !status;
973 }
974
975
976
977 /************************************************************************
978  *
979  *  outer_loop ()
980  *
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
984  *
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.
989  *
990  *  added VBR support mt 5/99
991  *
992  *  some code shuffle rh 9/00
993  ************************************************************************/
994
995 static int
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;
1001     gr_info cod_info_w;
1002     FLOAT   save_xrpow[576];
1003     FLOAT   distort[SFBMAX];
1004     calc_noise_result best_noise_info;
1005     int     huff_bits;
1006     int     better;
1007     int     age;
1008     calc_noise_data prev_noise;
1009     int     best_part2_3_length = 9999999;
1010     int     bEndOfSearch = 0;
1011     int     bRefine = 0;
1012     int     best_ggain_pass1 = 0;
1013
1014     (void) bin_search_StepSize(gfc, cod_info, targ_bits, ch, xrpow);
1015
1016     if (!cfg->noise_shaping)
1017         /* fast mode, no noise shaping, we are ready */
1018         return 100;     /* default noise_info.over_count */
1019
1020     memset(&prev_noise, 0, sizeof(calc_noise_data));
1021
1022
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;
1027
1028     cod_info_w = *cod_info;
1029     age = 0;
1030     /* if (cfg->vbr == vbr_rh || cfg->vbr == vbr_mtrh) */
1031     memcpy(save_xrpow, xrpow, sizeof(FLOAT) * 576);
1032
1033     while (!bEndOfSearch) {
1034         /* BEGIN MAIN LOOP */
1035         do {
1036             calc_noise_result noise_info;
1037             int     search_limit;
1038             int     maxggain = 255;
1039
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.
1044              */
1045
1046             if (gfc->sv_qnt.substep_shaping & 2) {
1047                 search_limit = 20;
1048             }
1049             else {
1050                 search_limit = 3;
1051             }
1052
1053
1054
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
1061              */
1062             if (gfc->sv_qnt.sfb21_extra) {
1063                 if (distort[cod_info_w.sfbmax] > 1.0)
1064                     break;
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))
1068                     break;
1069             }
1070
1071             /* try a new scalefactor conbination on cod_info_w */
1072             if (balance_noise(gfc, &cod_info_w, distort, xrpow, bRefine) == 0)
1073                 break;
1074             if (cod_info_w.scalefac_scale)
1075                 maxggain = 254;
1076
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
1081              */
1082             huff_bits = targ_bits - cod_info_w.part2_length;
1083             if (huff_bits <= 0)
1084                 break;
1085
1086             /*  increase quantizer stepsize until needed bits are below maximum
1087              */
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++;
1092
1093             if (cod_info_w.global_gain > maxggain)
1094                 break;
1095
1096             if (best_noise_info.over_count == 0) {
1097
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++;
1102
1103                 if (cod_info_w.global_gain > maxggain)
1104                     break;
1105             }
1106
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;
1110
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;
1115             else
1116                 better = cfg->quant_comp_short;
1117
1118
1119             better = quant_compare(better, &best_noise_info, &noise_info, &cod_info_w, distort);
1120
1121
1122             /* save data so we can restore this quantization later */
1123             if (better) {
1124                 best_part2_3_length = cod_info->part2_3_length;
1125                 best_noise_info = noise_info;
1126                 *cod_info = cod_info_w;
1127                 age = 0;
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);
1132                 }
1133             }
1134             else {
1135                 /* early stop? */
1136                 if (cfg->full_outer_loop == 0) {
1137                     if (++age > search_limit && best_noise_info.over_count == 0)
1138                         break;
1139                     if ((cfg->noise_shaping_amp == 3) && bRefine && age > 30)
1140                         break;
1141                     if ((cfg->noise_shaping_amp == 3) && bRefine &&
1142                         (cod_info_w.global_gain - best_ggain_pass1) > 15)
1143                         break;
1144                 }
1145             }
1146         }
1147         while ((cod_info_w.global_gain + cod_info_w.scalefac_scale) < 255);
1148
1149         if (cfg->noise_shaping_amp == 3) {
1150             if (!bRefine) {
1151                 /* refine search */
1152                 cod_info_w = *cod_info;
1153                 memcpy(xrpow, save_xrpow, sizeof(FLOAT) * 576);
1154                 age = 0;
1155                 best_ggain_pass1 = cod_info_w.global_gain;
1156
1157                 bRefine = 1;
1158             }
1159             else {
1160                 /* search already refined, stop */
1161                 bEndOfSearch = 1;
1162             }
1163
1164         }
1165         else {
1166             bEndOfSearch = 1;
1167         }
1168     }
1169
1170     assert((cod_info->global_gain + cod_info->scalefac_scale) <= 255);
1171     /*  finish up
1172      */
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'
1177      */
1178     else if (gfc->sv_qnt.substep_shaping & 1)
1179         trancate_smallspectrums(gfc, cod_info, l3_xmin, xrpow);
1180
1181     return best_noise_info.over_count;
1182 }
1183
1184
1185
1186
1187
1188 /************************************************************************
1189  *
1190  *      iteration_finish_one()
1191  *
1192  *  Robert Hegemann 2000-09-06
1193  *
1194  *  update reservoir status after FINAL quantization/bitrate
1195  *
1196  ************************************************************************/
1197
1198 static void
1199 iteration_finish_one(lame_internal_flags * gfc, int gr, int ch)
1200 {
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];
1204
1205     /*  try some better scalefac storage
1206      */
1207     best_scalefac_store(gfc, gr, ch, l3_side);
1208
1209     /*  best huffman_divide may save some bits too
1210      */
1211     if (cfg->use_best_huffman == 1)
1212         best_huffman_divide(gfc, cod_info);
1213
1214     /*  update reservoir status after FINAL quantization/bitrate
1215      */
1216     ResvAdjust(gfc, cod_info);
1217 }
1218
1219
1220
1221 /*********************************************************************
1222  *
1223  *      VBR_encode_granule()
1224  *
1225  *  2000-09-04 Robert Hegemann
1226  *
1227  *********************************************************************/
1228
1229 static void
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)
1233 {
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;
1241
1242     assert(Max_bits <= MAX_BITS_PER_CHANNEL);
1243     memset(bst_cod_info.l3_enc, 0, sizeof(bst_cod_info.l3_enc));
1244
1245     /*  search within round about 40 bits of optimal
1246      */
1247     do {
1248         assert(this_bits >= min_bits);
1249         assert(this_bits <= max_bits);
1250         assert(min_bits <= max_bits);
1251
1252         if (this_bits > Max_bits - 42)
1253             gfc->sv_qnt.sfb21_extra = 0;
1254         else
1255             gfc->sv_qnt.sfb21_extra = sfb21_extra;
1256
1257         over = outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, this_bits);
1258
1259         /*  is quantization as good as we are looking for ?
1260          *  in this case: is no scalefactor band distorted?
1261          */
1262         if (over <= 0) {
1263             found = 1;
1264             /*  now we know it can be done with "real_bits"
1265              *  and maybe we can skip some iterations
1266              */
1267             real_bits = cod_info->part2_3_length;
1268
1269             /*  store best quantization so far
1270              */
1271             bst_cod_info = *cod_info;
1272             memcpy(bst_xrpow, xrpow, sizeof(FLOAT) * 576);
1273
1274             /*  try with fewer bits
1275              */
1276             max_bits = real_bits - 32;
1277             dbits = max_bits - min_bits;
1278             this_bits = (max_bits + min_bits) / 2;
1279         }
1280         else {
1281             /*  try with more bits
1282              */
1283             min_bits = this_bits + 32;
1284             dbits = max_bits - min_bits;
1285             this_bits = (max_bits + min_bits) / 2;
1286
1287             if (found) {
1288                 found = 2;
1289                 /*  start again with best quantization so far
1290                  */
1291                 *cod_info = bst_cod_info;
1292                 memcpy(xrpow, bst_xrpow, sizeof(FLOAT) * 576);
1293             }
1294         }
1295     } while (dbits > 12);
1296
1297     gfc->sv_qnt.sfb21_extra = sfb21_extra;
1298
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
1302      */
1303     if (found == 2) {
1304         memcpy(cod_info->l3_enc, bst_cod_info.l3_enc, sizeof(int) * 576);
1305     }
1306     assert(cod_info->part2_3_length <= Max_bits);
1307
1308 }
1309
1310
1311
1312 /************************************************************************
1313  *
1314  *      get_framebits()
1315  *
1316  *  Robert Hegemann 2000-09-05
1317  *
1318  *  calculates
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
1322  *
1323  ************************************************************************/
1324
1325 static void
1326 get_framebits(lame_internal_flags * gfc, int frameBits[15])
1327 {
1328     SessionConfig_t const *const cfg = &gfc->cfg;
1329     EncResult_t *const eov = &gfc->ov_enc;
1330     int     bitsPerFrame, i;
1331
1332     /*  always use at least this many bits per granule per channel
1333      *  unless we detect analog silence, see below
1334      */
1335     eov->bitrate_index = cfg->vbr_min_bitrate_index;
1336     bitsPerFrame = getframebits(gfc);
1337
1338     /*  bits for analog silence
1339      */
1340     eov->bitrate_index = 1;
1341     bitsPerFrame = getframebits(gfc);
1342
1343     for (i = 1; i <= cfg->vbr_max_bitrate_index; i++) {
1344         eov->bitrate_index = i;
1345         frameBits[i] = ResvFrameBegin(gfc, &bitsPerFrame);
1346     }
1347 }
1348
1349
1350
1351 /*********************************************************************
1352  *
1353  *      VBR_prepare()
1354  *
1355  *  2000-09-04 Robert Hegemann
1356  *
1357  *  * converts LR to MS coding when necessary
1358  *  * calculates allowed/adjusted quantization noise amounts
1359  *  * detects analog silent frames
1360  *
1361  *  some remarks:
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
1369  *
1370  *********************************************************************/
1371
1372 /* RH: this one needs to be overhauled sometime */
1373
1374 static int
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])
1380 {
1381     SessionConfig_t const *const cfg = &gfc->cfg;
1382     EncResult_t *const eov = &gfc->ov_enc;
1383
1384     FLOAT   masking_lower_db, adjust = 0.0;
1385     int     gr, ch;
1386     int     analog_silence = 1;
1387     int     avg, mxb, bits = 0;
1388
1389     eov->bitrate_index = cfg->vbr_max_bitrate_index;
1390     avg = ResvFrameBegin(gfc, &avg) / cfg->mode_gr;
1391
1392     get_framebits(gfc, frameBits);
1393
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);
1399         }
1400         for (ch = 0; ch < cfg->channels_out; ++ch) {
1401             gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
1402
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;
1406             }
1407             else {
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;
1410             }
1411             gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
1412
1413             init_outer_loop(gfc, cod_info);
1414             bands[gr][ch] = calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]);
1415             if (bands[gr][ch])
1416                 analog_silence = 0;
1417
1418             min_bits[gr][ch] = 126;
1419
1420             bits += max_bits[gr][ch];
1421         }
1422     }
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;
1428             }
1429             if (min_bits[gr][ch] > max_bits[gr][ch])
1430                 min_bits[gr][ch] = max_bits[gr][ch];
1431
1432         }               /* for ch */
1433     }                   /* for gr */
1434
1435     return analog_silence;
1436 }
1437
1438 static void
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])
1441 {
1442     SessionConfig_t const *const cfg = &gfc->cfg;
1443     int     gr, ch, sfb;
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;
1450
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;
1456                 }
1457             }
1458             max_bits[gr][ch] = Max(min_bits[gr][ch], 0.9 * max_bits[gr][ch]);
1459         }
1460     }
1461 }
1462
1463 /************************************************************************
1464  *
1465  *      VBR_iteration_loop()
1466  *
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
1470  *
1471  *  Robert Hegemann 2000-09-06 rewrite
1472  *
1473  ************************************************************************/
1474
1475 void
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])
1478 {
1479     SessionConfig_t const *const cfg = &gfc->cfg;
1480     EncResult_t *const eov = &gfc->ov_enc;
1481     FLOAT   l3_xmin[2][2][SFBMAX];
1482
1483     FLOAT   xrpow[576];
1484     int     bands[2][2];
1485     int     frameBits[15];
1486     int     used_bits;
1487     int     bits;
1488     int     min_bits[2][2], max_bits[2][2];
1489     int     mean_bits;
1490     int     ch, gr, analog_silence;
1491     III_side_info_t *const l3_side = &gfc->l3_side;
1492
1493     analog_silence = VBR_old_prepare(gfc, pe, ms_ener_ratio, ratio,
1494                                      l3_xmin, frameBits, min_bits, max_bits, bands);
1495
1496     /*---------------------------------*/
1497     for (;;) {
1498
1499         /*  quantize granules with lowest possible number of bits
1500          */
1501
1502         used_bits = 0;
1503
1504         for (gr = 0; gr < cfg->mode_gr; gr++) {
1505             for (ch = 0; ch < cfg->channels_out; ch++) {
1506                 int     ret;
1507                 gr_info *const cod_info = &l3_side->tt[gr][ch];
1508
1509                 /*  init_outer_loop sets up cod_info, scalefac and xrpow
1510                  */
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
1515                      */
1516                     continue; /* with next channel */
1517                 }
1518
1519                 VBR_encode_granule(gfc, cod_info, l3_xmin[gr][ch], xrpow,
1520                                    ch, min_bits[gr][ch], max_bits[gr][ch]);
1521
1522                 /*  do the 'substep shaping'
1523                  */
1524                 if (gfc->sv_qnt.substep_shaping & 1) {
1525                     trancate_smallspectrums(gfc, &l3_side->tt[gr][ch], l3_xmin[gr][ch], xrpow);
1526                 }
1527
1528                 ret = cod_info->part2_3_length + cod_info->part2_length;
1529                 used_bits += ret;
1530             }           /* for ch */
1531         }               /* for gr */
1532
1533         /*  find lowest bitrate able to hold used bits
1534          */
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
1538              */
1539             eov->bitrate_index = 1;
1540         else
1541             eov->bitrate_index = cfg->vbr_min_bitrate_index;
1542
1543         for (; eov->bitrate_index < cfg->vbr_max_bitrate_index; eov->bitrate_index++) {
1544             if (used_bits <= frameBits[eov->bitrate_index])
1545                 break;
1546         }
1547         bits = ResvFrameBegin(gfc, &mean_bits);
1548
1549         if (used_bits <= bits)
1550             break;
1551
1552         bitpressure_strategy(gfc, l3_xmin, (const int (*)[2])min_bits, max_bits);
1553
1554     }                   /* breaks adjusted */
1555     /*--------------------------------------*/
1556
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);
1560         }               /* for ch */
1561     }                   /* for gr */
1562     ResvFrameEnd(gfc, mean_bits);
1563 }
1564
1565
1566
1567 static int
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],
1571                 int* max_resv)
1572 {
1573     SessionConfig_t const *const cfg = &gfc->cfg;
1574     EncResult_t *const eov = &gfc->ov_enc;
1575
1576     int     gr, ch;
1577     int     analog_silence = 1;
1578     int     avg, bits = 0;
1579     int     maximum_framebits;
1580
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;
1585
1586         get_framebits(gfc, frameBits);
1587         maximum_framebits = frameBits[cfg->vbr_max_bitrate_index];
1588     }
1589     else {
1590         eov->bitrate_index = 0;
1591         maximum_framebits = ResvFrameBegin(gfc, &avg);
1592         frameBits[0] = maximum_framebits;
1593         *max_resv = gfc->sv_enc.ResvMax;
1594     }
1595
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);
1600         }
1601         for (ch = 0; ch < cfg->channels_out; ++ch) {
1602             gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
1603
1604             gfc->sv_qnt.masking_lower = pow(10.0, gfc->sv_qnt.mask_adjust * 0.1);
1605
1606             init_outer_loop(gfc, cod_info);
1607             if (0 != calc_xmin(gfc, &ratio[gr][ch], cod_info, l3_xmin[gr][ch]))
1608                 analog_silence = 0;
1609
1610             bits += max_bits[gr][ch];
1611         }
1612     }
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;
1618             }
1619
1620         }               /* for ch */
1621     }                   /* for gr */
1622     if (analog_silence) {
1623         *max_resv = 0;
1624     }
1625     return analog_silence;
1626 }
1627
1628
1629
1630 void
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])
1633 {
1634     SessionConfig_t const *const cfg = &gfc->cfg;
1635     EncResult_t *const eov = &gfc->ov_enc;
1636     FLOAT   l3_xmin[2][2][SFBMAX];
1637
1638     FLOAT   xrpow[2][2][576];
1639     int     frameBits[15];
1640     int     used_bits;
1641     int     max_bits[2][2];
1642     int     ch, gr, analog_silence, pad;
1643     III_side_info_t *const l3_side = &gfc->l3_side;
1644
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;
1648     
1649     (void) ms_ener_ratio; /* not used */
1650
1651     memset(xrpow, 0, sizeof(xrpow));
1652
1653     analog_silence = VBR_new_prepare(gfc, pe, ratio, l3_xmin, frameBits, max_bits, &pad);
1654
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];
1658
1659             /*  init_outer_loop sets up cod_info, scalefac and xrpow
1660              */
1661             if (0 == init_xrpow(gfc, cod_info, xrpow[gr][ch])) {
1662                 max_bits[gr][ch] = 0; /* silent granule needs no bits */
1663             }
1664         }               /* for ch */
1665     }                   /* for gr */
1666
1667     /*  quantize granules with lowest possible number of bits
1668      */
1669
1670     used_bits = VBR_encode_frame(gfc, const_xrpow, const_l3_xmin, const_max_bits);
1671
1672     if (!cfg->free_format) {
1673         int     i, j;
1674
1675         /*  find lowest bitrate able to hold used bits
1676          */
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
1680              */
1681             i = 1;
1682         }
1683         else {
1684             i = cfg->vbr_min_bitrate_index;
1685         }
1686
1687         for (; i < cfg->vbr_max_bitrate_index; i++) {
1688             if (used_bits <= frameBits[i]) 
1689                 break;
1690         }
1691         if (i > cfg->vbr_max_bitrate_index) {
1692             i = cfg->vbr_max_bitrate_index;
1693         }
1694         if (pad > 0) {
1695             for (j = cfg->vbr_max_bitrate_index; j > i; --j) {
1696                 int const unused = frameBits[j] - used_bits;
1697                 if (unused <= pad) 
1698                     break;
1699             }
1700             eov->bitrate_index = j;
1701         }
1702         else {
1703             eov->bitrate_index = i;
1704         }
1705     }
1706     else {
1707 #if 0
1708         static int mmm = 0;
1709         int     fff = getFramesize_kbps(gfc, used_bits);
1710         int     hhh = getFramesize_kbps(gfc, MAX_BITS_PER_GRANULE * cfg->mode_gr);
1711         if (mmm < fff)
1712             mmm = fff;
1713         printf("demand=%3d kbps  max=%3d kbps   limit=%3d kbps\n", fff, mmm, hhh);
1714 #endif
1715         eov->bitrate_index = 0;
1716     }
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);
1726             }
1727         }
1728         ResvFrameEnd(gfc, mean_bits);
1729     }
1730     else {
1731         /* SHOULD NOT HAPPEN INTERNAL ERROR
1732          */
1733         ERRORF(gfc, "INTERNAL ERROR IN VBR NEW CODE, please send bug report\n");
1734         exit(-1);
1735     }
1736 }
1737
1738
1739
1740
1741
1742 /********************************************************************
1743  *
1744  *  calc_target_bits()
1745  *
1746  *  calculates target bits for ABR encoding
1747  *
1748  *  mt 2000/05/31
1749  *
1750  ********************************************************************/
1751
1752 static void
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)
1757 {
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;
1761     FLOAT   res_factor;
1762     int     gr, ch, totbits, mean_bits;
1763     int     framesize = 576 * cfg->mode_gr;
1764
1765     eov->bitrate_index = cfg->vbr_max_bitrate_index;
1766     *max_frame_bits = ResvFrameBegin(gfc, &mean_bits);
1767
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);
1771
1772     mean_bits = cfg->vbr_avg_bitrate_kbps * framesize * 1000;
1773     if (gfc->sv_qnt.substep_shaping & 1)
1774         mean_bits *= 1.09;
1775     mean_bits /= cfg->samplerate_out;
1776     mean_bits -= cfg->sideinfo_len * 8;
1777     mean_bits /= (cfg->mode_gr * cfg->channels_out);
1778
1779     /*
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.
1783
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
1788
1789        Until we get the above suggestion working, we use the following
1790        tuning:
1791        compression ratio    res_factor
1792        5.5  (256kbps)         1.0      no need for bitreservoir
1793        11   (128kbps)         .93      7% held for reservoir
1794
1795        with linear interpolation for other values.
1796
1797      */
1798     res_factor = .93 + .07 * (11.0 - cfg->compression_ratio) / (11.0 - 5.5);
1799     if (res_factor < .90)
1800         res_factor = .90;
1801     if (res_factor > 1.00)
1802         res_factor = 1.00;
1803
1804     for (gr = 0; gr < cfg->mode_gr; gr++) {
1805         int     sum = 0;
1806         for (ch = 0; ch < cfg->channels_out; ch++) {
1807             targ_bits[gr][ch] = res_factor * mean_bits;
1808
1809             if (pe[gr][ch] > 700) {
1810                 int     add_bits = (pe[gr][ch] - 700) / 1.4;
1811
1812                 gr_info const *const cod_info = &l3_side->tt[gr][ch];
1813                 targ_bits[gr][ch] = res_factor * mean_bits;
1814
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;
1819                 }
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)
1824                     add_bits = 0;
1825
1826                 targ_bits[gr][ch] += add_bits;
1827             }
1828             if (targ_bits[gr][ch] > MAX_BITS_PER_CHANNEL) {
1829                 targ_bits[gr][ch] = MAX_BITS_PER_CHANNEL;
1830             }
1831             sum += targ_bits[gr][ch];
1832         }               /* for 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;
1837             }
1838         }
1839     }                   /* for gr */
1840
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);
1845         }
1846
1847     /*  sum target bits
1848      */
1849     totbits = 0;
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];
1855         }
1856     }
1857
1858     /*  repartion target bits if needed
1859      */
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;
1865             }
1866         }
1867     }
1868 }
1869
1870
1871
1872
1873
1874
1875 /********************************************************************
1876  *
1877  *  ABR_iteration_loop()
1878  *
1879  *  encode a frame with a disired average bitrate
1880  *
1881  *  mt 2000/05/31
1882  *
1883  ********************************************************************/
1884
1885 void
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])
1888 {
1889     SessionConfig_t const *const cfg = &gfc->cfg;
1890     EncResult_t *const eov = &gfc->ov_enc;
1891     FLOAT   l3_xmin[SFBMAX];
1892     FLOAT   xrpow[576];
1893     int     targ_bits[2][2];
1894     int     mean_bits, max_frame_bits;
1895     int     ch, gr, ath_over;
1896     int     analog_silence_bits;
1897     gr_info *cod_info;
1898     III_side_info_t *const l3_side = &gfc->l3_side;
1899
1900     mean_bits = 0;
1901
1902     calc_target_bits(gfc, pe, ms_ener_ratio, targ_bits, &analog_silence_bits, &max_frame_bits);
1903
1904     /*  encode granules
1905      */
1906     for (gr = 0; gr < cfg->mode_gr; gr++) {
1907
1908         if (gfc->ov_enc.mode_ext == MPG_MD_MS_LR) {
1909             ms_convert(&gfc->l3_side, gr);
1910         }
1911         for (ch = 0; ch < cfg->channels_out; ch++) {
1912             FLOAT   adjust, masking_lower_db;
1913             cod_info = &l3_side->tt[gr][ch];
1914
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; */
1917                 adjust = 0;
1918                 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
1919             }
1920             else {
1921                 /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
1922                 adjust = 0;
1923                 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
1924             }
1925             gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
1926
1927
1928             /*  cod_info, scalefac and xrpow get initialized in init_outer_loop
1929              */
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
1935                  */
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;
1939
1940                 (void) outer_loop(gfc, cod_info, l3_xmin, xrpow, ch, targ_bits[gr][ch]);
1941             }
1942             iteration_finish_one(gfc, gr, ch);
1943         }               /* ch */
1944     }                   /* gr */
1945
1946     /*  find a bitrate which can refill the resevoir to positive size.
1947      */
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)
1951             break;
1952     }
1953     assert(eov->bitrate_index <= cfg->vbr_max_bitrate_index);
1954
1955     ResvFrameEnd(gfc, mean_bits);
1956 }
1957
1958
1959
1960
1961
1962
1963 /************************************************************************
1964  *
1965  *      CBR_iteration_loop()
1966  *
1967  *  author/date??
1968  *
1969  *  encodes one frame of MP3 data with constant bitrate
1970  *
1971  ************************************************************************/
1972
1973 void
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])
1976 {
1977     SessionConfig_t const *const cfg = &gfc->cfg;
1978     FLOAT   l3_xmin[SFBMAX];
1979     FLOAT   xrpow[576];
1980     int     targ_bits[2];
1981     int     mean_bits, max_bits;
1982     int     gr, ch;
1983     III_side_info_t *const l3_side = &gfc->l3_side;
1984     gr_info *cod_info;
1985
1986     (void) ResvFrameBegin(gfc, &mean_bits);
1987
1988     /* quantize! */
1989     for (gr = 0; gr < cfg->mode_gr; gr++) {
1990
1991         /*  calculate needed bits
1992          */
1993         max_bits = on_pe(gfc, pe, targ_bits, mean_bits, gr, gr);
1994
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);
1998         }
1999
2000         for (ch = 0; ch < cfg->channels_out; ch++) {
2001             FLOAT   adjust, masking_lower_db;
2002             cod_info = &l3_side->tt[gr][ch];
2003
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; */
2006                 adjust = 0;
2007                 masking_lower_db = gfc->sv_qnt.mask_adjust - adjust;
2008             }
2009             else {
2010                 /* adjust = 2.56/(1+exp(3.5-pe[gr][ch]/300.))-0.14; */
2011                 adjust = 0;
2012                 masking_lower_db = gfc->sv_qnt.mask_adjust_short - adjust;
2013             }
2014             gfc->sv_qnt.masking_lower = pow(10.0, masking_lower_db * 0.1);
2015
2016             /*  init_outer_loop sets up cod_info, scalefac and xrpow
2017              */
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
2023                  */
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]);
2026             }
2027
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]);
2031         }               /* for ch */
2032     }                   /* for gr */
2033
2034     ResvFrameEnd(gfc, mean_bits);
2035 }