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