00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028
00029
00030
00031
00032
00033 #include "libavutil/libm.h"
00034
00035 #include <float.h>
00036 #include "libavutil/mathematics.h"
00037 #include "avcodec.h"
00038 #include "put_bits.h"
00039 #include "aac.h"
00040 #include "aacenc.h"
00041 #include "aactab.h"
00042
00044 static const uint8_t run_value_bits_long[64] = {
00045 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
00046 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10,
00047 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
00048 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
00049 };
00050
00052 static const uint8_t run_value_bits_short[16] = {
00053 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
00054 };
00055
00056 static const uint8_t *run_value_bits[2] = {
00057 run_value_bits_long, run_value_bits_short
00058 };
00059
00060
00066 static av_always_inline int quant(float coef, const float Q)
00067 {
00068 float a = coef * Q;
00069 return sqrtf(a * sqrtf(a)) + 0.4054;
00070 }
00071
00072 static void quantize_bands(int *out, const float *in, const float *scaled,
00073 int size, float Q34, int is_signed, int maxval)
00074 {
00075 int i;
00076 double qc;
00077 for (i = 0; i < size; i++) {
00078 qc = scaled[i] * Q34;
00079 out[i] = (int)FFMIN(qc + 0.4054, (double)maxval);
00080 if (is_signed && in[i] < 0.0f) {
00081 out[i] = -out[i];
00082 }
00083 }
00084 }
00085
00086 static void abs_pow34_v(float *out, const float *in, const int size)
00087 {
00088 #ifndef USE_REALLY_FULL_SEARCH
00089 int i;
00090 for (i = 0; i < size; i++) {
00091 float a = fabsf(in[i]);
00092 out[i] = sqrtf(a * sqrtf(a));
00093 }
00094 #endif
00095 }
00096
00097 static const uint8_t aac_cb_range [12] = {0, 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17};
00098 static const uint8_t aac_cb_maxval[12] = {0, 1, 1, 2, 2, 4, 4, 7, 7, 12, 12, 16};
00099
00105 static av_always_inline float quantize_and_encode_band_cost_template(
00106 struct AACEncContext *s,
00107 PutBitContext *pb, const float *in,
00108 const float *scaled, int size, int scale_idx,
00109 int cb, const float lambda, const float uplim,
00110 int *bits, int BT_ZERO, int BT_UNSIGNED,
00111 int BT_PAIR, int BT_ESC)
00112 {
00113 const 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.ch[s->cur_channel].psy_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 float cost_stay_here = path[swb][0].cost;
00437 float cost_get_here = next_minrd + run_bits + 4;
00438 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
00439 != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
00440 cost_stay_here += run_bits;
00441 if (cost_get_here < cost_stay_here) {
00442 path[swb+1][0].prev_idx = next_mincb;
00443 path[swb+1][0].cost = cost_get_here;
00444 path[swb+1][0].run = 1;
00445 } else {
00446 path[swb+1][0].prev_idx = 0;
00447 path[swb+1][0].cost = cost_stay_here;
00448 path[swb+1][0].run = path[swb][0].run + 1;
00449 }
00450 next_minrd = path[swb+1][0].cost;
00451 next_mincb = 0;
00452 for (cb = 1; cb < 12; cb++) {
00453 path[swb+1][cb].cost = 61450;
00454 path[swb+1][cb].prev_idx = -1;
00455 path[swb+1][cb].run = 0;
00456 }
00457 } else {
00458 float minrd = next_minrd;
00459 int mincb = next_mincb;
00460 int startcb = sce->band_type[win*16+swb];
00461 next_minrd = INFINITY;
00462 next_mincb = 0;
00463 for (cb = 0; cb < startcb; cb++) {
00464 path[swb+1][cb].cost = 61450;
00465 path[swb+1][cb].prev_idx = -1;
00466 path[swb+1][cb].run = 0;
00467 }
00468 for (cb = startcb; cb < 12; cb++) {
00469 float cost_stay_here, cost_get_here;
00470 float rd = 0.0f;
00471 for (w = 0; w < group_len; w++) {
00472 rd += quantize_band_cost(s, sce->coeffs + start + w*128,
00473 s->scoefs + start + w*128, size,
00474 sce->sf_idx[(win+w)*16+swb], cb,
00475 0, INFINITY, NULL);
00476 }
00477 cost_stay_here = path[swb][cb].cost + rd;
00478 cost_get_here = minrd + rd + run_bits + 4;
00479 if ( run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
00480 != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
00481 cost_stay_here += run_bits;
00482 if (cost_get_here < cost_stay_here) {
00483 path[swb+1][cb].prev_idx = mincb;
00484 path[swb+1][cb].cost = cost_get_here;
00485 path[swb+1][cb].run = 1;
00486 } else {
00487 path[swb+1][cb].prev_idx = cb;
00488 path[swb+1][cb].cost = cost_stay_here;
00489 path[swb+1][cb].run = path[swb][cb].run + 1;
00490 }
00491 if (path[swb+1][cb].cost < next_minrd) {
00492 next_minrd = path[swb+1][cb].cost;
00493 next_mincb = cb;
00494 }
00495 }
00496 }
00497 start += sce->ics.swb_sizes[swb];
00498 }
00499
00500
00501 stack_len = 0;
00502 idx = 0;
00503 for (cb = 1; cb < 12; cb++)
00504 if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
00505 idx = cb;
00506 ppos = max_sfb;
00507 while (ppos > 0) {
00508 assert(idx >= 0);
00509 cb = idx;
00510 stackrun[stack_len] = path[ppos][cb].run;
00511 stackcb [stack_len] = cb;
00512 idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
00513 ppos -= path[ppos][cb].run;
00514 stack_len++;
00515 }
00516
00517 start = 0;
00518 for (i = stack_len - 1; i >= 0; i--) {
00519 put_bits(&s->pb, 4, stackcb[i]);
00520 count = stackrun[i];
00521 memset(sce->zeroes + win*16 + start, !stackcb[i], count);
00522
00523 for (j = 0; j < count; j++) {
00524 sce->band_type[win*16 + start] = stackcb[i];
00525 start++;
00526 }
00527 while (count >= run_esc) {
00528 put_bits(&s->pb, run_bits, run_esc);
00529 count -= run_esc;
00530 }
00531 put_bits(&s->pb, run_bits, count);
00532 }
00533 }
00534
00536 static av_always_inline uint8_t coef2minsf(float coef) {
00537 return av_clip_uint8(log2f(coef)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
00538 }
00539
00541 static av_always_inline uint8_t coef2maxsf(float coef) {
00542 return av_clip_uint8(log2f(coef)*4 + 6 + SCALE_ONE_POS - SCALE_DIV_512);
00543 }
00544
00545 typedef struct TrellisPath {
00546 float cost;
00547 int prev;
00548 } TrellisPath;
00549
00550 #define TRELLIS_STAGES 121
00551 #define TRELLIS_STATES (SCALE_MAX_DIFF+1)
00552
00553 static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
00554 SingleChannelElement *sce,
00555 const float lambda)
00556 {
00557 int q, w, w2, g, start = 0;
00558 int i, j;
00559 int idx;
00560 TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
00561 int bandaddr[TRELLIS_STAGES];
00562 int minq;
00563 float mincost;
00564 float q0f = FLT_MAX, q1f = 0.0f, qnrgf = 0.0f;
00565 int q0, q1, qcnt = 0;
00566
00567 for (i = 0; i < 1024; i++) {
00568 float t = fabsf(sce->coeffs[i]);
00569 if (t > 0.0f) {
00570 q0f = FFMIN(q0f, t);
00571 q1f = FFMAX(q1f, t);
00572 qnrgf += t*t;
00573 qcnt++;
00574 }
00575 }
00576
00577 if (!qcnt) {
00578 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00579 memset(sce->zeroes, 1, sizeof(sce->zeroes));
00580 return;
00581 }
00582
00583
00584 q0 = coef2minsf(q0f);
00585
00586 q1 = coef2maxsf(q1f);
00587
00588 if (q1 - q0 > 60) {
00589 int q0low = q0;
00590 int q1high = q1;
00591
00592 int qnrg = av_clip_uint8(log2f(sqrtf(qnrgf/qcnt))*4 - 31 + SCALE_ONE_POS - SCALE_DIV_512);
00593 q1 = qnrg + 30;
00594 q0 = qnrg - 30;
00595
00596 if (q0 < q0low) {
00597 q1 += q0low - q0;
00598 q0 = q0low;
00599 } else if (q1 > q1high) {
00600 q0 -= q1 - q1high;
00601 q1 = q1high;
00602 }
00603 }
00604
00605
00606 for (i = 0; i < TRELLIS_STATES; i++) {
00607 paths[0][i].cost = 0.0f;
00608 paths[0][i].prev = -1;
00609 }
00610 for (j = 1; j < TRELLIS_STAGES; j++) {
00611 for (i = 0; i < TRELLIS_STATES; i++) {
00612 paths[j][i].cost = INFINITY;
00613 paths[j][i].prev = -2;
00614 }
00615 }
00616 idx = 1;
00617 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00618 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00619 start = w*128;
00620 for (g = 0; g < sce->ics.num_swb; g++) {
00621 const float *coefs = sce->coeffs + start;
00622 float qmin, qmax;
00623 int nz = 0;
00624
00625 bandaddr[idx] = w * 16 + g;
00626 qmin = INT_MAX;
00627 qmax = 0.0f;
00628 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00629 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
00630 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00631 sce->zeroes[(w+w2)*16+g] = 1;
00632 continue;
00633 }
00634 sce->zeroes[(w+w2)*16+g] = 0;
00635 nz = 1;
00636 for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
00637 float t = fabsf(coefs[w2*128+i]);
00638 if (t > 0.0f)
00639 qmin = FFMIN(qmin, t);
00640 qmax = FFMAX(qmax, t);
00641 }
00642 }
00643 if (nz) {
00644 int minscale, maxscale;
00645 float minrd = INFINITY;
00646 float maxval;
00647
00648 minscale = coef2minsf(qmin);
00649
00650 maxscale = coef2maxsf(qmax);
00651 minscale = av_clip(minscale - q0, 0, TRELLIS_STATES - 1);
00652 maxscale = av_clip(maxscale - q0, 0, TRELLIS_STATES);
00653 maxval = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], s->scoefs+start);
00654 for (q = minscale; q < maxscale; q++) {
00655 float dist = 0;
00656 int cb = find_min_book(maxval, sce->sf_idx[w*16+g]);
00657 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00658 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
00659 dist += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
00660 q + q0, cb, lambda / band->threshold, INFINITY, NULL);
00661 }
00662 minrd = FFMIN(minrd, dist);
00663
00664 for (i = 0; i < q1 - q0; i++) {
00665 float cost;
00666 cost = paths[idx - 1][i].cost + dist
00667 + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
00668 if (cost < paths[idx][q].cost) {
00669 paths[idx][q].cost = cost;
00670 paths[idx][q].prev = i;
00671 }
00672 }
00673 }
00674 } else {
00675 for (q = 0; q < q1 - q0; q++) {
00676 paths[idx][q].cost = paths[idx - 1][q].cost + 1;
00677 paths[idx][q].prev = q;
00678 }
00679 }
00680 sce->zeroes[w*16+g] = !nz;
00681 start += sce->ics.swb_sizes[g];
00682 idx++;
00683 }
00684 }
00685 idx--;
00686 mincost = paths[idx][0].cost;
00687 minq = 0;
00688 for (i = 1; i < TRELLIS_STATES; i++) {
00689 if (paths[idx][i].cost < mincost) {
00690 mincost = paths[idx][i].cost;
00691 minq = i;
00692 }
00693 }
00694 while (idx) {
00695 sce->sf_idx[bandaddr[idx]] = minq + q0;
00696 minq = paths[idx][minq].prev;
00697 idx--;
00698 }
00699
00700 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
00701 for (g = 0; g < sce->ics.num_swb; g++)
00702 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
00703 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
00704 }
00705
00709 static void search_for_quantizers_twoloop(AVCodecContext *avctx,
00710 AACEncContext *s,
00711 SingleChannelElement *sce,
00712 const float lambda)
00713 {
00714 int start = 0, i, w, w2, g;
00715 int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
00716 float dists[128], uplims[128];
00717 float maxvals[128];
00718 int fflag, minscaler;
00719 int its = 0;
00720 int allz = 0;
00721 float minthr = INFINITY;
00722
00723
00724 memset(dists, 0, sizeof(dists));
00725
00726 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00727 for (g = 0; g < sce->ics.num_swb; g++) {
00728 int nz = 0;
00729 float uplim = 0.0f;
00730 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00731 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
00732 uplim += band->threshold;
00733 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00734 sce->zeroes[(w+w2)*16+g] = 1;
00735 continue;
00736 }
00737 nz = 1;
00738 }
00739 uplims[w*16+g] = uplim *512;
00740 sce->zeroes[w*16+g] = !nz;
00741 if (nz)
00742 minthr = FFMIN(minthr, uplim);
00743 allz |= nz;
00744 }
00745 }
00746 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00747 for (g = 0; g < sce->ics.num_swb; g++) {
00748 if (sce->zeroes[w*16+g]) {
00749 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
00750 continue;
00751 }
00752 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
00753 }
00754 }
00755
00756 if (!allz)
00757 return;
00758 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00759
00760 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00761 start = w*128;
00762 for (g = 0; g < sce->ics.num_swb; g++) {
00763 const float *scaled = s->scoefs + start;
00764 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
00765 start += sce->ics.swb_sizes[g];
00766 }
00767 }
00768
00769
00770
00771 do {
00772 int tbits, qstep;
00773 minscaler = sce->sf_idx[0];
00774
00775 qstep = its ? 1 : 32;
00776 do {
00777 int prev = -1;
00778 tbits = 0;
00779 fflag = 0;
00780 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00781 start = w*128;
00782 for (g = 0; g < sce->ics.num_swb; g++) {
00783 const float *coefs = sce->coeffs + start;
00784 const float *scaled = s->scoefs + start;
00785 int bits = 0;
00786 int cb;
00787 float dist = 0.0f;
00788
00789 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
00790 start += sce->ics.swb_sizes[g];
00791 continue;
00792 }
00793 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
00794 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
00795 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00796 int b;
00797 dist += quantize_band_cost(s, coefs + w2*128,
00798 scaled + w2*128,
00799 sce->ics.swb_sizes[g],
00800 sce->sf_idx[w*16+g],
00801 cb,
00802 1.0f,
00803 INFINITY,
00804 &b);
00805 bits += b;
00806 }
00807 dists[w*16+g] = dist - bits;
00808 if (prev != -1) {
00809 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
00810 }
00811 tbits += bits;
00812 start += sce->ics.swb_sizes[g];
00813 prev = sce->sf_idx[w*16+g];
00814 }
00815 }
00816 if (tbits > destbits) {
00817 for (i = 0; i < 128; i++)
00818 if (sce->sf_idx[i] < 218 - qstep)
00819 sce->sf_idx[i] += qstep;
00820 } else {
00821 for (i = 0; i < 128; i++)
00822 if (sce->sf_idx[i] > 60 - qstep)
00823 sce->sf_idx[i] -= qstep;
00824 }
00825 qstep >>= 1;
00826 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
00827 qstep = 1;
00828 } while (qstep);
00829
00830 fflag = 0;
00831 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
00832 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00833 for (g = 0; g < sce->ics.num_swb; g++) {
00834 int prevsc = sce->sf_idx[w*16+g];
00835 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
00836 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
00837 sce->sf_idx[w*16+g]--;
00838 else
00839 sce->sf_idx[w*16+g]-=2;
00840 }
00841 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
00842 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
00843 if (sce->sf_idx[w*16+g] != prevsc)
00844 fflag = 1;
00845 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
00846 }
00847 }
00848 its++;
00849 } while (fflag && its < 10);
00850 }
00851
00852 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
00853 SingleChannelElement *sce,
00854 const float lambda)
00855 {
00856 int start = 0, i, w, w2, g;
00857 float uplim[128], maxq[128];
00858 int minq, maxsf;
00859 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
00860 int last = 0, lastband = 0, curband = 0;
00861 float avg_energy = 0.0;
00862 if (sce->ics.num_windows == 1) {
00863 start = 0;
00864 for (i = 0; i < 1024; i++) {
00865 if (i - start >= sce->ics.swb_sizes[curband]) {
00866 start += sce->ics.swb_sizes[curband];
00867 curband++;
00868 }
00869 if (sce->coeffs[i]) {
00870 avg_energy += sce->coeffs[i] * sce->coeffs[i];
00871 last = i;
00872 lastband = curband;
00873 }
00874 }
00875 } else {
00876 for (w = 0; w < 8; w++) {
00877 const float *coeffs = sce->coeffs + w*128;
00878 start = 0;
00879 for (i = 0; i < 128; i++) {
00880 if (i - start >= sce->ics.swb_sizes[curband]) {
00881 start += sce->ics.swb_sizes[curband];
00882 curband++;
00883 }
00884 if (coeffs[i]) {
00885 avg_energy += coeffs[i] * coeffs[i];
00886 last = FFMAX(last, i);
00887 lastband = FFMAX(lastband, curband);
00888 }
00889 }
00890 }
00891 }
00892 last++;
00893 avg_energy /= last;
00894 if (avg_energy == 0.0f) {
00895 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
00896 sce->sf_idx[i] = SCALE_ONE_POS;
00897 return;
00898 }
00899 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00900 start = w*128;
00901 for (g = 0; g < sce->ics.num_swb; g++) {
00902 float *coefs = sce->coeffs + start;
00903 const int size = sce->ics.swb_sizes[g];
00904 int start2 = start, end2 = start + size, peakpos = start;
00905 float maxval = -1, thr = 0.0f, t;
00906 maxq[w*16+g] = 0.0f;
00907 if (g > lastband) {
00908 maxq[w*16+g] = 0.0f;
00909 start += size;
00910 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
00911 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
00912 continue;
00913 }
00914 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00915 for (i = 0; i < size; i++) {
00916 float t = coefs[w2*128+i]*coefs[w2*128+i];
00917 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
00918 thr += t;
00919 if (sce->ics.num_windows == 1 && maxval < t) {
00920 maxval = t;
00921 peakpos = start+i;
00922 }
00923 }
00924 }
00925 if (sce->ics.num_windows == 1) {
00926 start2 = FFMAX(peakpos - 2, start2);
00927 end2 = FFMIN(peakpos + 3, end2);
00928 } else {
00929 start2 -= start;
00930 end2 -= start;
00931 }
00932 start += size;
00933 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
00934 t = 1.0 - (1.0 * start2 / last);
00935 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
00936 }
00937 }
00938 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00939 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00940 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00941 start = w*128;
00942 for (g = 0; g < sce->ics.num_swb; g++) {
00943 const float *coefs = sce->coeffs + start;
00944 const float *scaled = s->scoefs + start;
00945 const int size = sce->ics.swb_sizes[g];
00946 int scf, prev_scf, step;
00947 int min_scf = -1, max_scf = 256;
00948 float curdiff;
00949 if (maxq[w*16+g] < 21.544) {
00950 sce->zeroes[w*16+g] = 1;
00951 start += size;
00952 continue;
00953 }
00954 sce->zeroes[w*16+g] = 0;
00955 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
00956 step = 16;
00957 for (;;) {
00958 float dist = 0.0f;
00959 int quant_max;
00960
00961 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00962 int b;
00963 dist += quantize_band_cost(s, coefs + w2*128,
00964 scaled + w2*128,
00965 sce->ics.swb_sizes[g],
00966 scf,
00967 ESC_BT,
00968 lambda,
00969 INFINITY,
00970 &b);
00971 dist -= b;
00972 }
00973 dist *= 1.0f / 512.0f / lambda;
00974 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
00975 if (quant_max >= 8191) {
00976 sce->sf_idx[w*16+g] = prev_scf;
00977 break;
00978 }
00979 prev_scf = scf;
00980 curdiff = fabsf(dist - uplim[w*16+g]);
00981 if (curdiff <= 1.0f)
00982 step = 0;
00983 else
00984 step = log2f(curdiff);
00985 if (dist > uplim[w*16+g])
00986 step = -step;
00987 scf += step;
00988 scf = av_clip_uint8(scf);
00989 step = scf - prev_scf;
00990 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
00991 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
00992 break;
00993 }
00994 if (step > 0)
00995 min_scf = prev_scf;
00996 else
00997 max_scf = prev_scf;
00998 }
00999 start += size;
01000 }
01001 }
01002 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
01003 for (i = 1; i < 128; i++) {
01004 if (!sce->sf_idx[i])
01005 sce->sf_idx[i] = sce->sf_idx[i-1];
01006 else
01007 minq = FFMIN(minq, sce->sf_idx[i]);
01008 }
01009 if (minq == INT_MAX)
01010 minq = 0;
01011 minq = FFMIN(minq, SCALE_MAX_POS);
01012 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
01013 for (i = 126; i >= 0; i--) {
01014 if (!sce->sf_idx[i])
01015 sce->sf_idx[i] = sce->sf_idx[i+1];
01016 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
01017 }
01018 }
01019
01020 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
01021 SingleChannelElement *sce,
01022 const float lambda)
01023 {
01024 int i, w, w2, g;
01025 int minq = 255;
01026
01027 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
01028 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
01029 for (g = 0; g < sce->ics.num_swb; g++) {
01030 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
01031 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
01032 if (band->energy <= band->threshold) {
01033 sce->sf_idx[(w+w2)*16+g] = 218;
01034 sce->zeroes[(w+w2)*16+g] = 1;
01035 } else {
01036 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
01037 sce->zeroes[(w+w2)*16+g] = 0;
01038 }
01039 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
01040 }
01041 }
01042 }
01043 for (i = 0; i < 128; i++) {
01044 sce->sf_idx[i] = 140;
01045
01046 }
01047
01048 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
01049 for (g = 0; g < sce->ics.num_swb; g++)
01050 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
01051 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
01052 }
01053
01054 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
01055 const float lambda)
01056 {
01057 int start = 0, i, w, w2, g;
01058 float M[128], S[128];
01059 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
01060 SingleChannelElement *sce0 = &cpe->ch[0];
01061 SingleChannelElement *sce1 = &cpe->ch[1];
01062 if (!cpe->common_window)
01063 return;
01064 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
01065 for (g = 0; g < sce0->ics.num_swb; g++) {
01066 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
01067 float dist1 = 0.0f, dist2 = 0.0f;
01068 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
01069 FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
01070 FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
01071 float minthr = FFMIN(band0->threshold, band1->threshold);
01072 float maxthr = FFMAX(band0->threshold, band1->threshold);
01073 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
01074 M[i] = (sce0->coeffs[start+w2*128+i]
01075 + sce1->coeffs[start+w2*128+i]) * 0.5;
01076 S[i] = M[i]
01077 - sce1->coeffs[start+w2*128+i];
01078 }
01079 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
01080 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
01081 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
01082 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
01083 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
01084 L34,
01085 sce0->ics.swb_sizes[g],
01086 sce0->sf_idx[(w+w2)*16+g],
01087 sce0->band_type[(w+w2)*16+g],
01088 lambda / band0->threshold, INFINITY, NULL);
01089 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
01090 R34,
01091 sce1->ics.swb_sizes[g],
01092 sce1->sf_idx[(w+w2)*16+g],
01093 sce1->band_type[(w+w2)*16+g],
01094 lambda / band1->threshold, INFINITY, NULL);
01095 dist2 += quantize_band_cost(s, M,
01096 M34,
01097 sce0->ics.swb_sizes[g],
01098 sce0->sf_idx[(w+w2)*16+g],
01099 sce0->band_type[(w+w2)*16+g],
01100 lambda / maxthr, INFINITY, NULL);
01101 dist2 += quantize_band_cost(s, S,
01102 S34,
01103 sce1->ics.swb_sizes[g],
01104 sce1->sf_idx[(w+w2)*16+g],
01105 sce1->band_type[(w+w2)*16+g],
01106 lambda / minthr, INFINITY, NULL);
01107 }
01108 cpe->ms_mask[w*16+g] = dist2 < dist1;
01109 }
01110 start += sce0->ics.swb_sizes[g];
01111 }
01112 }
01113 }
01114
01115 AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
01116 {
01117 search_for_quantizers_faac,
01118 encode_window_bands_info,
01119 quantize_and_encode_band,
01120 search_for_ms,
01121 },
01122 {
01123 search_for_quantizers_anmr,
01124 encode_window_bands_info,
01125 quantize_and_encode_band,
01126 search_for_ms,
01127 },
01128 {
01129 search_for_quantizers_twoloop,
01130 codebook_trellis_rate,
01131 quantize_and_encode_band,
01132 search_for_ms,
01133 },
01134 {
01135 search_for_quantizers_fast,
01136 encode_window_bands_info,
01137 quantize_and_encode_band,
01138 search_for_ms,
01139 },
01140 };