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