43 #ifndef AVCODEC_AACCODER_NMR_H
44 #define AVCODEC_AACCODER_NMR_H
58 #define NMR_SFBITS(d) ff_aac_scalefactor_bits[av_clip((d) + SCALE_DIFF_ZERO, 0, 2*SCALE_MAX_DIFF)]
70 #define NMR_PNS_BITS 9
74 #define NMR_PNS_HOLE_FRAC 0.5f
75 #define NMR_PNS_HOLE_SPREAD 0.5f
79 #define NMR_RC_K_CBR 0.5f
81 #define NMR_RC_ITERS 8
84 #define NMR_RC_CORR 1.5f
88 #define NMR_CBR_BUF 512
89 #define NMR_RC_CITERS 3
93 #define NMR_BURST_GAP 10
94 #define NMR_BURST_GAIN 8.0f
95 #define NMR_RC_FITERS 4
96 #define NMR_RC_TRACK 0.1f
99 #define NMR_PNS_NDGATE 4.0f
103 #define NMR_PNS_MAX_ET 8.0f
107 #define NMR_PNS_LAM 100.0f
118 const int *blo,
const int *bnc,
int step,
119 const int *act,
int nact,
int destbits,
int *chosen,
120 float lo_l,
float hi_l,
int iters)
130 for (
int it = 0;
it < iters;
it++) {
131 lam =
sqrtf(lo_l * hi_l);
136 for (
int o = 0; o < bnc[
b0]; o++)
137 dp[o] = nd[
b0][o] + lam * nb[
b0][o];
139 for (
int k = 1; k < nact; k++) {
140 int b = act[k], pb = act[k-1];
141 memcpy(dpp, dp,
sizeof(dp));
142 for (
int o = 0; o < bnc[
b]; o++)
143 node[o] = nd[
b][o] + lam * nb[
b][o];
145 s->aacdsp.nmr_trellis_step(dp, bp[k], dpp, node, lamsf,
146 bnc[
b], bnc[pb], blo[
b] - blo[pb],
step,
151 int beo = 0,
b = act[nact-1];
153 for (
int o = 0; o < bnc[
b]; o++)
154 if (dp[o] < bec) { bec = dp[o]; beo = o; }
156 for (
int k = nact-1; k > 0; k--)
157 chosen[act[k-1]] = bp[k][chosen[act[k]]];
161 for (
int k = 0; k < nact; k++)
162 total += nb[act[k]][chosen[act[k]]];
163 for (
int k = 1; k < nact; k++)
164 total +=
NMR_SFBITS((blo[act[k]]+chosen[act[k]]*
step) - (blo[act[k-1]]+chosen[act[k-1]]*
step));
170 if (total > destbits)
181 int start,
int lo,
int step,
int maxn,
float invthr,
182 float maxval,
float *nd_row,
int *nb_row)
197 nd_row[ncand] = (dist - btot) * invthr;
198 nb_row[ncand] = btot;
211 int allz = 0, cutoff = 1024, nbnd = 0;
222 int bw[128], bg[128], bst[128];
236 s->nmr->counted[
s->cur_channel] = 0;
246 if (rc_eligible && avctx->
frame_num !=
s->nmr->rc_frame_num) {
247 if (
s->nmr->rc_frame_num > 0 &&
s->nmr->lam_rc > 0.0f)
248 s->nmr->rc_fill =
av_clip(
s->nmr->rc_fill + rc_rate_frame -
s->last_frame_pb_count,
256 if (!
s->nmr->prev_was_short)
259 s->nmr->frames_since_short = 0;
261 s->nmr->run_burst = 1.0f;
262 s->nmr->frames_since_short++;
264 s->nmr->prev_was_short = is_short;
266 int rc_global = rc_eligible &&
s->nmr->lam_rc > 0.0f;
268 if (
s->psy.bitres.alloc >= 0)
269 destbits =
s->psy.bitres.alloc *
271 if (rc_global &&
s->psy.bitres.alloc >= 0)
274 +
s->nmr->rc_fill / 2.0) /
s->channels;
275 destbits =
FFMIN(destbits, 5800);
278 if (
s->nmr->side_inited)
279 destbits =
av_clip(destbits - (
int)(
s->nmr->side_ema /
s->channels), 64, 5800);
283 destbits =
av_clip((
int)(destbits *
s->nmr->run_burst), 64, 6800);
294 const float pm_p1 = 0.1f, pm_p2 = 2.0f, pm_p3 = 4.0f;
296 float t1 = FLT_MAX, t2 = FLT_MAX;
299 float t =
b->threshold;
301 b->threshold =
FFMAX(
c, t*pm_p1);
307 float sum = 0.0f;
int n = 0;
310 if (
b->energy >
b->threshold &&
b->threshold > 0.0f) { sum +=
b->threshold; n++; }
313 float mean = sum / n;
316 if (
b->energy >
b->threshold &&
b->threshold > 0.0f)
325 const float a_ae = 0.443f, a_at = 0.111f;
329 float uplim = 0.0f, ener = 0.0f, spread = 2.0f;
340 FFPsyBand *band = &
s->psy.ch[
s->cur_channel].psy_bands[(
w+w2)*16+
g];
351 thr_real[
w*16+
g] = uplim;
352 if (nz && ener > 0.0
f && uplim > 0.0
f)
353 uplim =
expf(a_ae * logf(ener) + a_at * logf(uplim));
355 pener[
w*16+
g] = ener;
356 pspread[
w*16+
g] = spread;
363 s->aacdsp.abs_pow34(
s->scoefs, sce->
coeffs, 1024);
383 if (!sce->
zeroes[
w*16+
g] && maxvals[
w*16+
g] > 0 && nbnd < 128) {
385 float invthr = 1.0f /
FFMAX(thr[
w*16+
g], 1e-9
f);
387 invthr, maxvals[
w*16+
g], nd[nbnd], nb[nbnd]);
415 int nact = nbnd, pns_count = 0;
416 float lam0 =
s->nmr->lam[
s->cur_channel];
419 for (
int b = 0;
b < nbnd;
b++) {
433 lo /=
s->nmr->run_burst;
434 lam =
nmr_solve(
s, nd, nb, blo, bnc, cstep, act, nact, destbits, chosen,
439 for (
int k = 0; k < nact; k++)
440 tot += nb[act[k]][chosen[act[k]]];
441 for (
int k = 1; k < nact; k++)
442 tot +=
NMR_SFBITS((blo[act[k]]+chosen[act[k]]*cstep) - (blo[act[k-1]]+chosen[act[k-1]]*cstep));
443 int hardcap =
av_clip((
int)(5800.
f *
FFMIN(1.
f, lambda / 120.
f)), 256, 5800);
445 int rc_cap =
FFMIN(hardcap, (
s->nmr->rc_fill + rc_rate_frame + rc_bmax) /
s->channels);
446 int rc_floor =
FFMAX(0, (
s->nmr->rc_fill + rc_rate_frame - rc_bmax) /
s->channels);
448 lam =
nmr_solve(
s, nd, nb, blo, bnc, cstep, act, nact, rc_cap, chosen,
450 else if (tot < rc_floor)
451 lam =
nmr_solve(
s, nd, nb, blo, bnc, cstep, act, nact, rc_floor, chosen,
458 lam =
nmr_solve(
s, nd, nb, blo, bnc, cstep, act, nact, destbits, chosen,
460 if (lam < lam0/16.0f || lam > lam0*16.0
f)
463 if (!rc_global && lam0 <= 0.0
f)
464 lam =
nmr_solve(
s, nd, nb, blo, bnc, cstep, act, nact, destbits, chosen,
480 for (
int b = 0;
b < nbnd;
b++) {
481 int center = blo[
b] + chosen[
b]*cstep;
484 float invthr = 1.0f /
FFMAX(thr[bidx[
b]], 1e-9
f);
486 invthr, maxvals[bidx[
b]], nd[
b], nb[
b]);
492 lam =
nmr_solve(
s, nd, nb, blo, bnc,
NMR_STEP, act, nact, destbits, chosen,
495 lam =
nmr_solve(
s, nd, nb, blo, bnc,
NMR_STEP, act, nact, destbits, chosen,
505 int hardcap =
av_clip((
int)(5800.
f *
FFMIN(1.
f, lambda / 120.
f)), 256, 5800);
507 for (
int k = 0; k < nact; k++)
508 tot += nb[act[k]][chosen[act[k]]];
509 for (
int k = 1; k < nact; k++)
511 int rc_cap =
FFMIN(hardcap, (
s->nmr->rc_fill + rc_rate_frame + rc_bmax) /
s->channels);
512 int rc_floor =
FFMAX(0, (
s->nmr->rc_fill + rc_rate_frame - rc_bmax) /
s->channels);
514 lam =
nmr_solve(
s, nd, nb, blo, bnc,
NMR_STEP, act, nact, rc_cap, chosen,
516 else if (tot < rc_floor)
517 lam =
nmr_solve(
s, nd, nb, blo, bnc,
NMR_STEP, act, nact, rc_floor, chosen,
521 s->nmr->lam[
s->cur_channel] = lam;
534 }
else if (rc_eligible && nbnd >= 8) {
544 for (
int b = 1;
b < nbnd;
b++) {
546 float spread = pspread[bi];
547 float nmr_pns, cost_keep, cost_pns, frac;
563 frac = nd[
b][chosen[
b]] * thr[bi] /
FFMAX(pener[bi], 1e-9
f);
576 nmr_pns =
FFMAX(0.0
f, pener[bi] * (1.0
f - spread*spread))
578 cost_keep = nd[
b][chosen[
b]] + lam * nb[
b][chosen[
b]];
580 if (cost_pns < cost_keep) {
588 for (
int b = 0;
b < nbnd;
b++)
595 nmr_solve(
s, nd, nb, blo, bnc,
NMR_STEP, act, nact, budget2, chosen,
598 nmr_solve(
s, nd, nb, blo, bnc,
NMR_STEP, act, nact, budget2, chosen,
602 for (
int b = 0;
b < nbnd;
b++) {
607 sce->
pns_ener[bi] = pener[bi] *
FFMIN(1.0
f, pspread[bi]*pspread[bi]);
615 int tot = 0, prevb = -1;
616 for (
int b = 0;
b < nbnd;
b++) {
619 tot += nb[
b][chosen[
b]];
624 s->nmr->counted[
s->cur_channel] = tot;
632 uint8_t nextband[128];
672 int last = sce->
sf_idx[0];
692 for (
int i = 0;
i < 128;
i++) {