Remove useless costly inf checks from the trellis scalefactor search.
[ffmpeg.git] / libavcodec / aaccoder.c
1 /*
2  * AAC coefficients encoder
3  * Copyright (C) 2008-2009 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * AAC coefficients encoder
25  */
26
27 /***********************************
28  *              TODOs:
29  * speedup quantizer selection
30  * add sane pulse detection
31  ***********************************/
32
33 #include <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                         cost = paths[idx - 1][i].cost + dist
583                                + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
584                         if (cost < paths[idx][q].cost) {
585                             paths[idx][q].cost    = cost;
586                             paths[idx][q].prev    = i;
587                         }
588                     }
589                 }
590             } else {
591                 for (q = 0; q < q1 - q0; q++) {
592                         paths[idx][q].cost = paths[idx - 1][q].cost + 1;
593                         paths[idx][q].prev = q;
594                 }
595             }
596             sce->zeroes[w*16+g] = !nz;
597             start += sce->ics.swb_sizes[g];
598             idx++;
599         }
600     }
601     idx--;
602     mincost = paths[idx][0].cost;
603     minq    = 0;
604     for (i = 1; i < TRELLIS_STATES; i++) {
605         if (paths[idx][i].cost < mincost) {
606             mincost = paths[idx][i].cost;
607             minq = i;
608         }
609     }
610     while (idx) {
611         sce->sf_idx[bandaddr[idx]] = minq + q0;
612         minq = paths[idx][minq].prev;
613         idx--;
614     }
615     //set the same quantizers inside window groups
616     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
617         for (g = 0;  g < sce->ics.num_swb; g++)
618             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
619                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
620 }
621
622 /**
623  * two-loop quantizers search taken from ISO 13818-7 Appendix C
624  */
625 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
626                                           AACEncContext *s,
627                                           SingleChannelElement *sce,
628                                           const float lambda)
629 {
630     int start = 0, i, w, w2, g;
631     int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
632     float dists[128], uplims[128];
633     int fflag, minscaler;
634     int its  = 0;
635     int allz = 0;
636     float minthr = INFINITY;
637
638     //XXX: some heuristic to determine initial quantizers will reduce search time
639     memset(dists, 0, sizeof(dists));
640     //determine zero bands and upper limits
641     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
642         for (g = 0;  g < sce->ics.num_swb; g++) {
643             int nz = 0;
644             float uplim = 0.0f;
645             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
646                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
647                 uplim += band->threshold;
648                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
649                     sce->zeroes[(w+w2)*16+g] = 1;
650                     continue;
651                 }
652                 nz = 1;
653             }
654             uplims[w*16+g] = uplim *512;
655             sce->zeroes[w*16+g] = !nz;
656             if (nz)
657                 minthr = FFMIN(minthr, uplim);
658             allz = FFMAX(allz, nz);
659         }
660     }
661     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
662         for (g = 0;  g < sce->ics.num_swb; g++) {
663             if (sce->zeroes[w*16+g]) {
664                 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
665                 continue;
666             }
667             sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
668         }
669     }
670
671     if (!allz)
672         return;
673     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
674     //perform two-loop search
675     //outer loop - improve quality
676     do {
677         int tbits, qstep;
678         minscaler = sce->sf_idx[0];
679         //inner loop - quantize spectrum to fit into given number of bits
680         qstep = its ? 1 : 32;
681         do {
682             int prev = -1;
683             tbits = 0;
684             fflag = 0;
685             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
686                 start = w*128;
687                 for (g = 0;  g < sce->ics.num_swb; g++) {
688                     const float *coefs = sce->coeffs + start;
689                     const float *scaled = s->scoefs + start;
690                     int bits = 0;
691                     int cb;
692                     float mindist = INFINITY;
693                     int minbits = 0;
694
695                     if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
696                         start += sce->ics.swb_sizes[g];
697                         continue;
698                     }
699                     minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
700                     {
701                         float dist = 0.0f;
702                         int bb = 0;
703                         cb = find_min_book(sce->sf_idx[w*16+g], sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
704                         sce->band_type[w*16+g] = cb;
705                         for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
706                             int b;
707                             dist += quantize_band_cost(s, coefs + w2*128,
708                                                        scaled + w2*128,
709                                                        sce->ics.swb_sizes[g],
710                                                        sce->sf_idx[w*16+g],
711                                                        cb,
712                                                        lambda,
713                                                        INFINITY,
714                                                        &b);
715                             bb += b;
716                         }
717                             mindist = dist;
718                             minbits = bb;
719                     }
720                     dists[w*16+g] = (mindist - minbits) / lambda;
721                     bits = minbits;
722                     if (prev != -1) {
723                         bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
724                     }
725                     tbits += bits;
726                     start += sce->ics.swb_sizes[g];
727                     prev = sce->sf_idx[w*16+g];
728                 }
729             }
730             if (tbits > destbits) {
731                 for (i = 0; i < 128; i++)
732                     if (sce->sf_idx[i] < 218 - qstep)
733                         sce->sf_idx[i] += qstep;
734             } else {
735                 for (i = 0; i < 128; i++)
736                     if (sce->sf_idx[i] > 60 - qstep)
737                         sce->sf_idx[i] -= qstep;
738             }
739             qstep >>= 1;
740             if (!qstep && tbits > destbits*1.02)
741                 qstep = 1;
742             if (sce->sf_idx[0] >= 217)
743                 break;
744         } while (qstep);
745
746         fflag = 0;
747         minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
748         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
749             start = w*128;
750             for (g = 0; g < sce->ics.num_swb; g++) {
751                 int prevsc = sce->sf_idx[w*16+g];
752                 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
753                     sce->sf_idx[w*16+g]--;
754                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
755                 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
756                 if (sce->sf_idx[w*16+g] != prevsc)
757                     fflag = 1;
758             }
759         }
760         its++;
761     } while (fflag && its < 10);
762 }
763
764 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
765                                        SingleChannelElement *sce,
766                                        const float lambda)
767 {
768     int start = 0, i, w, w2, g;
769     float uplim[128], maxq[128];
770     int minq, maxsf;
771     float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
772     int last = 0, lastband = 0, curband = 0;
773     float avg_energy = 0.0;
774     if (sce->ics.num_windows == 1) {
775         start = 0;
776         for (i = 0; i < 1024; i++) {
777             if (i - start >= sce->ics.swb_sizes[curband]) {
778                 start += sce->ics.swb_sizes[curband];
779                 curband++;
780             }
781             if (sce->coeffs[i]) {
782                 avg_energy += sce->coeffs[i] * sce->coeffs[i];
783                 last = i;
784                 lastband = curband;
785             }
786         }
787     } else {
788         for (w = 0; w < 8; w++) {
789             const float *coeffs = sce->coeffs + w*128;
790             start = 0;
791             for (i = 0; i < 128; i++) {
792                 if (i - start >= sce->ics.swb_sizes[curband]) {
793                     start += sce->ics.swb_sizes[curband];
794                     curband++;
795                 }
796                 if (coeffs[i]) {
797                     avg_energy += coeffs[i] * coeffs[i];
798                     last = FFMAX(last, i);
799                     lastband = FFMAX(lastband, curband);
800                 }
801             }
802         }
803     }
804     last++;
805     avg_energy /= last;
806     if (avg_energy == 0.0f) {
807         for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
808             sce->sf_idx[i] = SCALE_ONE_POS;
809         return;
810     }
811     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
812         start = w*128;
813         for (g = 0; g < sce->ics.num_swb; g++) {
814             float *coefs   = sce->coeffs + start;
815             const int size = sce->ics.swb_sizes[g];
816             int start2 = start, end2 = start + size, peakpos = start;
817             float maxval = -1, thr = 0.0f, t;
818             maxq[w*16+g] = 0.0f;
819             if (g > lastband) {
820                 maxq[w*16+g] = 0.0f;
821                 start += size;
822                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
823                     memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
824                 continue;
825             }
826             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
827                 for (i = 0; i < size; i++) {
828                     float t = coefs[w2*128+i]*coefs[w2*128+i];
829                     maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
830                     thr += t;
831                     if (sce->ics.num_windows == 1 && maxval < t) {
832                         maxval  = t;
833                         peakpos = start+i;
834                     }
835                 }
836             }
837             if (sce->ics.num_windows == 1) {
838                 start2 = FFMAX(peakpos - 2, start2);
839                 end2   = FFMIN(peakpos + 3, end2);
840             } else {
841                 start2 -= start;
842                 end2   -= start;
843             }
844             start += size;
845             thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
846             t   = 1.0 - (1.0 * start2 / last);
847             uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
848         }
849     }
850     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
851     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
852     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
853         start = w*128;
854         for (g = 0;  g < sce->ics.num_swb; g++) {
855             const float *coefs  = sce->coeffs + start;
856             const float *scaled = s->scoefs   + start;
857             const int size      = sce->ics.swb_sizes[g];
858             int scf, prev_scf, step;
859             int min_scf = -1, max_scf = 256;
860             float curdiff;
861             if (maxq[w*16+g] < 21.544) {
862                 sce->zeroes[w*16+g] = 1;
863                 start += size;
864                 continue;
865             }
866             sce->zeroes[w*16+g] = 0;
867             scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
868             step = 16;
869             for (;;) {
870                 float dist = 0.0f;
871                 int quant_max;
872
873                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
874                     int b;
875                     dist += quantize_band_cost(s, coefs + w2*128,
876                                                scaled + w2*128,
877                                                sce->ics.swb_sizes[g],
878                                                scf,
879                                                ESC_BT,
880                                                lambda,
881                                                INFINITY,
882                                                &b);
883                     dist -= b;
884                 }
885                 dist *= 1.0f / 512.0f / lambda;
886                 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
887                 if (quant_max >= 8191) { // too much, return to the previous quantizer
888                     sce->sf_idx[w*16+g] = prev_scf;
889                     break;
890                 }
891                 prev_scf = scf;
892                 curdiff = fabsf(dist - uplim[w*16+g]);
893                 if (curdiff <= 1.0f)
894                     step = 0;
895                 else
896                     step = log2(curdiff);
897                 if (dist > uplim[w*16+g])
898                     step = -step;
899                 scf += step;
900                 scf = av_clip_uint8(scf);
901                 step = scf - prev_scf;
902                 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
903                     sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
904                     break;
905                 }
906                 if (step > 0)
907                     min_scf = prev_scf;
908                 else
909                     max_scf = prev_scf;
910             }
911             start += size;
912         }
913     }
914     minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
915     for (i = 1; i < 128; i++) {
916         if (!sce->sf_idx[i])
917             sce->sf_idx[i] = sce->sf_idx[i-1];
918         else
919             minq = FFMIN(minq, sce->sf_idx[i]);
920     }
921     if (minq == INT_MAX)
922         minq = 0;
923     minq = FFMIN(minq, SCALE_MAX_POS);
924     maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
925     for (i = 126; i >= 0; i--) {
926         if (!sce->sf_idx[i])
927             sce->sf_idx[i] = sce->sf_idx[i+1];
928         sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
929     }
930 }
931
932 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
933                                        SingleChannelElement *sce,
934                                        const float lambda)
935 {
936     int start = 0, i, w, w2, g;
937     int minq = 255;
938
939     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
940     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
941         start = w*128;
942         for (g = 0; g < sce->ics.num_swb; g++) {
943             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
944                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
945                 if (band->energy <= band->threshold) {
946                     sce->sf_idx[(w+w2)*16+g] = 218;
947                     sce->zeroes[(w+w2)*16+g] = 1;
948                 } else {
949                     sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
950                     sce->zeroes[(w+w2)*16+g] = 0;
951                 }
952                 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
953             }
954         }
955     }
956     for (i = 0; i < 128; i++) {
957         sce->sf_idx[i] = 140;
958         //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
959     }
960     //set the same quantizers inside window groups
961     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
962         for (g = 0;  g < sce->ics.num_swb; g++)
963             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
964                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
965 }
966
967 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
968                           const float lambda)
969 {
970     int start = 0, i, w, w2, g;
971     float M[128], S[128];
972     float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
973     SingleChannelElement *sce0 = &cpe->ch[0];
974     SingleChannelElement *sce1 = &cpe->ch[1];
975     if (!cpe->common_window)
976         return;
977     for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
978         for (g = 0;  g < sce0->ics.num_swb; g++) {
979             if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
980                 float dist1 = 0.0f, dist2 = 0.0f;
981                 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
982                     FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
983                     FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
984                     float minthr = FFMIN(band0->threshold, band1->threshold);
985                     float maxthr = FFMAX(band0->threshold, band1->threshold);
986                     for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
987                         M[i] = (sce0->coeffs[start+w2*128+i]
988                               + sce1->coeffs[start+w2*128+i]) * 0.5;
989                         S[i] =  sce0->coeffs[start+w2*128+i]
990                               - sce1->coeffs[start+w2*128+i];
991                     }
992                     abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
993                     abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
994                     abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
995                     abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
996                     dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
997                                                 L34,
998                                                 sce0->ics.swb_sizes[g],
999                                                 sce0->sf_idx[(w+w2)*16+g],
1000                                                 sce0->band_type[(w+w2)*16+g],
1001                                                 lambda / band0->threshold, INFINITY, NULL);
1002                     dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1003                                                 R34,
1004                                                 sce1->ics.swb_sizes[g],
1005                                                 sce1->sf_idx[(w+w2)*16+g],
1006                                                 sce1->band_type[(w+w2)*16+g],
1007                                                 lambda / band1->threshold, INFINITY, NULL);
1008                     dist2 += quantize_band_cost(s, M,
1009                                                 M34,
1010                                                 sce0->ics.swb_sizes[g],
1011                                                 sce0->sf_idx[(w+w2)*16+g],
1012                                                 sce0->band_type[(w+w2)*16+g],
1013                                                 lambda / maxthr, INFINITY, NULL);
1014                     dist2 += quantize_band_cost(s, S,
1015                                                 S34,
1016                                                 sce1->ics.swb_sizes[g],
1017                                                 sce1->sf_idx[(w+w2)*16+g],
1018                                                 sce1->band_type[(w+w2)*16+g],
1019                                                 lambda / minthr, INFINITY, NULL);
1020                 }
1021                 cpe->ms_mask[w*16+g] = dist2 < dist1;
1022             }
1023             start += sce0->ics.swb_sizes[g];
1024         }
1025     }
1026 }
1027
1028 AACCoefficientsEncoder ff_aac_coders[] = {
1029     {
1030         search_for_quantizers_faac,
1031         encode_window_bands_info,
1032         quantize_and_encode_band,
1033         search_for_ms,
1034     },
1035     {
1036         search_for_quantizers_anmr,
1037         encode_window_bands_info,
1038         quantize_and_encode_band,
1039         search_for_ms,
1040     },
1041     {
1042         search_for_quantizers_twoloop,
1043         codebook_trellis_rate,
1044         quantize_and_encode_band,
1045         search_for_ms,
1046     },
1047     {
1048         search_for_quantizers_fast,
1049         encode_window_bands_info,
1050         quantize_and_encode_band,
1051         search_for_ms,
1052     },
1053 };