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
53 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
69 return sqrtf(a * sqrtf(a)) + 0.4054;
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.0
f) {
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));
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};
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)
115 const float CLIPPED_ESCAPE = 165140.0f*IQ;
118 const int dim = BT_PAIR ? 2 : 4;
120 const float Q34 = sqrtf(Q * sqrtf(Q));
126 for (i = 0; i <
size; i++)
130 return cost * lambda;
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;
155 for (j = 0; j <
dim; j++) {
156 float t = fabsf(in[i+j]);
158 if (BT_ESC && vec[j] == 64.0
f) {
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;
187 for (j = 0; j <
dim; j++)
191 for (j = 0; j < 2; j++) {
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); \
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, \
257 const float *scaled,
int size,
int scale_idx,
258 int cb,
const float lambda,
const float uplim,
262 cb, lambda, uplim, bits);
266 const float *in,
int size,
int scale_idx,
267 int cb,
const float 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]);
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;
312 int win,
int group_len,
const float lambda)
315 int w, swb, cb, start,
size;
319 const int run_esc = (1 <<
run_bits) - 1;
320 int idx, ppos, count;
321 int stackrun[120], stackcb[120], stack_len;
327 for (cb = 0; cb < 12; cb++) {
328 path[0][cb].
cost = 0.0f;
332 for (swb = 0; swb < max_sfb; swb++) {
334 if (sce->
zeroes[win*16 + swb]) {
335 for (cb = 0; cb < 12; cb++) {
337 path[swb+1][cb].
cost = path[swb][cb].
cost;
338 path[swb+1][cb].
run = path[swb][cb].
run + 1;
341 float minrd = next_minrd;
342 int mincb = next_mincb;
345 for (cb = 0; cb < 12; cb++) {
346 float cost_stay_here, cost_get_here;
348 for (w = 0; w < group_len; w++) {
351 s->
scoefs + start + w*128, size,
352 sce->
sf_idx[(win+w)*16+swb], cb,
355 cost_stay_here = path[swb][cb].
cost + rd;
356 cost_get_here = minrd + rd + run_bits + 4;
360 if (cost_get_here < cost_stay_here) {
362 path[swb+1][cb].
cost = cost_get_here;
363 path[swb+1][cb].
run = 1;
366 path[swb+1][cb].
cost = cost_stay_here;
367 path[swb+1][cb].
run = path[swb][cb].
run + 1;
369 if (path[swb+1][cb].cost < next_minrd) {
370 next_minrd = path[swb+1][cb].
cost;
381 for (cb = 1; cb < 12; cb++)
382 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
387 stackrun[stack_len] = path[ppos][cb].
run;
388 stackcb [stack_len] = cb;
389 idx = path[ppos-path[ppos][cb].
run+1][cb].
prev_idx;
390 ppos -= path[ppos][cb].
run;
395 for (i = stack_len - 1; i >= 0; i--) {
398 memset(sce->
zeroes + win*16 + start, !stackcb[i], count);
400 for (j = 0; j < count; j++) {
401 sce->
band_type[win*16 + start] = stackcb[i];
404 while (count >= run_esc) {
413 int win,
int group_len,
const float lambda)
416 int w, swb, cb, start,
size;
420 const int run_esc = (1 <<
run_bits) - 1;
421 int idx, ppos, count;
422 int stackrun[120], stackcb[120], stack_len;
428 for (cb = 0; cb < 12; cb++) {
429 path[0][cb].
cost = run_bits+4;
433 for (swb = 0; swb < max_sfb; swb++) {
435 if (sce->
zeroes[win*16 + swb]) {
436 float cost_stay_here = path[swb][0].
cost;
437 float cost_get_here = next_minrd + run_bits + 4;
441 if (cost_get_here < cost_stay_here) {
442 path[swb+1][0].
prev_idx = next_mincb;
443 path[swb+1][0].
cost = cost_get_here;
444 path[swb+1][0].
run = 1;
447 path[swb+1][0].
cost = cost_stay_here;
448 path[swb+1][0].
run = path[swb][0].
run + 1;
450 next_minrd = path[swb+1][0].
cost;
452 for (cb = 1; cb < 12; cb++) {
453 path[swb+1][cb].
cost = 61450;
455 path[swb+1][cb].
run = 0;
458 float minrd = next_minrd;
459 int mincb = next_mincb;
460 int startcb = sce->
band_type[win*16+swb];
463 for (cb = 0; cb < startcb; cb++) {
464 path[swb+1][cb].
cost = 61450;
466 path[swb+1][cb].
run = 0;
468 for (cb = startcb; cb < 12; cb++) {
469 float cost_stay_here, cost_get_here;
471 for (w = 0; w < group_len; w++) {
473 s->
scoefs + start + w*128, size,
474 sce->
sf_idx[(win+w)*16+swb], cb,
477 cost_stay_here = path[swb][cb].
cost + rd;
478 cost_get_here = minrd + rd + run_bits + 4;
482 if (cost_get_here < cost_stay_here) {
484 path[swb+1][cb].
cost = cost_get_here;
485 path[swb+1][cb].
run = 1;
488 path[swb+1][cb].
cost = cost_stay_here;
489 path[swb+1][cb].
run = path[swb][cb].
run + 1;
491 if (path[swb+1][cb].cost < next_minrd) {
492 next_minrd = path[swb+1][cb].
cost;
503 for (cb = 1; cb < 12; cb++)
504 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
510 stackrun[stack_len] = path[ppos][cb].
run;
511 stackcb [stack_len] = cb;
512 idx = path[ppos-path[ppos][cb].
run+1][cb].
prev_idx;
513 ppos -= path[ppos][cb].
run;
518 for (i = stack_len - 1; i >= 0; i--) {
521 memset(sce->
zeroes + win*16 + start, !stackcb[i], count);
523 for (j = 0; j < count; j++) {
524 sce->
band_type[win*16 + start] = stackcb[i];
527 while (count >= run_esc) {
550 #define TRELLIS_STAGES 121
551 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
557 int q, w, w2,
g, start = 0;
564 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
565 int q0, q1, qcnt = 0;
567 for (i = 0; i < 1024; i++) {
568 float t = fabsf(sce->
coeffs[i]);
599 }
else if (q1 > q1high) {
607 paths[0][i].
cost = 0.0f;
608 paths[0][i].
prev = -1;
613 paths[j][i].
prev = -2;
621 const float *coefs = sce->
coeffs + start;
625 bandaddr[idx] = w * 16 +
g;
631 sce->
zeroes[(w+w2)*16+g] = 1;
634 sce->
zeroes[(w+w2)*16+g] = 0;
637 float t = fabsf(coefs[w2*128+i]);
639 qmin =
FFMIN(qmin, t);
640 qmax =
FFMAX(qmax, t);
644 int minscale, maxscale;
651 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
652 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
654 for (q = minscale; q < maxscale; q++) {
662 minrd =
FFMIN(minrd, dist);
664 for (i = 0; i < q1 - q0; i++) {
666 cost = paths[idx - 1][i].
cost + dist
668 if (cost < paths[idx][q].cost) {
669 paths[idx][q].
cost = cost;
670 paths[idx][q].
prev = i;
675 for (q = 0; q < q1 - q0; q++) {
676 paths[idx][q].
cost = paths[idx - 1][q].
cost + 1;
677 paths[idx][q].
prev = q;
686 mincost = paths[idx][0].
cost;
689 if (paths[idx][i].cost < mincost) {
690 mincost = paths[idx][i].
cost;
695 sce->
sf_idx[bandaddr[idx]] = minq + q0;
696 minq = paths[idx][minq].
prev;
714 int start = 0, i, w, w2,
g;
716 float dists[128], uplims[128];
718 int fflag, minscaler;
724 memset(dists, 0,
sizeof(dists));
734 sce->
zeroes[(w+w2)*16+g] = 1;
739 uplims[w*16+
g] = uplim *512;
742 minthr =
FFMIN(minthr, uplim);
748 if (sce->
zeroes[w*16+g]) {
763 const float *scaled = s->
scoefs + start;
773 minscaler = sce->
sf_idx[0];
775 qstep = its ? 1 : 32;
783 const float *coefs = sce->
coeffs + start;
784 const float *scaled = s->
scoefs + start;
789 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
807 dists[w*16+
g] = dist -
bits;
816 if (tbits > destbits) {
817 for (i = 0; i < 128; i++)
818 if (sce->
sf_idx[i] < 218 - qstep)
821 for (i = 0; i < 128; i++)
822 if (sce->
sf_idx[i] > 60 - qstep)
826 if (!qstep && tbits > destbits*1.02 && sce->
sf_idx[0] < 217)
834 int prevsc = sce->
sf_idx[w*16+
g];
835 if (dists[w*16+g] > uplims[w*16+g] && sce->
sf_idx[w*16+g] > 60) {
843 if (sce->
sf_idx[w*16+g] != prevsc)
849 }
while (fflag && its < 10);
856 int start = 0, i, w, w2,
g;
857 float uplim[128], maxq[128];
859 float distfact = ((sce->
ics.
num_windows > 1) ? 85.80 : 147.84) / lambda;
860 int last = 0, lastband = 0, curband = 0;
861 float avg_energy = 0.0;
864 for (i = 0; i < 1024; i++) {
876 for (w = 0; w < 8; w++) {
879 for (i = 0; i < 128; i++) {
885 avg_energy += coeffs[i] * coeffs[i];
886 last =
FFMAX(last, i);
887 lastband =
FFMAX(lastband, curband);
894 if (avg_energy == 0.0
f) {
902 float *coefs = sce->
coeffs + start;
904 int start2 = start, end2 = start +
size, peakpos = start;
905 float maxval = -1, thr = 0.0f,
t;
911 memset(coefs + w2*128, 0,
sizeof(coefs[0])*
size);
915 for (i = 0; i <
size; i++) {
916 float t = coefs[w2*128+i]*coefs[w2*128+i];
917 maxq[w*16+
g] =
FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
926 start2 =
FFMAX(peakpos - 2, start2);
927 end2 =
FFMIN(peakpos + 3, end2);
933 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
934 t = 1.0 - (1.0 * start2 / last);
935 uplim[w*16+
g] = distfact / (1.4 * thr +
t*
t*
t + 0.075);
943 const float *coefs = sce->
coeffs + start;
944 const float *scaled = s->
scoefs + start;
946 int scf, prev_scf,
step;
947 int min_scf = -1, max_scf = 256;
949 if (maxq[w*16+g] < 21.544) {
973 dist *= 1.0f / 512.0f / lambda;
975 if (quant_max >= 8191) {
976 sce->
sf_idx[w*16+
g] = prev_scf;
980 curdiff = fabsf(dist - uplim[w*16+g]);
984 step =
log2f(curdiff);
985 if (dist > uplim[w*16+g])
988 scf = av_clip_uint8(scf);
989 step = scf - prev_scf;
990 if (
FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
991 sce->
sf_idx[w*16+
g] = av_clip(scf, min_scf, max_scf);
1003 for (i = 1; i < 128; i++) {
1009 if (minq == INT_MAX)
1013 for (i = 126; i >= 0; i--) {
1033 sce->
sf_idx[(w+w2)*16+g] = 218;
1034 sce->
zeroes[(w+w2)*16+g] = 1;
1037 sce->
zeroes[(w+w2)*16+g] = 0;
1043 for (i = 0; i < 128; i++) {
1057 int start = 0, i, w, w2,
g;
1058 float M[128],
S[128];
1067 float dist1 = 0.0f, dist2 = 0.0f;
1074 M[i] = (sce0->
coeffs[start+w2*128+i]
1075 + sce1->
coeffs[start+w2*128+i]) * 0.5;
1077 - sce1->
coeffs[start+w2*128+i];
1086 sce0->
sf_idx[(w+w2)*16+g],
1092 sce1->
sf_idx[(w+w2)*16+g],
1098 sce0->
sf_idx[(w+w2)*16+g],
1104 sce1->
sf_idx[(w+w2)*16+g],
1108 cpe->
ms_mask[w*16+
g] = dist2 < dist1;