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