]> git.sesse.net Git - ffmpeg/blob - libavcodec/aaccoder.c
avcodec/ass: fix doxygen typo
[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 "libavutil/libm.h" // brought forward to work around cygwin header breakage
34
35 #include <float.h>
36 #include "libavutil/mathematics.h"
37 #include "avcodec.h"
38 #include "put_bits.h"
39 #include "aac.h"
40 #include "aacenc.h"
41 #include "aactab.h"
42 #include "aacenctab.h"
43 #include "aacenc_utils.h"
44 #include "aacenc_quantization.h"
45 #include "aac_tablegen_decl.h"
46
47 #include "aacenc_is.h"
48 #include "aacenc_tns.h"
49 #include "aacenc_pred.h"
50
51 #include "libavcodec/aaccoder_twoloop.h"
52
53 /** Frequency in Hz for lower limit of noise substitution **/
54 #define NOISE_LOW_LIMIT 4000
55
56 /* Parameter of f(x) = a*(lambda/100), defines the maximum fourier spread
57  * beyond which no PNS is used (since the SFBs contain tone rather than noise) */
58 #define NOISE_SPREAD_THRESHOLD 0.5073f
59
60 /* Parameter of f(x) = a*(100/lambda), defines how much PNS is allowed to
61  * replace low energy non zero bands */
62 #define NOISE_LAMBDA_REPLACE 1.948f
63
64 #include "libavcodec/aaccoder_trellis.h"
65
66 /**
67  * structure used in optimal codebook search
68  */
69 typedef struct BandCodingPath {
70     int prev_idx; ///< pointer to the previous path point
71     float cost;   ///< path cost
72     int run;
73 } BandCodingPath;
74
75 /**
76  * Encode band info for single window group bands.
77  */
78 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
79                                      int win, int group_len, const float lambda)
80 {
81     BandCodingPath path[120][CB_TOT_ALL];
82     int w, swb, cb, start, size;
83     int i, j;
84     const int max_sfb  = sce->ics.max_sfb;
85     const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
86     const int run_esc  = (1 << run_bits) - 1;
87     int idx, ppos, count;
88     int stackrun[120], stackcb[120], stack_len;
89     float next_minrd = INFINITY;
90     int next_mincb = 0;
91
92     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
93     start = win*128;
94     for (cb = 0; cb < CB_TOT_ALL; cb++) {
95         path[0][cb].cost     = 0.0f;
96         path[0][cb].prev_idx = -1;
97         path[0][cb].run      = 0;
98     }
99     for (swb = 0; swb < max_sfb; swb++) {
100         size = sce->ics.swb_sizes[swb];
101         if (sce->zeroes[win*16 + swb]) {
102             for (cb = 0; cb < CB_TOT_ALL; cb++) {
103                 path[swb+1][cb].prev_idx = cb;
104                 path[swb+1][cb].cost     = path[swb][cb].cost;
105                 path[swb+1][cb].run      = path[swb][cb].run + 1;
106             }
107         } else {
108             float minrd = next_minrd;
109             int mincb = next_mincb;
110             next_minrd = INFINITY;
111             next_mincb = 0;
112             for (cb = 0; cb < CB_TOT_ALL; cb++) {
113                 float cost_stay_here, cost_get_here;
114                 float rd = 0.0f;
115                 if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] ||
116                     cb  < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) {
117                     path[swb+1][cb].prev_idx = -1;
118                     path[swb+1][cb].cost     = INFINITY;
119                     path[swb+1][cb].run      = path[swb][cb].run + 1;
120                     continue;
121                 }
122                 for (w = 0; w < group_len; w++) {
123                     FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
124                     rd += quantize_band_cost(s, &sce->coeffs[start + w*128],
125                                              &s->scoefs[start + w*128], size,
126                                              sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
127                                              lambda / band->threshold, INFINITY, NULL, 0);
128                 }
129                 cost_stay_here = path[swb][cb].cost + rd;
130                 cost_get_here  = minrd              + rd + run_bits + 4;
131                 if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
132                     != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
133                     cost_stay_here += run_bits;
134                 if (cost_get_here < cost_stay_here) {
135                     path[swb+1][cb].prev_idx = mincb;
136                     path[swb+1][cb].cost     = cost_get_here;
137                     path[swb+1][cb].run      = 1;
138                 } else {
139                     path[swb+1][cb].prev_idx = cb;
140                     path[swb+1][cb].cost     = cost_stay_here;
141                     path[swb+1][cb].run      = path[swb][cb].run + 1;
142                 }
143                 if (path[swb+1][cb].cost < next_minrd) {
144                     next_minrd = path[swb+1][cb].cost;
145                     next_mincb = cb;
146                 }
147             }
148         }
149         start += sce->ics.swb_sizes[swb];
150     }
151
152     //convert resulting path from backward-linked list
153     stack_len = 0;
154     idx       = 0;
155     for (cb = 1; cb < CB_TOT_ALL; cb++)
156         if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
157             idx = cb;
158     ppos = max_sfb;
159     while (ppos > 0) {
160         av_assert1(idx >= 0);
161         cb = idx;
162         stackrun[stack_len] = path[ppos][cb].run;
163         stackcb [stack_len] = cb;
164         idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
165         ppos -= path[ppos][cb].run;
166         stack_len++;
167     }
168     //perform actual band info encoding
169     start = 0;
170     for (i = stack_len - 1; i >= 0; i--) {
171         cb = aac_cb_out_map[stackcb[i]];
172         put_bits(&s->pb, 4, cb);
173         count = stackrun[i];
174         memset(sce->zeroes + win*16 + start, !cb, count);
175         //XXX: memset when band_type is also uint8_t
176         for (j = 0; j < count; j++) {
177             sce->band_type[win*16 + start] = cb;
178             start++;
179         }
180         while (count >= run_esc) {
181             put_bits(&s->pb, run_bits, run_esc);
182             count -= run_esc;
183         }
184         put_bits(&s->pb, run_bits, count);
185     }
186 }
187
188
189 typedef struct TrellisPath {
190     float cost;
191     int prev;
192 } TrellisPath;
193
194 #define TRELLIS_STAGES 121
195 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
196
197 static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
198 {
199     int w, g, start = 0;
200     int minscaler_n = sce->sf_idx[0], minscaler_i = sce->sf_idx[0];
201     int bands = 0;
202
203     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
204         start = 0;
205         for (g = 0;  g < sce->ics.num_swb; g++) {
206             if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
207                 sce->sf_idx[w*16+g] = av_clip(roundf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
208                 minscaler_i = FFMIN(minscaler_i, sce->sf_idx[w*16+g]);
209                 bands++;
210             } else if (sce->band_type[w*16+g] == NOISE_BT) {
211                 sce->sf_idx[w*16+g] = av_clip(3+ceilf(log2f(sce->pns_ener[w*16+g])*2), -100, 155);
212                 minscaler_n = FFMIN(minscaler_n, sce->sf_idx[w*16+g]);
213                 bands++;
214             }
215             start += sce->ics.swb_sizes[g];
216         }
217     }
218
219     if (!bands)
220         return;
221
222     /* Clip the scalefactor indices */
223     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
224         for (g = 0;  g < sce->ics.num_swb; g++) {
225             if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
226                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_i, minscaler_i + SCALE_MAX_DIFF);
227             } else if (sce->band_type[w*16+g] == NOISE_BT) {
228                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
229             }
230         }
231     }
232 }
233
234 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
235                                        SingleChannelElement *sce,
236                                        const float lambda)
237 {
238     int q, w, w2, g, start = 0;
239     int i, j;
240     int idx;
241     TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
242     int bandaddr[TRELLIS_STAGES];
243     int minq;
244     float mincost;
245     float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
246     int q0, q1, qcnt = 0;
247
248     for (i = 0; i < 1024; i++) {
249         float t = fabsf(sce->coeffs[i]);
250         if (t > 0.0f) {
251             q0f = FFMIN(q0f, t);
252             q1f = FFMAX(q1f, t);
253             qnrgf += t*t;
254             qcnt++;
255         }
256     }
257
258     if (!qcnt) {
259         memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
260         memset(sce->zeroes, 1, sizeof(sce->zeroes));
261         return;
262     }
263
264     //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
265     q0 = coef2minsf(q0f);
266     //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
267     q1 = coef2maxsf(q1f);
268     if (q1 - q0 > 60) {
269         int q0low  = q0;
270         int q1high = q1;
271         //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
272         int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
273         q1 = qnrg + 30;
274         q0 = qnrg - 30;
275         if (q0 < q0low) {
276             q1 += q0low - q0;
277             q0  = q0low;
278         } else if (q1 > q1high) {
279             q0 -= q1 - q1high;
280             q1  = q1high;
281         }
282     }
283
284     for (i = 0; i < TRELLIS_STATES; i++) {
285         paths[0][i].cost    = 0.0f;
286         paths[0][i].prev    = -1;
287     }
288     for (j = 1; j < TRELLIS_STAGES; j++) {
289         for (i = 0; i < TRELLIS_STATES; i++) {
290             paths[j][i].cost    = INFINITY;
291             paths[j][i].prev    = -2;
292         }
293     }
294     idx = 1;
295     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
296     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
297         start = w*128;
298         for (g = 0; g < sce->ics.num_swb; g++) {
299             const float *coefs = &sce->coeffs[start];
300             float qmin, qmax;
301             int nz = 0;
302
303             bandaddr[idx] = w * 16 + g;
304             qmin = INT_MAX;
305             qmax = 0.0f;
306             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
307                 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
308                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
309                     sce->zeroes[(w+w2)*16+g] = 1;
310                     continue;
311                 }
312                 sce->zeroes[(w+w2)*16+g] = 0;
313                 nz = 1;
314                 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
315                     float t = fabsf(coefs[w2*128+i]);
316                     if (t > 0.0f)
317                         qmin = FFMIN(qmin, t);
318                     qmax = FFMAX(qmax, t);
319                 }
320             }
321             if (nz) {
322                 int minscale, maxscale;
323                 float minrd = INFINITY;
324                 float maxval;
325                 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
326                 minscale = coef2minsf(qmin);
327                 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
328                 maxscale = coef2maxsf(qmax);
329                 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
330                 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
331                 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
332                 for (q = minscale; q < maxscale; q++) {
333                     float dist = 0;
334                     int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
335                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
336                         FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
337                         dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
338                                                    q + q0, cb, lambda / band->threshold, INFINITY, NULL, 0);
339                     }
340                     minrd = FFMIN(minrd, dist);
341
342                     for (i = 0; i < q1 - q0; i++) {
343                         float cost;
344                         cost = paths[idx - 1][i].cost + dist
345                                + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
346                         if (cost < paths[idx][q].cost) {
347                             paths[idx][q].cost    = cost;
348                             paths[idx][q].prev    = i;
349                         }
350                     }
351                 }
352             } else {
353                 for (q = 0; q < q1 - q0; q++) {
354                     paths[idx][q].cost = paths[idx - 1][q].cost + 1;
355                     paths[idx][q].prev = q;
356                 }
357             }
358             sce->zeroes[w*16+g] = !nz;
359             start += sce->ics.swb_sizes[g];
360             idx++;
361         }
362     }
363     idx--;
364     mincost = paths[idx][0].cost;
365     minq    = 0;
366     for (i = 1; i < TRELLIS_STATES; i++) {
367         if (paths[idx][i].cost < mincost) {
368             mincost = paths[idx][i].cost;
369             minq = i;
370         }
371     }
372     while (idx) {
373         sce->sf_idx[bandaddr[idx]] = minq + q0;
374         minq = paths[idx][minq].prev;
375         idx--;
376     }
377     //set the same quantizers inside window groups
378     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
379         for (g = 0;  g < sce->ics.num_swb; g++)
380             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
381                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
382 }
383
384
385 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
386                                        SingleChannelElement *sce,
387                                        const float lambda)
388 {
389     int start = 0, i, w, w2, g;
390     float uplim[128], maxq[128];
391     int minq, maxsf;
392     float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
393     int last = 0, lastband = 0, curband = 0;
394     float avg_energy = 0.0;
395     if (sce->ics.num_windows == 1) {
396         start = 0;
397         for (i = 0; i < 1024; i++) {
398             if (i - start >= sce->ics.swb_sizes[curband]) {
399                 start += sce->ics.swb_sizes[curband];
400                 curband++;
401             }
402             if (sce->coeffs[i]) {
403                 avg_energy += sce->coeffs[i] * sce->coeffs[i];
404                 last = i;
405                 lastband = curband;
406             }
407         }
408     } else {
409         for (w = 0; w < 8; w++) {
410             const float *coeffs = &sce->coeffs[w*128];
411             curband = start = 0;
412             for (i = 0; i < 128; i++) {
413                 if (i - start >= sce->ics.swb_sizes[curband]) {
414                     start += sce->ics.swb_sizes[curband];
415                     curband++;
416                 }
417                 if (coeffs[i]) {
418                     avg_energy += coeffs[i] * coeffs[i];
419                     last = FFMAX(last, i);
420                     lastband = FFMAX(lastband, curband);
421                 }
422             }
423         }
424     }
425     last++;
426     avg_energy /= last;
427     if (avg_energy == 0.0f) {
428         for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
429             sce->sf_idx[i] = SCALE_ONE_POS;
430         return;
431     }
432     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
433         start = w*128;
434         for (g = 0; g < sce->ics.num_swb; g++) {
435             float *coefs   = &sce->coeffs[start];
436             const int size = sce->ics.swb_sizes[g];
437             int start2 = start, end2 = start + size, peakpos = start;
438             float maxval = -1, thr = 0.0f, t;
439             maxq[w*16+g] = 0.0f;
440             if (g > lastband) {
441                 maxq[w*16+g] = 0.0f;
442                 start += size;
443                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
444                     memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
445                 continue;
446             }
447             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
448                 for (i = 0; i < size; i++) {
449                     float t = coefs[w2*128+i]*coefs[w2*128+i];
450                     maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
451                     thr += t;
452                     if (sce->ics.num_windows == 1 && maxval < t) {
453                         maxval  = t;
454                         peakpos = start+i;
455                     }
456                 }
457             }
458             if (sce->ics.num_windows == 1) {
459                 start2 = FFMAX(peakpos - 2, start2);
460                 end2   = FFMIN(peakpos + 3, end2);
461             } else {
462                 start2 -= start;
463                 end2   -= start;
464             }
465             start += size;
466             thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
467             t   = 1.0 - (1.0 * start2 / last);
468             uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
469         }
470     }
471     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
472     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
473     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
474         start = w*128;
475         for (g = 0;  g < sce->ics.num_swb; g++) {
476             const float *coefs  = &sce->coeffs[start];
477             const float *scaled = &s->scoefs[start];
478             const int size      = sce->ics.swb_sizes[g];
479             int scf, prev_scf, step;
480             int min_scf = -1, max_scf = 256;
481             float curdiff;
482             if (maxq[w*16+g] < 21.544) {
483                 sce->zeroes[w*16+g] = 1;
484                 start += size;
485                 continue;
486             }
487             sce->zeroes[w*16+g] = 0;
488             scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
489             for (;;) {
490                 float dist = 0.0f;
491                 int quant_max;
492
493                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
494                     int b;
495                     dist += quantize_band_cost(s, coefs + w2*128,
496                                                scaled + w2*128,
497                                                sce->ics.swb_sizes[g],
498                                                scf,
499                                                ESC_BT,
500                                                lambda,
501                                                INFINITY,
502                                                &b,
503                                                0);
504                     dist -= b;
505                 }
506                 dist *= 1.0f / 512.0f / lambda;
507                 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512], ROUND_STANDARD);
508                 if (quant_max >= 8191) { // too much, return to the previous quantizer
509                     sce->sf_idx[w*16+g] = prev_scf;
510                     break;
511                 }
512                 prev_scf = scf;
513                 curdiff = fabsf(dist - uplim[w*16+g]);
514                 if (curdiff <= 1.0f)
515                     step = 0;
516                 else
517                     step = log2f(curdiff);
518                 if (dist > uplim[w*16+g])
519                     step = -step;
520                 scf += step;
521                 scf = av_clip_uint8(scf);
522                 step = scf - prev_scf;
523                 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
524                     sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
525                     break;
526                 }
527                 if (step > 0)
528                     min_scf = prev_scf;
529                 else
530                     max_scf = prev_scf;
531             }
532             start += size;
533         }
534     }
535     minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
536     for (i = 1; i < 128; i++) {
537         if (!sce->sf_idx[i])
538             sce->sf_idx[i] = sce->sf_idx[i-1];
539         else
540             minq = FFMIN(minq, sce->sf_idx[i]);
541     }
542     if (minq == INT_MAX)
543         minq = 0;
544     minq = FFMIN(minq, SCALE_MAX_POS);
545     maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
546     for (i = 126; i >= 0; i--) {
547         if (!sce->sf_idx[i])
548             sce->sf_idx[i] = sce->sf_idx[i+1];
549         sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
550     }
551 }
552
553 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
554                                        SingleChannelElement *sce,
555                                        const float lambda)
556 {
557     int i, w, w2, g;
558     int minq = 255;
559
560     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
561     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
562         for (g = 0; g < sce->ics.num_swb; g++) {
563             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
564                 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
565                 if (band->energy <= band->threshold) {
566                     sce->sf_idx[(w+w2)*16+g] = 218;
567                     sce->zeroes[(w+w2)*16+g] = 1;
568                 } else {
569                     sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
570                     sce->zeroes[(w+w2)*16+g] = 0;
571                 }
572                 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
573             }
574         }
575     }
576     for (i = 0; i < 128; i++) {
577         sce->sf_idx[i] = 140;
578         //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
579     }
580     //set the same quantizers inside window groups
581     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
582         for (g = 0;  g < sce->ics.num_swb; g++)
583             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
584                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
585 }
586
587 static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
588 {
589     FFPsyBand *band;
590     int w, g, w2, i;
591     float *PNS = &s->scoefs[0*128], *PNS34 = &s->scoefs[1*128];
592     float *NOR34 = &s->scoefs[3*128];
593     const float lambda = s->lambda;
594     const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
595     const float thr_mult = NOISE_LAMBDA_REPLACE*(100.0f/lambda);
596     const float spread_threshold = NOISE_SPREAD_THRESHOLD*FFMAX(0.5f, lambda/100.f);
597
598     memcpy(sce->band_alt, sce->band_type, sizeof(sce->band_type));
599     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
600         int wstart = w*128;
601         for (g = 0;  g < sce->ics.num_swb; g++) {
602             int noise_sfi;
603             float dist1 = 0.0f, dist2 = 0.0f, noise_amp;
604             float pns_energy = 0.0f, pns_tgt_energy, energy_ratio, dist_thresh;
605             float sfb_energy = 0.0f, threshold = 0.0f, spread = 0.0f;
606             const int start = wstart+sce->ics.swb_offset[g];
607             const float freq = (start-wstart)*freq_mult;
608             const float freq_boost = FFMAX(0.88f*freq/NOISE_LOW_LIMIT, 1.0f);
609             if (freq < NOISE_LOW_LIMIT || avctx->cutoff && freq >= avctx->cutoff)
610                 continue;
611             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
612                 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
613                 sfb_energy += band->energy;
614                 spread     += band->spread;
615                 threshold  += band->threshold;
616             }
617
618             /* Ramps down at ~8000Hz and loosens the dist threshold */
619             dist_thresh = FFMIN(2.5f*NOISE_LOW_LIMIT/freq, 2.5f);
620
621             /* zero and energy close to threshold usually means hole avoidance,
622              * we do want to remain avoiding holes with PNS
623              */
624             if (((sce->zeroes[w*16+g] || !sce->band_alt[w*16+g]) && sfb_energy < threshold*sqrtf(1.5f/freq_boost)) || spread < spread_threshold ||
625                 (sce->band_alt[w*16+g] && sfb_energy > threshold*thr_mult*freq_boost)) {
626                 sce->pns_ener[w*16+g] = sfb_energy;
627                 continue;
628             }
629
630             pns_tgt_energy = sfb_energy*spread*spread/sce->ics.group_len[w];
631             noise_sfi = av_clip(roundf(log2f(pns_tgt_energy)*2), -100, 155); /* Quantize */
632             noise_amp = -ff_aac_pow2sf_tab[noise_sfi + POW_SF2_ZERO];    /* Dequantize */
633             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
634                 float band_energy, scale, pns_senergy;
635                 const int start_c = (w+w2)*128+sce->ics.swb_offset[g];
636                 band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
637                 for (i = 0; i < sce->ics.swb_sizes[g]; i++)
638                     PNS[i] = s->random_state = lcg_random(s->random_state);
639                 band_energy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
640                 scale = noise_amp/sqrtf(band_energy);
641                 s->fdsp->vector_fmul_scalar(PNS, PNS, scale, sce->ics.swb_sizes[g]);
642                 pns_senergy = s->fdsp->scalarproduct_float(PNS, PNS, sce->ics.swb_sizes[g]);
643                 pns_energy += pns_senergy;
644                 abs_pow34_v(NOR34, &sce->coeffs[start_c], sce->ics.swb_sizes[g]);
645                 abs_pow34_v(PNS34, PNS, sce->ics.swb_sizes[g]);
646                 dist1 += quantize_band_cost(s, &sce->coeffs[start_c],
647                                             NOR34,
648                                             sce->ics.swb_sizes[g],
649                                             sce->sf_idx[(w+w2)*16+g],
650                                             sce->band_alt[(w+w2)*16+g],
651                                             lambda/band->threshold, INFINITY, NULL, 0);
652                 /* Estimate rd on average as 9 bits for CB and sf + spread energy * lambda/thr */
653                 dist2 += 9+band->energy/(band->spread*band->spread)*lambda/band->threshold;
654             }
655             energy_ratio = pns_tgt_energy/pns_energy; /* Compensates for quantization error */
656             sce->pns_ener[w*16+g] = energy_ratio*pns_tgt_energy;
657             if (energy_ratio > 0.85f && energy_ratio < 1.25f && (sce->zeroes[w*16+g] || !sce->band_alt[w*16+g] || dist2*dist_thresh < dist1)) {
658                 sce->band_type[w*16+g] = NOISE_BT;
659                 sce->zeroes[w*16+g] = 0;
660             }
661         }
662     }
663 }
664
665 static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
666 {
667     int start = 0, i, w, w2, g;
668     float M[128], S[128];
669     float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
670     const float lambda = s->lambda;
671     SingleChannelElement *sce0 = &cpe->ch[0];
672     SingleChannelElement *sce1 = &cpe->ch[1];
673     if (!cpe->common_window)
674         return;
675     for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
676         start = 0;
677         for (g = 0;  g < sce0->ics.num_swb; g++) {
678             if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
679                 float dist1 = 0.0f, dist2 = 0.0f;
680                 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
681                     FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
682                     FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
683                     float minthr = FFMIN(band0->threshold, band1->threshold);
684                     float maxthr = FFMAX(band0->threshold, band1->threshold);
685                     for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
686                         M[i] = (sce0->coeffs[start+(w+w2)*128+i]
687                               + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
688                         S[i] =  M[i]
689                               - sce1->coeffs[start+(w+w2)*128+i];
690                     }
691                     abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
692                     abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
693                     abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
694                     abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
695                     dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
696                                                 L34,
697                                                 sce0->ics.swb_sizes[g],
698                                                 sce0->sf_idx[(w+w2)*16+g],
699                                                 sce0->band_type[(w+w2)*16+g],
700                                                 lambda / band0->threshold, INFINITY, NULL, 0);
701                     dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
702                                                 R34,
703                                                 sce1->ics.swb_sizes[g],
704                                                 sce1->sf_idx[(w+w2)*16+g],
705                                                 sce1->band_type[(w+w2)*16+g],
706                                                 lambda / band1->threshold, INFINITY, NULL, 0);
707                     dist2 += quantize_band_cost(s, M,
708                                                 M34,
709                                                 sce0->ics.swb_sizes[g],
710                                                 sce0->sf_idx[(w+w2)*16+g],
711                                                 sce0->band_type[(w+w2)*16+g],
712                                                 lambda / maxthr, INFINITY, NULL, 0);
713                     dist2 += quantize_band_cost(s, S,
714                                                 S34,
715                                                 sce1->ics.swb_sizes[g],
716                                                 sce1->sf_idx[(w+w2)*16+g],
717                                                 sce1->band_type[(w+w2)*16+g],
718                                                 lambda / minthr, INFINITY, NULL, 0);
719                 }
720                 cpe->ms_mask[w*16+g] = dist2 < dist1;
721             }
722             start += sce0->ics.swb_sizes[g];
723         }
724     }
725 }
726
727 AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
728     [AAC_CODER_FAAC] = {
729         search_for_quantizers_faac,
730         encode_window_bands_info,
731         quantize_and_encode_band,
732         ff_aac_encode_tns_info,
733         ff_aac_encode_main_pred,
734         ff_aac_adjust_common_prediction,
735         ff_aac_apply_main_pred,
736         ff_aac_apply_tns,
737         set_special_band_scalefactors,
738         search_for_pns,
739         ff_aac_search_for_tns,
740         search_for_ms,
741         ff_aac_search_for_is,
742         ff_aac_search_for_pred,
743     },
744     [AAC_CODER_ANMR] = {
745         search_for_quantizers_anmr,
746         encode_window_bands_info,
747         quantize_and_encode_band,
748         ff_aac_encode_tns_info,
749         ff_aac_encode_main_pred,
750         ff_aac_adjust_common_prediction,
751         ff_aac_apply_main_pred,
752         ff_aac_apply_tns,
753         set_special_band_scalefactors,
754         search_for_pns,
755         ff_aac_search_for_tns,
756         search_for_ms,
757         ff_aac_search_for_is,
758         ff_aac_search_for_pred,
759     },
760     [AAC_CODER_TWOLOOP] = {
761         search_for_quantizers_twoloop,
762         codebook_trellis_rate,
763         quantize_and_encode_band,
764         ff_aac_encode_tns_info,
765         ff_aac_encode_main_pred,
766         ff_aac_adjust_common_prediction,
767         ff_aac_apply_main_pred,
768         ff_aac_apply_tns,
769         set_special_band_scalefactors,
770         search_for_pns,
771         ff_aac_search_for_tns,
772         search_for_ms,
773         ff_aac_search_for_is,
774         ff_aac_search_for_pred,
775     },
776     [AAC_CODER_FAST] = {
777         search_for_quantizers_fast,
778         encode_window_bands_info,
779         quantize_and_encode_band,
780         ff_aac_encode_tns_info,
781         ff_aac_encode_main_pred,
782         ff_aac_adjust_common_prediction,
783         ff_aac_apply_main_pred,
784         ff_aac_apply_tns,
785         set_special_band_scalefactors,
786         search_for_pns,
787         ff_aac_search_for_tns,
788         search_for_ms,
789         ff_aac_search_for_is,
790         ff_aac_search_for_pred,
791     },
792 };