40 #ifndef AVCODEC_AACCODER_TWOLOOP_H
41 #define AVCODEC_AACCODER_TWOLOOP_H
54 #define NOISE_LOW_LIMIT 4000
70 int start = 0,
i,
w, w2,
g, recomprd;
74 int refbits = destbits;
75 int toomanybits, toofewbits;
77 uint8_t nextband[128];
78 int maxsf[128], minsf[128];
79 float dists[128] = { 0 }, qenergies[128] = { 0 }, uplims[128], euplims[128], energies[128];
80 float maxvals[128], spread_thr_r[128];
81 float min_spread_thr_r, max_spread_thr_r;
92 float rdlambda =
av_clipf(2.0
f * 120.
f / lambda, 0.0625
f, 16.0
f);
93 const float nzslope = 1.5f;
94 float rdmin = 0.03125f;
104 int fflag, minscaler, nminscaler;
122 if (lambda > 120.
f) {
128 if (
s->psy.bitres.alloc >= 0) {
133 destbits =
s->psy.bitres.alloc
143 if (
s->options.mid_side &&
s->cur_type ==
TYPE_CPE)
151 toofewbits = destbits / 16;
155 rdlambda =
sqrtf(rdlambda);
165 toomanybits = destbits + destbits/8;
166 toofewbits = destbits - destbits/8;
169 rdlambda =
sqrtf(rdlambda);
184 float rate_bandwidth_multiplier = 1.5f;
186 ? (refbits * rate_bandwidth_multiplier * avctx->
sample_rate / 1024)
190 if (
s->options.pns ||
s->options.intensity_stereo)
191 frame_bit_rate *= 1.15f;
194 bandwidth = avctx->
cutoff;
197 s->psy.cutoff = bandwidth;
200 cutoff = bandwidth * 2 * wlen / avctx->
sample_rate;
208 destbits =
FFMIN(destbits, 5800);
209 toomanybits =
FFMIN(toomanybits, 5800);
210 toofewbits =
FFMIN(toofewbits, 5800);
215 min_spread_thr_r = -1;
216 max_spread_thr_r = -1;
220 float uplim = 0.0f, energy = 0.0f, spread = 0.0f;
222 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
234 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
243 uplims[
w*16+
g] = uplim;
244 energies[
w*16+
g] = energy;
249 spread_thr_r[
w*16+
g] = energy * nz / (uplim * spread);
250 if (min_spread_thr_r < 0) {
251 min_spread_thr_r = max_spread_thr_r = spread_thr_r[
w*16+
g];
253 min_spread_thr_r =
FFMIN(min_spread_thr_r, spread_thr_r[
w*16+
g]);
254 max_spread_thr_r =
FFMAX(max_spread_thr_r, spread_thr_r[
w*16+
g]);
292 s->aacdsp.abs_pow34(
s->scoefs, sce->
coeffs, 1024);
295 for (
i = 0;
i <
sizeof(minsf) /
sizeof(minsf[0]); ++
i)
300 const float *scaled =
s->scoefs + start;
303 if (maxvals[
w*16+
g] > 0) {
306 minsf[(
w+w2)*16+
g] = minsfidx;
319 memcpy(euplims, uplims,
sizeof(euplims));
331 nzslope * cleanup_factor);
332 energy2uplim *= de_psy_factor;
335 energy2uplim =
sqrtf(energy2uplim);
337 energy2uplim =
FFMAX(0.015625
f,
FFMIN(1.0
f, energy2uplim));
338 uplims[
w*16+
g] *=
av_clipf(rdlambda * energy2uplim, rdmin, rdmax)
346 energy2uplim *= de_psy_factor;
349 energy2uplim =
sqrtf(energy2uplim);
351 energy2uplim =
FFMAX(0.015625
f,
FFMIN(1.0
f, energy2uplim));
359 for (
i = 0;
i <
sizeof(maxsf) /
sizeof(maxsf[0]); ++
i)
367 int qstep = its ? 1 : 32;
376 const float *coefs = &sce->
coeffs[start];
377 const float *scaled = &
s->scoefs[start];
381 float qenergy = 0.0f;
407 dists[
w*16+
g] = dist -
bits;
408 qenergies[
w*16+
g] = qenergy;
418 if (tbits > toomanybits) {
420 for (
i = 0;
i < 128;
i++) {
424 if (new_sf != sce->
sf_idx[
i]) {
430 }
else if (tbits < toofewbits) {
432 for (
i = 0;
i < 128;
i++) {
435 if (new_sf != sce->
sf_idx[
i]) {
443 if (!qstep && tbits > toomanybits && sce->
sf_idx[0] < 217 && changed)
448 fflag = tbits < toofewbits;
449 for (
i = 0;
i < 2 && (overdist || recomprd); ++
i) {
457 const float *coefs = sce->
coeffs + start;
458 const float *scaled =
s->scoefs + start;
462 float qenergy = 0.0f;
488 dists[
w*16+
g] = dist -
bits;
489 qenergies[
w*16+
g] = qenergy;
500 if (!
i &&
s->options.pns && its > maxits/2 && tbits > toofewbits) {
501 float maxoverdist = 0.0f;
502 float ovrfactor = 1.f+(maxits-its)*16.
f/maxits;
503 overdist = recomprd = 0;
507 float ovrdist = dists[
w*16+
g] /
FFMAX(uplims[
w*16+
g],euplims[
w*16+
g]);
508 maxoverdist =
FFMAX(maxoverdist, ovrdist);
517 float minspread = max_spread_thr_r;
518 float maxspread = min_spread_thr_r;
522 int maxzeroed, zloop;
526 minspread =
FFMIN(minspread, spread_thr_r[
w*16+
g]);
527 maxspread =
FFMAX(maxspread, spread_thr_r[
w*16+
g]);
532 zspread = (maxspread-minspread) * 0.0125
f + minspread;
538 zspread =
FFMIN3(min_spread_thr_r * 8.
f, zspread,
539 ((toomanybits - tbits) * min_spread_thr_r + (tbits - toofewbits) * max_spread_thr_r) / (toomanybits - toofewbits + 1));
540 maxzeroed =
FFMIN(zeroable,
FFMAX(1, (zeroable * its + maxits - 1) / (2 * maxits)));
541 for (zloop = 0; zloop < 2; zloop++) {
548 float loopovrfactor = (zloop) ? 1.0
f : ovrfactor;
551 for (
g = sce->
ics.
num_swb-1;
g > 0 && zeroed < maxzeroed;
g--) {
558 || (mcb <= 1 && dists[w*16+g] >
FFMIN(uplims[
w*16+
g], euplims[
w*16+
g]))) ) {
567 recomprd = fflag = 1;
587 int depth = (its > maxits/2) ? ((its > maxits*2/3) ? 1 : 3) : 10;
588 int edepth = depth+2;
589 float uplmax = its / (maxits*0.25f) + 1.0
f;
590 uplmax *= (tbits > destbits) ?
FFMIN(2.0
f, tbits / (
float)
FFMAX(1,destbits)) : 1.0
f;
594 if (prev < 0 && !sce->zeroes[
w*16+
g])
597 const float *coefs = sce->
coeffs + start;
598 const float *scaled =
s->scoefs + start;
602 if ((!cmb || dists[
w*16+
g] > uplims[
w*16+
g]) && sce->
sf_idx[
w*16+
g] >
FFMAX(mindeltasf, minsf[
w*16+
g])) {
609 for (
i = 0;
i < edepth && sce->
sf_idx[
w*16+
g] > mindeltasf; ++
i) {
614 dist = qenergy = 0.f;
618 }
else if (
i >= depth && dists[
w*16+
g] < euplims[
w*16+
g]) {
643 dists[
w*16+
g] = dist -
bits;
644 qenergies[
w*16+
g] = qenergy;
645 if (
mb && (sce->
sf_idx[
w*16+
g] < mindeltasf || (
646 (dists[
w*16+
g] <
FFMIN(uplmax*uplims[
w*16+
g], euplims[
w*16+
g]))
647 && (
fabsf(qenergies[
w*16+
g]-energies[
w*16+
g]) < euplims[
w*16+
g])
652 }
else if (tbits > toofewbits && sce->
sf_idx[
w*16+
g] <
FFMIN(maxdeltasf, maxsf[
w*16+
g])
653 && (dists[
w*16+
g] <
FFMIN(euplims[
w*16+
g], uplims[
w*16+
g]))
654 && (
fabsf(qenergies[
w*16+
g]-energies[
w*16+
g]) < euplims[
w*16+
g])
657 for (
i = 0;
i < depth && sce->
sf_idx[
w*16+
g] < maxdeltasf; ++
i) {
662 dist = qenergy = 0.f;
680 if (dist <
FFMIN(euplims[
w*16+
g], uplims[
w*16+
g])) {
682 dists[
w*16+
g] = dist;
683 qenergies[
w*16+
g] = qenergy;
714 if (!fflag && prevsf != sce->
sf_idx[
w*16+
g])
721 }
while (fflag && its < maxits);
749 }
else if (sce->
zeroes[0]) {