cosmetics, rename loop to frame_available
[ffmpeg.git] / libavcodec / aaccoder.c
1 /*
2  * AAC coefficients encoder
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 coefficients encoder
25  */
26
27 /***********************************
28  *              TODOs:
29  * speedup quantizer selection
30  * add sane pulse detection
31  ***********************************/
32
33 #include "avcodec.h"
34 #include "put_bits.h"
35 #include "aac.h"
36 #include "aacenc.h"
37 #include "aactab.h"
38
39 /** bits needed to code codebook run value for long windows */
40 static const uint8_t run_value_bits_long[64] = {
41      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
42      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
43     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
44     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
45 };
46
47 /** bits needed to code codebook run value for short windows */
48 static const uint8_t run_value_bits_short[16] = {
49     3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
50 };
51
52 static const uint8_t *run_value_bits[2] = {
53     run_value_bits_long, run_value_bits_short
54 };
55
56
57 /**
58  * Quantize one coefficient.
59  * @return absolute value of the quantized coefficient
60  * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
61  */
62 static av_always_inline int quant(float coef, const float Q)
63 {
64     float a = coef * Q;
65     return sqrtf(a * sqrtf(a)) + 0.4054;
66 }
67
68 static void quantize_bands(int (*out)[2], const float *in, const float *scaled,
69                            int size, float Q34, int is_signed, int maxval)
70 {
71     int i;
72     double qc;
73     for (i = 0; i < size; i++) {
74         qc = scaled[i] * Q34;
75         out[i][0] = (int)FFMIN(qc,          (double)maxval);
76         out[i][1] = (int)FFMIN(qc + 0.4054, (double)maxval);
77         if (is_signed && in[i] < 0.0f) {
78             out[i][0] = -out[i][0];
79             out[i][1] = -out[i][1];
80         }
81     }
82 }
83
84 static void abs_pow34_v(float *out, const float *in, const int size)
85 {
86 #ifndef USE_REALLY_FULL_SEARCH
87     int i;
88     for (i = 0; i < size; i++) {
89         float a = fabsf(in[i]);
90         out[i] = sqrtf(a * sqrtf(a));
91     }
92 #endif /* USE_REALLY_FULL_SEARCH */
93 }
94
95 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
96 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
97
98 /**
99  * Calculate rate distortion cost for quantizing with given codebook
100  *
101  * @return quantization distortion
102  */
103 static float quantize_and_encode_band_cost(struct AACEncContext *s,
104                                 PutBitContext *pb, const float *in,
105                                 const float *scaled, int size, int scale_idx,
106                                 int cb, const float lambda, const float uplim,
107                                 int *bits)
108 {
109     const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
110     const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
111     const float CLIPPED_ESCAPE = 165140.0f*IQ;
112     int i, j, k;
113     float cost = 0;
114     const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
115     int resbits = 0;
116 #ifndef USE_REALLY_FULL_SEARCH
117     const float  Q34 = sqrtf(Q * sqrtf(Q));
118     const int range  = aac_cb_range[cb];
119     const int maxval = aac_cb_maxval[cb];
120     int offs[4];
121 #endif /* USE_REALLY_FULL_SEARCH */
122
123     if (!cb) {
124         for (i = 0; i < size; i++)
125             cost += in[i]*in[i];
126         if (bits)
127             *bits = 0;
128         return cost * lambda;
129     }
130 #ifndef USE_REALLY_FULL_SEARCH
131     offs[0] = 1;
132     for (i = 1; i < dim; i++)
133         offs[i] = offs[i-1]*range;
134     if (!scaled) {
135         abs_pow34_v(s->scoefs, in, size);
136         scaled = s->scoefs;
137     }
138     quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
139 #endif /* USE_REALLY_FULL_SEARCH */
140     for (i = 0; i < size; i += dim) {
141         float mincost;
142         int minidx  = 0;
143         int minbits = 0;
144         const float *vec;
145 #ifndef USE_REALLY_FULL_SEARCH
146         int (*quants)[2] = &s->qcoefs[i];
147         mincost = 0.0f;
148         for (j = 0; j < dim; j++)
149             mincost += in[i+j]*in[i+j];
150         minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
151         minbits = ff_aac_spectral_bits[cb-1][minidx];
152         mincost = mincost * lambda + minbits;
153         for (j = 0; j < (1<<dim); j++) {
154             float rd = 0.0f;
155             int curbits;
156             int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
157             int same   = 0;
158             for (k = 0; k < dim; k++) {
159                 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
160                     same = 1;
161                     break;
162                 }
163             }
164             if (same)
165                 continue;
166             for (k = 0; k < dim; k++)
167                 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
168             curbits =  ff_aac_spectral_bits[cb-1][curidx];
169             vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
170 #else
171         mincost = INFINITY;
172         vec = ff_aac_codebook_vectors[cb-1];
173         for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
174             float rd = 0.0f;
175             int curbits = ff_aac_spectral_bits[cb-1][j];
176             int curidx = j;
177 #endif /* USE_REALLY_FULL_SEARCH */
178             if (IS_CODEBOOK_UNSIGNED(cb)) {
179                 for (k = 0; k < dim; k++) {
180                     float t = fabsf(in[i+k]);
181                     float di;
182                     if (vec[k] == 64.0f) { //FIXME: slow
183                         //do not code with escape sequence small values
184                         if (t < 39.0f*IQ) {
185                             rd = INFINITY;
186                             break;
187                         }
188                         if (t >= CLIPPED_ESCAPE) {
189                             di = t - CLIPPED_ESCAPE;
190                             curbits += 21;
191                         } else {
192                             int c = av_clip(quant(t, Q), 0, 8191);
193                             di = t - c*cbrtf(c)*IQ;
194                             curbits += av_log2(c)*2 - 4 + 1;
195                         }
196                     } else {
197                         di = t - vec[k]*IQ;
198                     }
199                     if (vec[k] != 0.0f)
200                         curbits++;
201                     rd += di*di;
202                 }
203             } else {
204                 for (k = 0; k < dim; k++) {
205                     float di = in[i+k] - vec[k]*IQ;
206                     rd += di*di;
207                 }
208             }
209             rd = rd * lambda + curbits;
210             if (rd < mincost) {
211                 mincost = rd;
212                 minidx  = curidx;
213                 minbits = curbits;
214             }
215         }
216         cost    += mincost;
217         resbits += minbits;
218         if (cost >= uplim)
219             return uplim;
220         if (pb) {
221         put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
222         if (IS_CODEBOOK_UNSIGNED(cb))
223             for (j = 0; j < dim; j++)
224                 if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
225                     put_bits(pb, 1, in[i+j] < 0.0f);
226         if (cb == ESC_BT) {
227             for (j = 0; j < 2; j++) {
228                 if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
229                     int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
230                     int len = av_log2(coef);
231
232                     put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
233                     put_bits(pb, len, coef & ((1 << len) - 1));
234                 }
235             }
236         }
237         }
238     }
239
240     if (bits)
241         *bits = resbits;
242     return cost;
243 }
244 static float quantize_band_cost(struct AACEncContext *s, const float *in,
245                                 const float *scaled, int size, int scale_idx,
246                                 int cb, const float lambda, const float uplim,
247                                 int *bits)
248 {
249     return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
250                                          cb, lambda, uplim, bits);
251 }
252
253 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
254                                      const float *in, int size, int scale_idx,
255                                      int cb, const float lambda)
256 {
257     quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
258                                   INFINITY, NULL);
259 }
260
261 /**
262  * structure used in optimal codebook search
263  */
264 typedef struct BandCodingPath {
265     int prev_idx; ///< pointer to the previous path point
266     float cost;   ///< path cost
267     int run;
268 } BandCodingPath;
269
270 /**
271  * Encode band info for single window group bands.
272  */
273 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
274                                      int win, int group_len, const float lambda)
275 {
276     BandCodingPath path[120][12];
277     int w, swb, cb, start, start2, size;
278     int i, j;
279     const int max_sfb  = sce->ics.max_sfb;
280     const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
281     const int run_esc  = (1 << run_bits) - 1;
282     int idx, ppos, count;
283     int stackrun[120], stackcb[120], stack_len;
284     float next_minrd = INFINITY;
285     int next_mincb = 0;
286
287     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
288     start = win*128;
289     for (cb = 0; cb < 12; cb++) {
290         path[0][cb].cost     = 0.0f;
291         path[0][cb].prev_idx = -1;
292         path[0][cb].run      = 0;
293     }
294     for (swb = 0; swb < max_sfb; swb++) {
295         start2 = start;
296         size = sce->ics.swb_sizes[swb];
297         if (sce->zeroes[win*16 + swb]) {
298             for (cb = 0; cb < 12; cb++) {
299                 path[swb+1][cb].prev_idx = cb;
300                 path[swb+1][cb].cost     = path[swb][cb].cost;
301                 path[swb+1][cb].run      = path[swb][cb].run + 1;
302             }
303         } else {
304             float minrd = next_minrd;
305             int mincb = next_mincb;
306             next_minrd = INFINITY;
307             next_mincb = 0;
308             for (cb = 0; cb < 12; cb++) {
309                 float cost_stay_here, cost_get_here;
310                 float rd = 0.0f;
311                 for (w = 0; w < group_len; w++) {
312                     FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
313                     rd += quantize_band_cost(s, sce->coeffs + start + w*128,
314                                              s->scoefs + start + w*128, size,
315                                              sce->sf_idx[(win+w)*16+swb], cb,
316                                              lambda / band->threshold, INFINITY, NULL);
317                 }
318                 cost_stay_here = path[swb][cb].cost + rd;
319                 cost_get_here  = minrd              + rd + run_bits + 4;
320                 if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
321                     != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
322                     cost_stay_here += run_bits;
323                 if (cost_get_here < cost_stay_here) {
324                     path[swb+1][cb].prev_idx = mincb;
325                     path[swb+1][cb].cost     = cost_get_here;
326                     path[swb+1][cb].run      = 1;
327                 } else {
328                     path[swb+1][cb].prev_idx = cb;
329                     path[swb+1][cb].cost     = cost_stay_here;
330                     path[swb+1][cb].run      = path[swb][cb].run + 1;
331                 }
332                 if (path[swb+1][cb].cost < next_minrd) {
333                     next_minrd = path[swb+1][cb].cost;
334                     next_mincb = cb;
335                 }
336             }
337         }
338         start += sce->ics.swb_sizes[swb];
339     }
340
341     //convert resulting path from backward-linked list
342     stack_len = 0;
343     idx       = 0;
344     for (cb = 1; cb < 12; cb++)
345         if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
346             idx = cb;
347     ppos = max_sfb;
348     while (ppos > 0) {
349         cb = idx;
350         stackrun[stack_len] = path[ppos][cb].run;
351         stackcb [stack_len] = cb;
352         idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
353         ppos -= path[ppos][cb].run;
354         stack_len++;
355     }
356     //perform actual band info encoding
357     start = 0;
358     for (i = stack_len - 1; i >= 0; i--) {
359         put_bits(&s->pb, 4, stackcb[i]);
360         count = stackrun[i];
361         memset(sce->zeroes + win*16 + start, !stackcb[i], count);
362         //XXX: memset when band_type is also uint8_t
363         for (j = 0; j < count; j++) {
364             sce->band_type[win*16 + start] =  stackcb[i];
365             start++;
366         }
367         while (count >= run_esc) {
368             put_bits(&s->pb, run_bits, run_esc);
369             count -= run_esc;
370         }
371         put_bits(&s->pb, run_bits, count);
372     }
373 }
374
375 typedef struct TrellisPath {
376     float cost;
377     int prev;
378     int min_val;
379     int max_val;
380 } TrellisPath;
381
382 #define TRELLIS_STAGES 121
383 #define TRELLIS_STATES 256
384
385 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
386                                        SingleChannelElement *sce,
387                                        const float lambda)
388 {
389     int q, w, w2, g, start = 0;
390     int i, j;
391     int idx;
392     TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
393     int bandaddr[TRELLIS_STAGES];
394     int minq;
395     float mincost;
396
397     for (i = 0; i < TRELLIS_STATES; i++) {
398         paths[0][i].cost    = 0.0f;
399         paths[0][i].prev    = -1;
400         paths[0][i].min_val = i;
401         paths[0][i].max_val = i;
402     }
403     for (j = 1; j < TRELLIS_STAGES; j++) {
404         for (i = 0; i < TRELLIS_STATES; i++) {
405             paths[j][i].cost    = INFINITY;
406             paths[j][i].prev    = -2;
407             paths[j][i].min_val = INT_MAX;
408             paths[j][i].max_val = 0;
409         }
410     }
411     idx = 1;
412     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
413     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
414         start = w*128;
415         for (g = 0; g < sce->ics.num_swb; g++) {
416             const float *coefs = sce->coeffs + start;
417             float qmin, qmax;
418             int nz = 0;
419
420             bandaddr[idx] = w * 16 + g;
421             qmin = INT_MAX;
422             qmax = 0.0f;
423             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
424                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
425                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
426                     sce->zeroes[(w+w2)*16+g] = 1;
427                     continue;
428                 }
429                 sce->zeroes[(w+w2)*16+g] = 0;
430                 nz = 1;
431                 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
432                     float t = fabsf(coefs[w2*128+i]);
433                     if (t > 0.0f)
434                         qmin = FFMIN(qmin, t);
435                     qmax = FFMAX(qmax, t);
436                 }
437             }
438             if (nz) {
439                 int minscale, maxscale;
440                 float minrd = INFINITY;
441                 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
442                 minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
443                 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
444                 maxscale = av_clip_uint8(log2(qmax)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
445                 for (q = minscale; q < maxscale; q++) {
446                     float dists[12], dist;
447                     memset(dists, 0, sizeof(dists));
448                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
449                         FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
450                         int cb;
451                         for (cb = 0; cb <= ESC_BT; cb++)
452                             dists[cb] += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
453                                                             q, cb, lambda / band->threshold, INFINITY, NULL);
454                     }
455                     dist = dists[0];
456                     for (i = 1; i <= ESC_BT; i++)
457                         dist = FFMIN(dist, dists[i]);
458                     minrd = FFMIN(minrd, dist);
459
460                     for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
461                         float cost;
462                         int minv, maxv;
463                         if (isinf(paths[idx - 1][i].cost))
464                             continue;
465                         cost = paths[idx - 1][i].cost + dist
466                                + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
467                         minv = FFMIN(paths[idx - 1][i].min_val, q);
468                         maxv = FFMAX(paths[idx - 1][i].max_val, q);
469                         if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
470                             paths[idx][q].cost    = cost;
471                             paths[idx][q].prev    = i;
472                             paths[idx][q].min_val = minv;
473                             paths[idx][q].max_val = maxv;
474                         }
475                     }
476                 }
477             } else {
478                 for (q = 0; q < TRELLIS_STATES; q++) {
479                     if (!isinf(paths[idx - 1][q].cost)) {
480                         paths[idx][q].cost = paths[idx - 1][q].cost + 1;
481                         paths[idx][q].prev = q;
482                         paths[idx][q].min_val = FFMIN(paths[idx - 1][q].min_val, q);
483                         paths[idx][q].max_val = FFMAX(paths[idx - 1][q].max_val, q);
484                         continue;
485                     }
486                     for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
487                         float cost;
488                         int minv, maxv;
489                         if (isinf(paths[idx - 1][i].cost))
490                             continue;
491                         cost = paths[idx - 1][i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
492                         minv = FFMIN(paths[idx - 1][i].min_val, q);
493                         maxv = FFMAX(paths[idx - 1][i].max_val, q);
494                         if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
495                             paths[idx][q].cost    = cost;
496                             paths[idx][q].prev    = i;
497                             paths[idx][q].min_val = minv;
498                             paths[idx][q].max_val = maxv;
499                         }
500                     }
501                 }
502             }
503             sce->zeroes[w*16+g] = !nz;
504             start += sce->ics.swb_sizes[g];
505             idx++;
506         }
507     }
508     idx--;
509     mincost = paths[idx][0].cost;
510     minq    = 0;
511     for (i = 1; i < TRELLIS_STATES; i++) {
512         if (paths[idx][i].cost < mincost) {
513             mincost = paths[idx][i].cost;
514             minq = i;
515         }
516     }
517     while (idx) {
518         sce->sf_idx[bandaddr[idx]] = minq;
519         minq = paths[idx][minq].prev;
520         idx--;
521     }
522     //set the same quantizers inside window groups
523     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
524         for (g = 0;  g < sce->ics.num_swb; g++)
525             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
526                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
527 }
528
529 /**
530  * two-loop quantizers search taken from ISO 13818-7 Appendix C
531  */
532 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
533                                           AACEncContext *s,
534                                           SingleChannelElement *sce,
535                                           const float lambda)
536 {
537     int start = 0, i, w, w2, g;
538     int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
539     float dists[128], uplims[128];
540     int fflag, minscaler;
541     int its  = 0;
542     int allz = 0;
543     float minthr = INFINITY;
544
545     //XXX: some heuristic to determine initial quantizers will reduce search time
546     memset(dists, 0, sizeof(dists));
547     //determine zero bands and upper limits
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             int nz = 0;
551             float uplim = 0.0f;
552             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
553                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
554                 uplim += band->threshold;
555                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
556                     sce->zeroes[(w+w2)*16+g] = 1;
557                     continue;
558                 }
559                 nz = 1;
560             }
561             uplims[w*16+g] = uplim *512;
562             sce->zeroes[w*16+g] = !nz;
563             if (nz)
564                 minthr = FFMIN(minthr, uplim);
565             allz = FFMAX(allz, nz);
566         }
567     }
568     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
569         for (g = 0;  g < sce->ics.num_swb; g++) {
570             if (sce->zeroes[w*16+g]) {
571                 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
572                 continue;
573             }
574             sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
575         }
576     }
577
578     if (!allz)
579         return;
580     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
581     //perform two-loop search
582     //outer loop - improve quality
583     do {
584         int tbits, qstep;
585         minscaler = sce->sf_idx[0];
586         //inner loop - quantize spectrum to fit into given number of bits
587         qstep = its ? 1 : 32;
588         do {
589             int prev = -1;
590             tbits = 0;
591             fflag = 0;
592             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
593                 start = w*128;
594                 for (g = 0;  g < sce->ics.num_swb; g++) {
595                     const float *coefs = sce->coeffs + start;
596                     const float *scaled = s->scoefs + start;
597                     int bits = 0;
598                     int cb;
599                     float mindist = INFINITY;
600                     int minbits = 0;
601
602                     if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
603                         start += sce->ics.swb_sizes[g];
604                         continue;
605                     }
606                     minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
607                     for (cb = 0; cb <= ESC_BT; cb++) {
608                         float dist = 0.0f;
609                         int bb = 0;
610                         for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
611                             int b;
612                             dist += quantize_band_cost(s, coefs + w2*128,
613                                                        scaled + w2*128,
614                                                        sce->ics.swb_sizes[g],
615                                                        sce->sf_idx[w*16+g],
616                                                        cb,
617                                                        lambda,
618                                                        INFINITY,
619                                                        &b);
620                             bb += b;
621                         }
622                         if (dist < mindist) {
623                             mindist = dist;
624                             minbits = bb;
625                         }
626                     }
627                     dists[w*16+g] = (mindist - minbits) / lambda;
628                     bits = minbits;
629                     if (prev != -1) {
630                         bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
631                     }
632                     tbits += bits;
633                     start += sce->ics.swb_sizes[g];
634                     prev = sce->sf_idx[w*16+g];
635                 }
636             }
637             if (tbits > destbits) {
638                 for (i = 0; i < 128; i++)
639                     if (sce->sf_idx[i] < 218 - qstep)
640                         sce->sf_idx[i] += qstep;
641             } else {
642                 for (i = 0; i < 128; i++)
643                     if (sce->sf_idx[i] > 60 - qstep)
644                         sce->sf_idx[i] -= qstep;
645             }
646             qstep >>= 1;
647             if (!qstep && tbits > destbits*1.02)
648                 qstep = 1;
649             if (sce->sf_idx[0] >= 217)
650                 break;
651         } while (qstep);
652
653         fflag = 0;
654         minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
655         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
656             start = w*128;
657             for (g = 0; g < sce->ics.num_swb; g++) {
658                 int prevsc = sce->sf_idx[w*16+g];
659                 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
660                     sce->sf_idx[w*16+g]--;
661                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
662                 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
663                 if (sce->sf_idx[w*16+g] != prevsc)
664                     fflag = 1;
665             }
666         }
667         its++;
668     } while (fflag && its < 10);
669 }
670
671 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
672                                        SingleChannelElement *sce,
673                                        const float lambda)
674 {
675     int start = 0, i, w, w2, g;
676     float uplim[128], maxq[128];
677     int minq, maxsf;
678     float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
679     int last = 0, lastband = 0, curband = 0;
680     float avg_energy = 0.0;
681     if (sce->ics.num_windows == 1) {
682         start = 0;
683         for (i = 0; i < 1024; i++) {
684             if (i - start >= sce->ics.swb_sizes[curband]) {
685                 start += sce->ics.swb_sizes[curband];
686                 curband++;
687             }
688             if (sce->coeffs[i]) {
689                 avg_energy += sce->coeffs[i] * sce->coeffs[i];
690                 last = i;
691                 lastband = curband;
692             }
693         }
694     } else {
695         for (w = 0; w < 8; w++) {
696             const float *coeffs = sce->coeffs + w*128;
697             start = 0;
698             for (i = 0; i < 128; i++) {
699                 if (i - start >= sce->ics.swb_sizes[curband]) {
700                     start += sce->ics.swb_sizes[curband];
701                     curband++;
702                 }
703                 if (coeffs[i]) {
704                     avg_energy += coeffs[i] * coeffs[i];
705                     last = FFMAX(last, i);
706                     lastband = FFMAX(lastband, curband);
707                 }
708             }
709         }
710     }
711     last++;
712     avg_energy /= last;
713     if (avg_energy == 0.0f) {
714         for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
715             sce->sf_idx[i] = SCALE_ONE_POS;
716         return;
717     }
718     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
719         start = w*128;
720         for (g = 0; g < sce->ics.num_swb; g++) {
721             float *coefs   = sce->coeffs + start;
722             const int size = sce->ics.swb_sizes[g];
723             int start2 = start, end2 = start + size, peakpos = start;
724             float maxval = -1, thr = 0.0f, t;
725             maxq[w*16+g] = 0.0f;
726             if (g > lastband) {
727                 maxq[w*16+g] = 0.0f;
728                 start += size;
729                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
730                     memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
731                 continue;
732             }
733             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
734                 for (i = 0; i < size; i++) {
735                     float t = coefs[w2*128+i]*coefs[w2*128+i];
736                     maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
737                     thr += t;
738                     if (sce->ics.num_windows == 1 && maxval < t) {
739                         maxval  = t;
740                         peakpos = start+i;
741                     }
742                 }
743             }
744             if (sce->ics.num_windows == 1) {
745                 start2 = FFMAX(peakpos - 2, start2);
746                 end2   = FFMIN(peakpos + 3, end2);
747             } else {
748                 start2 -= start;
749                 end2   -= start;
750             }
751             start += size;
752             thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
753             t   = 1.0 - (1.0 * start2 / last);
754             uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
755         }
756     }
757     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
758     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
759     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
760         start = w*128;
761         for (g = 0;  g < sce->ics.num_swb; g++) {
762             const float *coefs  = sce->coeffs + start;
763             const float *scaled = s->scoefs   + start;
764             const int size      = sce->ics.swb_sizes[g];
765             int scf, prev_scf, step;
766             int min_scf = -1, max_scf = 256;
767             float curdiff;
768             if (maxq[w*16+g] < 21.544) {
769                 sce->zeroes[w*16+g] = 1;
770                 start += size;
771                 continue;
772             }
773             sce->zeroes[w*16+g] = 0;
774             scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
775             step = 16;
776             for (;;) {
777                 float dist = 0.0f;
778                 int quant_max;
779
780                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
781                     int b;
782                     dist += quantize_band_cost(s, coefs + w2*128,
783                                                scaled + w2*128,
784                                                sce->ics.swb_sizes[g],
785                                                scf,
786                                                ESC_BT,
787                                                lambda,
788                                                INFINITY,
789                                                &b);
790                     dist -= b;
791                 }
792                 dist *= 1.0f / 512.0f / lambda;
793                 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
794                 if (quant_max >= 8191) { // too much, return to the previous quantizer
795                     sce->sf_idx[w*16+g] = prev_scf;
796                     break;
797                 }
798                 prev_scf = scf;
799                 curdiff = fabsf(dist - uplim[w*16+g]);
800                 if (curdiff <= 1.0f)
801                     step = 0;
802                 else
803                     step = log2(curdiff);
804                 if (dist > uplim[w*16+g])
805                     step = -step;
806                 scf += step;
807                 scf = av_clip_uint8(scf);
808                 step = scf - prev_scf;
809                 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
810                     sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
811                     break;
812                 }
813                 if (step > 0)
814                     min_scf = prev_scf;
815                 else
816                     max_scf = prev_scf;
817             }
818             start += size;
819         }
820     }
821     minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
822     for (i = 1; i < 128; i++) {
823         if (!sce->sf_idx[i])
824             sce->sf_idx[i] = sce->sf_idx[i-1];
825         else
826             minq = FFMIN(minq, sce->sf_idx[i]);
827     }
828     if (minq == INT_MAX)
829         minq = 0;
830     minq = FFMIN(minq, SCALE_MAX_POS);
831     maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
832     for (i = 126; i >= 0; i--) {
833         if (!sce->sf_idx[i])
834             sce->sf_idx[i] = sce->sf_idx[i+1];
835         sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
836     }
837 }
838
839 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
840                                        SingleChannelElement *sce,
841                                        const float lambda)
842 {
843     int start = 0, i, w, w2, g;
844     int minq = 255;
845
846     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
847     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
848         start = w*128;
849         for (g = 0; g < sce->ics.num_swb; g++) {
850             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
851                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
852                 if (band->energy <= band->threshold) {
853                     sce->sf_idx[(w+w2)*16+g] = 218;
854                     sce->zeroes[(w+w2)*16+g] = 1;
855                 } else {
856                     sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
857                     sce->zeroes[(w+w2)*16+g] = 0;
858                 }
859                 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
860             }
861         }
862     }
863     for (i = 0; i < 128; i++) {
864         sce->sf_idx[i] = 140;
865         //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
866     }
867     //set the same quantizers inside window groups
868     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
869         for (g = 0;  g < sce->ics.num_swb; g++)
870             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
871                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
872 }
873
874 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
875                           const float lambda)
876 {
877     int start = 0, i, w, w2, g;
878     float M[128], S[128];
879     float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
880     SingleChannelElement *sce0 = &cpe->ch[0];
881     SingleChannelElement *sce1 = &cpe->ch[1];
882     if (!cpe->common_window)
883         return;
884     for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
885         for (g = 0;  g < sce0->ics.num_swb; g++) {
886             if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
887                 float dist1 = 0.0f, dist2 = 0.0f;
888                 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
889                     FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
890                     FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
891                     float minthr = FFMIN(band0->threshold, band1->threshold);
892                     float maxthr = FFMAX(band0->threshold, band1->threshold);
893                     for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
894                         M[i] = (sce0->coeffs[start+w2*128+i]
895                               + sce1->coeffs[start+w2*128+i]) * 0.5;
896                         S[i] =  sce0->coeffs[start+w2*128+i]
897                               - sce1->coeffs[start+w2*128+i];
898                     }
899                     abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
900                     abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
901                     abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
902                     abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
903                     dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
904                                                 L34,
905                                                 sce0->ics.swb_sizes[g],
906                                                 sce0->sf_idx[(w+w2)*16+g],
907                                                 sce0->band_type[(w+w2)*16+g],
908                                                 lambda / band0->threshold, INFINITY, NULL);
909                     dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
910                                                 R34,
911                                                 sce1->ics.swb_sizes[g],
912                                                 sce1->sf_idx[(w+w2)*16+g],
913                                                 sce1->band_type[(w+w2)*16+g],
914                                                 lambda / band1->threshold, INFINITY, NULL);
915                     dist2 += quantize_band_cost(s, M,
916                                                 M34,
917                                                 sce0->ics.swb_sizes[g],
918                                                 sce0->sf_idx[(w+w2)*16+g],
919                                                 sce0->band_type[(w+w2)*16+g],
920                                                 lambda / maxthr, INFINITY, NULL);
921                     dist2 += quantize_band_cost(s, S,
922                                                 S34,
923                                                 sce1->ics.swb_sizes[g],
924                                                 sce1->sf_idx[(w+w2)*16+g],
925                                                 sce1->band_type[(w+w2)*16+g],
926                                                 lambda / minthr, INFINITY, NULL);
927                 }
928                 cpe->ms_mask[w*16+g] = dist2 < dist1;
929             }
930             start += sce0->ics.swb_sizes[g];
931         }
932     }
933 }
934
935 AACCoefficientsEncoder ff_aac_coders[] = {
936     {
937         search_for_quantizers_faac,
938         encode_window_bands_info,
939         quantize_and_encode_band,
940         search_for_ms,
941     },
942     {
943         search_for_quantizers_anmr,
944         encode_window_bands_info,
945         quantize_and_encode_band,
946         search_for_ms,
947     },
948     {
949         search_for_quantizers_twoloop,
950         encode_window_bands_info,
951         quantize_and_encode_band,
952         search_for_ms,
953     },
954     {
955         search_for_quantizers_fast,
956         encode_window_bands_info,
957         quantize_and_encode_band,
958         search_for_ms,
959     },
960 };