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
43 /** bits needed to code codebook run value for long windows */
44 static const uint8_t run_value_bits_long[64] = {
45 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
46 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
47 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
48 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
51 /** bits needed to code codebook run value for short windows */
52 static const uint8_t run_value_bits_short[16] = {
53 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
56 static const uint8_t *run_value_bits[2] = {
57 run_value_bits_long, run_value_bits_short
62 * Quantize one coefficient.
63 * @return absolute value of the quantized coefficient
64 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
66 static av_always_inline int quant(float coef, const float Q)
69 return sqrtf(a * sqrtf(a)) + 0.4054;
72 static void quantize_bands(int *out, const float *in, const float *scaled,
73 int size, float Q34, int is_signed, int maxval)
77 for (i = 0; i < size; i++) {
79 out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
80 if (is_signed && in[i] < 0.0f) {
86 static void abs_pow34_v(float *out, const float *in, const int size)
88 #ifndef USE_REALLY_FULL_SEARCH
90 for (i = 0; i < size; i++) {
91 float a = fabsf(in[i]);
92 out[i] = sqrtf(a * sqrtf(a));
94 #endif /* USE_REALLY_FULL_SEARCH */
97 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
98 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
101 * Calculate rate distortion cost for quantizing with given codebook
103 * @return quantization distortion
105 static av_always_inline float quantize_and_encode_band_cost_template(
106 struct AACEncContext *s,
107 PutBitContext *pb, const float *in,
108 const float *scaled, int size, int scale_idx,
109 int cb, const float lambda, const float uplim,
110 int *bits, int BT_ZERO, int BT_UNSIGNED,
111 int BT_PAIR, int BT_ESC)
113 const float IQ = ff_aac_pow2sf_tab[POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
114 const float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
115 const float CLIPPED_ESCAPE = 165140.0f*IQ;
118 const int dim = BT_PAIR ? 2 : 4;
120 const float Q34 = sqrtf(Q * sqrtf(Q));
121 const int range = aac_cb_range[cb];
122 const int maxval = aac_cb_maxval[cb];
126 for (i = 0; i < size; i++)
130 return cost * lambda;
133 abs_pow34_v(s->scoefs, in, size);
136 quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
142 for (i = 0; i < size; i += dim) {
144 int *quants = s->qcoefs + i;
148 for (j = 0; j < dim; j++) {
150 curidx += quants[j] + off;
152 curbits = ff_aac_spectral_bits[cb-1][curidx];
153 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
155 for (j = 0; j < dim; j++) {
156 float t = fabsf(in[i+j]);
158 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
159 if (t >= CLIPPED_ESCAPE) {
160 di = t - CLIPPED_ESCAPE;
163 int c = av_clip(quant(t, Q), 0, 8191);
164 di = t - c*cbrtf(c)*IQ;
165 curbits += av_log2(c)*2 - 4 + 1;
175 for (j = 0; j < dim; j++) {
176 float di = in[i+j] - vec[j]*IQ;
180 cost += rd * lambda + curbits;
185 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
187 for (j = 0; j < dim; j++)
188 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
189 put_bits(pb, 1, in[i+j] < 0.0f);
191 for (j = 0; j < 2; j++) {
192 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
193 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
194 int len = av_log2(coef);
196 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
197 put_bits(pb, len, coef & ((1 << len) - 1));
209 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
210 static float quantize_and_encode_band_cost_ ## NAME( \
211 struct AACEncContext *s, \
212 PutBitContext *pb, const float *in, \
213 const float *scaled, int size, int scale_idx, \
214 int cb, const float lambda, const float uplim, \
216 return quantize_and_encode_band_cost_template( \
217 s, pb, in, scaled, size, scale_idx, \
218 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
219 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \
222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0)
223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0)
224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0)
225 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0)
226 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0)
227 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1)
229 static float (*const quantize_and_encode_band_cost_arr[])(
230 struct AACEncContext *s,
231 PutBitContext *pb, const float *in,
232 const float *scaled, int size, int scale_idx,
233 int cb, const float lambda, const float uplim,
235 quantize_and_encode_band_cost_ZERO,
236 quantize_and_encode_band_cost_SQUAD,
237 quantize_and_encode_band_cost_SQUAD,
238 quantize_and_encode_band_cost_UQUAD,
239 quantize_and_encode_band_cost_UQUAD,
240 quantize_and_encode_band_cost_SPAIR,
241 quantize_and_encode_band_cost_SPAIR,
242 quantize_and_encode_band_cost_UPAIR,
243 quantize_and_encode_band_cost_UPAIR,
244 quantize_and_encode_band_cost_UPAIR,
245 quantize_and_encode_band_cost_UPAIR,
246 quantize_and_encode_band_cost_ESC,
249 #define quantize_and_encode_band_cost( \
250 s, pb, in, scaled, size, scale_idx, cb, \
251 lambda, uplim, bits) \
252 quantize_and_encode_band_cost_arr[cb]( \
253 s, pb, in, scaled, size, scale_idx, cb, \
256 static float quantize_band_cost(struct AACEncContext *s, const float *in,
257 const float *scaled, int size, int scale_idx,
258 int cb, const float lambda, const float uplim,
261 return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
262 cb, lambda, uplim, bits);
265 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
266 const float *in, int size, int scale_idx,
267 int cb, const float lambda)
269 quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
273 static float find_max_val(int group_len, int swb_size, const float *scaled) {
276 for (w2 = 0; w2 < group_len; w2++) {
277 for (i = 0; i < swb_size; i++) {
278 maxval = FFMAX(maxval, scaled[w2*128+i]);
284 static int find_min_book(float maxval, int sf) {
285 float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
286 float Q34 = sqrtf(Q * sqrtf(Q));
288 qmaxval = maxval * Q34 + 0.4054f;
289 if (qmaxval == 0) cb = 0;
290 else if (qmaxval == 1) cb = 1;
291 else if (qmaxval == 2) cb = 3;
292 else if (qmaxval <= 4) cb = 5;
293 else if (qmaxval <= 7) cb = 7;
294 else if (qmaxval <= 12) cb = 9;
300 * structure used in optimal codebook search
302 typedef struct BandCodingPath {
303 int prev_idx; ///< pointer to the previous path point
304 float cost; ///< path cost
309 * Encode band info for single window group bands.
311 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
312 int win, int group_len, const float lambda)
314 BandCodingPath path[120][12];
315 int w, swb, cb, start, start2, size;
317 const int max_sfb = sce->ics.max_sfb;
318 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
319 const int run_esc = (1 << run_bits) - 1;
320 int idx, ppos, count;
321 int stackrun[120], stackcb[120], stack_len;
322 float next_minrd = INFINITY;
325 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
327 for (cb = 0; cb < 12; cb++) {
328 path[0][cb].cost = 0.0f;
329 path[0][cb].prev_idx = -1;
332 for (swb = 0; swb < max_sfb; swb++) {
334 size = sce->ics.swb_sizes[swb];
335 if (sce->zeroes[win*16 + swb]) {
336 for (cb = 0; cb < 12; cb++) {
337 path[swb+1][cb].prev_idx = cb;
338 path[swb+1][cb].cost = path[swb][cb].cost;
339 path[swb+1][cb].run = path[swb][cb].run + 1;
342 float minrd = next_minrd;
343 int mincb = next_mincb;
344 next_minrd = INFINITY;
346 for (cb = 0; cb < 12; cb++) {
347 float cost_stay_here, cost_get_here;
349 for (w = 0; w < group_len; w++) {
350 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
351 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
352 s->scoefs + start + w*128, size,
353 sce->sf_idx[(win+w)*16+swb], cb,
354 lambda / band->threshold, INFINITY, NULL);
356 cost_stay_here = path[swb][cb].cost + rd;
357 cost_get_here = minrd + rd + run_bits + 4;
358 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
359 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
360 cost_stay_here += run_bits;
361 if (cost_get_here < cost_stay_here) {
362 path[swb+1][cb].prev_idx = mincb;
363 path[swb+1][cb].cost = cost_get_here;
364 path[swb+1][cb].run = 1;
366 path[swb+1][cb].prev_idx = cb;
367 path[swb+1][cb].cost = cost_stay_here;
368 path[swb+1][cb].run = path[swb][cb].run + 1;
370 if (path[swb+1][cb].cost < next_minrd) {
371 next_minrd = path[swb+1][cb].cost;
376 start += sce->ics.swb_sizes[swb];
379 //convert resulting path from backward-linked list
382 for (cb = 1; cb < 12; cb++)
383 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
388 stackrun[stack_len] = path[ppos][cb].run;
389 stackcb [stack_len] = cb;
390 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
391 ppos -= path[ppos][cb].run;
394 //perform actual band info encoding
396 for (i = stack_len - 1; i >= 0; i--) {
397 put_bits(&s->pb, 4, stackcb[i]);
399 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
400 //XXX: memset when band_type is also uint8_t
401 for (j = 0; j < count; j++) {
402 sce->band_type[win*16 + start] = stackcb[i];
405 while (count >= run_esc) {
406 put_bits(&s->pb, run_bits, run_esc);
409 put_bits(&s->pb, run_bits, count);
413 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
414 int win, int group_len, const float lambda)
416 BandCodingPath path[120][12];
417 int w, swb, cb, start, start2, size;
419 const int max_sfb = sce->ics.max_sfb;
420 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
421 const int run_esc = (1 << run_bits) - 1;
422 int idx, ppos, count;
423 int stackrun[120], stackcb[120], stack_len;
424 float next_minrd = INFINITY;
427 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
429 for (cb = 0; cb < 12; cb++) {
430 path[0][cb].cost = run_bits+4;
431 path[0][cb].prev_idx = -1;
434 for (swb = 0; swb < max_sfb; swb++) {
436 size = sce->ics.swb_sizes[swb];
437 if (sce->zeroes[win*16 + swb]) {
438 for (cb = 0; cb < 12; cb++) {
439 path[swb+1][cb].prev_idx = cb;
440 path[swb+1][cb].cost = path[swb][cb].cost;
441 path[swb+1][cb].run = path[swb][cb].run + 1;
444 float minrd = next_minrd;
445 int mincb = next_mincb;
446 int startcb = sce->band_type[win*16+swb];
447 next_minrd = INFINITY;
449 for (cb = 0; cb < startcb; cb++) {
450 path[swb+1][cb].cost = 61450;
451 path[swb+1][cb].prev_idx = -1;
452 path[swb+1][cb].run = 0;
454 for (cb = startcb; cb < 12; cb++) {
455 float cost_stay_here, cost_get_here;
457 for (w = 0; w < group_len; w++) {
458 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
459 s->scoefs + start + w*128, size,
460 sce->sf_idx[(win+w)*16+swb], cb,
463 cost_stay_here = path[swb][cb].cost + rd;
464 cost_get_here = minrd + rd + run_bits + 4;
465 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
466 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
467 cost_stay_here += run_bits;
468 if (cost_get_here < cost_stay_here) {
469 path[swb+1][cb].prev_idx = mincb;
470 path[swb+1][cb].cost = cost_get_here;
471 path[swb+1][cb].run = 1;
473 path[swb+1][cb].prev_idx = cb;
474 path[swb+1][cb].cost = cost_stay_here;
475 path[swb+1][cb].run = path[swb][cb].run + 1;
477 if (path[swb+1][cb].cost < next_minrd) {
478 next_minrd = path[swb+1][cb].cost;
483 start += sce->ics.swb_sizes[swb];
486 //convert resulting path from backward-linked list
489 for (cb = 1; cb < 12; cb++)
490 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
496 stackrun[stack_len] = path[ppos][cb].run;
497 stackcb [stack_len] = cb;
498 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
499 ppos -= path[ppos][cb].run;
502 //perform actual band info encoding
504 for (i = stack_len - 1; i >= 0; i--) {
505 put_bits(&s->pb, 4, stackcb[i]);
507 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
508 //XXX: memset when band_type is also uint8_t
509 for (j = 0; j < count; j++) {
510 sce->band_type[win*16 + start] = stackcb[i];
513 while (count >= run_esc) {
514 put_bits(&s->pb, run_bits, run_esc);
517 put_bits(&s->pb, run_bits, count);
521 /** Return the minimum scalefactor where the quantized coef does not clip. */
522 static av_always_inline uint8_t coef2minsf(float coef) {
523 return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
526 /** Return the maximum scalefactor where the quantized coef is not zero. */
527 static av_always_inline uint8_t coef2maxsf(float coef) {
528 return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
531 typedef struct TrellisPath {
536 #define TRELLIS_STAGES 121
537 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
539 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
540 SingleChannelElement *sce,
543 int q, w, w2, g, start = 0;
546 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
547 int bandaddr[TRELLIS_STAGES];
550 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
551 int q0, q1, qcnt = 0;
553 for (i = 0; i < 1024; i++) {
554 float t = fabsf(sce->coeffs[i]);
564 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
565 memset(sce->zeroes, 1, sizeof(sce->zeroes));
569 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
570 q0 = coef2minsf(q0f);
571 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
572 q1 = coef2maxsf(q1f);
573 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
577 //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
578 int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
581 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
585 } else if (q1 > q1high) {
590 //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
592 for (i = 0; i < TRELLIS_STATES; i++) {
593 paths[0][i].cost = 0.0f;
594 paths[0][i].prev = -1;
596 for (j = 1; j < TRELLIS_STAGES; j++) {
597 for (i = 0; i < TRELLIS_STATES; i++) {
598 paths[j][i].cost = INFINITY;
599 paths[j][i].prev = -2;
603 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
604 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
606 for (g = 0; g < sce->ics.num_swb; g++) {
607 const float *coefs = sce->coeffs + start;
611 bandaddr[idx] = w * 16 + g;
614 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
615 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
616 if (band->energy <= band->threshold || band->threshold == 0.0f) {
617 sce->zeroes[(w+w2)*16+g] = 1;
620 sce->zeroes[(w+w2)*16+g] = 0;
622 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
623 float t = fabsf(coefs[w2*128+i]);
625 qmin = FFMIN(qmin, t);
626 qmax = FFMAX(qmax, t);
630 int minscale, maxscale;
631 float minrd = INFINITY;
633 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
634 minscale = coef2minsf(qmin);
635 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
636 maxscale = coef2maxsf(qmax);
637 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
638 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
639 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
640 for (q = minscale; q < maxscale; q++) {
642 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
643 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
644 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
645 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
646 q + q0, cb, lambda / band->threshold, INFINITY, NULL);
648 minrd = FFMIN(minrd, dist);
650 for (i = 0; i < q1 - q0; i++) {
652 cost = paths[idx - 1][i].cost + dist
653 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
654 if (cost < paths[idx][q].cost) {
655 paths[idx][q].cost = cost;
656 paths[idx][q].prev = i;
661 for (q = 0; q < q1 - q0; q++) {
662 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
663 paths[idx][q].prev = q;
666 sce->zeroes[w*16+g] = !nz;
667 start += sce->ics.swb_sizes[g];
672 mincost = paths[idx][0].cost;
674 for (i = 1; i < TRELLIS_STATES; i++) {
675 if (paths[idx][i].cost < mincost) {
676 mincost = paths[idx][i].cost;
681 sce->sf_idx[bandaddr[idx]] = minq + q0;
682 minq = paths[idx][minq].prev;
685 //set the same quantizers inside window groups
686 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
687 for (g = 0; g < sce->ics.num_swb; g++)
688 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
689 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
693 * two-loop quantizers search taken from ISO 13818-7 Appendix C
695 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
697 SingleChannelElement *sce,
700 int start = 0, i, w, w2, g;
701 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
702 float dists[128], uplims[128];
704 int fflag, minscaler;
707 float minthr = INFINITY;
709 //XXX: some heuristic to determine initial quantizers will reduce search time
710 memset(dists, 0, sizeof(dists));
711 //determine zero bands and upper limits
712 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
713 for (g = 0; g < sce->ics.num_swb; g++) {
716 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
717 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
718 uplim += band->threshold;
719 if (band->energy <= band->threshold || band->threshold == 0.0f) {
720 sce->zeroes[(w+w2)*16+g] = 1;
725 uplims[w*16+g] = uplim *512;
726 sce->zeroes[w*16+g] = !nz;
728 minthr = FFMIN(minthr, uplim);
732 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
733 for (g = 0; g < sce->ics.num_swb; g++) {
734 if (sce->zeroes[w*16+g]) {
735 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
738 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
744 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
746 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
748 for (g = 0; g < sce->ics.num_swb; g++) {
749 const float *scaled = s->scoefs + start;
750 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
751 start += sce->ics.swb_sizes[g];
755 //perform two-loop search
756 //outer loop - improve quality
759 minscaler = sce->sf_idx[0];
760 //inner loop - quantize spectrum to fit into given number of bits
761 qstep = its ? 1 : 32;
766 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
768 for (g = 0; g < sce->ics.num_swb; g++) {
769 const float *coefs = sce->coeffs + start;
770 const float *scaled = s->scoefs + start;
775 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
776 start += sce->ics.swb_sizes[g];
779 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
780 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
781 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
783 dist += quantize_band_cost(s, coefs + w2*128,
785 sce->ics.swb_sizes[g],
793 dists[w*16+g] = dist - bits;
795 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
798 start += sce->ics.swb_sizes[g];
799 prev = sce->sf_idx[w*16+g];
802 if (tbits > destbits) {
803 for (i = 0; i < 128; i++)
804 if (sce->sf_idx[i] < 218 - qstep)
805 sce->sf_idx[i] += qstep;
807 for (i = 0; i < 128; i++)
808 if (sce->sf_idx[i] > 60 - qstep)
809 sce->sf_idx[i] -= qstep;
812 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
817 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
818 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
819 for (g = 0; g < sce->ics.num_swb; g++) {
820 int prevsc = sce->sf_idx[w*16+g];
821 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
822 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
823 sce->sf_idx[w*16+g]--;
824 else //Try to make sure there is some energy in every band
825 sce->sf_idx[w*16+g]-=2;
827 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
828 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
829 if (sce->sf_idx[w*16+g] != prevsc)
831 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
835 } while (fflag && its < 10);
838 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
839 SingleChannelElement *sce,
842 int start = 0, i, w, w2, g;
843 float uplim[128], maxq[128];
845 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
846 int last = 0, lastband = 0, curband = 0;
847 float avg_energy = 0.0;
848 if (sce->ics.num_windows == 1) {
850 for (i = 0; i < 1024; i++) {
851 if (i - start >= sce->ics.swb_sizes[curband]) {
852 start += sce->ics.swb_sizes[curband];
855 if (sce->coeffs[i]) {
856 avg_energy += sce->coeffs[i] * sce->coeffs[i];
862 for (w = 0; w < 8; w++) {
863 const float *coeffs = sce->coeffs + w*128;
865 for (i = 0; i < 128; i++) {
866 if (i - start >= sce->ics.swb_sizes[curband]) {
867 start += sce->ics.swb_sizes[curband];
871 avg_energy += coeffs[i] * coeffs[i];
872 last = FFMAX(last, i);
873 lastband = FFMAX(lastband, curband);
880 if (avg_energy == 0.0f) {
881 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
882 sce->sf_idx[i] = SCALE_ONE_POS;
885 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
887 for (g = 0; g < sce->ics.num_swb; g++) {
888 float *coefs = sce->coeffs + start;
889 const int size = sce->ics.swb_sizes[g];
890 int start2 = start, end2 = start + size, peakpos = start;
891 float maxval = -1, thr = 0.0f, t;
896 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
897 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
900 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
901 for (i = 0; i < size; i++) {
902 float t = coefs[w2*128+i]*coefs[w2*128+i];
903 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
905 if (sce->ics.num_windows == 1 && maxval < t) {
911 if (sce->ics.num_windows == 1) {
912 start2 = FFMAX(peakpos - 2, start2);
913 end2 = FFMIN(peakpos + 3, end2);
919 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
920 t = 1.0 - (1.0 * start2 / last);
921 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
924 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
925 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
926 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
928 for (g = 0; g < sce->ics.num_swb; g++) {
929 const float *coefs = sce->coeffs + start;
930 const float *scaled = s->scoefs + start;
931 const int size = sce->ics.swb_sizes[g];
932 int scf, prev_scf, step;
933 int min_scf = -1, max_scf = 256;
935 if (maxq[w*16+g] < 21.544) {
936 sce->zeroes[w*16+g] = 1;
940 sce->zeroes[w*16+g] = 0;
941 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
947 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
949 dist += quantize_band_cost(s, coefs + w2*128,
951 sce->ics.swb_sizes[g],
959 dist *= 1.0f / 512.0f / lambda;
960 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
961 if (quant_max >= 8191) { // too much, return to the previous quantizer
962 sce->sf_idx[w*16+g] = prev_scf;
966 curdiff = fabsf(dist - uplim[w*16+g]);
970 step = log2f(curdiff);
971 if (dist > uplim[w*16+g])
974 scf = av_clip_uint8(scf);
975 step = scf - prev_scf;
976 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
977 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
988 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
989 for (i = 1; i < 128; i++) {
991 sce->sf_idx[i] = sce->sf_idx[i-1];
993 minq = FFMIN(minq, sce->sf_idx[i]);
997 minq = FFMIN(minq, SCALE_MAX_POS);
998 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
999 for (i = 126; i >= 0; i--) {
1000 if (!sce->sf_idx[i])
1001 sce->sf_idx[i] = sce->sf_idx[i+1];
1002 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
1006 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
1007 SingleChannelElement *sce,
1010 int start = 0, i, w, w2, g;
1013 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
1014 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1016 for (g = 0; g < sce->ics.num_swb; g++) {
1017 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
1018 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
1019 if (band->energy <= band->threshold) {
1020 sce->sf_idx[(w+w2)*16+g] = 218;
1021 sce->zeroes[(w+w2)*16+g] = 1;
1023 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
1024 sce->zeroes[(w+w2)*16+g] = 0;
1026 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
1030 for (i = 0; i < 128; i++) {
1031 sce->sf_idx[i] = 140;
1032 //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
1034 //set the same quantizers inside window groups
1035 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
1036 for (g = 0; g < sce->ics.num_swb; g++)
1037 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
1038 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
1041 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
1044 int start = 0, i, w, w2, g;
1045 float M[128], S[128];
1046 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
1047 SingleChannelElement *sce0 = &cpe->ch[0];
1048 SingleChannelElement *sce1 = &cpe->ch[1];
1049 if (!cpe->common_window)
1051 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
1052 for (g = 0; g < sce0->ics.num_swb; g++) {
1053 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
1054 float dist1 = 0.0f, dist2 = 0.0f;
1055 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
1056 FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
1057 FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
1058 float minthr = FFMIN(band0->threshold, band1->threshold);
1059 float maxthr = FFMAX(band0->threshold, band1->threshold);
1060 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
1061 M[i] = (sce0->coeffs[start+w2*128+i]
1062 + sce1->coeffs[start+w2*128+i]) * 0.5;
1064 - sce1->coeffs[start+w2*128+i];
1066 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1067 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
1068 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
1069 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
1070 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
1072 sce0->ics.swb_sizes[g],
1073 sce0->sf_idx[(w+w2)*16+g],
1074 sce0->band_type[(w+w2)*16+g],
1075 lambda / band0->threshold, INFINITY, NULL);
1076 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
1078 sce1->ics.swb_sizes[g],
1079 sce1->sf_idx[(w+w2)*16+g],
1080 sce1->band_type[(w+w2)*16+g],
1081 lambda / band1->threshold, INFINITY, NULL);
1082 dist2 += quantize_band_cost(s, M,
1084 sce0->ics.swb_sizes[g],
1085 sce0->sf_idx[(w+w2)*16+g],
1086 sce0->band_type[(w+w2)*16+g],
1087 lambda / maxthr, INFINITY, NULL);
1088 dist2 += quantize_band_cost(s, S,
1090 sce1->ics.swb_sizes[g],
1091 sce1->sf_idx[(w+w2)*16+g],
1092 sce1->band_type[(w+w2)*16+g],
1093 lambda / minthr, INFINITY, NULL);
1095 cpe->ms_mask[w*16+g] = dist2 < dist1;
1097 start += sce0->ics.swb_sizes[g];
1102 AACCoefficientsEncoder ff_aac_coders[] = {
1104 search_for_quantizers_faac,
1105 encode_window_bands_info,
1106 quantize_and_encode_band,
1110 search_for_quantizers_anmr,
1111 encode_window_bands_info,
1112 quantize_and_encode_band,
1116 search_for_quantizers_twoloop,
1117 codebook_trellis_rate,
1118 quantize_and_encode_band,
1122 search_for_quantizers_fast,
1123 encode_window_bands_info,
1124 quantize_and_encode_band,