2 * AAC coefficients encoder
3 * Copyright (C) 2008-2009 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * AAC coefficients encoder
27 /***********************************
29 * speedup quantizer selection
30 * add sane pulse detection
31 ***********************************/
33 #include "libavutil/libm.h" // brought forward to work around cygwin header breakage
36 #include "libavutil/mathematics.h"
42 #include "aacenctab.h"
43 #include "aacenc_utils.h"
44 #include "aacenc_quantization.h"
45 #include "aac_tablegen_decl.h"
47 #include "aacenc_is.h"
48 #include "aacenc_tns.h"
49 #include "aacenc_pred.h"
51 /** Frequency in Hz for lower limit of noise substitution **/
52 #define NOISE_LOW_LIMIT 4500
54 /* Energy spread threshold value below which no PNS is used, this corresponds to
55 * typically around 17Khz, after which PNS usage decays ending at 19Khz */
56 #define NOISE_SPREAD_THRESHOLD 0.5f
58 /* This constant gets divided by lambda to return ~1.65 which when multiplied
59 * by the band->threshold and compared to band->energy is the boundary between
60 * excessive PNS and little PNS usage. */
61 #define NOISE_LAMBDA_NUMERATOR 252.1f
64 * structure used in optimal codebook search
66 typedef struct BandCodingPath {
67 int prev_idx; ///< pointer to the previous path point
68 float cost; ///< path cost
73 * Encode band info for single window group bands.
75 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
76 int win, int group_len, const float lambda)
78 BandCodingPath path[120][CB_TOT_ALL];
79 int w, swb, cb, start, size;
81 const int max_sfb = sce->ics.max_sfb;
82 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
83 const int run_esc = (1 << run_bits) - 1;
85 int stackrun[120], stackcb[120], stack_len;
86 float next_minrd = INFINITY;
89 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
91 for (cb = 0; cb < CB_TOT_ALL; cb++) {
92 path[0][cb].cost = 0.0f;
93 path[0][cb].prev_idx = -1;
96 for (swb = 0; swb < max_sfb; swb++) {
97 size = sce->ics.swb_sizes[swb];
98 if (sce->zeroes[win*16 + swb]) {
99 for (cb = 0; cb < CB_TOT_ALL; cb++) {
100 path[swb+1][cb].prev_idx = cb;
101 path[swb+1][cb].cost = path[swb][cb].cost;
102 path[swb+1][cb].run = path[swb][cb].run + 1;
105 float minrd = next_minrd;
106 int mincb = next_mincb;
107 next_minrd = INFINITY;
109 for (cb = 0; cb < CB_TOT_ALL; cb++) {
110 float cost_stay_here, cost_get_here;
112 if (cb >= 12 && sce->band_type[win*16+swb] < aac_cb_out_map[cb] ||
113 cb < aac_cb_in_map[sce->band_type[win*16+swb]] && sce->band_type[win*16+swb] > aac_cb_out_map[cb]) {
114 path[swb+1][cb].prev_idx = -1;
115 path[swb+1][cb].cost = INFINITY;
116 path[swb+1][cb].run = path[swb][cb].run + 1;
119 for (w = 0; w < group_len; w++) {
120 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(win+w)*16+swb];
121 rd += quantize_band_cost(s, &sce->coeffs[start + w*128],
122 &s->scoefs[start + w*128], size,
123 sce->sf_idx[(win+w)*16+swb], aac_cb_out_map[cb],
124 lambda / band->threshold, INFINITY, NULL, 0);
126 cost_stay_here = path[swb][cb].cost + rd;
127 cost_get_here = minrd + rd + run_bits + 4;
128 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
129 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
130 cost_stay_here += run_bits;
131 if (cost_get_here < cost_stay_here) {
132 path[swb+1][cb].prev_idx = mincb;
133 path[swb+1][cb].cost = cost_get_here;
134 path[swb+1][cb].run = 1;
136 path[swb+1][cb].prev_idx = cb;
137 path[swb+1][cb].cost = cost_stay_here;
138 path[swb+1][cb].run = path[swb][cb].run + 1;
140 if (path[swb+1][cb].cost < next_minrd) {
141 next_minrd = path[swb+1][cb].cost;
146 start += sce->ics.swb_sizes[swb];
149 //convert resulting path from backward-linked list
152 for (cb = 1; cb < CB_TOT_ALL; cb++)
153 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
157 av_assert1(idx >= 0);
159 stackrun[stack_len] = path[ppos][cb].run;
160 stackcb [stack_len] = cb;
161 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
162 ppos -= path[ppos][cb].run;
165 //perform actual band info encoding
167 for (i = stack_len - 1; i >= 0; i--) {
168 cb = aac_cb_out_map[stackcb[i]];
169 put_bits(&s->pb, 4, cb);
171 memset(sce->zeroes + win*16 + start, !cb, count);
172 //XXX: memset when band_type is also uint8_t
173 for (j = 0; j < count; j++) {
174 sce->band_type[win*16 + start] = cb;
177 while (count >= run_esc) {
178 put_bits(&s->pb, run_bits, run_esc);
181 put_bits(&s->pb, run_bits, count);
185 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
186 int win, int group_len, const float lambda)
188 BandCodingPath path[120][CB_TOT_ALL];
189 int w, swb, cb, start, size;
191 const int max_sfb = sce->ics.max_sfb;
192 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
193 const int run_esc = (1 << run_bits) - 1;
194 int idx, ppos, count;
195 int stackrun[120], stackcb[120], stack_len;
196 float next_minbits = INFINITY;
199 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
201 for (cb = 0; cb < CB_TOT_ALL; cb++) {
202 path[0][cb].cost = run_bits+4;
203 path[0][cb].prev_idx = -1;
206 for (swb = 0; swb < max_sfb; swb++) {
207 size = sce->ics.swb_sizes[swb];
208 if (sce->zeroes[win*16 + swb]) {
209 float cost_stay_here = path[swb][0].cost;
210 float cost_get_here = next_minbits + run_bits + 4;
211 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
212 != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
213 cost_stay_here += run_bits;
214 if (cost_get_here < cost_stay_here) {
215 path[swb+1][0].prev_idx = next_mincb;
216 path[swb+1][0].cost = cost_get_here;
217 path[swb+1][0].run = 1;
219 path[swb+1][0].prev_idx = 0;
220 path[swb+1][0].cost = cost_stay_here;
221 path[swb+1][0].run = path[swb][0].run + 1;
223 next_minbits = path[swb+1][0].cost;
225 for (cb = 1; cb < CB_TOT_ALL; cb++) {
226 path[swb+1][cb].cost = 61450;
227 path[swb+1][cb].prev_idx = -1;
228 path[swb+1][cb].run = 0;
231 float minbits = next_minbits;
232 int mincb = next_mincb;
233 int startcb = sce->band_type[win*16+swb];
234 startcb = aac_cb_in_map[startcb];
235 next_minbits = INFINITY;
237 for (cb = 0; cb < startcb; cb++) {
238 path[swb+1][cb].cost = 61450;
239 path[swb+1][cb].prev_idx = -1;
240 path[swb+1][cb].run = 0;
242 for (cb = startcb; cb < CB_TOT_ALL; cb++) {
243 float cost_stay_here, cost_get_here;
245 if (cb >= 12 && sce->band_type[win*16+swb] != aac_cb_out_map[cb]) {
246 path[swb+1][cb].cost = 61450;
247 path[swb+1][cb].prev_idx = -1;
248 path[swb+1][cb].run = 0;
251 for (w = 0; w < group_len; w++) {
252 bits += quantize_band_cost(s, &sce->coeffs[start + w*128],
253 &s->scoefs[start + w*128], size,
254 sce->sf_idx[win*16+swb],
256 0, INFINITY, NULL, 0);
258 cost_stay_here = path[swb][cb].cost + bits;
259 cost_get_here = minbits + bits + run_bits + 4;
260 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
261 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
262 cost_stay_here += run_bits;
263 if (cost_get_here < cost_stay_here) {
264 path[swb+1][cb].prev_idx = mincb;
265 path[swb+1][cb].cost = cost_get_here;
266 path[swb+1][cb].run = 1;
268 path[swb+1][cb].prev_idx = cb;
269 path[swb+1][cb].cost = cost_stay_here;
270 path[swb+1][cb].run = path[swb][cb].run + 1;
272 if (path[swb+1][cb].cost < next_minbits) {
273 next_minbits = path[swb+1][cb].cost;
278 start += sce->ics.swb_sizes[swb];
281 //convert resulting path from backward-linked list
284 for (cb = 1; cb < CB_TOT_ALL; cb++)
285 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
289 av_assert1(idx >= 0);
291 stackrun[stack_len] = path[ppos][cb].run;
292 stackcb [stack_len] = cb;
293 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
294 ppos -= path[ppos][cb].run;
297 //perform actual band info encoding
299 for (i = stack_len - 1; i >= 0; i--) {
300 cb = aac_cb_out_map[stackcb[i]];
301 put_bits(&s->pb, 4, cb);
303 memset(sce->zeroes + win*16 + start, !cb, count);
304 //XXX: memset when band_type is also uint8_t
305 for (j = 0; j < count; j++) {
306 sce->band_type[win*16 + start] = cb;
309 while (count >= run_esc) {
310 put_bits(&s->pb, run_bits, run_esc);
313 put_bits(&s->pb, run_bits, count);
317 typedef struct TrellisPath {
322 #define TRELLIS_STAGES 121
323 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
325 static void set_special_band_scalefactors(AACEncContext *s, SingleChannelElement *sce)
328 int minscaler_n = sce->sf_idx[0], minscaler_i = sce->sf_idx[0];
331 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
333 for (g = 0; g < sce->ics.num_swb; g++) {
334 if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
335 sce->sf_idx[w*16+g] = av_clip(ceilf(log2f(sce->is_ener[w*16+g])*2), -155, 100);
336 minscaler_i = FFMIN(minscaler_i, sce->sf_idx[w*16+g]);
338 } else if (sce->band_type[w*16+g] == NOISE_BT) {
339 sce->sf_idx[w*16+g] = av_clip(4+log2f(sce->pns_ener[w*16+g])*2, -100, 155);
340 minscaler_n = FFMIN(minscaler_n, sce->sf_idx[w*16+g]);
343 start += sce->ics.swb_sizes[g];
350 /* Clip the scalefactor indices */
351 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
352 for (g = 0; g < sce->ics.num_swb; g++) {
353 if (sce->band_type[w*16+g] == INTENSITY_BT || sce->band_type[w*16+g] == INTENSITY_BT2) {
354 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_i, minscaler_i + SCALE_MAX_DIFF);
355 } else if (sce->band_type[w*16+g] == NOISE_BT) {
356 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler_n, minscaler_n + SCALE_MAX_DIFF);
362 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
363 SingleChannelElement *sce,
366 int q, w, w2, g, start = 0;
369 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
370 int bandaddr[TRELLIS_STAGES];
373 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
374 int q0, q1, qcnt = 0;
376 for (i = 0; i < 1024; i++) {
377 float t = fabsf(sce->coeffs[i]);
387 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
388 memset(sce->zeroes, 1, sizeof(sce->zeroes));
392 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
393 q0 = coef2minsf(q0f);
394 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
395 q1 = coef2maxsf(q1f);
399 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
400 int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
406 } else if (q1 > q1high) {
412 for (i = 0; i < TRELLIS_STATES; i++) {
413 paths[0][i].cost = 0.0f;
414 paths[0][i].prev = -1;
416 for (j = 1; j < TRELLIS_STAGES; j++) {
417 for (i = 0; i < TRELLIS_STATES; i++) {
418 paths[j][i].cost = INFINITY;
419 paths[j][i].prev = -2;
423 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
424 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
426 for (g = 0; g < sce->ics.num_swb; g++) {
427 const float *coefs = &sce->coeffs[start];
431 bandaddr[idx] = w * 16 + g;
434 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
435 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
436 if (band->energy <= band->threshold || band->threshold == 0.0f) {
437 sce->zeroes[(w+w2)*16+g] = 1;
440 sce->zeroes[(w+w2)*16+g] = 0;
442 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
443 float t = fabsf(coefs[w2*128+i]);
445 qmin = FFMIN(qmin, t);
446 qmax = FFMAX(qmax, t);
450 int minscale, maxscale;
451 float minrd = INFINITY;
453 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
454 minscale = coef2minsf(qmin);
455 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
456 maxscale = coef2maxsf(qmax);
457 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
458 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
459 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
460 for (q = minscale; q < maxscale; q++) {
462 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
463 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
464 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
465 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
466 q + q0, cb, lambda / band->threshold, INFINITY, NULL, 0);
468 minrd = FFMIN(minrd, dist);
470 for (i = 0; i < q1 - q0; i++) {
472 cost = paths[idx - 1][i].cost + dist
473 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
474 if (cost < paths[idx][q].cost) {
475 paths[idx][q].cost = cost;
476 paths[idx][q].prev = i;
481 for (q = 0; q < q1 - q0; q++) {
482 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
483 paths[idx][q].prev = q;
486 sce->zeroes[w*16+g] = !nz;
487 start += sce->ics.swb_sizes[g];
492 mincost = paths[idx][0].cost;
494 for (i = 1; i < TRELLIS_STATES; i++) {
495 if (paths[idx][i].cost < mincost) {
496 mincost = paths[idx][i].cost;
501 sce->sf_idx[bandaddr[idx]] = minq + q0;
502 minq = paths[idx][minq].prev;
505 //set the same quantizers inside window groups
506 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
507 for (g = 0; g < sce->ics.num_swb; g++)
508 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
509 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
513 * two-loop quantizers search taken from ISO 13818-7 Appendix C
515 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
517 SingleChannelElement *sce,
520 int start = 0, i, w, w2, g;
521 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels * (lambda / 120.f);
522 float dists[128] = { 0 }, uplims[128] = { 0 };
524 int fflag, minscaler;
527 float minthr = INFINITY;
529 // for values above this the decoder might end up in an endless loop
530 // due to always having more bits than what can be encoded.
531 destbits = FFMIN(destbits, 5800);
532 //XXX: some heuristic to determine initial quantizers will reduce search time
533 //determine zero bands and upper limits
534 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
535 for (g = 0; g < sce->ics.num_swb; g++) {
537 float uplim = 0.0f, energy = 0.0f;
538 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
539 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
540 uplim += band->threshold;
541 energy += band->energy;
542 if (band->energy <= band->threshold || band->threshold == 0.0f) {
543 sce->zeroes[(w+w2)*16+g] = 1;
548 uplims[w*16+g] = uplim *512;
549 sce->zeroes[w*16+g] = !nz;
551 minthr = FFMIN(minthr, uplim);
555 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
556 for (g = 0; g < sce->ics.num_swb; g++) {
557 if (sce->zeroes[w*16+g]) {
558 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
561 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
567 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
569 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
571 for (g = 0; g < sce->ics.num_swb; g++) {
572 const float *scaled = s->scoefs + start;
573 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
574 start += sce->ics.swb_sizes[g];
578 //perform two-loop search
579 //outer loop - improve quality
582 minscaler = sce->sf_idx[0];
583 //inner loop - quantize spectrum to fit into given number of bits
584 qstep = its ? 1 : 32;
588 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
590 for (g = 0; g < sce->ics.num_swb; g++) {
591 const float *coefs = &sce->coeffs[start];
592 const float *scaled = &s->scoefs[start];
597 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
598 start += sce->ics.swb_sizes[g];
601 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
602 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
603 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
605 dist += quantize_band_cost(s, coefs + w2*128,
607 sce->ics.swb_sizes[g],
616 dists[w*16+g] = dist - bits;
618 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
621 start += sce->ics.swb_sizes[g];
622 prev = sce->sf_idx[w*16+g];
625 if (tbits > destbits) {
626 for (i = 0; i < 128; i++)
627 if (sce->sf_idx[i] < 218 - qstep)
628 sce->sf_idx[i] += qstep;
630 for (i = 0; i < 128; i++)
631 if (sce->sf_idx[i] > 60 - qstep)
632 sce->sf_idx[i] -= qstep;
635 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
640 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
642 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
643 for (g = 0; g < sce->ics.num_swb; g++) {
644 int prevsc = sce->sf_idx[w*16+g];
645 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
646 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
647 sce->sf_idx[w*16+g]--;
648 else //Try to make sure there is some energy in every band
649 sce->sf_idx[w*16+g]-=2;
651 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
652 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
653 if (sce->sf_idx[w*16+g] != prevsc)
655 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
659 } while (fflag && its < 10);
662 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
663 SingleChannelElement *sce,
666 int start = 0, i, w, w2, g;
667 float uplim[128], maxq[128];
669 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
670 int last = 0, lastband = 0, curband = 0;
671 float avg_energy = 0.0;
672 if (sce->ics.num_windows == 1) {
674 for (i = 0; i < 1024; i++) {
675 if (i - start >= sce->ics.swb_sizes[curband]) {
676 start += sce->ics.swb_sizes[curband];
679 if (sce->coeffs[i]) {
680 avg_energy += sce->coeffs[i] * sce->coeffs[i];
686 for (w = 0; w < 8; w++) {
687 const float *coeffs = &sce->coeffs[w*128];
689 for (i = 0; i < 128; i++) {
690 if (i - start >= sce->ics.swb_sizes[curband]) {
691 start += sce->ics.swb_sizes[curband];
695 avg_energy += coeffs[i] * coeffs[i];
696 last = FFMAX(last, i);
697 lastband = FFMAX(lastband, curband);
704 if (avg_energy == 0.0f) {
705 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
706 sce->sf_idx[i] = SCALE_ONE_POS;
709 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
711 for (g = 0; g < sce->ics.num_swb; g++) {
712 float *coefs = &sce->coeffs[start];
713 const int size = sce->ics.swb_sizes[g];
714 int start2 = start, end2 = start + size, peakpos = start;
715 float maxval = -1, thr = 0.0f, t;
720 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
721 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
724 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
725 for (i = 0; i < size; i++) {
726 float t = coefs[w2*128+i]*coefs[w2*128+i];
727 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
729 if (sce->ics.num_windows == 1 && maxval < t) {
735 if (sce->ics.num_windows == 1) {
736 start2 = FFMAX(peakpos - 2, start2);
737 end2 = FFMIN(peakpos + 3, end2);
743 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
744 t = 1.0 - (1.0 * start2 / last);
745 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
748 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
749 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
750 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
752 for (g = 0; g < sce->ics.num_swb; g++) {
753 const float *coefs = &sce->coeffs[start];
754 const float *scaled = &s->scoefs[start];
755 const int size = sce->ics.swb_sizes[g];
756 int scf, prev_scf, step;
757 int min_scf = -1, max_scf = 256;
759 if (maxq[w*16+g] < 21.544) {
760 sce->zeroes[w*16+g] = 1;
764 sce->zeroes[w*16+g] = 0;
765 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
770 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
772 dist += quantize_band_cost(s, coefs + w2*128,
774 sce->ics.swb_sizes[g],
783 dist *= 1.0f / 512.0f / lambda;
784 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512], ROUND_STANDARD);
785 if (quant_max >= 8191) { // too much, return to the previous quantizer
786 sce->sf_idx[w*16+g] = prev_scf;
790 curdiff = fabsf(dist - uplim[w*16+g]);
794 step = log2f(curdiff);
795 if (dist > uplim[w*16+g])
798 scf = av_clip_uint8(scf);
799 step = scf - prev_scf;
800 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
801 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
812 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
813 for (i = 1; i < 128; i++) {
815 sce->sf_idx[i] = sce->sf_idx[i-1];
817 minq = FFMIN(minq, sce->sf_idx[i]);
821 minq = FFMIN(minq, SCALE_MAX_POS);
822 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
823 for (i = 126; i >= 0; i--) {
825 sce->sf_idx[i] = sce->sf_idx[i+1];
826 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
830 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
831 SingleChannelElement *sce,
837 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
838 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
839 for (g = 0; g < sce->ics.num_swb; g++) {
840 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
841 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
842 if (band->energy <= band->threshold) {
843 sce->sf_idx[(w+w2)*16+g] = 218;
844 sce->zeroes[(w+w2)*16+g] = 1;
846 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
847 sce->zeroes[(w+w2)*16+g] = 0;
849 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
853 for (i = 0; i < 128; i++) {
854 sce->sf_idx[i] = 140;
855 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
857 //set the same quantizers inside window groups
858 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
859 for (g = 0; g < sce->ics.num_swb; g++)
860 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
861 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
864 static void search_for_pns(AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce)
866 int start = 0, w, w2, g;
867 const float lambda = s->lambda;
868 const float freq_mult = avctx->sample_rate/(1024.0f/sce->ics.num_windows)/2.0f;
869 const float spread_threshold = NOISE_SPREAD_THRESHOLD*(lambda/120.f);
870 const float thr_mult = NOISE_LAMBDA_NUMERATOR/lambda;
872 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
874 for (g = 0; g < sce->ics.num_swb; g++) {
875 if (start*freq_mult > NOISE_LOW_LIMIT*(lambda/170.0f)) {
876 float energy = 0.0f, threshold = 0.0f, spread = 0.0f;
877 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
878 FFPsyBand *band = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
879 energy += band->energy;
880 threshold += band->threshold;
881 spread += band->spread;
883 if (spread > spread_threshold*sce->ics.group_len[w] &&
884 ((sce->zeroes[w*16+g] && energy >= threshold) ||
885 energy < threshold*thr_mult*sce->ics.group_len[w])) {
886 sce->band_type[w*16+g] = NOISE_BT;
887 sce->pns_ener[w*16+g] = energy / sce->ics.group_len[w];
888 sce->zeroes[w*16+g] = 0;
891 start += sce->ics.swb_sizes[g];
896 static void search_for_ms(AACEncContext *s, ChannelElement *cpe)
898 int start = 0, i, w, w2, g;
899 float M[128], S[128];
900 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
901 const float lambda = s->lambda;
902 SingleChannelElement *sce0 = &cpe->ch[0];
903 SingleChannelElement *sce1 = &cpe->ch[1];
904 if (!cpe->common_window)
906 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
908 for (g = 0; g < sce0->ics.num_swb; g++) {
909 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
910 float dist1 = 0.0f, dist2 = 0.0f;
911 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
912 FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
913 FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
914 float minthr = FFMIN(band0->threshold, band1->threshold);
915 float maxthr = FFMAX(band0->threshold, band1->threshold);
916 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
917 M[i] = (sce0->coeffs[start+(w+w2)*128+i]
918 + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
920 - sce1->coeffs[start+(w+w2)*128+i];
922 abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
923 abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
924 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
925 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
926 dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
928 sce0->ics.swb_sizes[g],
929 sce0->sf_idx[(w+w2)*16+g],
930 sce0->band_type[(w+w2)*16+g],
931 lambda / band0->threshold, INFINITY, NULL, 0);
932 dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
934 sce1->ics.swb_sizes[g],
935 sce1->sf_idx[(w+w2)*16+g],
936 sce1->band_type[(w+w2)*16+g],
937 lambda / band1->threshold, INFINITY, NULL, 0);
938 dist2 += quantize_band_cost(s, M,
940 sce0->ics.swb_sizes[g],
941 sce0->sf_idx[(w+w2)*16+g],
942 sce0->band_type[(w+w2)*16+g],
943 lambda / maxthr, INFINITY, NULL, 0);
944 dist2 += quantize_band_cost(s, S,
946 sce1->ics.swb_sizes[g],
947 sce1->sf_idx[(w+w2)*16+g],
948 sce1->band_type[(w+w2)*16+g],
949 lambda / minthr, INFINITY, NULL, 0);
951 cpe->ms_mask[w*16+g] = dist2 < dist1;
953 start += sce0->ics.swb_sizes[g];
958 AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
960 search_for_quantizers_faac,
961 encode_window_bands_info,
962 quantize_and_encode_band,
963 ff_aac_encode_tns_info,
964 ff_aac_encode_main_pred,
965 ff_aac_adjust_common_prediction,
966 ff_aac_apply_main_pred,
968 set_special_band_scalefactors,
970 ff_aac_search_for_tns,
972 ff_aac_search_for_is,
973 ff_aac_search_for_pred,
976 search_for_quantizers_anmr,
977 encode_window_bands_info,
978 quantize_and_encode_band,
979 ff_aac_encode_tns_info,
980 ff_aac_encode_main_pred,
981 ff_aac_adjust_common_prediction,
982 ff_aac_apply_main_pred,
984 set_special_band_scalefactors,
986 ff_aac_search_for_tns,
988 ff_aac_search_for_is,
989 ff_aac_search_for_pred,
991 [AAC_CODER_TWOLOOP] = {
992 search_for_quantizers_twoloop,
993 codebook_trellis_rate,
994 quantize_and_encode_band,
995 ff_aac_encode_tns_info,
996 ff_aac_encode_main_pred,
997 ff_aac_adjust_common_prediction,
998 ff_aac_apply_main_pred,
1000 set_special_band_scalefactors,
1002 ff_aac_search_for_tns,
1004 ff_aac_search_for_is,
1005 ff_aac_search_for_pred,
1007 [AAC_CODER_FAST] = {
1008 search_for_quantizers_fast,
1009 encode_window_bands_info,
1010 quantize_and_encode_band,
1011 ff_aac_encode_tns_info,
1012 ff_aac_encode_main_pred,
1013 ff_aac_adjust_common_prediction,
1014 ff_aac_apply_main_pred,
1016 set_special_band_scalefactors,
1018 ff_aac_search_for_tns,
1020 ff_aac_search_for_is,
1021 ff_aac_search_for_pred,