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