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 <float.h>
00034 #include "avcodec.h"
00035 #include "put_bits.h"
00036 #include "aac.h"
00037 #include "aacenc.h"
00038 #include "aactab.h"
00039
00041 static const uint8_t run_value_bits_long[64] = {
00042 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00043 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
00044 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
00045 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
00046 };
00047
00049 static const uint8_t run_value_bits_short[16] = {
00050 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
00051 };
00052
00053 static const uint8_t *run_value_bits[2] = {
00054 run_value_bits_long, run_value_bits_short
00055 };
00056
00057
00063 static av_always_inline int quant(float coef, const float Q)
00064 {
00065 float a = coef * Q;
00066 return sqrtf(a * sqrtf(a)) + 0.4054;
00067 }
00068
00069 static void quantize_bands(int *out, const float *in, const float *scaled,
00070 int size, float Q34, int is_signed, int maxval)
00071 {
00072 int i;
00073 double qc;
00074 for (i = 0; i < size; i++) {
00075 qc = scaled[i] * Q34;
00076 out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
00077 if (is_signed && in[i] < 0.0f) {
00078 out[i] = -out[i];
00079 }
00080 }
00081 }
00082
00083 static void abs_pow34_v(float *out, const float *in, const int size)
00084 {
00085 #ifndef USE_REALLY_FULL_SEARCH
00086 int i;
00087 for (i = 0; i < size; i++) {
00088 float a = fabsf(in[i]);
00089 out[i] = sqrtf(a * sqrtf(a));
00090 }
00091 #endif
00092 }
00093
00094 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
00095 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
00096
00102 static av_always_inline float quantize_and_encode_band_cost_template(
00103 struct AACEncContext *s,
00104 PutBitContext *pb, const float *in,
00105 const float *scaled, int size, int scale_idx,
00106 int cb, const float lambda, const float uplim,
00107 int *bits, int BT_ZERO, int BT_UNSIGNED,
00108 int BT_PAIR, int BT_ESC)
00109 {
00110 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
00111 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
00112 const float CLIPPED_ESCAPE = 165140.0f*IQ;
00113 int i, j, k;
00114 float cost = 0;
00115 const int dim = BT_PAIR ? 2 : 4;
00116 int resbits = 0;
00117 const float Q34 = sqrtf(Q * sqrtf(Q));
00118 const int range = aac_cb_range[cb];
00119 const int maxval = aac_cb_maxval[cb];
00120 int off;
00121
00122 if (BT_ZERO) {
00123 for (i = 0; i < size; i++)
00124 cost += in[i]*in[i];
00125 if (bits)
00126 *bits = 0;
00127 return cost * lambda;
00128 }
00129 if (!scaled) {
00130 abs_pow34_v(s->scoefs, in, size);
00131 scaled = s->scoefs;
00132 }
00133 quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
00134 if (BT_UNSIGNED) {
00135 off = 0;
00136 } else {
00137 off = maxval;
00138 }
00139 for (i = 0; i < size; i += dim) {
00140 const float *vec;
00141 int *quants = s->qcoefs + i;
00142 int curidx = 0;
00143 int curbits;
00144 float rd = 0.0f;
00145 for (j = 0; j < dim; j++) {
00146 curidx *= range;
00147 curidx += quants[j] + off;
00148 }
00149 curbits = ff_aac_spectral_bits[cb-1][curidx];
00150 vec = &ff_aac_codebook_vectors[cb-1][curidx*dim];
00151 if (BT_UNSIGNED) {
00152 for (k = 0; k < dim; k++) {
00153 float t = fabsf(in[i+k]);
00154 float di;
00155 if (BT_ESC && vec[k] == 64.0f) {
00156 if (t >= CLIPPED_ESCAPE) {
00157 di = t - CLIPPED_ESCAPE;
00158 curbits += 21;
00159 } else {
00160 int c = av_clip(quant(t, Q), 0, 8191);
00161 di = t - c*cbrtf(c)*IQ;
00162 curbits += av_log2(c)*2 - 4 + 1;
00163 }
00164 } else {
00165 di = t - vec[k]*IQ;
00166 }
00167 if (vec[k] != 0.0f)
00168 curbits++;
00169 rd += di*di;
00170 }
00171 } else {
00172 for (k = 0; k < dim; k++) {
00173 float di = in[i+k] - vec[k]*IQ;
00174 rd += di*di;
00175 }
00176 }
00177 cost += rd * lambda + curbits;
00178 resbits += curbits;
00179 if (cost >= uplim)
00180 return uplim;
00181 if (pb) {
00182 put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
00183 if (BT_UNSIGNED)
00184 for (j = 0; j < dim; j++)
00185 if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
00186 put_bits(pb, 1, in[i+j] < 0.0f);
00187 if (BT_ESC) {
00188 for (j = 0; j < 2; j++) {
00189 if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
00190 int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
00191 int len = av_log2(coef);
00192
00193 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
00194 put_bits(pb, len, coef & ((1 << len) - 1));
00195 }
00196 }
00197 }
00198 }
00199 }
00200
00201 if (bits)
00202 *bits = resbits;
00203 return cost;
00204 }
00205
00206 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC) \
00207 static float quantize_and_encode_band_cost_ ## NAME( \
00208 struct AACEncContext *s, \
00209 PutBitContext *pb, const float *in, \
00210 const float *scaled, int size, int scale_idx, \
00211 int cb, const float lambda, const float uplim, \
00212 int *bits) { \
00213 return quantize_and_encode_band_cost_template( \
00214 s, pb, in, scaled, size, scale_idx, \
00215 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
00216 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC); \
00217 }
00218
00219 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ZERO, 1, 0, 0, 0)
00220 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SQUAD, 0, 0, 0, 0)
00221 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UQUAD, 0, 1, 0, 0)
00222 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(SPAIR, 0, 0, 1, 0)
00223 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(UPAIR, 0, 1, 1, 0)
00224 QUANTIZE_AND_ENCODE_BAND_COST_FUNC(ESC, 0, 1, 1, 1)
00225
00226 static float (*const quantize_and_encode_band_cost_arr[])(
00227 struct AACEncContext *s,
00228 PutBitContext *pb, const float *in,
00229 const float *scaled, int size, int scale_idx,
00230 int cb, const float lambda, const float uplim,
00231 int *bits) = {
00232 quantize_and_encode_band_cost_ZERO,
00233 quantize_and_encode_band_cost_SQUAD,
00234 quantize_and_encode_band_cost_SQUAD,
00235 quantize_and_encode_band_cost_UQUAD,
00236 quantize_and_encode_band_cost_UQUAD,
00237 quantize_and_encode_band_cost_SPAIR,
00238 quantize_and_encode_band_cost_SPAIR,
00239 quantize_and_encode_band_cost_UPAIR,
00240 quantize_and_encode_band_cost_UPAIR,
00241 quantize_and_encode_band_cost_UPAIR,
00242 quantize_and_encode_band_cost_UPAIR,
00243 quantize_and_encode_band_cost_ESC,
00244 };
00245
00246 #define quantize_and_encode_band_cost( \
00247 s, pb, in, scaled, size, scale_idx, cb, \
00248 lambda, uplim, bits) \
00249 quantize_and_encode_band_cost_arr[cb]( \
00250 s, pb, in, scaled, size, scale_idx, cb, \
00251 lambda, uplim, bits)
00252
00253 static float quantize_band_cost(struct AACEncContext *s, const float *in,
00254 const float *scaled, int size, int scale_idx,
00255 int cb, const float lambda, const float uplim,
00256 int *bits)
00257 {
00258 return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
00259 cb, lambda, uplim, bits);
00260 }
00261
00262 static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
00263 const float *in, int size, int scale_idx,
00264 int cb, const float lambda)
00265 {
00266 quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
00267 INFINITY, NULL);
00268 }
00269
00270 static float find_max_val(int group_len, int swb_size, const float *scaled) {
00271 float maxval = 0.0f;
00272 int w2, i;
00273 for (w2 = 0; w2 < group_len; w2++) {
00274 for (i = 0; i < swb_size; i++) {
00275 maxval = FFMAX(maxval, scaled[w2*128+i]);
00276 }
00277 }
00278 return maxval;
00279 }
00280
00281 static int find_min_book(float maxval, int sf) {
00282 float Q = ff_aac_pow2sf_tab[200 - sf + SCALE_ONE_POS - SCALE_DIV_512];
00283 float Q34 = sqrtf(Q * sqrtf(Q));
00284 int qmaxval, cb;
00285 qmaxval = maxval * Q34 + 0.4054f;
00286 if (qmaxval == 0) cb = 0;
00287 else if (qmaxval == 1) cb = 1;
00288 else if (qmaxval == 2) cb = 3;
00289 else if (qmaxval <= 4) cb = 5;
00290 else if (qmaxval <= 7) cb = 7;
00291 else if (qmaxval <= 12) cb = 9;
00292 else cb = 11;
00293 return cb;
00294 }
00295
00299 typedef struct BandCodingPath {
00300 int prev_idx;
00301 float cost;
00302 int run;
00303 } BandCodingPath;
00304
00308 static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
00309 int win, int group_len, const float lambda)
00310 {
00311 BandCodingPath path[120][12];
00312 int w, swb, cb, start, start2, size;
00313 int i, j;
00314 const int max_sfb = sce->ics.max_sfb;
00315 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
00316 const int run_esc = (1 << run_bits) - 1;
00317 int idx, ppos, count;
00318 int stackrun[120], stackcb[120], stack_len;
00319 float next_minrd = INFINITY;
00320 int next_mincb = 0;
00321
00322 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00323 start = win*128;
00324 for (cb = 0; cb < 12; cb++) {
00325 path[0][cb].cost = 0.0f;
00326 path[0][cb].prev_idx = -1;
00327 path[0][cb].run = 0;
00328 }
00329 for (swb = 0; swb < max_sfb; swb++) {
00330 start2 = start;
00331 size = sce->ics.swb_sizes[swb];
00332 if (sce->zeroes[win*16 + swb]) {
00333 for (cb = 0; cb < 12; cb++) {
00334 path[swb+1][cb].prev_idx = cb;
00335 path[swb+1][cb].cost = path[swb][cb].cost;
00336 path[swb+1][cb].run = path[swb][cb].run + 1;
00337 }
00338 } else {
00339 float minrd = next_minrd;
00340 int mincb = next_mincb;
00341 next_minrd = INFINITY;
00342 next_mincb = 0;
00343 for (cb = 0; cb < 12; cb++) {
00344 float cost_stay_here, cost_get_here;
00345 float rd = 0.0f;
00346 for (w = 0; w < group_len; w++) {
00347 FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
00348 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
00349 s->scoefs + start + w*128, size,
00350 sce->sf_idx[(win+w)*16+swb], cb,
00351 lambda / band->threshold, INFINITY, NULL);
00352 }
00353 cost_stay_here = path[swb][cb].cost + rd;
00354 cost_get_here = minrd + rd + run_bits + 4;
00355 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
00356 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
00357 cost_stay_here += run_bits;
00358 if (cost_get_here < cost_stay_here) {
00359 path[swb+1][cb].prev_idx = mincb;
00360 path[swb+1][cb].cost = cost_get_here;
00361 path[swb+1][cb].run = 1;
00362 } else {
00363 path[swb+1][cb].prev_idx = cb;
00364 path[swb+1][cb].cost = cost_stay_here;
00365 path[swb+1][cb].run = path[swb][cb].run + 1;
00366 }
00367 if (path[swb+1][cb].cost < next_minrd) {
00368 next_minrd = path[swb+1][cb].cost;
00369 next_mincb = cb;
00370 }
00371 }
00372 }
00373 start += sce->ics.swb_sizes[swb];
00374 }
00375
00376
00377 stack_len = 0;
00378 idx = 0;
00379 for (cb = 1; cb < 12; cb++)
00380 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
00381 idx = cb;
00382 ppos = max_sfb;
00383 while (ppos > 0) {
00384 cb = idx;
00385 stackrun[stack_len] = path[ppos][cb].run;
00386 stackcb [stack_len] = cb;
00387 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
00388 ppos -= path[ppos][cb].run;
00389 stack_len++;
00390 }
00391
00392 start = 0;
00393 for (i = stack_len - 1; i >= 0; i--) {
00394 put_bits(&s->pb, 4, stackcb[i]);
00395 count = stackrun[i];
00396 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
00397
00398 for (j = 0; j < count; j++) {
00399 sce->band_type[win*16 + start] = stackcb[i];
00400 start++;
00401 }
00402 while (count >= run_esc) {
00403 put_bits(&s->pb, run_bits, run_esc);
00404 count -= run_esc;
00405 }
00406 put_bits(&s->pb, run_bits, count);
00407 }
00408 }
00409
00410 static void codebook_trellis_rate(AACEncContext *s, SingleChannelElement *sce,
00411 int win, int group_len, const float lambda)
00412 {
00413 BandCodingPath path[120][12];
00414 int w, swb, cb, start, start2, size;
00415 int i, j;
00416 const int max_sfb = sce->ics.max_sfb;
00417 const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
00418 const int run_esc = (1 << run_bits) - 1;
00419 int idx, ppos, count;
00420 int stackrun[120], stackcb[120], stack_len;
00421 float next_minrd = INFINITY;
00422 int next_mincb = 0;
00423
00424 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00425 start = win*128;
00426 for (cb = 0; cb < 12; cb++) {
00427 path[0][cb].cost = run_bits+4;
00428 path[0][cb].prev_idx = -1;
00429 path[0][cb].run = 0;
00430 }
00431 for (swb = 0; swb < max_sfb; swb++) {
00432 start2 = start;
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
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
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
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
00567 q0 = coef2minsf(q0f);
00568
00569 q1 = coef2maxsf(q1f);
00570
00571 if (q1 - q0 > 60) {
00572 int q0low = q0;
00573 int q1high = q1;
00574
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
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
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
00631 minscale = coef2minsf(qmin);
00632
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
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
00707 memset(dists, 0, sizeof(dists));
00708
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 = FFMAX(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
00753
00754 do {
00755 int tbits, qstep;
00756 minscaler = sce->sf_idx[0];
00757
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
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[200 - scf + SCALE_ONE_POS - SCALE_DIV_512]);
00958 if (quant_max >= 8191) {
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 start = 0, 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 start = w*128;
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] = sce0->coeffs[start+w2*128+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 };