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