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 av_always_inline float quantize_and_encode_band_cost_template(
103 struct AACEncContext *s,
104 PutBitContext *pb, const float *in,
105 const float *scaled, int size, int scale_idx,
106 int cb, const float lambda, const float uplim,
107 int *bits, int BT_ZERO, int BT_UNSIGNED,
108 int BT_PAIR, int BT_ESC)
110 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
111 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
112 const float CLIPPED_ESCAPE = 165140.0f*IQ;
115 const int dim = BT_PAIR ? 2 : 4;
117 const float Q34 = sqrtf(Q * sqrtf(Q));
118 const int range = aac_cb_range[cb];
119 const int maxval = aac_cb_maxval[cb];
123 for (i = 0; i < size; i++)
127 return cost * lambda;
130 abs_pow34_v(s->scoefs, in, size);
133 quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
139 for (i = 0; i < size; i += dim) {
141 int *quants = s->qcoefs + i;
145 for (j = 0; j < dim; j++) {
147 curidx += quants[j] + off;
149 curbits = ff_aac_spectral_bits[cb-1][curidx];
150 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
152 for (k = 0; k < dim; k++) {
153 float t = fabsf(in[i+k]);
155 if (BT_ESC && vec[k] == 64.0f) { //FIXME: slow
156 if (t >= CLIPPED_ESCAPE) {
157 di = t - CLIPPED_ESCAPE;
160 int c = av_clip(quant(t, Q), 0, 8191);
161 di = t - c*cbrtf(c)*IQ;
162 curbits += av_log2(c)*2 - 4 + 1;
172 for (k = 0; k < dim; k++) {
173 float di = in[i+k] - vec[k]*IQ;
177 cost += rd * lambda + curbits;
182 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
184 for (j = 0; j < dim; j++)
185 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
186 put_bits(pb, 1, in[i+j] < 0.0f);
188 for (j = 0; j < 2; j++) {
189 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
190 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
191 int len = av_log2(coef);
193 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
194 put_bits(pb, len, coef & ((1 << len) - 1));
206 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
207 static float quantize_and_encode_band_cost_ ## NAME( \
208 struct AACEncContext *s, \
209 PutBitContext *pb, const float *in, \
210 const float *scaled, int size, int scale_idx, \
211 int cb, const float lambda, const float uplim, \
213 return quantize_and_encode_band_cost_template( \
214 s, pb, in, scaled, size, scale_idx, \
215 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
216 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \
219 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0)
220 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0)
221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0)
222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0)
223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0)
224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1)
226 static float (*const quantize_and_encode_band_cost_arr[])(
227 struct AACEncContext *s,
228 PutBitContext *pb, const float *in,
229 const float *scaled, int size, int scale_idx,
230 int cb, const float lambda, const float uplim,
232 quantize_and_encode_band_cost_ZERO,
233 quantize_and_encode_band_cost_SQUAD,
234 quantize_and_encode_band_cost_SQUAD,
235 quantize_and_encode_band_cost_UQUAD,
236 quantize_and_encode_band_cost_UQUAD,
237 quantize_and_encode_band_cost_SPAIR,
238 quantize_and_encode_band_cost_SPAIR,
239 quantize_and_encode_band_cost_UPAIR,
240 quantize_and_encode_band_cost_UPAIR,
241 quantize_and_encode_band_cost_UPAIR,
242 quantize_and_encode_band_cost_UPAIR,
243 quantize_and_encode_band_cost_ESC,
246 #define quantize_and_encode_band_cost( \
247 s, pb, in, scaled, size, scale_idx, cb, \
248 lambda, uplim, bits) \
249 quantize_and_encode_band_cost_arr[cb]( \
250 s, pb, in, scaled, size, scale_idx, cb, \
253 static float quantize_band_cost(struct AACEncContext *s, const float *in,
254 const float *scaled, int size, int scale_idx,
255 int cb, const float lambda, const float uplim,
258 return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
259 cb, lambda, uplim, bits);
262 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
263 const float *in, int size, int scale_idx,
264 int cb, const float lambda)
266 quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
270 static float find_max_val(int group_len, int swb_size, const float *scaled) {
273 for (w2 = 0; w2 < group_len; w2++) {
274 for (i = 0; i < swb_size; i++) {
275 maxval = FFMAX(maxval, scaled[w2*128+i]);
281 static int find_min_book(float maxval, int sf) {
282 float Q = ff_aac_pow2sf_tab[200 - sf + SCALE_ONE_POS - SCALE_DIV_512];
283 float Q34 = sqrtf(Q * sqrtf(Q));
285 qmaxval = maxval * Q34 + 0.4054f;
286 if (qmaxval == 0) cb = 0;
287 else if (qmaxval == 1) cb = 1;
288 else if (qmaxval == 2) cb = 3;
289 else if (qmaxval <= 4) cb = 5;
290 else if (qmaxval <= 7) cb = 7;
291 else if (qmaxval <= 12) cb = 9;
297 * structure used in optimal codebook search
299 typedef struct BandCodingPath {
300 int prev_idx; ///< pointer to the previous path point
301 float cost; ///< path cost
306 * Encode band info for single window group bands.
308 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
309 int win, int group_len, const float lambda)
311 BandCodingPath path[120][12];
312 int w, swb, cb, start, start2, size;
314 const int max_sfb = sce->ics.max_sfb;
315 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
316 const int run_esc = (1 << run_bits) - 1;
317 int idx, ppos, count;
318 int stackrun[120], stackcb[120], stack_len;
319 float next_minrd = INFINITY;
322 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
324 for (cb = 0; cb < 12; cb++) {
325 path[0][cb].cost = 0.0f;
326 path[0][cb].prev_idx = -1;
329 for (swb = 0; swb < max_sfb; swb++) {
331 size = sce->ics.swb_sizes[swb];
332 if (sce->zeroes[win*16 + swb]) {
333 for (cb = 0; cb < 12; cb++) {
334 path[swb+1][cb].prev_idx = cb;
335 path[swb+1][cb].cost = path[swb][cb].cost;
336 path[swb+1][cb].run = path[swb][cb].run + 1;
339 float minrd = next_minrd;
340 int mincb = next_mincb;
341 next_minrd = INFINITY;
343 for (cb = 0; cb < 12; cb++) {
344 float cost_stay_here, cost_get_here;
346 for (w = 0; w < group_len; w++) {
347 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
348 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
349 s->scoefs + start + w*128, size,
350 sce->sf_idx[(win+w)*16+swb], cb,
351 lambda / band->threshold, INFINITY, NULL);
353 cost_stay_here = path[swb][cb].cost + rd;
354 cost_get_here = minrd + rd + run_bits + 4;
355 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
356 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
357 cost_stay_here += run_bits;
358 if (cost_get_here < cost_stay_here) {
359 path[swb+1][cb].prev_idx = mincb;
360 path[swb+1][cb].cost = cost_get_here;
361 path[swb+1][cb].run = 1;
363 path[swb+1][cb].prev_idx = cb;
364 path[swb+1][cb].cost = cost_stay_here;
365 path[swb+1][cb].run = path[swb][cb].run + 1;
367 if (path[swb+1][cb].cost < next_minrd) {
368 next_minrd = path[swb+1][cb].cost;
373 start += sce->ics.swb_sizes[swb];
376 //convert resulting path from backward-linked list
379 for (cb = 1; cb < 12; cb++)
380 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
385 stackrun[stack_len] = path[ppos][cb].run;
386 stackcb [stack_len] = cb;
387 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
388 ppos -= path[ppos][cb].run;
391 //perform actual band info encoding
393 for (i = stack_len - 1; i >= 0; i--) {
394 put_bits(&s->pb, 4, stackcb[i]);
396 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
397 //XXX: memset when band_type is also uint8_t
398 for (j = 0; j < count; j++) {
399 sce->band_type[win*16 + start] = stackcb[i];
402 while (count >= run_esc) {
403 put_bits(&s->pb, run_bits, run_esc);
406 put_bits(&s->pb, run_bits, count);
410 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
411 int win, int group_len, const float lambda)
413 BandCodingPath path[120][12];
414 int w, swb, cb, start, start2, size;
416 const int max_sfb = sce->ics.max_sfb;
417 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
418 const int run_esc = (1 << run_bits) - 1;
419 int idx, ppos, count;
420 int stackrun[120], stackcb[120], stack_len;
421 float next_minrd = INFINITY;
424 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
426 for (cb = 0; cb < 12; cb++) {
427 path[0][cb].cost = run_bits+4;
428 path[0][cb].prev_idx = -1;
431 for (swb = 0; swb < max_sfb; swb++) {
433 size = sce->ics.swb_sizes[swb];
434 if (sce->zeroes[win*16 + swb]) {
435 for (cb = 0; cb < 12; cb++) {
436 path[swb+1][cb].prev_idx = cb;
437 path[swb+1][cb].cost = path[swb][cb].cost;
438 path[swb+1][cb].run = path[swb][cb].run + 1;
441 float minrd = next_minrd;
442 int mincb = next_mincb;
443 int startcb = sce->band_type[win*16+swb];
444 next_minrd = INFINITY;
446 for (cb = 0; cb < startcb; cb++) {
447 path[swb+1][cb].cost = 61450;
448 path[swb+1][cb].prev_idx = -1;
449 path[swb+1][cb].run = 0;
451 for (cb = startcb; cb < 12; cb++) {
452 float cost_stay_here, cost_get_here;
454 for (w = 0; w < group_len; w++) {
455 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
456 s->scoefs + start + w*128, size,
457 sce->sf_idx[(win+w)*16+swb], cb,
460 cost_stay_here = path[swb][cb].cost + rd;
461 cost_get_here = minrd + rd + run_bits + 4;
462 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
463 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
464 cost_stay_here += run_bits;
465 if (cost_get_here < cost_stay_here) {
466 path[swb+1][cb].prev_idx = mincb;
467 path[swb+1][cb].cost = cost_get_here;
468 path[swb+1][cb].run = 1;
470 path[swb+1][cb].prev_idx = cb;
471 path[swb+1][cb].cost = cost_stay_here;
472 path[swb+1][cb].run = path[swb][cb].run + 1;
474 if (path[swb+1][cb].cost < next_minrd) {
475 next_minrd = path[swb+1][cb].cost;
480 start += sce->ics.swb_sizes[swb];
483 //convert resulting path from backward-linked list
486 for (cb = 1; cb < 12; cb++)
487 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
493 stackrun[stack_len] = path[ppos][cb].run;
494 stackcb [stack_len] = cb;
495 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
496 ppos -= path[ppos][cb].run;
499 //perform actual band info encoding
501 for (i = stack_len - 1; i >= 0; i--) {
502 put_bits(&s->pb, 4, stackcb[i]);
504 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
505 //XXX: memset when band_type is also uint8_t
506 for (j = 0; j < count; j++) {
507 sce->band_type[win*16 + start] = stackcb[i];
510 while (count >= run_esc) {
511 put_bits(&s->pb, run_bits, run_esc);
514 put_bits(&s->pb, run_bits, count);
518 /** Return the minimum scalefactor where the quantized coef does not clip. */
519 static av_always_inline uint8_t coef2minsf(float coef) {
520 return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
523 /** Return the maximum scalefactor where the quantized coef is not zero. */
524 static av_always_inline uint8_t coef2maxsf(float coef) {
525 return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
528 typedef struct TrellisPath {
533 #define TRELLIS_STAGES 121
534 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
536 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
537 SingleChannelElement *sce,
540 int q, w, w2, g, start = 0;
543 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
544 int bandaddr[TRELLIS_STAGES];
547 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
548 int q0, q1, qcnt = 0;
550 for (i = 0; i < 1024; i++) {
551 float t = fabsf(sce->coeffs[i]);
561 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
562 memset(sce->zeroes, 1, sizeof(sce->zeroes));
566 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
567 q0 = coef2minsf(q0f);
568 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
569 q1 = coef2maxsf(q1f);
570 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
574 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
575 int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
578 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
582 } else if (q1 > q1high) {
587 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
589 for (i = 0; i < TRELLIS_STATES; i++) {
590 paths[0][i].cost = 0.0f;
591 paths[0][i].prev = -1;
593 for (j = 1; j < TRELLIS_STAGES; j++) {
594 for (i = 0; i < TRELLIS_STATES; i++) {
595 paths[j][i].cost = INFINITY;
596 paths[j][i].prev = -2;
600 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
601 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
603 for (g = 0; g < sce->ics.num_swb; g++) {
604 const float *coefs = sce->coeffs + start;
608 bandaddr[idx] = w * 16 + g;
611 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
612 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
613 if (band->energy <= band->threshold || band->threshold == 0.0f) {
614 sce->zeroes[(w+w2)*16+g] = 1;
617 sce->zeroes[(w+w2)*16+g] = 0;
619 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
620 float t = fabsf(coefs[w2*128+i]);
622 qmin = FFMIN(qmin, t);
623 qmax = FFMAX(qmax, t);
627 int minscale, maxscale;
628 float minrd = INFINITY;
630 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
631 minscale = coef2minsf(qmin);
632 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
633 maxscale = coef2maxsf(qmax);
634 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
635 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
636 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
637 for (q = minscale; q < maxscale; q++) {
639 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
640 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
641 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
642 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
643 q + q0, cb, lambda / band->threshold, INFINITY, NULL);
645 minrd = FFMIN(minrd, dist);
647 for (i = 0; i < q1 - q0; i++) {
649 cost = paths[idx - 1][i].cost + dist
650 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
651 if (cost < paths[idx][q].cost) {
652 paths[idx][q].cost = cost;
653 paths[idx][q].prev = i;
658 for (q = 0; q < q1 - q0; q++) {
659 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
660 paths[idx][q].prev = q;
663 sce->zeroes[w*16+g] = !nz;
664 start += sce->ics.swb_sizes[g];
669 mincost = paths[idx][0].cost;
671 for (i = 1; i < TRELLIS_STATES; i++) {
672 if (paths[idx][i].cost < mincost) {
673 mincost = paths[idx][i].cost;
678 sce->sf_idx[bandaddr[idx]] = minq + q0;
679 minq = paths[idx][minq].prev;
682 //set the same quantizers inside window groups
683 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
684 for (g = 0; g < sce->ics.num_swb; g++)
685 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
686 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
690 * two-loop quantizers search taken from ISO 13818-7 Appendix C
692 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
694 SingleChannelElement *sce,
697 int start = 0, i, w, w2, g;
698 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
699 float dists[128], uplims[128];
701 int fflag, minscaler;
704 float minthr = INFINITY;
706 //XXX: some heuristic to determine initial quantizers will reduce search time
707 memset(dists, 0, sizeof(dists));
708 //determine zero bands and upper limits
709 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
710 for (g = 0; g < sce->ics.num_swb; g++) {
713 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
714 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
715 uplim += band->threshold;
716 if (band->energy <= band->threshold || band->threshold == 0.0f) {
717 sce->zeroes[(w+w2)*16+g] = 1;
722 uplims[w*16+g] = uplim *512;
723 sce->zeroes[w*16+g] = !nz;
725 minthr = FFMIN(minthr, uplim);
726 allz = FFMAX(allz, nz);
729 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
730 for (g = 0; g < sce->ics.num_swb; g++) {
731 if (sce->zeroes[w*16+g]) {
732 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
735 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
741 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
743 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
745 for (g = 0; g < sce->ics.num_swb; g++) {
746 const float *scaled = s->scoefs + start;
747 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
748 start += sce->ics.swb_sizes[g];
752 //perform two-loop search
753 //outer loop - improve quality
756 minscaler = sce->sf_idx[0];
757 //inner loop - quantize spectrum to fit into given number of bits
758 qstep = its ? 1 : 32;
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 const float *coefs = sce->coeffs + start;
767 const float *scaled = s->scoefs + start;
772 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
773 start += sce->ics.swb_sizes[g];
776 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
777 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
778 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
780 dist += quantize_band_cost(s, coefs + w2*128,
782 sce->ics.swb_sizes[g],
790 dists[w*16+g] = dist - bits;
792 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
795 start += sce->ics.swb_sizes[g];
796 prev = sce->sf_idx[w*16+g];
799 if (tbits > destbits) {
800 for (i = 0; i < 128; i++)
801 if (sce->sf_idx[i] < 218 - qstep)
802 sce->sf_idx[i] += qstep;
804 for (i = 0; i < 128; i++)
805 if (sce->sf_idx[i] > 60 - qstep)
806 sce->sf_idx[i] -= qstep;
809 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
814 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
815 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
816 for (g = 0; g < sce->ics.num_swb; g++) {
817 int prevsc = sce->sf_idx[w*16+g];
818 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
819 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
820 sce->sf_idx[w*16+g]--;
821 else //Try to make sure there is some energy in every band
822 sce->sf_idx[w*16+g]-=2;
824 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
825 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
826 if (sce->sf_idx[w*16+g] != prevsc)
828 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
832 } while (fflag && its < 10);
835 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
836 SingleChannelElement *sce,
839 int start = 0, i, w, w2, g;
840 float uplim[128], maxq[128];
842 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
843 int last = 0, lastband = 0, curband = 0;
844 float avg_energy = 0.0;
845 if (sce->ics.num_windows == 1) {
847 for (i = 0; i < 1024; i++) {
848 if (i - start >= sce->ics.swb_sizes[curband]) {
849 start += sce->ics.swb_sizes[curband];
852 if (sce->coeffs[i]) {
853 avg_energy += sce->coeffs[i] * sce->coeffs[i];
859 for (w = 0; w < 8; w++) {
860 const float *coeffs = sce->coeffs + w*128;
862 for (i = 0; i < 128; i++) {
863 if (i - start >= sce->ics.swb_sizes[curband]) {
864 start += sce->ics.swb_sizes[curband];
868 avg_energy += coeffs[i] * coeffs[i];
869 last = FFMAX(last, i);
870 lastband = FFMAX(lastband, curband);
877 if (avg_energy == 0.0f) {
878 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
879 sce->sf_idx[i] = SCALE_ONE_POS;
882 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
884 for (g = 0; g < sce->ics.num_swb; g++) {
885 float *coefs = sce->coeffs + start;
886 const int size = sce->ics.swb_sizes[g];
887 int start2 = start, end2 = start + size, peakpos = start;
888 float maxval = -1, thr = 0.0f, t;
893 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
894 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
897 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
898 for (i = 0; i < size; i++) {
899 float t = coefs[w2*128+i]*coefs[w2*128+i];
900 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
902 if (sce->ics.num_windows == 1 && maxval < t) {
908 if (sce->ics.num_windows == 1) {
909 start2 = FFMAX(peakpos - 2, start2);
910 end2 = FFMIN(peakpos + 3, end2);
916 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
917 t = 1.0 - (1.0 * start2 / last);
918 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
921 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
922 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
923 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
925 for (g = 0; g < sce->ics.num_swb; g++) {
926 const float *coefs = sce->coeffs + start;
927 const float *scaled = s->scoefs + start;
928 const int size = sce->ics.swb_sizes[g];
929 int scf, prev_scf, step;
930 int min_scf = -1, max_scf = 256;
932 if (maxq[w*16+g] < 21.544) {
933 sce->zeroes[w*16+g] = 1;
937 sce->zeroes[w*16+g] = 0;
938 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
944 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
946 dist += quantize_band_cost(s, coefs + w2*128,
948 sce->ics.swb_sizes[g],
956 dist *= 1.0f / 512.0f / lambda;
957 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
958 if (quant_max >= 8191) { // too much, return to the previous quantizer
959 sce->sf_idx[w*16+g] = prev_scf;
963 curdiff = fabsf(dist - uplim[w*16+g]);
967 step = log2f(curdiff);
968 if (dist > uplim[w*16+g])
971 scf = av_clip_uint8(scf);
972 step = scf - prev_scf;
973 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
974 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
985 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
986 for (i = 1; i < 128; i++) {
988 sce->sf_idx[i] = sce->sf_idx[i-1];
990 minq = FFMIN(minq, sce->sf_idx[i]);
994 minq = FFMIN(minq, SCALE_MAX_POS);
995 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
996 for (i = 126; i >= 0; i--) {
998 sce->sf_idx[i] = sce->sf_idx[i+1];
999 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
1003 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
1004 SingleChannelElement *sce,
1007 int start = 0, i, w, w2, g;
1010 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
1011 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1013 for (g = 0; g < sce->ics.num_swb; g++) {
1014 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
1015 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
1016 if (band->energy <= band->threshold) {
1017 sce->sf_idx[(w+w2)*16+g] = 218;
1018 sce->zeroes[(w+w2)*16+g] = 1;
1020 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
1021 sce->zeroes[(w+w2)*16+g] = 0;
1023 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
1027 for (i = 0; i < 128; i++) {
1028 sce->sf_idx[i] = 140;
1029 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
1031 //set the same quantizers inside window groups
1032 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
1033 for (g = 0; g < sce->ics.num_swb; g++)
1034 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
1035 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
1038 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
1041 int start = 0, i, w, w2, g;
1042 float M[128], S[128];
1043 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
1044 SingleChannelElement *sce0 = &cpe->ch[0];
1045 SingleChannelElement *sce1 = &cpe->ch[1];
1046 if (!cpe->common_window)
1048 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
1049 for (g = 0; g < sce0->ics.num_swb; g++) {
1050 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
1051 float dist1 = 0.0f, dist2 = 0.0f;
1052 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1053 FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
1054 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
1055 float minthr = FFMIN(band0->threshold, band1->threshold);
1056 float maxthr = FFMAX(band0->threshold, band1->threshold);
1057 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1058 M[i] = (sce0->coeffs[start+w2*128+i]
1059 + sce1->coeffs[start+w2*128+i]) * 0.5;
1060 S[i] = sce0->coeffs[start+w2*128+i]
1061 - sce1->coeffs[start+w2*128+i];
1063 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1064 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1065 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
1066 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
1067 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
1069 sce0->ics.swb_sizes[g],
1070 sce0->sf_idx[(w+w2)*16+g],
1071 sce0->band_type[(w+w2)*16+g],
1072 lambda / band0->threshold, INFINITY, NULL);
1073 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1075 sce1->ics.swb_sizes[g],
1076 sce1->sf_idx[(w+w2)*16+g],
1077 sce1->band_type[(w+w2)*16+g],
1078 lambda / band1->threshold, INFINITY, NULL);
1079 dist2 += quantize_band_cost(s, M,
1081 sce0->ics.swb_sizes[g],
1082 sce0->sf_idx[(w+w2)*16+g],
1083 sce0->band_type[(w+w2)*16+g],
1084 lambda / maxthr, INFINITY, NULL);
1085 dist2 += quantize_band_cost(s, S,
1087 sce1->ics.swb_sizes[g],
1088 sce1->sf_idx[(w+w2)*16+g],
1089 sce1->band_type[(w+w2)*16+g],
1090 lambda / minthr, INFINITY, NULL);
1092 cpe->ms_mask[w*16+g] = dist2 < dist1;
1094 start += sce0->ics.swb_sizes[g];
1099 AACCoefficientsEncoder ff_aac_coders[] = {
1101 search_for_quantizers_faac,
1102 encode_window_bands_info,
1103 quantize_and_encode_band,
1107 search_for_quantizers_anmr,
1108 encode_window_bands_info,
1109 quantize_and_encode_band,
1113 search_for_quantizers_twoloop,
1114 codebook_trellis_rate,
1115 quantize_and_encode_band,
1119 search_for_quantizers_fast,
1120 encode_window_bands_info,
1121 quantize_and_encode_band,