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;
 
  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.0f) {
 
  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);
 
  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;