Libav 0.7.1
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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 "libavutil/libm.h" // brought forward to work around cygwin header breakage
00034 
00035 #include <float.h>
00036 #include "avcodec.h"
00037 #include "put_bits.h"
00038 #include "aac.h"
00039 #include "aacenc.h"
00040 #include "aactab.h"
00041 
00043 static const uint8_t run_value_bits_long[64] = {
00044      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
00045      5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
00046     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
00047     10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
00048 };
00049 
00051 static const uint8_t run_value_bits_short[16] = {
00052     3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
00053 };
00054 
00055 static const uint8_t *run_value_bits[2] = {
00056     run_value_bits_long, run_value_bits_short
00057 };
00058 
00059 
00065 static av_always_inline int quant(float coef, const float Q)
00066 {
00067     float a = coef * Q;
00068     return sqrtf(a * sqrtf(a)) + 0.4054;
00069 }
00070 
00071 static void quantize_bands(int *out, const float *in, const float *scaled,
00072                            int size, float Q34, int is_signed, int maxval)
00073 {
00074     int i;
00075     double qc;
00076     for (i = 0; i < size; i++) {
00077         qc = scaled[i] * Q34;
00078         out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
00079         if (is_signed && in[i] < 0.0f) {
00080             out[i] = -out[i];
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 av_always_inline float quantize_and_encode_band_cost_template(
00105                                 struct AACEncContext *s,
00106                                 PutBitContext *pb, const float *in,
00107                                 const float *scaled, int size, int scale_idx,
00108                                 int cb, const float lambda, const float uplim,
00109                                 int *bits, int BT_ZERO, int BT_UNSIGNED,
00110                                 int BT_PAIR, int BT_ESC)
00111 {
00112     const float IQ = ff_aac_pow2sf_tab[POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
00113     const float  Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
00114     const float CLIPPED_ESCAPE = 165140.0f*IQ;
00115     int i, j;
00116     float cost = 0;
00117     const int dim = BT_PAIR ? 2 : 4;
00118     int resbits = 0;
00119     const float  Q34 = sqrtf(Q * sqrtf(Q));
00120     const int range  = aac_cb_range[cb];
00121     const int maxval = aac_cb_maxval[cb];
00122     int off;
00123 
00124     if (BT_ZERO) {
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     if (!scaled) {
00132         abs_pow34_v(s->scoefs, in, size);
00133         scaled = s->scoefs;
00134     }
00135     quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
00136     if (BT_UNSIGNED) {
00137         off = 0;
00138     } else {
00139         off = maxval;
00140     }
00141     for (i = 0; i < size; i += dim) {
00142         const float *vec;
00143         int *quants = s->qcoefs + i;
00144         int curidx = 0;
00145         int curbits;
00146         float rd = 0.0f;
00147         for (j = 0; j < dim; j++) {
00148             curidx *= range;
00149             curidx += quants[j] + off;
00150         }
00151         curbits =  ff_aac_spectral_bits[cb-1][curidx];
00152         vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
00153         if (BT_UNSIGNED) {
00154             for (j = 0; j < dim; j++) {
00155                 float t = fabsf(in[i+j]);
00156                 float di;
00157                 if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
00158                     if (t >= CLIPPED_ESCAPE) {
00159                         di = t - CLIPPED_ESCAPE;
00160                         curbits += 21;
00161                     } else {
00162                         int c = av_clip(quant(t, Q), 0, 8191);
00163                         di = t - c*cbrtf(c)*IQ;
00164                         curbits += av_log2(c)*2 - 4 + 1;
00165                     }
00166                 } else {
00167                     di = t - vec[j]*IQ;
00168                 }
00169                 if (vec[j] != 0.0f)
00170                     curbits++;
00171                 rd += di*di;
00172             }
00173         } else {
00174             for (j = 0; j < dim; j++) {
00175                 float di = in[i+j] - vec[j]*IQ;
00176                 rd += di*di;
00177             }
00178         }
00179         cost    += rd * lambda + curbits;
00180         resbits += curbits;
00181         if (cost >= uplim)
00182             return uplim;
00183         if (pb) {
00184             put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
00185             if (BT_UNSIGNED)
00186                 for (j = 0; j < dim; j++)
00187                     if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
00188                         put_bits(pb, 1, in[i+j] < 0.0f);
00189             if (BT_ESC) {
00190                 for (j = 0; j < 2; j++) {
00191                     if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
00192                         int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
00193                         int len = av_log2(coef);
00194 
00195                         put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
00196                         put_bits(pb, len, coef & ((1 << len) - 1));
00197                     }
00198                 }
00199             }
00200         }
00201     }
00202 
00203     if (bits)
00204         *bits = resbits;
00205     return cost;
00206 }
00207 
00208 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
00209 static float quantize_and_encode_band_cost_ ## NAME(                                        \
00210                                 struct AACEncContext *s,                                \
00211                                 PutBitContext *pb, const float *in,                     \
00212                                 const float *scaled, int size, int scale_idx,           \
00213                                 int cb, const float lambda, const float uplim,          \
00214                                 int *bits) {                                            \
00215     return quantize_and_encode_band_cost_template(                                      \
00216                                 s, pb, in, scaled, size, scale_idx,                     \
00217                                 BT_ESC ? ESC_BT : cb, lambda, uplim, bits,              \
00218                                 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC);                 \
00219 }
00220 
00221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO,  1, 0, 0, 0)
00222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0)
00223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0)
00224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0)
00225 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0)
00226 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC,   0, 1, 1, 1)
00227 
00228 static float (*const quantize_and_encode_band_cost_arr[])(
00229                                 struct AACEncContext *s,
00230                                 PutBitContext *pb, const float *in,
00231                                 const float *scaled, int size, int scale_idx,
00232                                 int cb, const float lambda, const float uplim,
00233                                 int *bits) = {
00234     quantize_and_encode_band_cost_ZERO,
00235     quantize_and_encode_band_cost_SQUAD,
00236     quantize_and_encode_band_cost_SQUAD,
00237     quantize_and_encode_band_cost_UQUAD,
00238     quantize_and_encode_band_cost_UQUAD,
00239     quantize_and_encode_band_cost_SPAIR,
00240     quantize_and_encode_band_cost_SPAIR,
00241     quantize_and_encode_band_cost_UPAIR,
00242     quantize_and_encode_band_cost_UPAIR,
00243     quantize_and_encode_band_cost_UPAIR,
00244     quantize_and_encode_band_cost_UPAIR,
00245     quantize_and_encode_band_cost_ESC,
00246 };
00247 
00248 #define quantize_and_encode_band_cost(                                  \
00249                                 s, pb, in, scaled, size, scale_idx, cb, \
00250                                 lambda, uplim, bits)                    \
00251     quantize_and_encode_band_cost_arr[cb](                              \
00252                                 s, pb, in, scaled, size, scale_idx, cb, \
00253                                 lambda, uplim, bits)
00254 
00255 static float quantize_band_cost(struct AACEncContext *s, const float *in,
00256                                 const float *scaled, int size, int scale_idx,
00257                                 int cb, const float lambda, const float uplim,
00258                                 int *bits)
00259 {
00260     return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
00261                                          cb, lambda, uplim, bits);
00262 }
00263 
00264 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
00265                                      const float *in, int size, int scale_idx,
00266                                      int cb, const float lambda)
00267 {
00268     quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
00269                                   INFINITY, NULL);
00270 }
00271 
00272 static float find_max_val(int group_len, int swb_size, const float *scaled) {
00273     float maxval = 0.0f;
00274     int w2, i;
00275     for (w2 = 0; w2 < group_len; w2++) {
00276         for (i = 0; i < swb_size; i++) {
00277             maxval = FFMAX(maxval, scaled[w2*128+i]);
00278         }
00279     }
00280     return maxval;
00281 }
00282 
00283 static int find_min_book(float maxval, int sf) {
00284     float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
00285     float Q34 = sqrtf(Q * sqrtf(Q));
00286     int qmaxval, cb;
00287     qmaxval = maxval * Q34 + 0.4054f;
00288     if      (qmaxval ==  0) cb = 0;
00289     else if (qmaxval ==  1) cb = 1;
00290     else if (qmaxval ==  2) cb = 3;
00291     else if (qmaxval <=  4) cb = 5;
00292     else if (qmaxval <=  7) cb = 7;
00293     else if (qmaxval <= 12) cb = 9;
00294     else                    cb = 11;
00295     return cb;
00296 }
00297 
00301 typedef struct BandCodingPath {
00302     int prev_idx; 
00303     float cost;   
00304     int run;
00305 } BandCodingPath;
00306 
00310 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
00311                                      int win, int group_len, const float lambda)
00312 {
00313     BandCodingPath path[120][12];
00314     int w, swb, cb, start, size;
00315     int i, j;
00316     const int max_sfb  = sce->ics.max_sfb;
00317     const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
00318     const int run_esc  = (1 << run_bits) - 1;
00319     int idx, ppos, count;
00320     int stackrun[120], stackcb[120], stack_len;
00321     float next_minrd = INFINITY;
00322     int next_mincb = 0;
00323 
00324     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00325     start = win*128;
00326     for (cb = 0; cb < 12; cb++) {
00327         path[0][cb].cost     = 0.0f;
00328         path[0][cb].prev_idx = -1;
00329         path[0][cb].run      = 0;
00330     }
00331     for (swb = 0; swb < max_sfb; swb++) {
00332         size = sce->ics.swb_sizes[swb];
00333         if (sce->zeroes[win*16 + swb]) {
00334             for (cb = 0; cb < 12; cb++) {
00335                 path[swb+1][cb].prev_idx = cb;
00336                 path[swb+1][cb].cost     = path[swb][cb].cost;
00337                 path[swb+1][cb].run      = path[swb][cb].run + 1;
00338             }
00339         } else {
00340             float minrd = next_minrd;
00341             int mincb = next_mincb;
00342             next_minrd = INFINITY;
00343             next_mincb = 0;
00344             for (cb = 0; cb < 12; cb++) {
00345                 float cost_stay_here, cost_get_here;
00346                 float rd = 0.0f;
00347                 for (w = 0; w < group_len; w++) {
00348                     FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
00349                     rd += quantize_band_cost(s, sce->coeffs + start + w*128,
00350                                              s->scoefs + start + w*128, size,
00351                                              sce->sf_idx[(win+w)*16+swb], cb,
00352                                              lambda / band->threshold, INFINITY, NULL);
00353                 }
00354                 cost_stay_here = path[swb][cb].cost + rd;
00355                 cost_get_here  = minrd              + rd + run_bits + 4;
00356                 if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
00357                     != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
00358                     cost_stay_here += run_bits;
00359                 if (cost_get_here < cost_stay_here) {
00360                     path[swb+1][cb].prev_idx = mincb;
00361                     path[swb+1][cb].cost     = cost_get_here;
00362                     path[swb+1][cb].run      = 1;
00363                 } else {
00364                     path[swb+1][cb].prev_idx = cb;
00365                     path[swb+1][cb].cost     = cost_stay_here;
00366                     path[swb+1][cb].run      = path[swb][cb].run + 1;
00367                 }
00368                 if (path[swb+1][cb].cost < next_minrd) {
00369                     next_minrd = path[swb+1][cb].cost;
00370                     next_mincb = cb;
00371                 }
00372             }
00373         }
00374         start += sce->ics.swb_sizes[swb];
00375     }
00376 
00377     //convert resulting path from backward-linked list
00378     stack_len = 0;
00379     idx       = 0;
00380     for (cb = 1; cb < 12; cb++)
00381         if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
00382             idx = cb;
00383     ppos = max_sfb;
00384     while (ppos > 0) {
00385         cb = idx;
00386         stackrun[stack_len] = path[ppos][cb].run;
00387         stackcb [stack_len] = cb;
00388         idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
00389         ppos -= path[ppos][cb].run;
00390         stack_len++;
00391     }
00392     //perform actual band info encoding
00393     start = 0;
00394     for (i = stack_len - 1; i >= 0; i--) {
00395         put_bits(&s->pb, 4, stackcb[i]);
00396         count = stackrun[i];
00397         memset(sce->zeroes + win*16 + start, !stackcb[i], count);
00398         //XXX: memset when band_type is also uint8_t
00399         for (j = 0; j < count; j++) {
00400             sce->band_type[win*16 + start] =  stackcb[i];
00401             start++;
00402         }
00403         while (count >= run_esc) {
00404             put_bits(&s->pb, run_bits, run_esc);
00405             count -= run_esc;
00406         }
00407         put_bits(&s->pb, run_bits, count);
00408     }
00409 }
00410 
00411 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
00412                                   int win, int group_len, const float lambda)
00413 {
00414     BandCodingPath path[120][12];
00415     int w, swb, cb, start, size;
00416     int i, j;
00417     const int max_sfb  = sce->ics.max_sfb;
00418     const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
00419     const int run_esc  = (1 << run_bits) - 1;
00420     int idx, ppos, count;
00421     int stackrun[120], stackcb[120], stack_len;
00422     float next_minrd = INFINITY;
00423     int next_mincb = 0;
00424 
00425     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00426     start = win*128;
00427     for (cb = 0; cb < 12; cb++) {
00428         path[0][cb].cost     = run_bits+4;
00429         path[0][cb].prev_idx = -1;
00430         path[0][cb].run      = 0;
00431     }
00432     for (swb = 0; swb < max_sfb; swb++) {
00433         size = sce->ics.swb_sizes[swb];
00434         if (sce->zeroes[win*16 + swb]) {
00435             for (cb = 0; cb < 12; cb++) {
00436                 path[swb+1][cb].prev_idx = cb;
00437                 path[swb+1][cb].cost     = path[swb][cb].cost;
00438                 path[swb+1][cb].run      = path[swb][cb].run + 1;
00439             }
00440         } else {
00441             float minrd = next_minrd;
00442             int mincb = next_mincb;
00443             int startcb = sce->band_type[win*16+swb];
00444             next_minrd = INFINITY;
00445             next_mincb = 0;
00446             for (cb = 0; cb < startcb; cb++) {
00447                 path[swb+1][cb].cost = 61450;
00448                 path[swb+1][cb].prev_idx = -1;
00449                 path[swb+1][cb].run = 0;
00450             }
00451             for (cb = startcb; cb < 12; cb++) {
00452                 float cost_stay_here, cost_get_here;
00453                 float rd = 0.0f;
00454                 for (w = 0; w < group_len; w++) {
00455                     rd += quantize_band_cost(s, sce->coeffs + start + w*128,
00456                                              s->scoefs + start + w*128, size,
00457                                              sce->sf_idx[(win+w)*16+swb], cb,
00458                                              0, INFINITY, NULL);
00459                 }
00460                 cost_stay_here = path[swb][cb].cost + rd;
00461                 cost_get_here  = minrd              + rd + run_bits + 4;
00462                 if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
00463                     != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
00464                     cost_stay_here += run_bits;
00465                 if (cost_get_here < cost_stay_here) {
00466                     path[swb+1][cb].prev_idx = mincb;
00467                     path[swb+1][cb].cost     = cost_get_here;
00468                     path[swb+1][cb].run      = 1;
00469                 } else {
00470                     path[swb+1][cb].prev_idx = cb;
00471                     path[swb+1][cb].cost     = cost_stay_here;
00472                     path[swb+1][cb].run      = path[swb][cb].run + 1;
00473                 }
00474                 if (path[swb+1][cb].cost < next_minrd) {
00475                     next_minrd = path[swb+1][cb].cost;
00476                     next_mincb = cb;
00477                 }
00478             }
00479         }
00480         start += sce->ics.swb_sizes[swb];
00481     }
00482 
00483     //convert resulting path from backward-linked list
00484     stack_len = 0;
00485     idx       = 0;
00486     for (cb = 1; cb < 12; cb++)
00487         if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
00488             idx = cb;
00489     ppos = max_sfb;
00490     while (ppos > 0) {
00491         assert(idx >= 0);
00492         cb = idx;
00493         stackrun[stack_len] = path[ppos][cb].run;
00494         stackcb [stack_len] = cb;
00495         idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
00496         ppos -= path[ppos][cb].run;
00497         stack_len++;
00498     }
00499     //perform actual band info encoding
00500     start = 0;
00501     for (i = stack_len - 1; i >= 0; i--) {
00502         put_bits(&s->pb, 4, stackcb[i]);
00503         count = stackrun[i];
00504         memset(sce->zeroes + win*16 + start, !stackcb[i], count);
00505         //XXX: memset when band_type is also uint8_t
00506         for (j = 0; j < count; j++) {
00507             sce->band_type[win*16 + start] =  stackcb[i];
00508             start++;
00509         }
00510         while (count >= run_esc) {
00511             put_bits(&s->pb, run_bits, run_esc);
00512             count -= run_esc;
00513         }
00514         put_bits(&s->pb, run_bits, count);
00515     }
00516 }
00517 
00519 static av_always_inline uint8_t coef2minsf(float coef) {
00520     return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
00521 }
00522 
00524 static av_always_inline uint8_t coef2maxsf(float coef) {
00525     return av_clip_uint8(log2f(coef)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
00526 }
00527 
00528 typedef struct TrellisPath {
00529     float cost;
00530     int prev;
00531 } TrellisPath;
00532 
00533 #define TRELLIS_STAGES 121
00534 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
00535 
00536 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
00537                                        SingleChannelElement *sce,
00538                                        const float lambda)
00539 {
00540     int q, w, w2, g, start = 0;
00541     int i, j;
00542     int idx;
00543     TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
00544     int bandaddr[TRELLIS_STAGES];
00545     int minq;
00546     float mincost;
00547     float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
00548     int q0, q1, qcnt = 0;
00549 
00550     for (i = 0; i < 1024; i++) {
00551         float t = fabsf(sce->coeffs[i]);
00552         if (t > 0.0f) {
00553             q0f = FFMIN(q0f, t);
00554             q1f = FFMAX(q1f, t);
00555             qnrgf += t*t;
00556             qcnt++;
00557         }
00558     }
00559 
00560     if (!qcnt) {
00561         memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00562         memset(sce->zeroes, 1, sizeof(sce->zeroes));
00563         return;
00564     }
00565 
00566     //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
00567     q0 = coef2minsf(q0f);
00568     //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
00569     q1 = coef2maxsf(q1f);
00570     //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
00571     if (q1 - q0 > 60) {
00572         int q0low  = q0;
00573         int q1high = q1;
00574         //minimum scalefactor index is when maximum nonzero coefficient after quantizing is not clipped
00575         int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
00576         q1 = qnrg + 30;
00577         q0 = qnrg - 30;
00578         //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
00579         if (q0 < q0low) {
00580             q1 += q0low - q0;
00581             q0  = q0low;
00582         } else if (q1 > q1high) {
00583             q0 -= q1 - q1high;
00584             q1  = q1high;
00585         }
00586     }
00587     //av_log(NULL, AV_LOG_ERROR, "q0 %d, q1 %d\n", q0, q1);
00588 
00589     for (i = 0; i < TRELLIS_STATES; i++) {
00590         paths[0][i].cost    = 0.0f;
00591         paths[0][i].prev    = -1;
00592     }
00593     for (j = 1; j < TRELLIS_STAGES; j++) {
00594         for (i = 0; i < TRELLIS_STATES; i++) {
00595             paths[j][i].cost    = INFINITY;
00596             paths[j][i].prev    = -2;
00597         }
00598     }
00599     idx = 1;
00600     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00601     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00602         start = w*128;
00603         for (g = 0; g < sce->ics.num_swb; g++) {
00604             const float *coefs = sce->coeffs + start;
00605             float qmin, qmax;
00606             int nz = 0;
00607 
00608             bandaddr[idx] = w * 16 + g;
00609             qmin = INT_MAX;
00610             qmax = 0.0f;
00611             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00612                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00613                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00614                     sce->zeroes[(w+w2)*16+g] = 1;
00615                     continue;
00616                 }
00617                 sce->zeroes[(w+w2)*16+g] = 0;
00618                 nz = 1;
00619                 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
00620                     float t = fabsf(coefs[w2*128+i]);
00621                     if (t > 0.0f)
00622                         qmin = FFMIN(qmin, t);
00623                     qmax = FFMAX(qmax, t);
00624                 }
00625             }
00626             if (nz) {
00627                 int minscale, maxscale;
00628                 float minrd = INFINITY;
00629                 float maxval;
00630                 //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
00631                 minscale = coef2minsf(qmin);
00632                 //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
00633                 maxscale = coef2maxsf(qmax);
00634                 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
00635                 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
00636                 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
00637                 for (q = minscale; q < maxscale; q++) {
00638                     float dist = 0;
00639                     int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
00640                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00641                         FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00642                         dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
00643                                                    q + q0, cb, lambda / band->threshold, INFINITY, NULL);
00644                     }
00645                     minrd = FFMIN(minrd, dist);
00646 
00647                     for (i = 0; i < q1 - q0; i++) {
00648                         float cost;
00649                         cost = paths[idx - 1][i].cost + dist
00650                                + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
00651                         if (cost < paths[idx][q].cost) {
00652                             paths[idx][q].cost    = cost;
00653                             paths[idx][q].prev    = i;
00654                         }
00655                     }
00656                 }
00657             } else {
00658                 for (q = 0; q < q1 - q0; q++) {
00659                     paths[idx][q].cost = paths[idx - 1][q].cost + 1;
00660                     paths[idx][q].prev = q;
00661                 }
00662             }
00663             sce->zeroes[w*16+g] = !nz;
00664             start += sce->ics.swb_sizes[g];
00665             idx++;
00666         }
00667     }
00668     idx--;
00669     mincost = paths[idx][0].cost;
00670     minq    = 0;
00671     for (i = 1; i < TRELLIS_STATES; i++) {
00672         if (paths[idx][i].cost < mincost) {
00673             mincost = paths[idx][i].cost;
00674             minq = i;
00675         }
00676     }
00677     while (idx) {
00678         sce->sf_idx[bandaddr[idx]] = minq + q0;
00679         minq = paths[idx][minq].prev;
00680         idx--;
00681     }
00682     //set the same quantizers inside window groups
00683     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00684         for (g = 0;  g < sce->ics.num_swb; g++)
00685             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
00686                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
00687 }
00688 
00692 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
00693                                           AACEncContext *s,
00694                                           SingleChannelElement *sce,
00695                                           const float lambda)
00696 {
00697     int start = 0, i, w, w2, g;
00698     int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
00699     float dists[128], uplims[128];
00700     float maxvals[128];
00701     int fflag, minscaler;
00702     int its  = 0;
00703     int allz = 0;
00704     float minthr = INFINITY;
00705 
00706     //XXX: some heuristic to determine initial quantizers will reduce search time
00707     memset(dists, 0, sizeof(dists));
00708     //determine zero bands and upper limits
00709     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00710         for (g = 0;  g < sce->ics.num_swb; g++) {
00711             int nz = 0;
00712             float uplim = 0.0f;
00713             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00714                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
00715                 uplim += band->threshold;
00716                 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00717                     sce->zeroes[(w+w2)*16+g] = 1;
00718                     continue;
00719                 }
00720                 nz = 1;
00721             }
00722             uplims[w*16+g] = uplim *512;
00723             sce->zeroes[w*16+g] = !nz;
00724             if (nz)
00725                 minthr = FFMIN(minthr, uplim);
00726             allz |= nz;
00727         }
00728     }
00729     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00730         for (g = 0;  g < sce->ics.num_swb; g++) {
00731             if (sce->zeroes[w*16+g]) {
00732                 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
00733                 continue;
00734             }
00735             sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
00736         }
00737     }
00738 
00739     if (!allz)
00740         return;
00741     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00742 
00743     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00744         start = w*128;
00745         for (g = 0;  g < sce->ics.num_swb; g++) {
00746             const float *scaled = s->scoefs + start;
00747             maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
00748             start += sce->ics.swb_sizes[g];
00749         }
00750     }
00751 
00752     //perform two-loop search
00753     //outer loop - improve quality
00754     do {
00755         int tbits, qstep;
00756         minscaler = sce->sf_idx[0];
00757         //inner loop - quantize spectrum to fit into given number of bits
00758         qstep = its ? 1 : 32;
00759         do {
00760             int prev = -1;
00761             tbits = 0;
00762             fflag = 0;
00763             for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00764                 start = w*128;
00765                 for (g = 0;  g < sce->ics.num_swb; g++) {
00766                     const float *coefs = sce->coeffs + start;
00767                     const float *scaled = s->scoefs + start;
00768                     int bits = 0;
00769                     int cb;
00770                     float dist = 0.0f;
00771 
00772                     if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
00773                         start += sce->ics.swb_sizes[g];
00774                         continue;
00775                     }
00776                     minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
00777                     cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
00778                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00779                         int b;
00780                         dist += quantize_band_cost(s, coefs + w2*128,
00781                                                    scaled + w2*128,
00782                                                    sce->ics.swb_sizes[g],
00783                                                    sce->sf_idx[w*16+g],
00784                                                    cb,
00785                                                    1.0f,
00786                                                    INFINITY,
00787                                                    &b);
00788                         bits += b;
00789                     }
00790                     dists[w*16+g] = dist - bits;
00791                     if (prev != -1) {
00792                         bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
00793                     }
00794                     tbits += bits;
00795                     start += sce->ics.swb_sizes[g];
00796                     prev = sce->sf_idx[w*16+g];
00797                 }
00798             }
00799             if (tbits > destbits) {
00800                 for (i = 0; i < 128; i++)
00801                     if (sce->sf_idx[i] < 218 - qstep)
00802                         sce->sf_idx[i] += qstep;
00803             } else {
00804                 for (i = 0; i < 128; i++)
00805                     if (sce->sf_idx[i] > 60 - qstep)
00806                         sce->sf_idx[i] -= qstep;
00807             }
00808             qstep >>= 1;
00809             if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
00810                 qstep = 1;
00811         } while (qstep);
00812 
00813         fflag = 0;
00814         minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
00815         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00816             for (g = 0; g < sce->ics.num_swb; g++) {
00817                 int prevsc = sce->sf_idx[w*16+g];
00818                 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
00819                     if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
00820                         sce->sf_idx[w*16+g]--;
00821                     else //Try to make sure there is some energy in every band
00822                         sce->sf_idx[w*16+g]-=2;
00823                 }
00824                 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
00825                 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
00826                 if (sce->sf_idx[w*16+g] != prevsc)
00827                     fflag = 1;
00828                 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
00829             }
00830         }
00831         its++;
00832     } while (fflag && its < 10);
00833 }
00834 
00835 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
00836                                        SingleChannelElement *sce,
00837                                        const float lambda)
00838 {
00839     int start = 0, i, w, w2, g;
00840     float uplim[128], maxq[128];
00841     int minq, maxsf;
00842     float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
00843     int last = 0, lastband = 0, curband = 0;
00844     float avg_energy = 0.0;
00845     if (sce->ics.num_windows == 1) {
00846         start = 0;
00847         for (i = 0; i < 1024; i++) {
00848             if (i - start >= sce->ics.swb_sizes[curband]) {
00849                 start += sce->ics.swb_sizes[curband];
00850                 curband++;
00851             }
00852             if (sce->coeffs[i]) {
00853                 avg_energy += sce->coeffs[i] * sce->coeffs[i];
00854                 last = i;
00855                 lastband = curband;
00856             }
00857         }
00858     } else {
00859         for (w = 0; w < 8; w++) {
00860             const float *coeffs = sce->coeffs + w*128;
00861             start = 0;
00862             for (i = 0; i < 128; i++) {
00863                 if (i - start >= sce->ics.swb_sizes[curband]) {
00864                     start += sce->ics.swb_sizes[curband];
00865                     curband++;
00866                 }
00867                 if (coeffs[i]) {
00868                     avg_energy += coeffs[i] * coeffs[i];
00869                     last = FFMAX(last, i);
00870                     lastband = FFMAX(lastband, curband);
00871                 }
00872             }
00873         }
00874     }
00875     last++;
00876     avg_energy /= last;
00877     if (avg_energy == 0.0f) {
00878         for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
00879             sce->sf_idx[i] = SCALE_ONE_POS;
00880         return;
00881     }
00882     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00883         start = w*128;
00884         for (g = 0; g < sce->ics.num_swb; g++) {
00885             float *coefs   = sce->coeffs + start;
00886             const int size = sce->ics.swb_sizes[g];
00887             int start2 = start, end2 = start + size, peakpos = start;
00888             float maxval = -1, thr = 0.0f, t;
00889             maxq[w*16+g] = 0.0f;
00890             if (g > lastband) {
00891                 maxq[w*16+g] = 0.0f;
00892                 start += size;
00893                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
00894                     memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
00895                 continue;
00896             }
00897             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00898                 for (i = 0; i < size; i++) {
00899                     float t = coefs[w2*128+i]*coefs[w2*128+i];
00900                     maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
00901                     thr += t;
00902                     if (sce->ics.num_windows == 1 && maxval < t) {
00903                         maxval  = t;
00904                         peakpos = start+i;
00905                     }
00906                 }
00907             }
00908             if (sce->ics.num_windows == 1) {
00909                 start2 = FFMAX(peakpos - 2, start2);
00910                 end2   = FFMIN(peakpos + 3, end2);
00911             } else {
00912                 start2 -= start;
00913                 end2   -= start;
00914             }
00915             start += size;
00916             thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
00917             t   = 1.0 - (1.0 * start2 / last);
00918             uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
00919         }
00920     }
00921     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00922     abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00923     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00924         start = w*128;
00925         for (g = 0;  g < sce->ics.num_swb; g++) {
00926             const float *coefs  = sce->coeffs + start;
00927             const float *scaled = s->scoefs   + start;
00928             const int size      = sce->ics.swb_sizes[g];
00929             int scf, prev_scf, step;
00930             int min_scf = -1, max_scf = 256;
00931             float curdiff;
00932             if (maxq[w*16+g] < 21.544) {
00933                 sce->zeroes[w*16+g] = 1;
00934                 start += size;
00935                 continue;
00936             }
00937             sce->zeroes[w*16+g] = 0;
00938             scf  = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
00939             step = 16;
00940             for (;;) {
00941                 float dist = 0.0f;
00942                 int quant_max;
00943 
00944                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00945                     int b;
00946                     dist += quantize_band_cost(s, coefs + w2*128,
00947                                                scaled + w2*128,
00948                                                sce->ics.swb_sizes[g],
00949                                                scf,
00950                                                ESC_BT,
00951                                                lambda,
00952                                                INFINITY,
00953                                                &b);
00954                     dist -= b;
00955                 }
00956                 dist *= 1.0f / 512.0f / lambda;
00957                 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
00958                 if (quant_max >= 8191) { // too much, return to the previous quantizer
00959                     sce->sf_idx[w*16+g] = prev_scf;
00960                     break;
00961                 }
00962                 prev_scf = scf;
00963                 curdiff = fabsf(dist - uplim[w*16+g]);
00964                 if (curdiff <= 1.0f)
00965                     step = 0;
00966                 else
00967                     step = log2f(curdiff);
00968                 if (dist > uplim[w*16+g])
00969                     step = -step;
00970                 scf += step;
00971                 scf = av_clip_uint8(scf);
00972                 step = scf - prev_scf;
00973                 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
00974                     sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
00975                     break;
00976                 }
00977                 if (step > 0)
00978                     min_scf = prev_scf;
00979                 else
00980                     max_scf = prev_scf;
00981             }
00982             start += size;
00983         }
00984     }
00985     minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
00986     for (i = 1; i < 128; i++) {
00987         if (!sce->sf_idx[i])
00988             sce->sf_idx[i] = sce->sf_idx[i-1];
00989         else
00990             minq = FFMIN(minq, sce->sf_idx[i]);
00991     }
00992     if (minq == INT_MAX)
00993         minq = 0;
00994     minq = FFMIN(minq, SCALE_MAX_POS);
00995     maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
00996     for (i = 126; i >= 0; i--) {
00997         if (!sce->sf_idx[i])
00998             sce->sf_idx[i] = sce->sf_idx[i+1];
00999         sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
01000     }
01001 }
01002 
01003 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
01004                                        SingleChannelElement *sce,
01005                                        const float lambda)
01006 {
01007     int i, w, w2, g;
01008     int minq = 255;
01009 
01010     memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
01011     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
01012         for (g = 0; g < sce->ics.num_swb; g++) {
01013             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
01014                 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
01015                 if (band->energy <= band->threshold) {
01016                     sce->sf_idx[(w+w2)*16+g] = 218;
01017                     sce->zeroes[(w+w2)*16+g] = 1;
01018                 } else {
01019                     sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
01020                     sce->zeroes[(w+w2)*16+g] = 0;
01021                 }
01022                 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
01023             }
01024         }
01025     }
01026     for (i = 0; i < 128; i++) {
01027         sce->sf_idx[i] = 140;
01028         //av_clip(sce->sf_idx[i], minq, minq + SCALE_MAX_DIFF - 1);
01029     }
01030     //set the same quantizers inside window groups
01031     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
01032         for (g = 0;  g < sce->ics.num_swb; g++)
01033             for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
01034                 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
01035 }
01036 
01037 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
01038                           const float lambda)
01039 {
01040     int start = 0, i, w, w2, g;
01041     float M[128], S[128];
01042     float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
01043     SingleChannelElement *sce0 = &cpe->ch[0];
01044     SingleChannelElement *sce1 = &cpe->ch[1];
01045     if (!cpe->common_window)
01046         return;
01047     for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
01048         for (g = 0;  g < sce0->ics.num_swb; g++) {
01049             if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
01050                 float dist1 = 0.0f, dist2 = 0.0f;
01051                 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
01052                     FFPsyBand *band0 = &s->psy.psy_bands[(s->cur_channel+0)*PSY_MAX_BANDS+(w+w2)*16+g];
01053                     FFPsyBand *band1 = &s->psy.psy_bands[(s->cur_channel+1)*PSY_MAX_BANDS+(w+w2)*16+g];
01054                     float minthr = FFMIN(band0->threshold, band1->threshold);
01055                     float maxthr = FFMAX(band0->threshold, band1->threshold);
01056                     for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
01057                         M[i] = (sce0->coeffs[start+w2*128+i]
01058                               + sce1->coeffs[start+w2*128+i]) * 0.5;
01059                         S[i] =  M[i]
01060                               - sce1->coeffs[start+w2*128+i];
01061                     }
01062                     abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
01063                     abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
01064                     abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
01065                     abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
01066                     dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
01067                                                 L34,
01068                                                 sce0->ics.swb_sizes[g],
01069                                                 sce0->sf_idx[(w+w2)*16+g],
01070                                                 sce0->band_type[(w+w2)*16+g],
01071                                                 lambda / band0->threshold, INFINITY, NULL);
01072                     dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
01073                                                 R34,
01074                                                 sce1->ics.swb_sizes[g],
01075                                                 sce1->sf_idx[(w+w2)*16+g],
01076                                                 sce1->band_type[(w+w2)*16+g],
01077                                                 lambda / band1->threshold, INFINITY, NULL);
01078                     dist2 += quantize_band_cost(s, M,
01079                                                 M34,
01080                                                 sce0->ics.swb_sizes[g],
01081                                                 sce0->sf_idx[(w+w2)*16+g],
01082                                                 sce0->band_type[(w+w2)*16+g],
01083                                                 lambda / maxthr, INFINITY, NULL);
01084                     dist2 += quantize_band_cost(s, S,
01085                                                 S34,
01086                                                 sce1->ics.swb_sizes[g],
01087                                                 sce1->sf_idx[(w+w2)*16+g],
01088                                                 sce1->band_type[(w+w2)*16+g],
01089                                                 lambda / minthr, INFINITY, NULL);
01090                 }
01091                 cpe->ms_mask[w*16+g] = dist2 < dist1;
01092             }
01093             start += sce0->ics.swb_sizes[g];
01094         }
01095     }
01096 }
01097 
01098 AACCoefficientsEncoder ff_aac_coders[] = {
01099     {
01100         search_for_quantizers_faac,
01101         encode_window_bands_info,
01102         quantize_and_encode_band,
01103         search_for_ms,
01104     },
01105     {
01106         search_for_quantizers_anmr,
01107         encode_window_bands_info,
01108         quantize_and_encode_band,
01109         search_for_ms,
01110     },
01111     {
01112         search_for_quantizers_twoloop,
01113         codebook_trellis_rate,
01114         quantize_and_encode_band,
01115         search_for_ms,
01116     },
01117     {
01118         search_for_quantizers_fast,
01119         encode_window_bands_info,
01120         quantize_and_encode_band,
01121         search_for_ms,
01122     },
01123 };