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