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.0f) {
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)
117 const float CLIPPED_ESCAPE = 165140.0f*IQ;
120 const int dim = BT_PAIR ? 2 : 4;
127 for (i = 0; i <
size; i++)
131 return cost * lambda;
143 for (i = 0; i <
size; i +=
dim) {
145 int *quants = s->
qcoefs + i;
149 for (j = 0; j <
dim; j++) {
151 curidx += quants[j] + off;
156 for (j = 0; j <
dim; j++) {
157 float t = fabsf(in[i+j]);
159 if (BT_ESC && vec[j] == 64.0f) {
160 if (t >= CLIPPED_ESCAPE) {
161 di = t - CLIPPED_ESCAPE;
164 int c = av_clip(
quant(t, Q), 0, 8191);
165 di = t - c*
cbrtf(c)*IQ;
166 curbits +=
av_log2(c)*2 - 4 + 1;
176 for (j = 0; j <
dim; j++) {
177 float di = in[i+j] - vec[j]*IQ;
181 cost += rd * lambda + curbits;
188 for (j = 0; j <
dim; j++)
192 for (j = 0; j < 2; j++) {
194 int coef = av_clip(
quant(fabsf(in[i+j]), Q), 0, 8191);
197 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
198 put_bits(pb, len, coef & ((1 << len) - 1));
210 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
211 static float quantize_and_encode_band_cost_ ## NAME( \
212 struct AACEncContext *s, \
213 PutBitContext *pb, const float *in, \
214 const float *scaled, int size, int scale_idx, \
215 int cb, const float lambda, const float uplim, \
217 return quantize_and_encode_band_cost_template( \
218 s, pb, in, scaled, size, scale_idx, \
219 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
220 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \
233 const
float *scaled,
int size,
int scale_idx,
234 int cb, const
float lambda, const
float uplim,
236 quantize_and_encode_band_cost_ZERO,
237 quantize_and_encode_band_cost_SQUAD,
238 quantize_and_encode_band_cost_SQUAD,
239 quantize_and_encode_band_cost_UQUAD,
240 quantize_and_encode_band_cost_UQUAD,
241 quantize_and_encode_band_cost_SPAIR,
242 quantize_and_encode_band_cost_SPAIR,
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_UPAIR,
247 quantize_and_encode_band_cost_ESC,
250 #define quantize_and_encode_band_cost( \
251 s, pb, in, scaled, size, scale_idx, cb, \
252 lambda, uplim, bits) \
253 quantize_and_encode_band_cost_arr[cb]( \
254 s, pb, in, scaled, size, scale_idx, cb, \
258 const float *scaled,
int size,
int scale_idx,
259 int cb,
const float lambda,
const float uplim,
263 cb, lambda, uplim, bits);
267 const float *
in,
int size,
int scale_idx,
268 int cb,
const float lambda)
274 static float find_max_val(
int group_len,
int swb_size,
const float *scaled) {
277 for (w2 = 0; w2 < group_len; w2++) {
278 for (i = 0; i < swb_size; i++) {
279 maxval =
FFMAX(maxval, scaled[w2*128+i]);
287 float Q34 = sqrtf(Q * sqrtf(Q));
289 qmaxval = maxval * Q34 + 0.4054f;
290 if (qmaxval == 0) cb = 0;
291 else if (qmaxval == 1) cb = 1;
292 else if (qmaxval == 2) cb = 3;
293 else if (qmaxval <= 4) cb = 5;
294 else if (qmaxval <= 7) cb = 7;
295 else if (qmaxval <= 12) cb = 9;
313 int win,
int group_len,
const float lambda)
320 const int run_esc = (1 <<
run_bits) - 1;
321 int idx, ppos,
count;
322 int stackrun[120], stackcb[120], stack_len;
328 for (cb = 0; cb < 12; cb++) {
333 for (swb = 0; swb < max_sfb; swb++) {
335 if (sce->
zeroes[win*16 + swb]) {
336 for (cb = 0; cb < 12; cb++) {
342 float minrd = next_minrd;
343 int mincb = next_mincb;
346 for (cb = 0; cb < 12; cb++) {
347 float cost_stay_here, cost_get_here;
349 for (w = 0; w < group_len; w++) {
352 s->
scoefs + start + w*128, size,
353 sce->
sf_idx[(win+w)*16+swb], cb,
356 cost_stay_here = path[swb][
cb].
cost + rd;
357 cost_get_here = minrd + rd + run_bits + 4;
361 if (cost_get_here < cost_stay_here) {
363 path[swb+1][
cb].
cost = cost_get_here;
364 path[swb+1][
cb].
run = 1;
367 path[swb+1][
cb].
cost = cost_stay_here;
370 if (path[swb+1][cb].cost < next_minrd) {
371 next_minrd = path[swb+1][
cb].
cost;
382 for (cb = 1; cb < 12; cb++)
383 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
388 stackrun[stack_len] = path[ppos][
cb].
run;
389 stackcb [stack_len] =
cb;
391 ppos -= path[ppos][
cb].
run;
396 for (i = stack_len - 1; i >= 0; i--) {
399 memset(sce->
zeroes + win*16 + start, !stackcb[i], count);
401 for (j = 0; j <
count; j++) {
405 while (count >= run_esc) {
414 int win,
int group_len,
const float lambda)
421 const int run_esc = (1 <<
run_bits) - 1;
422 int idx, ppos,
count;
423 int stackrun[120], stackcb[120], stack_len;
429 for (cb = 0; cb < 12; cb++) {
430 path[0][
cb].
cost = run_bits+4;
434 for (swb = 0; swb < max_sfb; swb++) {
436 if (sce->
zeroes[win*16 + swb]) {
437 float cost_stay_here = path[swb][0].
cost;
438 float cost_get_here = next_minbits + run_bits + 4;
442 if (cost_get_here < cost_stay_here) {
443 path[swb+1][0].
prev_idx = next_mincb;
444 path[swb+1][0].
cost = cost_get_here;
445 path[swb+1][0].
run = 1;
448 path[swb+1][0].
cost = cost_stay_here;
449 path[swb+1][0].
run = path[swb][0].
run + 1;
451 next_minbits = path[swb+1][0].
cost;
453 for (cb = 1; cb < 12; cb++) {
454 path[swb+1][
cb].
cost = 61450;
456 path[swb+1][
cb].
run = 0;
459 float minbits = next_minbits;
460 int mincb = next_mincb;
461 int startcb = sce->
band_type[win*16+swb];
464 for (cb = 0; cb < startcb; cb++) {
465 path[swb+1][
cb].
cost = 61450;
467 path[swb+1][
cb].
run = 0;
469 for (cb = startcb; cb < 12; cb++) {
470 float cost_stay_here, cost_get_here;
472 for (w = 0; w < group_len; w++) {
474 s->
scoefs + start + w*128, size,
475 sce->
sf_idx[(win+w)*16+swb], cb,
479 cost_get_here = minbits + bits + run_bits + 4;
483 if (cost_get_here < cost_stay_here) {
485 path[swb+1][
cb].
cost = cost_get_here;
486 path[swb+1][
cb].
run = 1;
489 path[swb+1][
cb].
cost = cost_stay_here;
492 if (path[swb+1][cb].cost < next_minbits) {
493 next_minbits = path[swb+1][
cb].
cost;
504 for (cb = 1; cb < 12; cb++)
505 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
511 stackrun[stack_len] = path[ppos][
cb].
run;
512 stackcb [stack_len] =
cb;
514 ppos -= path[ppos][
cb].
run;
519 for (i = stack_len - 1; i >= 0; i--) {
522 memset(sce->
zeroes + win*16 + start, !stackcb[i], count);
524 for (j = 0; j <
count; j++) {
528 while (count >= run_esc) {
551 #define TRELLIS_STAGES 121
552 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
558 int q, w, w2,
g,
start = 0;
565 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
566 int q0, q1, qcnt = 0;
568 for (i = 0; i < 1024; i++) {
569 float t = fabsf(sce->
coeffs[i]);
598 }
else if (q1 > q1high) {
605 paths[0][i].
cost = 0.0f;
606 paths[0][i].
prev = -1;
611 paths[j][i].
prev = -2;
623 bandaddr[idx] = w * 16 +
g;
629 sce->
zeroes[(w+w2)*16+g] = 1;
632 sce->
zeroes[(w+w2)*16+g] = 0;
635 float t = fabsf(coefs[w2*128+i]);
637 qmin =
FFMIN(qmin, t);
638 qmax =
FFMAX(qmax, t);
642 int minscale, maxscale;
649 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
650 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
652 for (q = minscale; q < maxscale; q++) {
660 minrd =
FFMIN(minrd, dist);
662 for (i = 0; i < q1 - q0; i++) {
664 cost = paths[idx - 1][i].
cost + dist
666 if (cost < paths[idx][q].cost) {
667 paths[idx][q].
cost = cost;
668 paths[idx][q].
prev = i;
673 for (q = 0; q < q1 - q0; q++) {
674 paths[idx][q].
cost = paths[idx - 1][q].
cost + 1;
675 paths[idx][q].
prev = q;
684 mincost = paths[idx][0].
cost;
687 if (paths[idx][i].cost < mincost) {
688 mincost = paths[idx][i].
cost;
693 sce->
sf_idx[bandaddr[idx]] = minq + q0;
694 minq = paths[idx][minq].
prev;
712 int start = 0, i, w, w2,
g;
714 float dists[128] = { 0 }, uplims[128];
716 int fflag, minscaler;
723 destbits =
FFMIN(destbits, 5800);
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]) {
773 minscaler = sce->
sf_idx[0];
775 qstep = its ? 1 : 32;
788 if (sce->
zeroes[w*16+g] || sce->
sf_idx[w*16+g] >= 218) {
806 dists[w*16+
g] = dist -
bits;
815 if (tbits > destbits) {
816 for (i = 0; i < 128; i++)
817 if (sce->
sf_idx[i] < 218 - qstep)
820 for (i = 0; i < 128; i++)
821 if (sce->
sf_idx[i] > 60 - qstep)
825 if (!qstep && tbits > destbits*1.02 && sce->
sf_idx[0] < 217)
833 int prevsc = sce->
sf_idx[w*16+
g];
834 if (dists[w*16+g] > uplims[w*16+g] && sce->
sf_idx[w*16+g] > 60) {
842 if (sce->
sf_idx[w*16+g] != prevsc)
848 }
while (fflag && its < 10);
855 int start = 0, i, w, w2,
g;
856 float uplim[128], maxq[128];
858 float distfact = ((sce->
ics.
num_windows > 1) ? 85.80 : 147.84) / lambda;
859 int last = 0, lastband = 0, curband = 0;
860 float avg_energy = 0.0;
863 for (i = 0; i < 1024; i++) {
875 for (w = 0; w < 8; w++) {
876 const float *coeffs = sce->
coeffs + w*128;
878 for (i = 0; i < 128; i++) {
884 avg_energy += coeffs[i] * coeffs[i];
885 last =
FFMAX(last, i);
886 lastband =
FFMAX(lastband, curband);
893 if (avg_energy == 0.0f) {
904 float maxval = -1, thr = 0.0f, t;
910 memset(coefs + w2*128, 0,
sizeof(coefs[0])*
size);
914 for (i = 0; i <
size; i++) {
915 float t = coefs[w2*128+i]*coefs[w2*128+i];
916 maxq[w*16+
g] =
FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
925 start2 =
FFMAX(peakpos - 2, start2);
926 end2 =
FFMIN(peakpos + 3, end2);
932 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
933 t = 1.0 - (1.0 * start2 / last);
934 uplim[w*16+
g] = distfact / (1.4 * thr + t*t*t + 0.075);
945 int scf, prev_scf, step;
946 int min_scf = -1, max_scf = 256;
948 if (maxq[w*16+g] < 21.544) {
971 dist *= 1.0f / 512.0f / lambda;
973 if (quant_max >= 8191) {
974 sce->
sf_idx[w*16+
g] = prev_scf;
978 curdiff = fabsf(dist - uplim[w*16+g]);
982 step =
log2f(curdiff);
983 if (dist > uplim[w*16+g])
986 scf = av_clip_uint8(scf);
987 step = scf - prev_scf;
988 if (
FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
989 sce->
sf_idx[w*16+
g] = av_clip(scf, min_scf, max_scf);
1001 for (i = 1; i < 128; i++) {
1007 if (minq == INT_MAX)
1011 for (i = 126; i >= 0; i--) {
1031 sce->
sf_idx[(w+w2)*16+g] = 218;
1032 sce->
zeroes[(w+w2)*16+g] = 1;
1035 sce->
zeroes[(w+w2)*16+g] = 0;
1041 for (i = 0; i < 128; i++) {
1055 int start = 0, i, w, w2,
g;
1056 float M[128],
S[128];
1065 float dist1 = 0.0f, dist2 = 0.0f;
1072 M[i] = (sce0->
coeffs[start+w2*128+i]
1073 + sce1->
coeffs[start+w2*128+i]) * 0.5;
1075 - sce1->
coeffs[start+w2*128+i];
1084 sce0->
sf_idx[(w+w2)*16+g],
1090 sce1->
sf_idx[(w+w2)*16+g],
1096 sce0->
sf_idx[(w+w2)*16+g],
1102 sce1->
sf_idx[(w+w2)*16+g],
1106 cpe->
ms_mask[w*16+
g] = dist2 < dist1;