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 int find_min_book(int sf, int group_len, int swb_size, const float *scaled) {
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]);
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;
242 * structure used in optimal codebook search
244 typedef struct BandCodingPath {
245 int prev_idx; ///< pointer to the previous path point
246 float cost; ///< path cost
251 * Encode band info for single window group bands.
253 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
254 int win, int group_len, const float lambda)
256 BandCodingPath path[120][12];
257 int w, swb, cb, start, start2, size;
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;
267 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
269 for (cb = 0; cb < 12; cb++) {
270 path[0][cb].cost = 0.0f;
271 path[0][cb].prev_idx = -1;
274 for (swb = 0; swb < max_sfb; swb++) {
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;
284 float minrd = next_minrd;
285 int mincb = next_mincb;
286 next_minrd = INFINITY;
288 for (cb = 0; cb < 12; cb++) {
289 float cost_stay_here, cost_get_here;
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);
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;
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;
312 if (path[swb+1][cb].cost < next_minrd) {
313 next_minrd = path[swb+1][cb].cost;
318 start += sce->ics.swb_sizes[swb];
321 //convert resulting path from backward-linked list
324 for (cb = 1; cb < 12; cb++)
325 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
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;
336 //perform actual band info encoding
338 for (i = stack_len - 1; i >= 0; i--) {
339 put_bits(&s->pb, 4, stackcb[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];
347 while (count >= run_esc) {
348 put_bits(&s->pb, run_bits, run_esc);
351 put_bits(&s->pb, run_bits, count);
355 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
356 int win, int group_len, const float lambda)
358 BandCodingPath path[120][12];
359 int w, swb, cb, start, start2, size;
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;
369 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
371 for (cb = 0; cb < 12; cb++) {
372 path[0][cb].cost = run_bits+4;
373 path[0][cb].prev_idx = -1;
376 for (swb = 0; swb < max_sfb; swb++) {
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;
386 float minrd = next_minrd;
387 int mincb = next_mincb;
388 int startcb = sce->band_type[win*16+swb];
389 next_minrd = INFINITY;
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;
396 for (cb = startcb; cb < 12; cb++) {
397 float cost_stay_here, cost_get_here;
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,
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;
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;
419 if (path[swb+1][cb].cost < next_minrd) {
420 next_minrd = path[swb+1][cb].cost;
425 start += sce->ics.swb_sizes[swb];
428 //convert resulting path from backward-linked list
431 for (cb = 1; cb < 12; cb++)
432 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
436 if (idx < 0) abort();
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;
444 //perform actual band info encoding
446 for (i = stack_len - 1; i >= 0; i--) {
447 put_bits(&s->pb, 4, stackcb[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];
455 while (count >= run_esc) {
456 put_bits(&s->pb, run_bits, run_esc);
459 put_bits(&s->pb, run_bits, count);
463 typedef struct TrellisPath {
468 #define TRELLIS_STAGES 121
469 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
471 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
472 SingleChannelElement *sce,
475 int q, w, w2, g, start = 0;
478 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
479 int bandaddr[TRELLIS_STAGES];
482 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
483 int q0, q1, qcnt = 0;
485 for (i = 0; i < 1024; i++) {
486 float t = fabsf(sce->coeffs[i]);
496 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
497 memset(sce->zeroes, 1, sizeof(sce->zeroes));
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);
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);
513 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
517 } else if (q1 > q1high) {
522 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
524 for (i = 0; i < TRELLIS_STATES; i++) {
525 paths[0][i].cost = 0.0f;
526 paths[0][i].prev = -1;
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;
535 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
536 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
538 for (g = 0; g < sce->ics.num_swb; g++) {
539 const float *coefs = sce->coeffs + start;
543 bandaddr[idx] = w * 16 + g;
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;
552 sce->zeroes[(w+w2)*16+g] = 0;
554 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
555 float t = fabsf(coefs[w2*128+i]);
557 qmin = FFMIN(qmin, t);
558 qmax = FFMAX(qmax, t);
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++) {
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);
578 minrd = FFMIN(minrd, dist);
580 for (i = 0; i < q1 - q0; i++) {
582 if (isinf(paths[idx - 1][i].cost))
584 cost = paths[idx - 1][i].cost + dist
585 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
586 if (cost < paths[idx][q].cost) {
587 paths[idx][q].cost = cost;
588 paths[idx][q].prev = i;
593 for (q = 0; q < q1 - q0; q++) {
594 if (!isinf(paths[idx - 1][q].cost)) {
595 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
596 paths[idx][q].prev = q;
599 for (i = 0; i < q1 - q0; i++) {
601 if (isinf(paths[idx - 1][i].cost))
603 cost = paths[idx - 1][i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
604 if (cost < paths[idx][q].cost) {
605 paths[idx][q].cost = cost;
606 paths[idx][q].prev = i;
611 sce->zeroes[w*16+g] = !nz;
612 start += sce->ics.swb_sizes[g];
617 mincost = paths[idx][0].cost;
619 for (i = 1; i < TRELLIS_STATES; i++) {
620 if (paths[idx][i].cost < mincost) {
621 mincost = paths[idx][i].cost;
626 sce->sf_idx[bandaddr[idx]] = minq + q0;
627 minq = paths[idx][minq].prev;
630 //set the same quantizers inside window groups
631 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
632 for (g = 0; g < sce->ics.num_swb; g++)
633 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
634 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
638 * two-loop quantizers search taken from ISO 13818-7 Appendix C
640 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
642 SingleChannelElement *sce,
645 int start = 0, i, w, w2, g;
646 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
647 float dists[128], uplims[128];
648 int fflag, minscaler;
651 float minthr = INFINITY;
653 //XXX: some heuristic to determine initial quantizers will reduce search time
654 memset(dists, 0, sizeof(dists));
655 //determine zero bands and upper limits
656 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
657 for (g = 0; g < sce->ics.num_swb; g++) {
660 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
661 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
662 uplim += band->threshold;
663 if (band->energy <= band->threshold || band->threshold == 0.0f) {
664 sce->zeroes[(w+w2)*16+g] = 1;
669 uplims[w*16+g] = uplim *512;
670 sce->zeroes[w*16+g] = !nz;
672 minthr = FFMIN(minthr, uplim);
673 allz = FFMAX(allz, nz);
676 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
677 for (g = 0; g < sce->ics.num_swb; g++) {
678 if (sce->zeroes[w*16+g]) {
679 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
682 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
688 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
689 //perform two-loop search
690 //outer loop - improve quality
693 minscaler = sce->sf_idx[0];
694 //inner loop - quantize spectrum to fit into given number of bits
695 qstep = its ? 1 : 32;
700 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
702 for (g = 0; g < sce->ics.num_swb; g++) {
703 const float *coefs = sce->coeffs + start;
704 const float *scaled = s->scoefs + start;
707 float mindist = INFINITY;
710 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
711 start += sce->ics.swb_sizes[g];
714 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
718 cb = find_min_book(sce->sf_idx[w*16+g], sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
719 sce->band_type[w*16+g] = cb;
720 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
722 dist += quantize_band_cost(s, coefs + w2*128,
724 sce->ics.swb_sizes[g],
735 dists[w*16+g] = (mindist - minbits) / lambda;
738 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
741 start += sce->ics.swb_sizes[g];
742 prev = sce->sf_idx[w*16+g];
745 if (tbits > destbits) {
746 for (i = 0; i < 128; i++)
747 if (sce->sf_idx[i] < 218 - qstep)
748 sce->sf_idx[i] += qstep;
750 for (i = 0; i < 128; i++)
751 if (sce->sf_idx[i] > 60 - qstep)
752 sce->sf_idx[i] -= qstep;
755 if (!qstep && tbits > destbits*1.02)
757 if (sce->sf_idx[0] >= 217)
762 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
763 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
765 for (g = 0; g < sce->ics.num_swb; g++) {
766 int prevsc = sce->sf_idx[w*16+g];
767 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
768 sce->sf_idx[w*16+g]--;
769 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
770 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
771 if (sce->sf_idx[w*16+g] != prevsc)
776 } while (fflag && its < 10);
779 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
780 SingleChannelElement *sce,
783 int start = 0, i, w, w2, g;
784 float uplim[128], maxq[128];
786 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
787 int last = 0, lastband = 0, curband = 0;
788 float avg_energy = 0.0;
789 if (sce->ics.num_windows == 1) {
791 for (i = 0; i < 1024; i++) {
792 if (i - start >= sce->ics.swb_sizes[curband]) {
793 start += sce->ics.swb_sizes[curband];
796 if (sce->coeffs[i]) {
797 avg_energy += sce->coeffs[i] * sce->coeffs[i];
803 for (w = 0; w < 8; w++) {
804 const float *coeffs = sce->coeffs + w*128;
806 for (i = 0; i < 128; i++) {
807 if (i - start >= sce->ics.swb_sizes[curband]) {
808 start += sce->ics.swb_sizes[curband];
812 avg_energy += coeffs[i] * coeffs[i];
813 last = FFMAX(last, i);
814 lastband = FFMAX(lastband, curband);
821 if (avg_energy == 0.0f) {
822 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
823 sce->sf_idx[i] = SCALE_ONE_POS;
826 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
828 for (g = 0; g < sce->ics.num_swb; g++) {
829 float *coefs = sce->coeffs + start;
830 const int size = sce->ics.swb_sizes[g];
831 int start2 = start, end2 = start + size, peakpos = start;
832 float maxval = -1, thr = 0.0f, t;
837 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
838 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
841 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
842 for (i = 0; i < size; i++) {
843 float t = coefs[w2*128+i]*coefs[w2*128+i];
844 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
846 if (sce->ics.num_windows == 1 && maxval < t) {
852 if (sce->ics.num_windows == 1) {
853 start2 = FFMAX(peakpos - 2, start2);
854 end2 = FFMIN(peakpos + 3, end2);
860 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
861 t = 1.0 - (1.0 * start2 / last);
862 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
865 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
866 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
867 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
869 for (g = 0; g < sce->ics.num_swb; g++) {
870 const float *coefs = sce->coeffs + start;
871 const float *scaled = s->scoefs + start;
872 const int size = sce->ics.swb_sizes[g];
873 int scf, prev_scf, step;
874 int min_scf = -1, max_scf = 256;
876 if (maxq[w*16+g] < 21.544) {
877 sce->zeroes[w*16+g] = 1;
881 sce->zeroes[w*16+g] = 0;
882 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
888 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
890 dist += quantize_band_cost(s, coefs + w2*128,
892 sce->ics.swb_sizes[g],
900 dist *= 1.0f / 512.0f / lambda;
901 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
902 if (quant_max >= 8191) { // too much, return to the previous quantizer
903 sce->sf_idx[w*16+g] = prev_scf;
907 curdiff = fabsf(dist - uplim[w*16+g]);
911 step = log2(curdiff);
912 if (dist > uplim[w*16+g])
915 scf = av_clip_uint8(scf);
916 step = scf - prev_scf;
917 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
918 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
929 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
930 for (i = 1; i < 128; i++) {
932 sce->sf_idx[i] = sce->sf_idx[i-1];
934 minq = FFMIN(minq, sce->sf_idx[i]);
938 minq = FFMIN(minq, SCALE_MAX_POS);
939 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
940 for (i = 126; i >= 0; i--) {
942 sce->sf_idx[i] = sce->sf_idx[i+1];
943 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
947 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
948 SingleChannelElement *sce,
951 int start = 0, i, w, w2, g;
954 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
955 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
957 for (g = 0; g < sce->ics.num_swb; g++) {
958 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
959 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
960 if (band->energy <= band->threshold) {
961 sce->sf_idx[(w+w2)*16+g] = 218;
962 sce->zeroes[(w+w2)*16+g] = 1;
964 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
965 sce->zeroes[(w+w2)*16+g] = 0;
967 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
971 for (i = 0; i < 128; i++) {
972 sce->sf_idx[i] = 140;
973 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
975 //set the same quantizers inside window groups
976 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
977 for (g = 0; g < sce->ics.num_swb; g++)
978 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
979 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
982 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
985 int start = 0, i, w, w2, g;
986 float M[128], S[128];
987 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
988 SingleChannelElement *sce0 = &cpe->ch[0];
989 SingleChannelElement *sce1 = &cpe->ch[1];
990 if (!cpe->common_window)
992 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
993 for (g = 0; g < sce0->ics.num_swb; g++) {
994 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
995 float dist1 = 0.0f, dist2 = 0.0f;
996 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
997 FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
998 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
999 float minthr = FFMIN(band0->threshold, band1->threshold);
1000 float maxthr = FFMAX(band0->threshold, band1->threshold);
1001 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1002 M[i] = (sce0->coeffs[start+w2*128+i]
1003 + sce1->coeffs[start+w2*128+i]) * 0.5;
1004 S[i] = sce0->coeffs[start+w2*128+i]
1005 - sce1->coeffs[start+w2*128+i];
1007 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1008 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1009 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
1010 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
1011 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
1013 sce0->ics.swb_sizes[g],
1014 sce0->sf_idx[(w+w2)*16+g],
1015 sce0->band_type[(w+w2)*16+g],
1016 lambda / band0->threshold, INFINITY, NULL);
1017 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1019 sce1->ics.swb_sizes[g],
1020 sce1->sf_idx[(w+w2)*16+g],
1021 sce1->band_type[(w+w2)*16+g],
1022 lambda / band1->threshold, INFINITY, NULL);
1023 dist2 += quantize_band_cost(s, M,
1025 sce0->ics.swb_sizes[g],
1026 sce0->sf_idx[(w+w2)*16+g],
1027 sce0->band_type[(w+w2)*16+g],
1028 lambda / maxthr, INFINITY, NULL);
1029 dist2 += quantize_band_cost(s, S,
1031 sce1->ics.swb_sizes[g],
1032 sce1->sf_idx[(w+w2)*16+g],
1033 sce1->band_type[(w+w2)*16+g],
1034 lambda / minthr, INFINITY, NULL);
1036 cpe->ms_mask[w*16+g] = dist2 < dist1;
1038 start += sce0->ics.swb_sizes[g];
1043 AACCoefficientsEncoder ff_aac_coders[] = {
1045 search_for_quantizers_faac,
1046 encode_window_bands_info,
1047 quantize_and_encode_band,
1051 search_for_quantizers_anmr,
1052 encode_window_bands_info,
1053 quantize_and_encode_band,
1057 search_for_quantizers_twoloop,
1058 codebook_trellis_rate,
1059 quantize_and_encode_band,
1063 search_for_quantizers_fast,
1064 encode_window_bands_info,
1065 quantize_and_encode_band,