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], 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 memset(dists, 0, sizeof(dists));
00726
00727 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00728 for (g = 0; g < sce->ics.num_swb; g++) {
00729 int nz = 0;
00730 float uplim = 0.0f;
00731 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00732 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
00733 uplim += band->threshold;
00734 if (band->energy <= band->threshold || band->threshold == 0.0f) {
00735 sce->zeroes[(w+w2)*16+g] = 1;
00736 continue;
00737 }
00738 nz = 1;
00739 }
00740 uplims[w*16+g] = uplim *512;
00741 sce->zeroes[w*16+g] = !nz;
00742 if (nz)
00743 minthr = FFMIN(minthr, uplim);
00744 allz |= nz;
00745 }
00746 }
00747 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00748 for (g = 0; g < sce->ics.num_swb; g++) {
00749 if (sce->zeroes[w*16+g]) {
00750 sce->sf_idx[w*16+g] = SCALE_ONE_POS;
00751 continue;
00752 }
00753 sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
00754 }
00755 }
00756
00757 if (!allz)
00758 return;
00759 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00760
00761 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00762 start = w*128;
00763 for (g = 0; g < sce->ics.num_swb; g++) {
00764 const float *scaled = s->scoefs + start;
00765 maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
00766 start += sce->ics.swb_sizes[g];
00767 }
00768 }
00769
00770
00771
00772 do {
00773 int tbits, qstep;
00774 minscaler = sce->sf_idx[0];
00775
00776 qstep = its ? 1 : 32;
00777 do {
00778 int prev = -1;
00779 tbits = 0;
00780 fflag = 0;
00781 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00782 start = w*128;
00783 for (g = 0; g < sce->ics.num_swb; g++) {
00784 const float *coefs = sce->coeffs + start;
00785 const float *scaled = s->scoefs + start;
00786 int bits = 0;
00787 int cb;
00788 float dist = 0.0f;
00789
00790 if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
00791 start += sce->ics.swb_sizes[g];
00792 continue;
00793 }
00794 minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
00795 cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
00796 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00797 int b;
00798 dist += quantize_band_cost(s, coefs + w2*128,
00799 scaled + w2*128,
00800 sce->ics.swb_sizes[g],
00801 sce->sf_idx[w*16+g],
00802 cb,
00803 1.0f,
00804 INFINITY,
00805 &b);
00806 bits += b;
00807 }
00808 dists[w*16+g] = dist - bits;
00809 if (prev != -1) {
00810 bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
00811 }
00812 tbits += bits;
00813 start += sce->ics.swb_sizes[g];
00814 prev = sce->sf_idx[w*16+g];
00815 }
00816 }
00817 if (tbits > destbits) {
00818 for (i = 0; i < 128; i++)
00819 if (sce->sf_idx[i] < 218 - qstep)
00820 sce->sf_idx[i] += qstep;
00821 } else {
00822 for (i = 0; i < 128; i++)
00823 if (sce->sf_idx[i] > 60 - qstep)
00824 sce->sf_idx[i] -= qstep;
00825 }
00826 qstep >>= 1;
00827 if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
00828 qstep = 1;
00829 } while (qstep);
00830
00831 fflag = 0;
00832 minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
00833 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00834 for (g = 0; g < sce->ics.num_swb; g++) {
00835 int prevsc = sce->sf_idx[w*16+g];
00836 if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
00837 if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
00838 sce->sf_idx[w*16+g]--;
00839 else
00840 sce->sf_idx[w*16+g]-=2;
00841 }
00842 sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
00843 sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
00844 if (sce->sf_idx[w*16+g] != prevsc)
00845 fflag = 1;
00846 sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
00847 }
00848 }
00849 its++;
00850 } while (fflag && its < 10);
00851 }
00852
00853 static void search_for_quantizers_faac(AVCodecContext *avctx, AACEncContext *s,
00854 SingleChannelElement *sce,
00855 const float lambda)
00856 {
00857 int start = 0, i, w, w2, g;
00858 float uplim[128], maxq[128];
00859 int minq, maxsf;
00860 float distfact = ((sce->ics.num_windows > 1) ? 85.80 : 147.84) / lambda;
00861 int last = 0, lastband = 0, curband = 0;
00862 float avg_energy = 0.0;
00863 if (sce->ics.num_windows == 1) {
00864 start = 0;
00865 for (i = 0; i < 1024; i++) {
00866 if (i - start >= sce->ics.swb_sizes[curband]) {
00867 start += sce->ics.swb_sizes[curband];
00868 curband++;
00869 }
00870 if (sce->coeffs[i]) {
00871 avg_energy += sce->coeffs[i] * sce->coeffs[i];
00872 last = i;
00873 lastband = curband;
00874 }
00875 }
00876 } else {
00877 for (w = 0; w < 8; w++) {
00878 const float *coeffs = sce->coeffs + w*128;
00879 start = 0;
00880 for (i = 0; i < 128; i++) {
00881 if (i - start >= sce->ics.swb_sizes[curband]) {
00882 start += sce->ics.swb_sizes[curband];
00883 curband++;
00884 }
00885 if (coeffs[i]) {
00886 avg_energy += coeffs[i] * coeffs[i];
00887 last = FFMAX(last, i);
00888 lastband = FFMAX(lastband, curband);
00889 }
00890 }
00891 }
00892 }
00893 last++;
00894 avg_energy /= last;
00895 if (avg_energy == 0.0f) {
00896 for (i = 0; i < FF_ARRAY_ELEMS(sce->sf_idx); i++)
00897 sce->sf_idx[i] = SCALE_ONE_POS;
00898 return;
00899 }
00900 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00901 start = w*128;
00902 for (g = 0; g < sce->ics.num_swb; g++) {
00903 float *coefs = sce->coeffs + start;
00904 const int size = sce->ics.swb_sizes[g];
00905 int start2 = start, end2 = start + size, peakpos = start;
00906 float maxval = -1, thr = 0.0f, t;
00907 maxq[w*16+g] = 0.0f;
00908 if (g > lastband) {
00909 maxq[w*16+g] = 0.0f;
00910 start += size;
00911 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
00912 memset(coefs + w2*128, 0, sizeof(coefs[0])*size);
00913 continue;
00914 }
00915 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00916 for (i = 0; i < size; i++) {
00917 float t = coefs[w2*128+i]*coefs[w2*128+i];
00918 maxq[w*16+g] = FFMAX(maxq[w*16+g], fabsf(coefs[w2*128 + i]));
00919 thr += t;
00920 if (sce->ics.num_windows == 1 && maxval < t) {
00921 maxval = t;
00922 peakpos = start+i;
00923 }
00924 }
00925 }
00926 if (sce->ics.num_windows == 1) {
00927 start2 = FFMAX(peakpos - 2, start2);
00928 end2 = FFMIN(peakpos + 3, end2);
00929 } else {
00930 start2 -= start;
00931 end2 -= start;
00932 }
00933 start += size;
00934 thr = pow(thr / (avg_energy * (end2 - start2)), 0.3 + 0.1*(lastband - g) / lastband);
00935 t = 1.0 - (1.0 * start2 / last);
00936 uplim[w*16+g] = distfact / (1.4 * thr + t*t*t + 0.075);
00937 }
00938 }
00939 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
00940 abs_pow34_v(s->scoefs, sce->coeffs, 1024);
00941 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
00942 start = w*128;
00943 for (g = 0; g < sce->ics.num_swb; g++) {
00944 const float *coefs = sce->coeffs + start;
00945 const float *scaled = s->scoefs + start;
00946 const int size = sce->ics.swb_sizes[g];
00947 int scf, prev_scf, step;
00948 int min_scf = -1, max_scf = 256;
00949 float curdiff;
00950 if (maxq[w*16+g] < 21.544) {
00951 sce->zeroes[w*16+g] = 1;
00952 start += size;
00953 continue;
00954 }
00955 sce->zeroes[w*16+g] = 0;
00956 scf = prev_scf = av_clip(SCALE_ONE_POS - SCALE_DIV_512 - log2f(1/maxq[w*16+g])*16/3, 60, 218);
00957 step = 16;
00958 for (;;) {
00959 float dist = 0.0f;
00960 int quant_max;
00961
00962 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
00963 int b;
00964 dist += quantize_band_cost(s, coefs + w2*128,
00965 scaled + w2*128,
00966 sce->ics.swb_sizes[g],
00967 scf,
00968 ESC_BT,
00969 lambda,
00970 INFINITY,
00971 &b);
00972 dist -= b;
00973 }
00974 dist *= 1.0f / 512.0f / lambda;
00975 quant_max = quant(maxq[w*16+g], ff_aac_pow2sf_tab[POW_SF2_ZERO - scf + SCALE_ONE_POS - SCALE_DIV_512]);
00976 if (quant_max >= 8191) {
00977 sce->sf_idx[w*16+g] = prev_scf;
00978 break;
00979 }
00980 prev_scf = scf;
00981 curdiff = fabsf(dist - uplim[w*16+g]);
00982 if (curdiff <= 1.0f)
00983 step = 0;
00984 else
00985 step = log2f(curdiff);
00986 if (dist > uplim[w*16+g])
00987 step = -step;
00988 scf += step;
00989 scf = av_clip_uint8(scf);
00990 step = scf - prev_scf;
00991 if (FFABS(step) <= 1 || (step > 0 && scf >= max_scf) || (step < 0 && scf <= min_scf)) {
00992 sce->sf_idx[w*16+g] = av_clip(scf, min_scf, max_scf);
00993 break;
00994 }
00995 if (step > 0)
00996 min_scf = prev_scf;
00997 else
00998 max_scf = prev_scf;
00999 }
01000 start += size;
01001 }
01002 }
01003 minq = sce->sf_idx[0] ? sce->sf_idx[0] : INT_MAX;
01004 for (i = 1; i < 128; i++) {
01005 if (!sce->sf_idx[i])
01006 sce->sf_idx[i] = sce->sf_idx[i-1];
01007 else
01008 minq = FFMIN(minq, sce->sf_idx[i]);
01009 }
01010 if (minq == INT_MAX)
01011 minq = 0;
01012 minq = FFMIN(minq, SCALE_MAX_POS);
01013 maxsf = FFMIN(minq + SCALE_MAX_DIFF, SCALE_MAX_POS);
01014 for (i = 126; i >= 0; i--) {
01015 if (!sce->sf_idx[i])
01016 sce->sf_idx[i] = sce->sf_idx[i+1];
01017 sce->sf_idx[i] = av_clip(sce->sf_idx[i], minq, maxsf);
01018 }
01019 }
01020
01021 static void search_for_quantizers_fast(AVCodecContext *avctx, AACEncContext *s,
01022 SingleChannelElement *sce,
01023 const float lambda)
01024 {
01025 int i, w, w2, g;
01026 int minq = 255;
01027
01028 memset(sce->sf_idx, 0, sizeof(sce->sf_idx));
01029 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
01030 for (g = 0; g < sce->ics.num_swb; g++) {
01031 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
01032 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
01033 if (band->energy <= band->threshold) {
01034 sce->sf_idx[(w+w2)*16+g] = 218;
01035 sce->zeroes[(w+w2)*16+g] = 1;
01036 } else {
01037 sce->sf_idx[(w+w2)*16+g] = av_clip(SCALE_ONE_POS - SCALE_DIV_512 + log2f(band->threshold), 80, 218);
01038 sce->zeroes[(w+w2)*16+g] = 0;
01039 }
01040 minq = FFMIN(minq, sce->sf_idx[(w+w2)*16+g]);
01041 }
01042 }
01043 }
01044 for (i = 0; i < 128; i++) {
01045 sce->sf_idx[i] = 140;
01046
01047 }
01048
01049 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
01050 for (g = 0; g < sce->ics.num_swb; g++)
01051 for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
01052 sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
01053 }
01054
01055 static void search_for_ms(AACEncContext *s, ChannelElement *cpe,
01056 const float lambda)
01057 {
01058 int start = 0, i, w, w2, g;
01059 float M[128], S[128];
01060 float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
01061 SingleChannelElement *sce0 = &cpe->ch[0];
01062 SingleChannelElement *sce1 = &cpe->ch[1];
01063 if (!cpe->common_window)
01064 return;
01065 for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
01066 for (g = 0; g < sce0->ics.num_swb; g++) {
01067 if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
01068 float dist1 = 0.0f, dist2 = 0.0f;
01069 for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
01070 FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
01071 FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
01072 float minthr = FFMIN(band0->threshold, band1->threshold);
01073 float maxthr = FFMAX(band0->threshold, band1->threshold);
01074 for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
01075 M[i] = (sce0->coeffs[start+w2*128+i]
01076 + sce1->coeffs[start+w2*128+i]) * 0.5;
01077 S[i] = M[i]
01078 - sce1->coeffs[start+w2*128+i];
01079 }
01080 abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
01081 abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
01082 abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
01083 abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
01084 dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
01085 L34,
01086 sce0->ics.swb_sizes[g],
01087 sce0->sf_idx[(w+w2)*16+g],
01088 sce0->band_type[(w+w2)*16+g],
01089 lambda / band0->threshold, INFINITY, NULL);
01090 dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
01091 R34,
01092 sce1->ics.swb_sizes[g],
01093 sce1->sf_idx[(w+w2)*16+g],
01094 sce1->band_type[(w+w2)*16+g],
01095 lambda / band1->threshold, INFINITY, NULL);
01096 dist2 += quantize_band_cost(s, M,
01097 M34,
01098 sce0->ics.swb_sizes[g],
01099 sce0->sf_idx[(w+w2)*16+g],
01100 sce0->band_type[(w+w2)*16+g],
01101 lambda / maxthr, INFINITY, NULL);
01102 dist2 += quantize_band_cost(s, S,
01103 S34,
01104 sce1->ics.swb_sizes[g],
01105 sce1->sf_idx[(w+w2)*16+g],
01106 sce1->band_type[(w+w2)*16+g],
01107 lambda / minthr, INFINITY, NULL);
01108 }
01109 cpe->ms_mask[w*16+g] = dist2 < dist1;
01110 }
01111 start += sce0->ics.swb_sizes[g];
01112 }
01113 }
01114 }
01115
01116 AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
01117 {
01118 search_for_quantizers_faac,
01119 encode_window_bands_info,
01120 quantize_and_encode_band,
01121 search_for_ms,
01122 },
01123 {
01124 search_for_quantizers_anmr,
01125 encode_window_bands_info,
01126 quantize_and_encode_band,
01127 search_for_ms,
01128 },
01129 {
01130 search_for_quantizers_twoloop,
01131 codebook_trellis_rate,
01132 quantize_and_encode_band,
01133 search_for_ms,
01134 },
01135 {
01136 search_for_quantizers_fast,
01137 encode_window_bands_info,
01138 quantize_and_encode_band,
01139 search_for_ms,
01140 },
01141 };