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 ***********************************/
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
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
53 static const uint8_t *run_value_bits[2] = {
54 run_value_bits_long, run_value_bits_short
59 * Quantize one coefficient.
60 * @return absolute value of the quantized coefficient
61 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
63 static av_always_inline int quant(float coef, const float Q)
66 return sqrtf(a * sqrtf(a)) + 0.4054;
69 static void quantize_bands(int *out, const float *in, const float *scaled,
70 int size, float Q34, int is_signed, int maxval)
74 for (i = 0; i < size; i++) {
76 out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
77 if (is_signed && in[i] < 0.0f) {
83 static void abs_pow34_v(float *out, const float *in, const int size)
85 #ifndef USE_REALLY_FULL_SEARCH
87 for (i = 0; i < size; i++) {
88 float a = fabsf(in[i]);
89 out[i] = sqrtf(a * sqrtf(a));
91 #endif /* USE_REALLY_FULL_SEARCH */
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};
98 * Calculate rate distortion cost for quantizing with given codebook
100 * @return quantization distortion
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,
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;
113 const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
115 const float Q34 = sqrtf(Q * sqrtf(Q));
116 const int range = aac_cb_range[cb];
117 const int maxval = aac_cb_maxval[cb];
121 for (i = 0; i < size; i++)
125 return cost * lambda;
128 abs_pow34_v(s->scoefs, in, size);
131 quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
132 if (IS_CODEBOOK_UNSIGNED(cb)) {
137 for (i = 0; i < size; i += dim) {
139 int *quants = s->qcoefs + i;
143 for (j = 0; j < dim; j++) {
145 curidx += quants[j] + off;
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]);
153 if (vec[k] == 64.0f) { //FIXME: slow
154 if (t >= CLIPPED_ESCAPE) {
155 di = t - CLIPPED_ESCAPE;
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;
170 for (k = 0; k < dim; k++) {
171 float di = in[i+k] - vec[k]*IQ;
175 cost += rd * lambda + curbits;
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);
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);
191 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
192 put_bits(pb, len, coef & ((1 << len) - 1));
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,
208 return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
209 cb, lambda, uplim, bits);
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)
216 quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
220 static float find_max_val(int group_len, int swb_size, const float *scaled) {
223 for (w2 = 0; w2 < group_len; w2++) {
224 for (i = 0; i < swb_size; i++) {
225 maxval = FFMAX(maxval, scaled[w2*128+i]);
231 static int find_min_book(float maxval, int sf) {
232 float Q = ff_aac_pow2sf_tab[200 - sf + SCALE_ONE_POS - SCALE_DIV_512];
233 float Q34 = sqrtf(Q * sqrtf(Q));
235 qmaxval = maxval * Q34 + 0.4054f;
236 if (qmaxval == 0) cb = 0;
237 else if (qmaxval == 1) cb = 1;
238 else if (qmaxval == 2) cb = 3;
239 else if (qmaxval <= 4) cb = 5;
240 else if (qmaxval <= 7) cb = 7;
241 else if (qmaxval <= 12) cb = 9;
247 * structure used in optimal codebook search
249 typedef struct BandCodingPath {
250 int prev_idx; ///< pointer to the previous path point
251 float cost; ///< path cost
256 * Encode band info for single window group bands.
258 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
259 int win, int group_len, const float lambda)
261 BandCodingPath path[120][12];
262 int w, swb, cb, start, start2, size;
264 const int max_sfb = sce->ics.max_sfb;
265 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
266 const int run_esc = (1 << run_bits) - 1;
267 int idx, ppos, count;
268 int stackrun[120], stackcb[120], stack_len;
269 float next_minrd = INFINITY;
272 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
274 for (cb = 0; cb < 12; cb++) {
275 path[0][cb].cost = 0.0f;
276 path[0][cb].prev_idx = -1;
279 for (swb = 0; swb < max_sfb; swb++) {
281 size = sce->ics.swb_sizes[swb];
282 if (sce->zeroes[win*16 + swb]) {
283 for (cb = 0; cb < 12; cb++) {
284 path[swb+1][cb].prev_idx = cb;
285 path[swb+1][cb].cost = path[swb][cb].cost;
286 path[swb+1][cb].run = path[swb][cb].run + 1;
289 float minrd = next_minrd;
290 int mincb = next_mincb;
291 next_minrd = INFINITY;
293 for (cb = 0; cb < 12; cb++) {
294 float cost_stay_here, cost_get_here;
296 for (w = 0; w < group_len; w++) {
297 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
298 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
299 s->scoefs + start + w*128, size,
300 sce->sf_idx[(win+w)*16+swb], cb,
301 lambda / band->threshold, INFINITY, NULL);
303 cost_stay_here = path[swb][cb].cost + rd;
304 cost_get_here = minrd + rd + run_bits + 4;
305 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
306 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
307 cost_stay_here += run_bits;
308 if (cost_get_here < cost_stay_here) {
309 path[swb+1][cb].prev_idx = mincb;
310 path[swb+1][cb].cost = cost_get_here;
311 path[swb+1][cb].run = 1;
313 path[swb+1][cb].prev_idx = cb;
314 path[swb+1][cb].cost = cost_stay_here;
315 path[swb+1][cb].run = path[swb][cb].run + 1;
317 if (path[swb+1][cb].cost < next_minrd) {
318 next_minrd = path[swb+1][cb].cost;
323 start += sce->ics.swb_sizes[swb];
326 //convert resulting path from backward-linked list
329 for (cb = 1; cb < 12; cb++)
330 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
335 stackrun[stack_len] = path[ppos][cb].run;
336 stackcb [stack_len] = cb;
337 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
338 ppos -= path[ppos][cb].run;
341 //perform actual band info encoding
343 for (i = stack_len - 1; i >= 0; i--) {
344 put_bits(&s->pb, 4, stackcb[i]);
346 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
347 //XXX: memset when band_type is also uint8_t
348 for (j = 0; j < count; j++) {
349 sce->band_type[win*16 + start] = stackcb[i];
352 while (count >= run_esc) {
353 put_bits(&s->pb, run_bits, run_esc);
356 put_bits(&s->pb, run_bits, count);
360 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
361 int win, int group_len, const float lambda)
363 BandCodingPath path[120][12];
364 int w, swb, cb, start, start2, size;
366 const int max_sfb = sce->ics.max_sfb;
367 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
368 const int run_esc = (1 << run_bits) - 1;
369 int idx, ppos, count;
370 int stackrun[120], stackcb[120], stack_len;
371 float next_minrd = INFINITY;
374 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
376 for (cb = 0; cb < 12; cb++) {
377 path[0][cb].cost = run_bits+4;
378 path[0][cb].prev_idx = -1;
381 for (swb = 0; swb < max_sfb; swb++) {
383 size = sce->ics.swb_sizes[swb];
384 if (sce->zeroes[win*16 + swb]) {
385 for (cb = 0; cb < 12; cb++) {
386 path[swb+1][cb].prev_idx = cb;
387 path[swb+1][cb].cost = path[swb][cb].cost;
388 path[swb+1][cb].run = path[swb][cb].run + 1;
391 float minrd = next_minrd;
392 int mincb = next_mincb;
393 int startcb = sce->band_type[win*16+swb];
394 next_minrd = INFINITY;
396 for (cb = 0; cb < startcb; cb++) {
397 path[swb+1][cb].cost = 61450;
398 path[swb+1][cb].prev_idx = -1;
399 path[swb+1][cb].run = 0;
401 for (cb = startcb; cb < 12; cb++) {
402 float cost_stay_here, cost_get_here;
404 for (w = 0; w < group_len; w++) {
405 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
406 s->scoefs + start + w*128, size,
407 sce->sf_idx[(win+w)*16+swb], cb,
410 cost_stay_here = path[swb][cb].cost + rd;
411 cost_get_here = minrd + rd + run_bits + 4;
412 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
413 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
414 cost_stay_here += run_bits;
415 if (cost_get_here < cost_stay_here) {
416 path[swb+1][cb].prev_idx = mincb;
417 path[swb+1][cb].cost = cost_get_here;
418 path[swb+1][cb].run = 1;
420 path[swb+1][cb].prev_idx = cb;
421 path[swb+1][cb].cost = cost_stay_here;
422 path[swb+1][cb].run = path[swb][cb].run + 1;
424 if (path[swb+1][cb].cost < next_minrd) {
425 next_minrd = path[swb+1][cb].cost;
430 start += sce->ics.swb_sizes[swb];
433 //convert resulting path from backward-linked list
436 for (cb = 1; cb < 12; cb++)
437 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
441 if (idx < 0) abort();
443 stackrun[stack_len] = path[ppos][cb].run;
444 stackcb [stack_len] = cb;
445 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
446 ppos -= path[ppos][cb].run;
449 //perform actual band info encoding
451 for (i = stack_len - 1; i >= 0; i--) {
452 put_bits(&s->pb, 4, stackcb[i]);
454 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
455 //XXX: memset when band_type is also uint8_t
456 for (j = 0; j < count; j++) {
457 sce->band_type[win*16 + start] = stackcb[i];
460 while (count >= run_esc) {
461 put_bits(&s->pb, run_bits, run_esc);
464 put_bits(&s->pb, run_bits, count);
468 typedef struct TrellisPath {
473 #define TRELLIS_STAGES 121
474 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
476 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
477 SingleChannelElement *sce,
480 int q, w, w2, g, start = 0;
483 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
484 int bandaddr[TRELLIS_STAGES];
487 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
488 int q0, q1, qcnt = 0;
490 for (i = 0; i < 1024; i++) {
491 float t = fabsf(sce->coeffs[i]);
501 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
502 memset(sce->zeroes, 1, sizeof(sce->zeroes));
506 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
507 q0 = av_clip_uint8(log2(q0f)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
508 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
509 q1 = av_clip_uint8(log2(q1f)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
510 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
514 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
515 int qnrg = av_clip_uint8(log2(sqrt(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
518 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
522 } else if (q1 > q1high) {
527 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
529 for (i = 0; i < TRELLIS_STATES; i++) {
530 paths[0][i].cost = 0.0f;
531 paths[0][i].prev = -1;
533 for (j = 1; j < TRELLIS_STAGES; j++) {
534 for (i = 0; i < TRELLIS_STATES; i++) {
535 paths[j][i].cost = INFINITY;
536 paths[j][i].prev = -2;
540 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
541 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
543 for (g = 0; g < sce->ics.num_swb; g++) {
544 const float *coefs = sce->coeffs + start;
548 bandaddr[idx] = w * 16 + g;
551 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
552 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
553 if (band->energy <= band->threshold || band->threshold == 0.0f) {
554 sce->zeroes[(w+w2)*16+g] = 1;
557 sce->zeroes[(w+w2)*16+g] = 0;
559 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
560 float t = fabsf(coefs[w2*128+i]);
562 qmin = FFMIN(qmin, t);
563 qmax = FFMAX(qmax, t);
567 int minscale, maxscale;
568 float minrd = INFINITY;
570 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
571 minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
572 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
573 maxscale = av_clip_uint8(log2(qmax)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
574 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
575 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
576 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
577 for (q = minscale; q < maxscale; q++) {
579 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
580 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
581 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
582 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
583 q + q0, cb, lambda / band->threshold, INFINITY, NULL);
585 minrd = FFMIN(minrd, dist);
587 for (i = 0; i < q1 - q0; i++) {
589 cost = paths[idx - 1][i].cost + dist
590 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
591 if (cost < paths[idx][q].cost) {
592 paths[idx][q].cost = cost;
593 paths[idx][q].prev = i;
598 for (q = 0; q < q1 - q0; q++) {
599 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
600 paths[idx][q].prev = q;
603 sce->zeroes[w*16+g] = !nz;
604 start += sce->ics.swb_sizes[g];
609 mincost = paths[idx][0].cost;
611 for (i = 1; i < TRELLIS_STATES; i++) {
612 if (paths[idx][i].cost < mincost) {
613 mincost = paths[idx][i].cost;
618 sce->sf_idx[bandaddr[idx]] = minq + q0;
619 minq = paths[idx][minq].prev;
622 //set the same quantizers inside window groups
623 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
624 for (g = 0; g < sce->ics.num_swb; g++)
625 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
626 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
630 * two-loop quantizers search taken from ISO 13818-7 Appendix C
632 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
634 SingleChannelElement *sce,
637 int start = 0, i, w, w2, g;
638 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
639 float dists[128], uplims[128];
640 int fflag, minscaler;
643 float minthr = INFINITY;
645 //XXX: some heuristic to determine initial quantizers will reduce search time
646 memset(dists, 0, sizeof(dists));
647 //determine zero bands and upper limits
648 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
649 for (g = 0; g < sce->ics.num_swb; g++) {
652 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
653 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
654 uplim += band->threshold;
655 if (band->energy <= band->threshold || band->threshold == 0.0f) {
656 sce->zeroes[(w+w2)*16+g] = 1;
661 uplims[w*16+g] = uplim *512;
662 sce->zeroes[w*16+g] = !nz;
664 minthr = FFMIN(minthr, uplim);
665 allz = FFMAX(allz, nz);
668 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
669 for (g = 0; g < sce->ics.num_swb; g++) {
670 if (sce->zeroes[w*16+g]) {
671 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
674 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
680 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
681 //perform two-loop search
682 //outer loop - improve quality
685 minscaler = sce->sf_idx[0];
686 //inner loop - quantize spectrum to fit into given number of bits
687 qstep = its ? 1 : 32;
692 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
694 for (g = 0; g < sce->ics.num_swb; g++) {
695 const float *coefs = sce->coeffs + start;
696 const float *scaled = s->scoefs + start;
701 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
702 start += sce->ics.swb_sizes[g];
705 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
706 cb = find_min_book(find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled), sce->sf_idx[w*16+g]);
707 sce->band_type[w*16+g] = cb;
708 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
710 dist += quantize_band_cost(s, coefs + w2*128,
712 sce->ics.swb_sizes[g],
720 dists[w*16+g] = dist - bits;
722 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
725 start += sce->ics.swb_sizes[g];
726 prev = sce->sf_idx[w*16+g];
729 if (tbits > destbits) {
730 for (i = 0; i < 128; i++)
731 if (sce->sf_idx[i] < 218 - qstep)
732 sce->sf_idx[i] += qstep;
734 for (i = 0; i < 128; i++)
735 if (sce->sf_idx[i] > 60 - qstep)
736 sce->sf_idx[i] -= qstep;
739 if (!qstep && tbits > destbits*1.02)
741 if (sce->sf_idx[0] >= 217)
746 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
747 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
749 for (g = 0; g < sce->ics.num_swb; g++) {
750 int prevsc = sce->sf_idx[w*16+g];
751 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
752 sce->sf_idx[w*16+g]--;
753 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
754 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
755 if (sce->sf_idx[w*16+g] != prevsc)
760 } while (fflag && its < 10);
763 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
764 SingleChannelElement *sce,
767 int start = 0, i, w, w2, g;
768 float uplim[128], maxq[128];
770 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
771 int last = 0, lastband = 0, curband = 0;
772 float avg_energy = 0.0;
773 if (sce->ics.num_windows == 1) {
775 for (i = 0; i < 1024; i++) {
776 if (i - start >= sce->ics.swb_sizes[curband]) {
777 start += sce->ics.swb_sizes[curband];
780 if (sce->coeffs[i]) {
781 avg_energy += sce->coeffs[i] * sce->coeffs[i];
787 for (w = 0; w < 8; w++) {
788 const float *coeffs = sce->coeffs + w*128;
790 for (i = 0; i < 128; i++) {
791 if (i - start >= sce->ics.swb_sizes[curband]) {
792 start += sce->ics.swb_sizes[curband];
796 avg_energy += coeffs[i] * coeffs[i];
797 last = FFMAX(last, i);
798 lastband = FFMAX(lastband, curband);
805 if (avg_energy == 0.0f) {
806 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
807 sce->sf_idx[i] = SCALE_ONE_POS;
810 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
812 for (g = 0; g < sce->ics.num_swb; g++) {
813 float *coefs = sce->coeffs + start;
814 const int size = sce->ics.swb_sizes[g];
815 int start2 = start, end2 = start + size, peakpos = start;
816 float maxval = -1, thr = 0.0f, t;
821 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
822 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
825 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
826 for (i = 0; i < size; i++) {
827 float t = coefs[w2*128+i]*coefs[w2*128+i];
828 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
830 if (sce->ics.num_windows == 1 && maxval < t) {
836 if (sce->ics.num_windows == 1) {
837 start2 = FFMAX(peakpos - 2, start2);
838 end2 = FFMIN(peakpos + 3, end2);
844 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
845 t = 1.0 - (1.0 * start2 / last);
846 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
849 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
850 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
851 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
853 for (g = 0; g < sce->ics.num_swb; g++) {
854 const float *coefs = sce->coeffs + start;
855 const float *scaled = s->scoefs + start;
856 const int size = sce->ics.swb_sizes[g];
857 int scf, prev_scf, step;
858 int min_scf = -1, max_scf = 256;
860 if (maxq[w*16+g] < 21.544) {
861 sce->zeroes[w*16+g] = 1;
865 sce->zeroes[w*16+g] = 0;
866 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
872 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
874 dist += quantize_band_cost(s, coefs + w2*128,
876 sce->ics.swb_sizes[g],
884 dist *= 1.0f / 512.0f / lambda;
885 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
886 if (quant_max >= 8191) { // too much, return to the previous quantizer
887 sce->sf_idx[w*16+g] = prev_scf;
891 curdiff = fabsf(dist - uplim[w*16+g]);
895 step = log2(curdiff);
896 if (dist > uplim[w*16+g])
899 scf = av_clip_uint8(scf);
900 step = scf - prev_scf;
901 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
902 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
913 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
914 for (i = 1; i < 128; i++) {
916 sce->sf_idx[i] = sce->sf_idx[i-1];
918 minq = FFMIN(minq, sce->sf_idx[i]);
922 minq = FFMIN(minq, SCALE_MAX_POS);
923 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
924 for (i = 126; i >= 0; i--) {
926 sce->sf_idx[i] = sce->sf_idx[i+1];
927 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
931 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
932 SingleChannelElement *sce,
935 int start = 0, i, w, w2, g;
938 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
939 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
941 for (g = 0; g < sce->ics.num_swb; g++) {
942 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
943 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
944 if (band->energy <= band->threshold) {
945 sce->sf_idx[(w+w2)*16+g] = 218;
946 sce->zeroes[(w+w2)*16+g] = 1;
948 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
949 sce->zeroes[(w+w2)*16+g] = 0;
951 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
955 for (i = 0; i < 128; i++) {
956 sce->sf_idx[i] = 140;
957 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
959 //set the same quantizers inside window groups
960 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
961 for (g = 0; g < sce->ics.num_swb; g++)
962 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
963 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
966 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
969 int start = 0, i, w, w2, g;
970 float M[128], S[128];
971 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
972 SingleChannelElement *sce0 = &cpe->ch[0];
973 SingleChannelElement *sce1 = &cpe->ch[1];
974 if (!cpe->common_window)
976 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
977 for (g = 0; g < sce0->ics.num_swb; g++) {
978 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
979 float dist1 = 0.0f, dist2 = 0.0f;
980 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
981 FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
982 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
983 float minthr = FFMIN(band0->threshold, band1->threshold);
984 float maxthr = FFMAX(band0->threshold, band1->threshold);
985 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
986 M[i] = (sce0->coeffs[start+w2*128+i]
987 + sce1->coeffs[start+w2*128+i]) * 0.5;
988 S[i] = sce0->coeffs[start+w2*128+i]
989 - sce1->coeffs[start+w2*128+i];
991 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
992 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
993 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
994 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
995 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
997 sce0->ics.swb_sizes[g],
998 sce0->sf_idx[(w+w2)*16+g],
999 sce0->band_type[(w+w2)*16+g],
1000 lambda / band0->threshold, INFINITY, NULL);
1001 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1003 sce1->ics.swb_sizes[g],
1004 sce1->sf_idx[(w+w2)*16+g],
1005 sce1->band_type[(w+w2)*16+g],
1006 lambda / band1->threshold, INFINITY, NULL);
1007 dist2 += quantize_band_cost(s, M,
1009 sce0->ics.swb_sizes[g],
1010 sce0->sf_idx[(w+w2)*16+g],
1011 sce0->band_type[(w+w2)*16+g],
1012 lambda / maxthr, INFINITY, NULL);
1013 dist2 += quantize_band_cost(s, S,
1015 sce1->ics.swb_sizes[g],
1016 sce1->sf_idx[(w+w2)*16+g],
1017 sce1->band_type[(w+w2)*16+g],
1018 lambda / minthr, INFINITY, NULL);
1020 cpe->ms_mask[w*16+g] = dist2 < dist1;
1022 start += sce0->ics.swb_sizes[g];
1027 AACCoefficientsEncoder ff_aac_coders[] = {
1029 search_for_quantizers_faac,
1030 encode_window_bands_info,
1031 quantize_and_encode_band,
1035 search_for_quantizers_anmr,
1036 encode_window_bands_info,
1037 quantize_and_encode_band,
1041 search_for_quantizers_twoloop,
1042 codebook_trellis_rate,
1043 quantize_and_encode_band,
1047 search_for_quantizers_fast,
1048 encode_window_bands_info,
1049 quantize_and_encode_band,