40 #ifndef AVCODEC_AACCODER_TWOLOOP_H
41 #define AVCODEC_AACCODER_TWOLOOP_H
54 #define NOISE_LOW_LIMIT 4000
56 #define sclip(x) av_clip(x,60,218)
72 int start = 0,
i,
w, w2,
g, recomprd;
76 int refbits = destbits;
77 int toomanybits, toofewbits;
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;
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;
106 int fflag, minscaler, maxscaler, nminscaler;
124 if (lambda > 120.
f) {
130 if (
s->psy.bitres.alloc >= 0) {
135 destbits =
s->psy.bitres.alloc
145 if (
s->options.mid_side &&
s->cur_type ==
TYPE_CPE)
153 toofewbits = destbits / 16;
157 rdlambda =
sqrtf(rdlambda);
167 toomanybits = destbits + destbits/8;
168 toofewbits = destbits - destbits/8;
171 rdlambda =
sqrtf(rdlambda);
186 float rate_bandwidth_multiplier = 1.5f;
188 ? (refbits * rate_bandwidth_multiplier * avctx->
sample_rate / 1024)
192 if (
s->options.pns ||
s->options.intensity_stereo)
193 frame_bit_rate *= 1.15f;
196 bandwidth = avctx->
cutoff;
199 s->psy.cutoff = bandwidth;
202 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
210 destbits =
FFMIN(destbits, 5800);
211 toomanybits =
FFMIN(toomanybits, 5800);
212 toofewbits =
FFMIN(toofewbits, 5800);
217 min_spread_thr_r = -1;
218 max_spread_thr_r = -1;
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];
236 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
245 uplims[
w*16+
g] = uplim;
246 energies[
w*16+
g] = energy;
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];
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]);
294 s->abs_pow34(
s->scoefs, sce->
coeffs, 1024);
297 for (
i = 0;
i <
sizeof(minsf) /
sizeof(minsf[0]); ++
i)
302 const float *scaled =
s->scoefs + start;
305 if (maxvals[
w*16+
g] > 0) {
308 minsf[(
w+w2)*16+
g] = minsfidx;
321 memcpy(euplims, uplims,
sizeof(euplims));
333 nzslope * cleanup_factor);
334 energy2uplim *= de_psy_factor;
337 energy2uplim =
sqrtf(energy2uplim);
339 energy2uplim =
FFMAX(0.015625
f,
FFMIN(1.0
f, energy2uplim));
340 uplims[
w*16+
g] *=
av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
348 energy2uplim *= de_psy_factor;
351 energy2uplim =
sqrtf(energy2uplim);
353 energy2uplim =
FFMAX(0.015625
f,
FFMIN(1.0
f, energy2uplim));
361 for (
i = 0;
i <
sizeof(maxsf) /
sizeof(maxsf[0]); ++
i)
369 int qstep = its ? 1 : 32;
378 const float *coefs = &sce->
coeffs[start];
379 const float *scaled = &
s->scoefs[start];
383 float qenergy = 0.0f;
409 dists[
w*16+
g] = dist -
bits;
410 qenergies[
w*16+
g] = qenergy;
420 if (tbits > toomanybits) {
422 for (
i = 0;
i < 128;
i++) {
426 if (new_sf != sce->
sf_idx[
i]) {
432 }
else if (tbits < toofewbits) {
434 for (
i = 0;
i < 128;
i++) {
437 if (new_sf != sce->
sf_idx[
i]) {
445 if (!qstep && tbits > toomanybits && sce->
sf_idx[0] < 217 && changed)
450 fflag = tbits < toofewbits;
451 for (
i = 0;
i < 2 && (overdist || recomprd); ++
i) {
459 const float *coefs = sce->
coeffs + start;
460 const float *scaled =
s->scoefs + start;
464 float qenergy = 0.0f;
490 dists[
w*16+
g] = dist -
bits;
491 qenergies[
w*16+
g] = qenergy;
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);
519 float minspread = max_spread_thr_r;
520 float maxspread = min_spread_thr_r;
524 int maxzeroed, zloop;
528 minspread =
FFMIN(minspread, spread_thr_r[
w*16+
g]);
529 maxspread =
FFMAX(maxspread, spread_thr_r[
w*16+
g]);
534 zspread = (maxspread-minspread) * 0.0125
f + minspread;
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++) {
550 float loopovrfactor = (zloop) ? 1.0
f : ovrfactor;
553 for (
g = sce->
ics.
num_swb-1;
g > 0 && zeroed < maxzeroed;
g--) {
560 || (mcb <= 1 && dists[w*16+g] >
FFMIN(uplims[
w*16+
g], euplims[
w*16+
g]))) ) {
569 recomprd = fflag = 1;
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])) {
613 for (
i = 0;
i < edepth && sce->
sf_idx[
w*16+
g] > mindeltasf; ++
i) {
618 dist = qenergy = 0.f;
622 }
else if (
i >= depth && dists[
w*16+
g] < euplims[
w*16+
g]) {
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])
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])
661 for (
i = 0;
i < depth && sce->
sf_idx[
w*16+
g] < maxdeltasf; ++
i) {
666 dist = qenergy = 0.f;
684 if (dist <
FFMIN(euplims[
w*16+
g], uplims[
w*16+
g])) {
686 dists[
w*16+
g] = dist;
687 qenergies[
w*16+
g] = qenergy;
718 if (!fflag && prevsf != sce->
sf_idx[
w*16+
g])
725 }
while (fflag && its < maxits);
753 }
else if (sce->
zeroes[0]) {