1 /*
2 * AAC encoder twoloop coder
3 * Copyright (C) 2008-2009 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * AAC encoder twoloop coder
25 * @author Konstantin Shishkov, Claudio Freire
26 */
27
28 /**
29 * This file contains a template for the twoloop coder function.
30 * It needs to be provided, externally, as an already included declaration,
31 * the following functions from aacenc_quantization/util.h. They're not included
32 * explicitly here to make it possible to provide alternative implementations:
33 * - quantize_band_cost
34 * - abs_pow34_v
35 * - find_max_val
36 * - find_min_book
37 * - find_form_factor
38 */
39
40 #ifndef AVCODEC_AACCODER_TWOLOOP_H
41 #define AVCODEC_AACCODER_TWOLOOP_H
42
52
53 /** Frequency in Hz for lower limit of noise substitution **/
54 #define NOISE_LOW_LIMIT 4000
55
56 #define sclip(x) av_clip(x,60,218)
57
58 /* Reflects the cost to change codebooks */
60 {
62 }
63
64 /**
65 * two-loop quantizers search taken from ISO 13818-7 Appendix C
66 */
70 const float lambda)
71 {
72 int start = 0,
i,
w, w2,
g, recomprd;
75 * (lambda / 120.f);
76 int refbits = destbits;
77 int toomanybits, toofewbits;
78 char nzs[128];
79 uint8_t nextband[128];
80 int maxsf[128], minsf[128];
81 float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
82 float maxvals[128], spread_thr_r[128];
83 float min_spread_thr_r, max_spread_thr_r;
84
85 /**
86 * rdlambda controls the maximum tolerated distortion. Twoloop
87 * will keep iterating until it fails to lower it or it reaches
88 * ulimit * rdlambda. Keeping it low increases quality on difficult
89 * signals, but lower it too much, and bits will be taken from weak
90 * signals, creating "holes". A balance is necessary.
91 * rdmax and rdmin specify the relative deviation from rdlambda
92 * allowed for tonality compensation
93 */
94 float rdlambda =
av_clipf(2.0
f * 120.
f / lambda, 0.0625
f, 16.0
f);
95 const float nzslope = 1.5f;
96 float rdmin = 0.03125f;
97 float rdmax = 1.0f;
98
99 /**
100 * sfoffs controls an offset of optmium allocation that will be
101 * applied based on lambda. Keep it real and modest, the loop
102 * will take care of the rest, this just accelerates convergence
103 */
105
106 int fflag, minscaler, maxscaler, nminscaler;
107 int its = 0;
108 int maxits = 30;
109 int allz = 0;
110 int tbits;
111 int cutoff = 1024;
112 int pns_start_pos;
113 int prev;
114
115 /**
116 * zeroscale controls a multiplier of the threshold, if band energy
117 * is below this, a zero is forced. Keep it lower than 1, unless
118 * low lambda is used, because energy < threshold doesn't mean there's
119 * no audible signal outright, it's just energy. Also make it rise
120 * slower than rdlambda, as rdscale has due compensation with
121 * noisy band depriorization below, whereas zeroing logic is rather dumb
122 */
123 float zeroscale;
124 if (lambda > 120.
f) {
126 } else {
127 zeroscale = 1.f;
128 }
129
130 if (
s->psy.bitres.alloc >= 0) {
131 /**
132 * Psy granted us extra bits to use, from the reservoire
133 * adjust for lambda except what psy already did
134 */
135 destbits =
s->psy.bitres.alloc
137 }
138
140 /**
141 * Constant Q-scale doesn't compensate MS coding on its own
142 * No need to be overly precise, this only controls RD
143 * adjustment CB limits when going overboard
144 */
145 if (
s->options.mid_side &&
s->cur_type ==
TYPE_CPE)
146 destbits *= 2;
147
148 /**
149 * When using a constant Q-scale, don't adjust bits, just use RD
150 * Don't let it go overboard, though... 8x psy target is enough
151 */
152 toomanybits = 5800;
153 toofewbits = destbits / 16;
154
155 /** Don't offset scalers, just RD */
157 rdlambda =
sqrtf(rdlambda);
158
159 /** search further */
160 maxits *= 2;
161 } else {
162 /* When using ABR, be strict, but a reasonable leeway is
163 * critical to allow RC to smoothly track desired bitrate
164 * without sudden quality drops that cause audible artifacts.
165 * Symmetry is also desirable, to avoid systematic bias.
166 */
167 toomanybits = destbits + destbits/8;
168 toofewbits = destbits - destbits/8;
169
170 sfoffs = 0;
171 rdlambda =
sqrtf(rdlambda);
172 }
173
174 /** and zero out above cutoff frequency */
175 {
177 int bandwidth;
178
179 /**
180 * Scale, psy gives us constant quality, this LP only scales
181 * bitrate by lambda, so we save bits on subjectively unimportant HF
182 * rather than increase quantization noise. Adjust nominal bitrate
183 * to effective bitrate according to encoding parameters,
184 * AAC_CUTOFF_FROM_BITRATE is calibrated for effective bitrate.
185 */
186 float rate_bandwidth_multiplier = 1.5f;
188 ? (refbits * rate_bandwidth_multiplier * avctx->
sample_rate / 1024)
190
191 /** Compensate for extensions that increase efficiency */
192 if (
s->options.pns ||
s->options.intensity_stereo)
193 frame_bit_rate *= 1.15f;
194
196 bandwidth = avctx->
cutoff;
197 } else {
199 s->psy.cutoff = bandwidth;
200 }
201
202 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
204 }
205
206 /**
207 * for values above this the decoder might end up in an endless loop
208 * due to always having more bits than what can be encoded.
209 */
210 destbits =
FFMIN(destbits, 5800);
211 toomanybits =
FFMIN(toomanybits, 5800);
212 toofewbits =
FFMIN(toofewbits, 5800);
213 /**
214 * XXX: some heuristic to determine initial quantizers will reduce search time
215 * determine zero bands and upper distortion limits
216 */
217 min_spread_thr_r = -1;
218 max_spread_thr_r = -1;
221 int nz = 0;
222 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
224 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
227 continue;
228 }
229 nz = 1;
230 }
231 if (!nz) {
232 uplim = 0.0f;
233 } else {
234 nz = 0;
236 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
238 continue;
242 nz++;
243 }
244 }
245 uplims[
w*16+
g] = uplim;
246 energies[
w*16+
g] = energy;
249 allz |= nz;
251 spread_thr_r[
w*16+
g] = energy * nz / (uplim * spread);
252 if (min_spread_thr_r < 0) {
253 min_spread_thr_r = max_spread_thr_r = spread_thr_r[
w*16+
g];
254 } else {
255 min_spread_thr_r =
FFMIN(min_spread_thr_r, spread_thr_r[
w*16+
g]);
256 max_spread_thr_r =
FFMAX(max_spread_thr_r, spread_thr_r[
w*16+
g]);
257 }
258 }
259 }
260 }
261
262 /** Compute initial scalers */
263 minscaler = 65535;
268 continue;
269 }
270 /**
271 * log2f-to-distortion ratio is, technically, 2 (1.5db = 4, but it's power vs level so it's 2).
272 * But, as offsets are applied, low-frequency signals are too sensitive to the induced distortion,
273 * so we make scaling more conservative by choosing a lower log2f-to-distortion ratio, and thus
274 * more robust.
275 */
279 + sfoffs,
282 }
283 }
284
285 /** Clip */
291
292 if (!allz)
293 return;
294 s->abs_pow34(
s->scoefs, sce->
coeffs, 1024);
296
297 for (
i = 0;
i <
sizeof(minsf) /
sizeof(minsf[0]); ++
i)
302 const float *scaled =
s->scoefs + start;
303 int minsfidx;
305 if (maxvals[
w*16+
g] > 0) {
308 minsf[(
w+w2)*16+
g] = minsfidx;
309 }
311 }
312 }
313
314 /**
315 * Scale uplims to match rate distortion to quality
316 * bu applying noisy band depriorization and tonal band priorization.
317 * Maxval-energy ratio gives us an idea of how noisy/tonal the band is.
318 * If maxval^2 ~ energy, then that band is mostly noise, and we can relax
319 * rate distortion requirements.
320 */
321 memcpy(euplims, uplims, sizeof(euplims));
323 /** psy already priorizes transients to some extent */
333 nzslope * cleanup_factor);
334 energy2uplim *= de_psy_factor;
336 /** In ABR, we need to priorize less and let rate control do its thing */
337 energy2uplim =
sqrtf(energy2uplim);
338 }
339 energy2uplim =
FFMAX(0.015625
f,
FFMIN(1.0
f, energy2uplim));
340 uplims[
w*16+
g] *=
av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
342
347 2.0f);
348 energy2uplim *= de_psy_factor;
350 /** In ABR, we need to priorize less and let rate control do its thing */
351 energy2uplim =
sqrtf(energy2uplim);
352 }
353 energy2uplim =
FFMAX(0.015625
f,
FFMIN(1.0
f, energy2uplim));
355 0.5f, 1.0f);
356 }
358 }
359 }
360
361 for (
i = 0;
i <
sizeof(maxsf) /
sizeof(maxsf[0]); ++
i)
363
364 //perform two-loop search
365 //outer loop - improve quality
366 do {
367 //inner loop - quantize spectrum to fit into given number of bits
368 int overdist;
369 int qstep = its ? 1 : 32;
370 do {
371 int changed = 0;
372 prev = -1;
373 recomprd = 0;
374 tbits = 0;
378 const float *coefs = &sce->
coeffs[start];
379 const float *scaled = &
s->scoefs[start];
382 float dist = 0.0f;
383 float qenergy = 0.0f;
384
388 /** PNS isn't free */
390 }
391 continue;
392 }
396 float sqenergy;
398 scaled + w2*128,
402 1.0f,
405 0);
407 qenergy += sqenergy;
408 }
409 dists[
w*16+
g] = dist -
bits;
410 qenergies[
w*16+
g] = qenergy;
411 if (prev != -1) {
414 }
418 }
419 }
420 if (tbits > toomanybits) {
421 recomprd = 1;
422 for (
i = 0;
i < 128;
i++) {
426 if (new_sf != sce->
sf_idx[
i]) {
428 changed = 1;
429 }
430 }
431 }
432 } else if (tbits < toofewbits) {
433 recomprd = 1;
434 for (
i = 0;
i < 128;
i++) {
437 if (new_sf != sce->
sf_idx[
i]) {
439 changed = 1;
440 }
441 }
442 }
443 }
444 qstep >>= 1;
445 if (!qstep && tbits > toomanybits && sce->
sf_idx[0] < 217 && changed)
446 qstep = 1;
447 } while (qstep);
448
449 overdist = 1;
450 fflag = tbits < toofewbits;
451 for (
i = 0;
i < 2 && (overdist || recomprd); ++
i) {
452 if (recomprd) {
453 /** Must recompute distortion */
454 prev = -1;
455 tbits = 0;
459 const float *coefs = sce->
coeffs + start;
460 const float *scaled =
s->scoefs + start;
463 float dist = 0.0f;
464 float qenergy = 0.0f;
465
469 /** PNS isn't free */
471 }
472 continue;
473 }
477 float sqenergy;
479 scaled + w2*128,
483 1.0f,
486 0);
488 qenergy += sqenergy;
489 }
490 dists[
w*16+
g] = dist -
bits;
491 qenergies[
w*16+
g] = qenergy;
492 if (prev != -1) {
495 }
499 }
500 }
501 }
502 if (!
i &&
s->options.pns && its > maxits/2 && tbits > toofewbits) {
503 float maxoverdist = 0.0f;
504 float ovrfactor = 1.f+(maxits-its)*16.
f/maxits;
505 overdist = recomprd = 0;
509 float ovrdist = dists[
w*16+
g] /
FFMAX(uplims[
w*16+
g],euplims[
w*16+
g]);
510 maxoverdist =
FFMAX(maxoverdist, ovrdist);
511 overdist++;
512 }
513 }
514 }
515 if (overdist) {
516 /* We have overdistorted bands, trade for zeroes (that can be noise)
517 * Zero the bands in the lowest 1.25% spread-energy-threshold ranking
518 */
519 float minspread = max_spread_thr_r;
520 float maxspread = min_spread_thr_r;
521 float zspread;
522 int zeroable = 0;
523 int zeroed = 0;
524 int maxzeroed, zloop;
528 minspread =
FFMIN(minspread, spread_thr_r[
w*16+
g]);
529 maxspread =
FFMAX(maxspread, spread_thr_r[
w*16+
g]);
530 zeroable++;
531 }
532 }
533 }
534 zspread = (maxspread-minspread) * 0.0125
f + minspread;
535 /* Don't PNS everything even if allowed. It suppresses bit starvation signals from RC,
536 * and forced the hand of the later search_for_pns step.
537 * Instead, PNS a fraction of the spread_thr_r range depending on how starved for bits we are,
538 * and leave further PNSing to search_for_pns if worthwhile.
539 */
540 zspread =
FFMIN3(min_spread_thr_r * 8.
f, zspread,
541 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
542 maxzeroed =
FFMIN(zeroable,
FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
543 for (zloop = 0; zloop < 2; zloop++) {
544 /* Two passes: first distorted stuff - two birds in one shot and all that,
545 * then anything viable. Viable means not zero, but either CB=zero-able
546 * (too high SF), not SF <= 1 (that means we'd be operating at very high
547 * quality, we don't want PNS when doing VHQ), PNS allowed, and within
548 * the lowest ranking percentile.
549 */
550 float loopovrfactor = (zloop) ? 1.0
f : ovrfactor;
552 int mcb;
553 for (
g = sce->
ics.
num_swb-1;
g > 0 && zeroed < maxzeroed;
g--) {
555 continue;
560 || (mcb <= 1 && dists[w*16+g] >
FFMIN(uplims[
w*16+
g], euplims[
w*16+
g]))) ) {
563 zeroed++;
564 }
565 }
566 }
567 }
568 if (zeroed)
569 recomprd = fflag = 1;
570 } else {
571 overdist = 0;
572 }
573 }
574 }
575
577 maxscaler = 0;
583 }
584 }
585 }
586
588 prev = -1;
590 /** Start with big steps, end up fine-tunning */
591 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
592 int edepth = depth+2;
593 float uplmax = its / (maxits*0.25f) + 1.0
f;
594 uplmax *= (tbits > destbits) ?
FFMIN(2.0
f, tbits / (
float)
FFMAX(1,destbits)) : 1.0
f;
598 if (prev < 0 && !sce->zeroes[
w*16+
g])
601 const float *coefs = sce->
coeffs + start;
602 const float *scaled =
s->scoefs + start;
606 if ((!cmb || dists[
w*16+
g] > uplims[
w*16+
g]) && sce->
sf_idx[
w*16+
g] >
FFMAX(mindeltasf, minsf[
w*16+
g])) {
607 /* Try to make sure there is some energy in every nonzero band
608 * NOTE: This algorithm must be forcibly imbalanced, pushing harder
609 * on holes or more distorted bands at first, otherwise there's
610 * no net gain (since the next iteration will offset all bands
611 * on the opposite direction to compensate for extra bits)
612 */
613 for (
i = 0;
i < edepth && sce->
sf_idx[
w*16+
g] > mindeltasf; ++
i) {
615 float dist, qenergy;
618 dist = qenergy = 0.f;
622 }
else if (
i >= depth && dists[
w*16+
g] < euplims[
w*16+
g]) {
623 break;
624 }
625 /* !g is the DC band, it's important, since quantization error here
626 * applies to less than a cycle, it creates horrible intermodulation
627 * distortion if it doesn't stick to what psy requests
628 */
633 float sqenergy;
635 scaled + w2*128,
639 1.0f,
642 0);
644 qenergy += sqenergy;
645 }
647 dists[
w*16+
g] = dist -
bits;
648 qenergies[
w*16+
g] = qenergy;
649 if (
mb && (sce->
sf_idx[
w*16+
g] < mindeltasf || (
650 (dists[
w*16+
g] <
FFMIN(uplmax*uplims[
w*16+
g], euplims[
w*16+
g]))
651 && (
fabsf(qenergies[
w*16+
g]-energies[
w*16+
g]) < euplims[
w*16+
g])
652 ) )) {
653 break;
654 }
655 }
656 }
else if (tbits > toofewbits && sce->
sf_idx[
w*16+
g] <
FFMIN(maxdeltasf, maxsf[
w*16+
g])
657 && (dists[
w*16+
g] <
FFMIN(euplims[
w*16+
g], uplims[
w*16+
g]))
658 && (
fabsf(qenergies[
w*16+
g]-energies[
w*16+
g]) < euplims[
w*16+
g])
659 ) {
660 /** Um... over target. Save bits for more important stuff. */
661 for (
i = 0;
i < depth && sce->
sf_idx[
w*16+
g] < maxdeltasf; ++
i) {
663 float dist, qenergy;
666 dist = qenergy = 0.f;
670 float sqenergy;
672 scaled + w2*128,
676 1.0f,
679 0);
681 qenergy += sqenergy;
682 }
684 if (dist <
FFMIN(euplims[
w*16+
g], uplims[
w*16+
g])) {
686 dists[
w*16+
g] = dist;
687 qenergies[
w*16+
g] = qenergy;
688 } else {
689 break;
690 }
691 } else {
693 break;
694 }
695 }
696 }
699 fflag = 1;
702 }
704 }
705 }
706
707 /** SF difference limit violation risk. Must re-clamp. */
708 prev = -1;
713 if (prev < 0)
714 prev = prevsf;
718 if (!fflag && prevsf != sce->
sf_idx[
w*16+
g])
719 fflag = 1;
720 }
721 }
722 }
723
724 its++;
725 } while (fflag && its < maxits);
726
727 /** Scout out next nonzero bands */
729
730 prev = -1;
732 /** Make sure proper codebooks are set */
738 /** Cannot zero out, make sure it's not attempted */
740 } else {
743 }
744 }
745 } else {
747 }
748 /** Check that there's no SF delta range violations */
750 if (prev != -1) {
753 }
else if (sce->
zeroes[0]) {
754 /** Set global gain to something useful */
756 }
758 }
759 }
760 }
761 }
762
763 #endif /* AVCODEC_AACCODER_TWOLOOP_H */