AAC encoder: Extensive improvements
[ffmpeg.git] / libavcodec / aaccoder_twoloop.h
1 /*
2  * AAC encoder twoloop coder
3  * Copyright (C) 2008-2009 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * AAC encoder twoloop coder
25  * @author Konstantin Shishkov, Claudio Freire
26  */
27
28 /**
29  * This file contains a template for the twoloop coder function.
30  * It needs to be provided, externally, as an already included declaration,
31  * the following functions from aacenc_quantization/util.h. They're not included
32  * explicitly here to make it possible to provide alternative implementations:
33  *  - quantize_band_cost
34  *  - abs_pow34_v
35  *  - find_max_val
36  *  - find_min_book
37  *  - find_form_factor
38  */
39
40 #ifndef AVCODEC_AACCODER_TWOLOOP_H
41 #define AVCODEC_AACCODER_TWOLOOP_H
42
43 #include <float.h>
44 #include "libavutil/mathematics.h"
45 #include "mathops.h"
46 #include "avcodec.h"
47 #include "put_bits.h"
48 #include "aac.h"
49 #include "aacenc.h"
50 #include "aactab.h"
51 #include "aacenctab.h"
52 #include "aac_tablegen_decl.h"
53
54 /** Frequency in Hz for lower limit of noise substitution **/
55 #define NOISE_LOW_LIMIT 4000
56
57 #define sclip(x) av_clip(x,60,218)
58
59
60 static av_always_inline int ff_pns_bits(const SingleChannelElement *sce, int w, int g)
61 {
62     if (!g || !sce->zeroes[w*16+g-1] || !sce->can_pns[w*16+g-1]) {
63         return 9;
64     } else {
65         return 5;
66     }
67 }
68
69 /**
70  * two-loop quantizers search taken from ISO 13818-7 Appendix C
71  */
72 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
73                                           AACEncContext *s,
74                                           SingleChannelElement *sce,
75                                           const float lambda)
76 {
77     int start = 0, i, w, w2, g, recomprd;
78     int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate
79         / ((avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : avctx->channels)
80         * (lambda / 120.f);
81     int refbits = destbits;
82     int toomanybits, toofewbits;
83     char nzs[128];
84     int maxsf[128];
85     float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
86     float maxvals[128], spread_thr_r[128];
87     float min_spread_thr_r, max_spread_thr_r;
88
89     /**
90      * rdlambda controls the maximum tolerated distortion. Twoloop
91      * will keep iterating until it fails to lower it or it reaches
92      * ulimit * rdlambda. Keeping it low increases quality on difficult
93      * signals, but lower it too much, and bits will be taken from weak
94      * signals, creating "holes". A balance is necesary.
95      * rdmax and rdmin specify the relative deviation from rdlambda
96      * allowed for tonality compensation
97      */
98     float rdlambda = av_clipf(2.0f * 120.f / lambda, 0.0625f, 16.0f);
99     const float nzslope = 1.5f;
100     float rdmin = 0.03125f;
101     float rdmax = 1.0f;
102
103     /**
104      * sfoffs controls an offset of optmium allocation that will be
105      * applied based on lambda. Keep it real and modest, the loop
106      * will take care of the rest, this just accelerates convergence
107      */
108     float sfoffs = av_clipf(log2f(120.0f / lambda) * 4.0f, -5, 10);
109
110     int fflag, minscaler, maxscaler, nminscaler, minrdsf;
111     int its  = 0;
112     int maxits = 30;
113     int allz = 0;
114     int tbits;
115     int cutoff = 1024;
116     int pns_start_pos;
117
118     /**
119      * zeroscale controls a multiplier of the threshold, if band energy
120      * is below this, a zero is forced. Keep it lower than 1, unless
121      * low lambda is used, because energy < threshold doesn't mean there's
122      * no audible signal outright, it's just energy. Also make it rise
123      * slower than rdlambda, as rdscale has due compensation with
124      * noisy band depriorization below, whereas zeroing logic is rather dumb
125      */
126     float zeroscale;
127     if (lambda > 120.f) {
128         zeroscale = av_clipf(powf(120.f / lambda, 0.25f), 0.0625f, 1.0f);
129     } else {
130         zeroscale = 1.f;
131     }
132
133     if (s->psy.bitres.alloc >= 0) {
134         /**
135          * Psy granted us extra bits to use, from the reservoire
136          * adjust for lambda except what psy already did
137          */
138         destbits = s->psy.bitres.alloc
139             * (lambda / (avctx->global_quality ? avctx->global_quality : 120));
140     }
141
142     if (avctx->flags & CODEC_FLAG_QSCALE) {
143         /**
144          * Constant Q-scale doesn't compensate MS coding on its own
145          * No need to be overly precise, this only controls RD
146          * adjustment CB limits when going overboard
147          */
148         if (s->options.stereo_mode && s->cur_type == TYPE_CPE)
149             destbits *= 2;
150
151         /**
152          * When using a constant Q-scale, don't adjust bits, just use RD
153          * Don't let it go overboard, though... 8x psy target is enough
154          */
155         toomanybits = 5800;
156         toofewbits = destbits / 16;
157
158         /** Don't offset scalers, just RD */
159         sfoffs = sce->ics.num_windows - 1;
160         rdlambda = sqrtf(rdlambda);
161
162         /** search further */
163         maxits *= 2;
164     } else {
165         /** When using ABR, be strict */
166         toomanybits = destbits + destbits/16;
167         toofewbits = destbits - destbits/4;
168
169         sfoffs = 0;
170         rdlambda = sqrtf(rdlambda);
171     }
172
173     /** and zero out above cutoff frequency */
174     {
175         int wlen = 1024 / sce->ics.num_windows;
176         int bandwidth;
177
178         /**
179          * Scale, psy gives us constant quality, this LP only scales
180          * bitrate by lambda, so we save bits on subjectively unimportant HF
181          * rather than increase quantization noise. Adjust nominal bitrate
182          * to effective bitrate according to encoding parameters,
183          * AAC_CUTOFF_FROM_BITRATE is calibrated for effective bitrate.
184          */
185         float rate_bandwidth_multiplier = 1.5f;
186         int frame_bit_rate = (avctx->flags & CODEC_FLAG_QSCALE)
187             ? (refbits * rate_bandwidth_multiplier * avctx->sample_rate / 1024)
188             : (avctx->bit_rate / avctx->channels);
189
190         /** Compensate for extensions that increase efficiency */
191         if (s->options.pns || s->options.intensity_stereo)
192             frame_bit_rate *= 1.15f;
193
194         if (avctx->cutoff > 0) {
195             bandwidth = avctx->cutoff;
196         } else {
197             bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_bit_rate, 1, avctx->sample_rate));
198         }
199
200         cutoff = bandwidth * 2 * wlen / avctx->sample_rate;
201         pns_start_pos = NOISE_LOW_LIMIT * 2 * wlen / avctx->sample_rate;
202     }
203
204     /**
205      * for values above this the decoder might end up in an endless loop
206      * due to always having more bits than what can be encoded.
207      */
208     destbits = FFMIN(destbits, 5800);
209     toomanybits = FFMIN(toomanybits, 5800);
210     toofewbits = FFMIN(toofewbits, 5800);
211     /**
212      * XXX: some heuristic to determine initial quantizers will reduce search time
213      * determine zero bands and upper distortion limits
214      */
215     min_spread_thr_r = -1;
216     max_spread_thr_r = -1;
217     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
218         for (g = start = 0;  g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
219             int nz = 0;
220             float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
221             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
222                 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
223                 if (start >= cutoff || band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f) {
224                     sce->zeroes[(w+w2)*16+g] = 1;
225                     continue;
226                 }
227                 nz = 1;
228             }
229             if (!nz) {
230                 uplim = 0.0f;
231             } else {
232                 nz = 0;
233                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
234                     FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
235                     if (band->energy <= (band->threshold * zeroscale) || band->threshold == 0.0f)
236                         continue;
237                     uplim += band->threshold;
238                     energy += band->energy;
239                     spread += band->spread;
240                     nz++;
241                 }
242             }
243             uplims[w*16+g] = uplim;
244             energies[w*16+g] = energy;
245             nzs[w*16+g] = nz;
246             sce->zeroes[w*16+g] = !nz;
247             allz |= nz;
248             if (nz) {
249                 spread_thr_r[w*16+g] = energy * nz / (uplim * spread);
250                 if (min_spread_thr_r < 0) {
251                     min_spread_thr_r = max_spread_thr_r = spread_thr_r[w*16+g];
252                 } else {
253                     min_spread_thr_r = FFMIN(min_spread_thr_r, spread_thr_r[w*16+g]);
254                     max_spread_thr_r = FFMAX(max_spread_thr_r, spread_thr_r[w*16+g]);
255                 }
256             }
257         }
258     }
259
260     /** Compute initial scalers */
261     minscaler = 65535;
262     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
263         for (g = 0;  g < sce->ics.num_swb; g++) {
264             if (sce->zeroes[w*16+g]) {
265                 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
266                 continue;
267             }
268             /**
269              * log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2).
270              * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion,
271              * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus
272              * more robust.
273              */
274             sce->sf_idx[w*16+g] = av_clip(
275                 SCALE_ONE_POS
276                     + 1.75*log2f(FFMAX(0.00125f,uplims[w*16+g]) / sce->ics.swb_sizes[g])
277                     + sfoffs,
278                 60, SCALE_MAX_POS);
279             minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
280         }
281     }
282
283     /** Clip */
284     minscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
285     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
286         for (g = 0;  g < sce->ics.num_swb; g++)
287             if (!sce->zeroes[w*16+g])
288                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF - 1);
289
290     if (!allz)
291         return;
292     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
293
294     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
295         start = w*128;
296         for (g = 0;  g < sce->ics.num_swb; g++) {
297             const float *scaled = s->scoefs + start;
298             maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
299             start += sce->ics.swb_sizes[g];
300         }
301     }
302
303     /**
304      * Scale uplims to match rate distortion to quality
305      * bu applying noisy band depriorization and tonal band priorization.
306      * Maxval-energy ratio gives us an idea of how noisy/tonal the band is.
307      * If maxval^2 ~ energy, then that band is mostly noise, and we can relax
308      * rate distortion requirements.
309      */
310     memcpy(euplims, uplims, sizeof(euplims));
311     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
312         /** psy already priorizes transients to some extent */
313         float de_psy_factor = (sce->ics.num_windows > 1) ? 8.0f / sce->ics.group_len[w] : 1.0f;
314         start = w*128;
315         for (g = 0;  g < sce->ics.num_swb; g++) {
316             if (nzs[g] > 0) {
317                 float cleanup_factor = ff_sqrf(av_clipf(start / (cutoff * 0.75f), 1.0f, 2.0f));
318                 float energy2uplim = find_form_factor(
319                     sce->ics.group_len[w], sce->ics.swb_sizes[g],
320                     uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
321                     sce->coeffs + start,
322                     nzslope * cleanup_factor);
323                 energy2uplim *= de_psy_factor;
324                 if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
325                     /** In ABR, we need to priorize less and let rate control do its thing */
326                     energy2uplim = sqrtf(energy2uplim);
327                 }
328                 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
329                 uplims[w*16+g] *= av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
330                                   * sce->ics.group_len[w];
331
332                 energy2uplim = find_form_factor(
333                     sce->ics.group_len[w], sce->ics.swb_sizes[g],
334                     uplims[w*16+g] / (nzs[g] * sce->ics.swb_sizes[w]),
335                     sce->coeffs + start,
336                     2.0f);
337                 energy2uplim *= de_psy_factor;
338                 if (!(avctx->flags & CODEC_FLAG_QSCALE)) {
339                     /** In ABR, we need to priorize less and let rate control do its thing */
340                     energy2uplim = sqrtf(energy2uplim);
341                 }
342                 energy2uplim = FFMAX(0.015625f, FFMIN(1.0f, energy2uplim));
343                 euplims[w*16+g] *= av_clipf(rdlambda * energy2uplim * sce->ics.group_len[w],
344                     0.5f, 1.0f);
345             }
346             start += sce->ics.swb_sizes[g];
347         }
348     }
349
350     for (i = 0; i < sizeof(maxsf) / sizeof(maxsf[0]); ++i)
351         maxsf[i] = SCALE_MAX_POS;
352
353     //perform two-loop search
354     //outer loop - improve quality
355     do {
356         //inner loop - quantize spectrum to fit into given number of bits
357         int overdist;
358         int qstep = its ? 1 : 32;
359         do {
360             int prev = -1;
361             int changed = 0;
362             recomprd = 0;
363             tbits = 0;
364             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
365                 start = w*128;
366                 for (g = 0;  g < sce->ics.num_swb; g++) {
367                     const float *coefs = &sce->coeffs[start];
368                     const float *scaled = &s->scoefs[start];
369                     int bits = 0;
370                     int cb;
371                     float dist = 0.0f;
372                     float qenergy = 0.0f;
373
374                     if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
375                         start += sce->ics.swb_sizes[g];
376                         if (sce->can_pns[w*16+g]) {
377                             /** PNS isn't free */
378                             tbits += ff_pns_bits(sce, w, g);
379                         }
380                         continue;
381                     }
382                     cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
383                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
384                         int b;
385                         float sqenergy;
386                         dist += quantize_band_cost(s, coefs + w2*128,
387                                                    scaled + w2*128,
388                                                    sce->ics.swb_sizes[g],
389                                                    sce->sf_idx[w*16+g],
390                                                    cb,
391                                                    1.0f,
392                                                    INFINITY,
393                                                    &b, &sqenergy,
394                                                    0);
395                         bits += b;
396                         qenergy += sqenergy;
397                     }
398                     dists[w*16+g] = dist - bits;
399                     qenergies[w*16+g] = qenergy;
400                     if (prev != -1) {
401                         int sfdiff = sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO;
402                         av_assert1(sfdiff >= 0 && sfdiff <= 2*SCALE_MAX_DIFF);
403                         bits += ff_aac_scalefactor_bits[sfdiff];
404                     }
405                     tbits += bits;
406                     start += sce->ics.swb_sizes[g];
407                     prev = sce->sf_idx[w*16+g];
408                 }
409             }
410             if (tbits > toomanybits) {
411                 recomprd = 1;
412                 for (i = 0; i < 128; i++) {
413                     if (sce->sf_idx[i] < (SCALE_MAX_POS - SCALE_DIV_512)) {
414                         int maxsf_i = (tbits > 5800) ? SCALE_MAX_POS : maxsf[i];
415                         int new_sf = FFMIN(maxsf_i, sce->sf_idx[i] + qstep);
416                         if (new_sf != sce->sf_idx[i]) {
417                             sce->sf_idx[i] = new_sf;
418                             changed = 1;
419                         }
420                     }
421                 }
422             } else if (tbits < toofewbits) {
423                 recomprd = 1;
424                 for (i = 0; i < 128; i++) {
425                     if (sce->sf_idx[i] > SCALE_ONE_POS) {
426                         int new_sf = FFMAX(SCALE_ONE_POS, sce->sf_idx[i] - qstep);
427                         if (new_sf != sce->sf_idx[i]) {
428                             sce->sf_idx[i] = new_sf;
429                             changed = 1;
430                         }
431                     }
432                 }
433             }
434             qstep >>= 1;
435             if (!qstep && tbits > toomanybits && sce->sf_idx[0] < 217 && changed)
436                 qstep = 1;
437         } while (qstep);
438
439         overdist = 1;
440         for (i = 0; i < 2 && (overdist || recomprd); ++i) {
441             if (recomprd) {
442                 /** Must recompute distortion */
443                 int prev = -1;
444                 tbits = 0;
445                 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
446                     start = w*128;
447                     for (g = 0;  g < sce->ics.num_swb; g++) {
448                         const float *coefs = sce->coeffs + start;
449                         const float *scaled = s->scoefs + start;
450                         int bits = 0;
451                         int cb;
452                         float dist = 0.0f;
453                         float qenergy = 0.0f;
454
455                         if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
456                             start += sce->ics.swb_sizes[g];
457                             if (sce->can_pns[w*16+g]) {
458                                 /** PNS isn't free */
459                                 tbits += ff_pns_bits(sce, w, g);
460                             }
461                             continue;
462                         }
463                         cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
464                         for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
465                             int b;
466                             float sqenergy;
467                             dist += quantize_band_cost(s, coefs + w2*128,
468                                                     scaled + w2*128,
469                                                     sce->ics.swb_sizes[g],
470                                                     sce->sf_idx[w*16+g],
471                                                     cb,
472                                                     1.0f,
473                                                     INFINITY,
474                                                     &b, &sqenergy,
475                                                     0);
476                             bits += b;
477                             qenergy += sqenergy;
478                         }
479                         dists[w*16+g] = dist - bits;
480                         qenergies[w*16+g] = qenergy;
481                         if (prev != -1) {
482                             int sfdiff = sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO;
483                             av_assert1(sfdiff >= 0 && sfdiff <= 2*SCALE_MAX_DIFF);
484                             bits += ff_aac_scalefactor_bits[sfdiff];
485                         }
486                         tbits += bits;
487                         start += sce->ics.swb_sizes[g];
488                         prev = sce->sf_idx[w*16+g];
489                     }
490                 }
491             }
492             if (!i && s->options.pns && its > maxits/2) {
493                 float maxoverdist = 0.0f;
494                 overdist = recomprd = 0;
495                 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
496                     float ovrfactor = 2.f+(maxits-its)*16.f/maxits;
497                     for (g = start = 0;  g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
498                         if (!sce->zeroes[w*16+g] && dists[w*16+g] > uplims[w*16+g]*ovrfactor) {
499                             float ovrdist = dists[w*16+g] / FFMAX(uplims[w*16+g],euplims[w*16+g]);
500                             maxoverdist = FFMAX(maxoverdist, ovrdist);
501                             overdist++;
502                         }
503                     }
504                 }
505                 if (overdist) {
506                     /* We have overdistorted bands, trade for zeroes (that can be noise)
507                      * Zero the bands in the lowest 1.25% spread-energy-threshold ranking
508                      */
509                     float minspread = max_spread_thr_r;
510                     float maxspread = min_spread_thr_r;
511                     float zspread;
512                     int zeroable = 0;
513                     int zeroed = 0;
514                     int maxzeroed;
515                     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
516                         for (g = start = 0;  g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
517                             if (start >= pns_start_pos && !sce->zeroes[w*16+g] && sce->can_pns[w*16+g]) {
518                                 minspread = FFMIN(minspread, spread_thr_r[w*16+g]);
519                                 maxspread = FFMAX(maxspread, spread_thr_r[w*16+g]);
520                                 zeroable++;
521                             }
522                         }
523                     }
524                     zspread = (maxspread-minspread) * 0.0125f + minspread;
525                     zspread = FFMIN(maxoverdist, zspread);
526                     maxzeroed = zeroable * its / (2 * maxits);
527                     for (g = sce->ics.num_swb-1; g > 0 && zeroed < maxzeroed; g--) {
528                         if (sce->ics.swb_offset[g] < pns_start_pos)
529                             continue;
530                         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
531                             if (!sce->zeroes[w*16+g] && sce->can_pns[w*16+g] && spread_thr_r[w*16+g] <= zspread) {
532                                 sce->zeroes[w*16+g] = 1;
533                                 sce->band_type[w*16+g] = 0;
534                                 zeroed++;
535                             }
536                         }
537                     }
538                     if (zeroed)
539                         recomprd = 1;
540                 } else {
541                     overdist = 0;
542                 }
543             }
544         }
545
546         minscaler = SCALE_MAX_POS;
547         maxscaler = 0;
548         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
549             for (g = 0;  g < sce->ics.num_swb; g++) {
550                 if (!sce->zeroes[w*16+g]) {
551                     minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
552                     maxscaler = FFMAX(maxscaler, sce->sf_idx[w*16+g]);
553                 }
554             }
555         }
556
557         fflag = 0;
558         minscaler = nminscaler = av_clip(minscaler, SCALE_ONE_POS - SCALE_DIV_512, SCALE_MAX_POS - SCALE_DIV_512);
559         minrdsf = FFMAX3(60, minscaler - 1, maxscaler - SCALE_MAX_DIFF - 1);
560         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
561             /** Start with big steps, end up fine-tunning */
562             int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
563             int edepth = depth+2;
564             float uplmax = its / (maxits*0.25f) + 1.0f;
565             uplmax *= (tbits > destbits) ? FFMIN(2.0f, tbits / (float)FFMAX(1,destbits)) : 1.0f;
566             start = w * 128;
567             for (g = 0; g < sce->ics.num_swb; g++) {
568                 int prevsc = sce->sf_idx[w*16+g];
569                 int minrdsfboost = (sce->ics.num_windows > 1) ? av_clip(g-4, -2, 0) : av_clip(g-16, -4, 0);
570                 if (!sce->zeroes[w*16+g]) {
571                     const float *coefs = sce->coeffs + start;
572                     const float *scaled = s->scoefs + start;
573                     int cmb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
574                     if ((!cmb || dists[w*16+g] > uplims[w*16+g]) && sce->sf_idx[w*16+g] > minrdsf) {
575                         /* Try to make sure there is some energy in every nonzero band
576                          * NOTE: This algorithm must be forcibly imbalanced, pushing harder
577                          *  on holes or more distorted bands at first, otherwise there's
578                          *  no net gain (since the next iteration will offset all bands
579                          *  on the opposite direction to compensate for extra bits)
580                          */
581                         for (i = 0; i < edepth; ++i) {
582                             int cb, bits;
583                             float dist, qenergy;
584                             int mb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1);
585                             cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
586                             dist = qenergy = 0.f;
587                             bits = 0;
588                             if (!cb) {
589                                 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g]-1, maxsf[w*16+g]);
590                             } else if (i >= depth && dists[w*16+g] < euplims[w*16+g]) {
591                                 break;
592                             }
593                             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
594                                 int b;
595                                 float sqenergy;
596                                 dist += quantize_band_cost(s, coefs + w2*128,
597                                                         scaled + w2*128,
598                                                         sce->ics.swb_sizes[g],
599                                                         sce->sf_idx[w*16+g]-1,
600                                                         cb,
601                                                         1.0f,
602                                                         INFINITY,
603                                                         &b, &sqenergy,
604                                                         0);
605                                 bits += b;
606                                 qenergy += sqenergy;
607                             }
608                             sce->sf_idx[w*16+g]--;
609                             dists[w*16+g] = dist - bits;
610                             qenergies[w*16+g] = qenergy;
611                             if (mb && (sce->sf_idx[w*16+g] < (minrdsf+minrdsfboost) || (
612                                     (dists[w*16+g] < FFMIN(uplmax*uplims[w*16+g], euplims[w*16+g]))
613                                     && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
614                                 ) )) {
615                                 break;
616                             }
617                         }
618                     } else if (tbits > toofewbits && sce->sf_idx[w*16+g] < maxscaler
619                             && (dists[w*16+g] < FFMIN(euplims[w*16+g], uplims[w*16+g]))
620                             && (fabsf(qenergies[w*16+g]-energies[w*16+g]) < euplims[w*16+g])
621                         ) {
622                         /** Um... over target. Save bits for more important stuff. */
623                         for (i = 0; i < depth; ++i) {
624                             int cb, bits;
625                             float dist, qenergy;
626                             cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]+1);
627                             if (cb > 0) {
628                                 dist = qenergy = 0.f;
629                                 bits = 0;
630                                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
631                                     int b;
632                                     float sqenergy;
633                                     dist += quantize_band_cost(s, coefs + w2*128,
634                                                             scaled + w2*128,
635                                                             sce->ics.swb_sizes[g],
636                                                             sce->sf_idx[w*16+g]+1,
637                                                             cb,
638                                                             1.0f,
639                                                             INFINITY,
640                                                             &b, &sqenergy,
641                                                             0);
642                                     bits += b;
643                                     qenergy += sqenergy;
644                                 }
645                                 dist -= bits;
646                                 if (dist < FFMIN(euplims[w*16+g], uplims[w*16+g])) {
647                                     sce->sf_idx[w*16+g]++;
648                                     dists[w*16+g] = dist;
649                                     qenergies[w*16+g] = qenergy;
650                                 } else {
651                                     break;
652                                 }
653                             } else {
654                                 maxsf[w*16+g] = FFMIN(sce->sf_idx[w*16+g], maxsf[w*16+g]);
655                                 break;
656                             }
657                         }
658                     }
659                 }
660                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minrdsf, minscaler + SCALE_MAX_DIFF);
661                 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], SCALE_MAX_POS - SCALE_DIV_512);
662                 if (sce->sf_idx[w*16+g] != prevsc)
663                     fflag = 1;
664                 nminscaler = FFMIN(nminscaler, sce->sf_idx[w*16+g]);
665                 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
666                 start += sce->ics.swb_sizes[g];
667             }
668         }
669         if (nminscaler < minscaler) {
670             /** Drecreased some scalers below minscaler. Must re-clamp. */
671             minscaler = nminscaler;
672             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
673                 for (g = 0; g < sce->ics.num_swb; g++) {
674                     sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
675                     sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
676                 }
677             }
678         }
679         its++;
680     } while (fflag && its < maxits);
681
682     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
683         /** Make sure proper codebooks are set */
684         for (g = start = 0; g < sce->ics.num_swb; start += sce->ics.swb_sizes[g++]) {
685             if (!sce->zeroes[w*16+g]) {
686                 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
687                 if (sce->band_type[w*16+g] <= 0) {
688                     sce->zeroes[w*16+g] = 1;
689                     sce->band_type[w*16+g] = 0;
690                 }
691             } else {
692                 sce->band_type[w*16+g] = 0;
693             }
694         }
695     }
696 }
697
698 #endif /* AVCODEC_AACCODER_TWOLOOP_H */