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