Libav

libavcodec/aaccoder.c

Go to the documentation of this file.
00001 /*
00002  * AAC coefficients encoder
00003  * Copyright (C) 2008-2009 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 /***********************************
00028  *              TODOs:
00029  * speedup quantizer selection
00030  * add sane pulse detection
00031  ***********************************/
00032 
00033 #include "avcodec.h"
00034 #include "put_bits.h"
00035 #include "aac.h"
00036 #include "aacenc.h"
00037 #include "aactab.h"
00038 #include "libavutil/libm.h"
00039 
00041 static const uint8_t run_value_bits_long[64] = {
00042      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
00043      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
00044     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
00045     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
00046 };
00047 
00049 static const uint8_t run_value_bits_short[16] = {
00050     3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
00051 };
00052 
00053 static const uint8_t *run_value_bits[2] = {
00054     run_value_bits_long, run_value_bits_short
00055 };
00056 
00057 
00063 static av_always_inline int quant(float coef, const float Q)
00064 {
00065     float a = coef * Q;
00066     return sqrtf(a * sqrtf(a)) + 0.4054;
00067 }
00068 
00069 static void quantize_bands(int (*out)[2], const float *in, const float *scaled,
00070                            int size, float Q34, int is_signed, int maxval)
00071 {
00072     int i;
00073     double qc;
00074     for (i = 0; i < size; i++) {
00075         qc = scaled[i] * Q34;
00076         out[i][0] = (int)FFMIN(qc,          (double)maxval);
00077         out[i][1] = (int)FFMIN(qc + 0.4054, (double)maxval);
00078         if (is_signed && in[i] < 0.0f) {
00079             out[i][0] = -out[i][0];
00080             out[i][1] = -out[i][1];
00081         }
00082     }
00083 }
00084 
00085 static void abs_pow34_v(float *out, const float *in, const int size)
00086 {
00087 #ifndef USE_REALLY_FULL_SEARCH
00088     int i;
00089     for (i = 0; i < size; i++) {
00090         float a = fabsf(in[i]);
00091         out[i] = sqrtf(a * sqrtf(a));
00092     }
00093 #endif /* USE_REALLY_FULL_SEARCH */
00094 }
00095 
00096 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
00097 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
00098 
00104 static float quantize_and_encode_band_cost(struct AACEncContext *s,
00105                                 PutBitContext *pb, const float *in,
00106                                 const float *scaled, int size, int scale_idx,
00107                                 int cb, const float lambda, const float uplim,
00108                                 int *bits)
00109 {
00110     const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
00111     const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
00112     const float CLIPPED_ESCAPE = 165140.0f*IQ;
00113     int i, j, k;
00114     float cost = 0;
00115     const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
00116     int resbits = 0;
00117 #ifndef USE_REALLY_FULL_SEARCH
00118     const float  Q34 = sqrtf(Q * sqrtf(Q));
00119     const int range  = aac_cb_range[cb];
00120     const int maxval = aac_cb_maxval[cb];
00121     int offs[4];
00122 #endif /* USE_REALLY_FULL_SEARCH */
00123 
00124     if (!cb) {
00125         for (i = 0; i < size; i++)
00126             cost += in[i]*in[i];
00127         if (bits)
00128             *bits = 0;
00129         return cost * lambda;
00130     }
00131 #ifndef USE_REALLY_FULL_SEARCH
00132     offs[0] = 1;
00133     for (i = 1; i < dim; i++)
00134         offs[i] = offs[i-1]*range;
00135     if (!scaled) {
00136         abs_pow34_v(s->scoefs, in, size);
00137         scaled = s->scoefs;
00138     }
00139     quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
00140 #endif /* USE_REALLY_FULL_SEARCH */
00141     for (i = 0; i < size; i += dim) {
00142         float mincost;
00143         int minidx  = 0;
00144         int minbits = 0;
00145         const float *vec;
00146 #ifndef USE_REALLY_FULL_SEARCH
00147         int (*quants)[2] = &s->qcoefs[i];
00148         mincost = 0.0f;
00149         for (j = 0; j < dim; j++)
00150             mincost += in[i+j]*in[i+j];
00151         minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
00152         minbits = ff_aac_spectral_bits[cb-1][minidx];
00153         mincost = mincost * lambda + minbits;
00154         for (j = 0; j < (1<<dim); j++) {
00155             float rd = 0.0f;
00156             int curbits;
00157             int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
00158             int same   = 0;
00159             for (k = 0; k < dim; k++) {
00160                 if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
00161                     same = 1;
00162                     break;
00163                 }
00164             }
00165             if (same)
00166                 continue;
00167             for (k = 0; k < dim; k++)
00168                 curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
00169             curbits =  ff_aac_spectral_bits[cb-1][curidx];
00170             vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
00171 #else
00172         mincost = INFINITY;
00173         vec = ff_aac_codebook_vectors[cb-1];
00174         for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
00175             float rd = 0.0f;
00176             int curbits = ff_aac_spectral_bits[cb-1][j];
00177             int curidx = j;
00178 #endif /* USE_REALLY_FULL_SEARCH */
00179             if (IS_CODEBOOK_UNSIGNED(cb)) {
00180                 for (k = 0; k < dim; k++) {
00181                     float t = fabsf(in[i+k]);
00182                     float di;
00183                     if (vec[k] == 64.0f) { //FIXME: slow
00184                         //do not code with escape sequence small values
00185                         if (t < 39.0f*IQ) {
00186                             rd = INFINITY;
00187                             break;
00188                         }
00189                         if (t >= CLIPPED_ESCAPE) {
00190                             di = t - CLIPPED_ESCAPE;
00191                             curbits += 21;
00192                         } else {
00193                             int c = av_clip(quant(t, Q), 0, 8191);
00194                             di = t - c*cbrtf(c)*IQ;
00195                             curbits += av_log2(c)*2 - 4 + 1;
00196                         }
00197                     } else {
00198                         di = t - vec[k]*IQ;
00199                     }
00200                     if (vec[k] != 0.0f)
00201                         curbits++;
00202                     rd += di*di;
00203                 }
00204             } else {
00205                 for (k = 0; k < dim; k++) {
00206                     float di = in[i+k] - vec[k]*IQ;
00207                     rd += di*di;
00208                 }
00209             }
00210             rd = rd * lambda + curbits;
00211             if (rd < mincost) {
00212                 mincost = rd;
00213                 minidx  = curidx;
00214                 minbits = curbits;
00215             }
00216         }
00217         cost    += mincost;
00218         resbits += minbits;
00219         if (cost >= uplim)
00220             return uplim;
00221         if (pb) {
00222         put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
00223         if (IS_CODEBOOK_UNSIGNED(cb))
00224             for (j = 0; j < dim; j++)
00225                 if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
00226                     put_bits(pb, 1, in[i+j] < 0.0f);
00227         if (cb == ESC_BT) {
00228             for (j = 0; j < 2; j++) {
00229                 if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
00230                     int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
00231                     int len = av_log2(coef);
00232 
00233                     put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
00234                     put_bits(pb, len, coef & ((1 << len) - 1));
00235                 }
00236             }
00237         }
00238         }
00239     }
00240 
00241     if (bits)
00242         *bits = resbits;
00243     return cost;
00244 }
00245 static float quantize_band_cost(struct AACEncContext *s, const float *in,
00246                                 const float *scaled, int size, int scale_idx,
00247                                 int cb, const float lambda, const float uplim,
00248                                 int *bits)
00249 {
00250     return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
00251                                          cb, lambda, uplim, bits);
00252 }
00253 
00254 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
00255                                      const float *in, int size, int scale_idx,
00256                                      int cb, const float lambda)
00257 {
00258     quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
00259                                   INFINITY, NULL);
00260 }
00261 
00265 typedef struct BandCodingPath {
00266     int prev_idx; 
00267     float cost;   
00268     int run;
00269 } BandCodingPath;
00270 
00274 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
00275                                      int win, int group_len, const float lambda)
00276 {
00277     BandCodingPath path[120][12];
00278     int w, swb, cb, start, start2, size;
00279     int i, j;
00280     const int max_sfb  = sce->ics.max_sfb;
00281     const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
00282     const int run_esc  = (1 << run_bits) - 1;
00283     int idx, ppos, count;
00284     int stackrun[120], stackcb[120], stack_len;
00285     float next_minrd = INFINITY;
00286     int next_mincb = 0;
00287 
00288     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00289     start = win*128;
00290     for (cb = 0; cb < 12; cb++) {
00291         path[0][cb].cost     = 0.0f;
00292         path[0][cb].prev_idx = -1;
00293         path[0][cb].run      = 0;
00294     }
00295     for (swb = 0; swb < max_sfb; swb++) {
00296         start2 = start;
00297         size = sce->ics.swb_sizes[swb];
00298         if (sce->zeroes[win*16 + swb]) {
00299             for (cb = 0; cb < 12; cb++) {
00300                 path[swb+1][cb].prev_idx = cb;
00301                 path[swb+1][cb].cost     = path[swb][cb].cost;
00302                 path[swb+1][cb].run      = path[swb][cb].run + 1;
00303             }
00304         } else {
00305             float minrd = next_minrd;
00306             int mincb = next_mincb;
00307             next_minrd = INFINITY;
00308             next_mincb = 0;
00309             for (cb = 0; cb < 12; cb++) {
00310                 float cost_stay_here, cost_get_here;
00311                 float rd = 0.0f;
00312                 for (w = 0; w < group_len; w++) {
00313                     FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
00314                     rd += quantize_band_cost(s, sce->coeffs + start + w*128,
00315                                              s->scoefs + start + w*128, size,
00316                                              sce->sf_idx[(win+w)*16+swb], cb,
00317                                              lambda / band->threshold, INFINITY, NULL);
00318                 }
00319                 cost_stay_here = path[swb][cb].cost + rd;
00320                 cost_get_here  = minrd              + rd + run_bits + 4;
00321                 if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
00322                     != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
00323                     cost_stay_here += run_bits;
00324                 if (cost_get_here < cost_stay_here) {
00325                     path[swb+1][cb].prev_idx = mincb;
00326                     path[swb+1][cb].cost     = cost_get_here;
00327                     path[swb+1][cb].run      = 1;
00328                 } else {
00329                     path[swb+1][cb].prev_idx = cb;
00330                     path[swb+1][cb].cost     = cost_stay_here;
00331                     path[swb+1][cb].run      = path[swb][cb].run + 1;
00332                 }
00333                 if (path[swb+1][cb].cost < next_minrd) {
00334                     next_minrd = path[swb+1][cb].cost;
00335                     next_mincb = cb;
00336                 }
00337             }
00338         }
00339         start += sce->ics.swb_sizes[swb];
00340     }
00341 
00342     //convert resulting path from backward-linked list
00343     stack_len = 0;
00344     idx       = 0;
00345     for (cb = 1; cb < 12; cb++)
00346         if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
00347             idx = cb;
00348     ppos = max_sfb;
00349     while (ppos > 0) {
00350         cb = idx;
00351         stackrun[stack_len] = path[ppos][cb].run;
00352         stackcb [stack_len] = cb;
00353         idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
00354         ppos -= path[ppos][cb].run;
00355         stack_len++;
00356     }
00357     //perform actual band info encoding
00358     start = 0;
00359     for (i = stack_len - 1; i >= 0; i--) {
00360         put_bits(&s->pb, 4, stackcb[i]);
00361         count = stackrun[i];
00362         memset(sce->zeroes + win*16 + start, !stackcb[i], count);
00363         //XXX: memset when band_type is also uint8_t
00364         for (j = 0; j < count; j++) {
00365             sce->band_type[win*16 + start] =  stackcb[i];
00366             start++;
00367         }
00368         while (count >= run_esc) {
00369             put_bits(&s->pb, run_bits, run_esc);
00370             count -= run_esc;
00371         }
00372         put_bits(&s->pb, run_bits, count);
00373     }
00374 }
00375 
00376 typedef struct TrellisPath {
00377     float cost;
00378     int prev;
00379     int min_val;
00380     int max_val;
00381 } TrellisPath;
00382 
00383 #define TRELLIS_STAGES 121
00384 #define TRELLIS_STATES 256
00385 
00386 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
00387                                        SingleChannelElement *sce,
00388                                        const float lambda)
00389 {
00390     int q, w, w2, g, start = 0;
00391     int i, j;
00392     int idx;
00393     TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
00394     int bandaddr[TRELLIS_STAGES];
00395     int minq;
00396     float mincost;
00397 
00398     for (i = 0; i < TRELLIS_STATES; i++) {
00399         paths[0][i].cost    = 0.0f;
00400         paths[0][i].prev    = -1;
00401         paths[0][i].min_val = i;
00402         paths[0][i].max_val = i;
00403     }
00404     for (j = 1; j < TRELLIS_STAGES; j++) {
00405         for (i = 0; i < TRELLIS_STATES; i++) {
00406             paths[j][i].cost    = INFINITY;
00407             paths[j][i].prev    = -2;
00408             paths[j][i].min_val = INT_MAX;
00409             paths[j][i].max_val = 0;
00410         }
00411     }
00412     idx = 1;
00413     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00414     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00415         start = w*128;
00416         for (g = 0; g < sce->ics.num_swb; g++) {
00417             const float *coefs = sce->coeffs + start;
00418             float qmin, qmax;
00419             int nz = 0;
00420 
00421             bandaddr[idx] = w * 16 + g;
00422             qmin = INT_MAX;
00423             qmax = 0.0f;
00424             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00425                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00426                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00427                     sce->zeroes[(w+w2)*16+g] = 1;
00428                     continue;
00429                 }
00430                 sce->zeroes[(w+w2)*16+g] = 0;
00431                 nz = 1;
00432                 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
00433                     float t = fabsf(coefs[w2*128+i]);
00434                     if (t > 0.0f)
00435                         qmin = FFMIN(qmin, t);
00436                     qmax = FFMAX(qmax, t);
00437                 }
00438             }
00439             if (nz) {
00440                 int minscale, maxscale;
00441                 float minrd = INFINITY;
00442                 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
00443                 minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
00444                 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
00445                 maxscale = av_clip_uint8(log2(qmax)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
00446                 for (q = minscale; q < maxscale; q++) {
00447                     float dists[12], dist;
00448                     memset(dists, 0, sizeof(dists));
00449                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00450                         FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00451                         int cb;
00452                         for (cb = 0; cb <= ESC_BT; cb++)
00453                             dists[cb] += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
00454                                                             q, cb, lambda / band->threshold, INFINITY, NULL);
00455                     }
00456                     dist = dists[0];
00457                     for (i = 1; i <= ESC_BT; i++)
00458                         dist = FFMIN(dist, dists[i]);
00459                     minrd = FFMIN(minrd, dist);
00460 
00461                     for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
00462                         float cost;
00463                         int minv, maxv;
00464                         if (isinf(paths[idx - 1][i].cost))
00465                             continue;
00466                         cost = paths[idx - 1][i].cost + dist
00467                                + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
00468                         minv = FFMIN(paths[idx - 1][i].min_val, q);
00469                         maxv = FFMAX(paths[idx - 1][i].max_val, q);
00470                         if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
00471                             paths[idx][q].cost    = cost;
00472                             paths[idx][q].prev    = i;
00473                             paths[idx][q].min_val = minv;
00474                             paths[idx][q].max_val = maxv;
00475                         }
00476                     }
00477                 }
00478             } else {
00479                 for (q = 0; q < TRELLIS_STATES; q++) {
00480                     if (!isinf(paths[idx - 1][q].cost)) {
00481                         paths[idx][q].cost = paths[idx - 1][q].cost + 1;
00482                         paths[idx][q].prev = q;
00483                         paths[idx][q].min_val = FFMIN(paths[idx - 1][q].min_val, q);
00484                         paths[idx][q].max_val = FFMAX(paths[idx - 1][q].max_val, q);
00485                         continue;
00486                     }
00487                     for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
00488                         float cost;
00489                         int minv, maxv;
00490                         if (isinf(paths[idx - 1][i].cost))
00491                             continue;
00492                         cost = paths[idx - 1][i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
00493                         minv = FFMIN(paths[idx - 1][i].min_val, q);
00494                         maxv = FFMAX(paths[idx - 1][i].max_val, q);
00495                         if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
00496                             paths[idx][q].cost    = cost;
00497                             paths[idx][q].prev    = i;
00498                             paths[idx][q].min_val = minv;
00499                             paths[idx][q].max_val = maxv;
00500                         }
00501                     }
00502                 }
00503             }
00504             sce->zeroes[w*16+g] = !nz;
00505             start += sce->ics.swb_sizes[g];
00506             idx++;
00507         }
00508     }
00509     idx--;
00510     mincost = paths[idx][0].cost;
00511     minq    = 0;
00512     for (i = 1; i < TRELLIS_STATES; i++) {
00513         if (paths[idx][i].cost < mincost) {
00514             mincost = paths[idx][i].cost;
00515             minq = i;
00516         }
00517     }
00518     while (idx) {
00519         sce->sf_idx[bandaddr[idx]] = minq;
00520         minq = paths[idx][minq].prev;
00521         idx--;
00522     }
00523     //set the same quantizers inside window groups
00524     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00525         for (g = 0;  g < sce->ics.num_swb; g++)
00526             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
00527                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
00528 }
00529 
00533 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
00534                                           AACEncContext *s,
00535                                           SingleChannelElement *sce,
00536                                           const float lambda)
00537 {
00538     int start = 0, i, w, w2, g;
00539     int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
00540     float dists[128], uplims[128];
00541     int fflag, minscaler;
00542     int its  = 0;
00543     int allz = 0;
00544     float minthr = INFINITY;
00545 
00546     //XXX: some heuristic to determine initial quantizers will reduce search time
00547     memset(dists, 0, sizeof(dists));
00548     //determine zero bands and upper limits
00549     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00550         for (g = 0;  g < sce->ics.num_swb; g++) {
00551             int nz = 0;
00552             float uplim = 0.0f;
00553             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00554                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00555                 uplim += band->threshold;
00556                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00557                     sce->zeroes[(w+w2)*16+g] = 1;
00558                     continue;
00559                 }
00560                 nz = 1;
00561             }
00562             uplims[w*16+g] = uplim *512;
00563             sce->zeroes[w*16+g] = !nz;
00564             if (nz)
00565                 minthr = FFMIN(minthr, uplim);
00566             allz = FFMAX(allz, nz);
00567         }
00568     }
00569     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00570         for (g = 0;  g < sce->ics.num_swb; g++) {
00571             if (sce->zeroes[w*16+g]) {
00572                 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
00573                 continue;
00574             }
00575             sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2(uplims[w*16+g]/minthr)*4,59);
00576         }
00577     }
00578 
00579     if (!allz)
00580         return;
00581     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00582     //perform two-loop search
00583     //outer loop - improve quality
00584     do {
00585         int tbits, qstep;
00586         minscaler = sce->sf_idx[0];
00587         //inner loop - quantize spectrum to fit into given number of bits
00588         qstep = its ? 1 : 32;
00589         do {
00590             int prev = -1;
00591             tbits = 0;
00592             fflag = 0;
00593             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00594                 start = w*128;
00595                 for (g = 0;  g < sce->ics.num_swb; g++) {
00596                     const float *coefs = sce->coeffs + start;
00597                     const float *scaled = s->scoefs + start;
00598                     int bits = 0;
00599                     int cb;
00600                     float mindist = INFINITY;
00601                     int minbits = 0;
00602 
00603                     if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
00604                         start += sce->ics.swb_sizes[g];
00605                         continue;
00606                     }
00607                     minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
00608                     for (cb = 0; cb <= ESC_BT; cb++) {
00609                         float dist = 0.0f;
00610                         int bb = 0;
00611                         for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00612                             int b;
00613                             dist += quantize_band_cost(s, coefs + w2*128,
00614                                                        scaled + w2*128,
00615                                                        sce->ics.swb_sizes[g],
00616                                                        sce->sf_idx[w*16+g],
00617                                                        cb,
00618                                                        lambda,
00619                                                        INFINITY,
00620                                                        &b);
00621                             bb += b;
00622                         }
00623                         if (dist < mindist) {
00624                             mindist = dist;
00625                             minbits = bb;
00626                         }
00627                     }
00628                     dists[w*16+g] = (mindist - minbits) / lambda;
00629                     bits = minbits;
00630                     if (prev != -1) {
00631                         bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
00632                     }
00633                     tbits += bits;
00634                     start += sce->ics.swb_sizes[g];
00635                     prev = sce->sf_idx[w*16+g];
00636                 }
00637             }
00638             if (tbits > destbits) {
00639                 for (i = 0; i < 128; i++)
00640                     if (sce->sf_idx[i] < 218 - qstep)
00641                         sce->sf_idx[i] += qstep;
00642             } else {
00643                 for (i = 0; i < 128; i++)
00644                     if (sce->sf_idx[i] > 60 - qstep)
00645                         sce->sf_idx[i] -= qstep;
00646             }
00647             qstep >>= 1;
00648             if (!qstep && tbits > destbits*1.02)
00649                 qstep = 1;
00650             if (sce->sf_idx[0] >= 217)
00651                 break;
00652         } while (qstep);
00653 
00654         fflag = 0;
00655         minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
00656         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00657             start = w*128;
00658             for (g = 0; g < sce->ics.num_swb; g++) {
00659                 int prevsc = sce->sf_idx[w*16+g];
00660                 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60)
00661                     sce->sf_idx[w*16+g]--;
00662                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
00663                 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
00664                 if (sce->sf_idx[w*16+g] != prevsc)
00665                     fflag = 1;
00666             }
00667         }
00668         its++;
00669     } while (fflag && its < 10);
00670 }
00671 
00672 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
00673                                        SingleChannelElement *sce,
00674                                        const float lambda)
00675 {
00676     int start = 0, i, w, w2, g;
00677     float uplim[128], maxq[128];
00678     int minq, maxsf;
00679     float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
00680     int last = 0, lastband = 0, curband = 0;
00681     float avg_energy = 0.0;
00682     if (sce->ics.num_windows == 1) {
00683         start = 0;
00684         for (i = 0; i < 1024; i++) {
00685             if (i - start >= sce->ics.swb_sizes[curband]) {
00686                 start += sce->ics.swb_sizes[curband];
00687                 curband++;
00688             }
00689             if (sce->coeffs[i]) {
00690                 avg_energy += sce->coeffs[i] * sce->coeffs[i];
00691                 last = i;
00692                 lastband = curband;
00693             }
00694         }
00695     } else {
00696         for (w = 0; w < 8; w++) {
00697             const float *coeffs = sce->coeffs + w*128;
00698             start = 0;
00699             for (i = 0; i < 128; i++) {
00700                 if (i - start >= sce->ics.swb_sizes[curband]) {
00701                     start += sce->ics.swb_sizes[curband];
00702                     curband++;
00703                 }
00704                 if (coeffs[i]) {
00705                     avg_energy += coeffs[i] * coeffs[i];
00706                     last = FFMAX(last, i);
00707                     lastband = FFMAX(lastband, curband);
00708                 }
00709             }
00710         }
00711     }
00712     last++;
00713     avg_energy /= last;
00714     if (avg_energy == 0.0f) {
00715         for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
00716             sce->sf_idx[i] = SCALE_ONE_POS;
00717         return;
00718     }
00719     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00720         start = w*128;
00721         for (g = 0; g < sce->ics.num_swb; g++) {
00722             float *coefs   = sce->coeffs + start;
00723             const int size = sce->ics.swb_sizes[g];
00724             int start2 = start, end2 = start + size, peakpos = start;
00725             float maxval = -1, thr = 0.0f, t;
00726             maxq[w*16+g] = 0.0f;
00727             if (g > lastband) {
00728                 maxq[w*16+g] = 0.0f;
00729                 start += size;
00730                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
00731                     memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
00732                 continue;
00733             }
00734             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00735                 for (i = 0; i < size; i++) {
00736                     float t = coefs[w2*128+i]*coefs[w2*128+i];
00737                     maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
00738                     thr += t;
00739                     if (sce->ics.num_windows == 1 && maxval < t) {
00740                         maxval  = t;
00741                         peakpos = start+i;
00742                     }
00743                 }
00744             }
00745             if (sce->ics.num_windows == 1) {
00746                 start2 = FFMAX(peakpos - 2, start2);
00747                 end2   = FFMIN(peakpos + 3, end2);
00748             } else {
00749                 start2 -= start;
00750                 end2   -= start;
00751             }
00752             start += size;
00753             thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
00754             t   = 1.0 - (1.0 * start2 / last);
00755             uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
00756         }
00757     }
00758     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00759     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00760     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00761         start = w*128;
00762         for (g = 0;  g < sce->ics.num_swb; g++) {
00763             const float *coefs  = sce->coeffs + start;
00764             const float *scaled = s->scoefs   + start;
00765             const int size      = sce->ics.swb_sizes[g];
00766             int scf, prev_scf, step;
00767             int min_scf = -1, max_scf = 256;
00768             float curdiff;
00769             if (maxq[w*16+g] < 21.544) {
00770                 sce->zeroes[w*16+g] = 1;
00771                 start += size;
00772                 continue;
00773             }
00774             sce->zeroes[w*16+g] = 0;
00775             scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2(1/maxq[w*16+g])*16/3, 60, 218);
00776             step = 16;
00777             for (;;) {
00778                 float dist = 0.0f;
00779                 int quant_max;
00780 
00781                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00782                     int b;
00783                     dist += quantize_band_cost(s, coefs + w2*128,
00784                                                scaled + w2*128,
00785                                                sce->ics.swb_sizes[g],
00786                                                scf,
00787                                                ESC_BT,
00788                                                lambda,
00789                                                INFINITY,
00790                                                &b);
00791                     dist -= b;
00792                 }
00793                 dist *= 1.0f / 512.0f / lambda;
00794                 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
00795                 if (quant_max >= 8191) { // too much, return to the previous quantizer
00796                     sce->sf_idx[w*16+g] = prev_scf;
00797                     break;
00798                 }
00799                 prev_scf = scf;
00800                 curdiff = fabsf(dist - uplim[w*16+g]);
00801                 if (curdiff <= 1.0f)
00802                     step = 0;
00803                 else
00804                     step = log2(curdiff);
00805                 if (dist > uplim[w*16+g])
00806                     step = -step;
00807                 scf += step;
00808                 scf = av_clip_uint8(scf);
00809                 step = scf - prev_scf;
00810                 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
00811                     sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
00812                     break;
00813                 }
00814                 if (step > 0)
00815                     min_scf = prev_scf;
00816                 else
00817                     max_scf = prev_scf;
00818             }
00819             start += size;
00820         }
00821     }
00822     minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
00823     for (i = 1; i < 128; i++) {
00824         if (!sce->sf_idx[i])
00825             sce->sf_idx[i] = sce->sf_idx[i-1];
00826         else
00827             minq = FFMIN(minq, sce->sf_idx[i]);
00828     }
00829     if (minq == INT_MAX)
00830         minq = 0;
00831     minq = FFMIN(minq, SCALE_MAX_POS);
00832     maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
00833     for (i = 126; i >= 0; i--) {
00834         if (!sce->sf_idx[i])
00835             sce->sf_idx[i] = sce->sf_idx[i+1];
00836         sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
00837     }
00838 }
00839 
00840 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
00841                                        SingleChannelElement *sce,
00842                                        const float lambda)
00843 {
00844     int start = 0, i, w, w2, g;
00845     int minq = 255;
00846 
00847     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00848     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00849         start = w*128;
00850         for (g = 0; g < sce->ics.num_swb; g++) {
00851             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00852                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00853                 if (band->energy <= band->threshold) {
00854                     sce->sf_idx[(w+w2)*16+g] = 218;
00855                     sce->zeroes[(w+w2)*16+g] = 1;
00856                 } else {
00857                     sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2(band->threshold), 80, 218);
00858                     sce->zeroes[(w+w2)*16+g] = 0;
00859                 }
00860                 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
00861             }
00862         }
00863     }
00864     for (i = 0; i < 128; i++) {
00865         sce->sf_idx[i] = 140;
00866         //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
00867     }
00868     //set the same quantizers inside window groups
00869     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00870         for (g = 0;  g < sce->ics.num_swb; g++)
00871             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
00872                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
00873 }
00874 
00875 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
00876                           const float lambda)
00877 {
00878     int start = 0, i, w, w2, g;
00879     float M[128], S[128];
00880     float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
00881     SingleChannelElement *sce0 = &cpe->ch[0];
00882     SingleChannelElement *sce1 = &cpe->ch[1];
00883     if (!cpe->common_window)
00884         return;
00885     for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
00886         for (g = 0;  g < sce0->ics.num_swb; g++) {
00887             if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
00888                 float dist1 = 0.0f, dist2 = 0.0f;
00889                 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
00890                     FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
00891                     FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
00892                     float minthr = FFMIN(band0->threshold, band1->threshold);
00893                     float maxthr = FFMAX(band0->threshold, band1->threshold);
00894                     for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
00895                         M[i] = (sce0->coeffs[start+w2*128+i]
00896                               + sce1->coeffs[start+w2*128+i]) * 0.5;
00897                         S[i] =  sce0->coeffs[start+w2*128+i]
00898                               - sce1->coeffs[start+w2*128+i];
00899                     }
00900                     abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
00901                     abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
00902                     abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
00903                     abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
00904                     dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
00905                                                 L34,
00906                                                 sce0->ics.swb_sizes[g],
00907                                                 sce0->sf_idx[(w+w2)*16+g],
00908                                                 sce0->band_type[(w+w2)*16+g],
00909                                                 lambda / band0->threshold, INFINITY, NULL);
00910                     dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
00911                                                 R34,
00912                                                 sce1->ics.swb_sizes[g],
00913                                                 sce1->sf_idx[(w+w2)*16+g],
00914                                                 sce1->band_type[(w+w2)*16+g],
00915                                                 lambda / band1->threshold, INFINITY, NULL);
00916                     dist2 += quantize_band_cost(s, M,
00917                                                 M34,
00918                                                 sce0->ics.swb_sizes[g],
00919                                                 sce0->sf_idx[(w+w2)*16+g],
00920                                                 sce0->band_type[(w+w2)*16+g],
00921                                                 lambda / maxthr, INFINITY, NULL);
00922                     dist2 += quantize_band_cost(s, S,
00923                                                 S34,
00924                                                 sce1->ics.swb_sizes[g],
00925                                                 sce1->sf_idx[(w+w2)*16+g],
00926                                                 sce1->band_type[(w+w2)*16+g],
00927                                                 lambda / minthr, INFINITY, NULL);
00928                 }
00929                 cpe->ms_mask[w*16+g] = dist2 < dist1;
00930             }
00931             start += sce0->ics.swb_sizes[g];
00932         }
00933     }
00934 }
00935 
00936 AACCoefficientsEncoder ff_aac_coders[] = {
00937     {
00938         search_for_quantizers_faac,
00939         encode_window_bands_info,
00940         quantize_and_encode_band,
00941         search_for_ms,
00942     },
00943     {
00944         search_for_quantizers_anmr,
00945         encode_window_bands_info,
00946         quantize_and_encode_band,
00947         search_for_ms,
00948     },
00949     {
00950         search_for_quantizers_twoloop,
00951         encode_window_bands_info,
00952         quantize_and_encode_band,
00953         search_for_ms,
00954     },
00955     {
00956         search_for_quantizers_fast,
00957         encode_window_bands_info,
00958         quantize_and_encode_band,
00959         search_for_ms,
00960     },
00961 };