00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/libm.h"
00028
00029 #include "avcodec.h"
00030 #include "aactab.h"
00031 #include "psymodel.h"
00032
00033
00034
00035
00036
00037
00038
00043 #define PSY_3GPP_THR_SPREAD_HI 1.5f // spreading factor for low-to-hi threshold spreading (15 dB/Bark)
00044 #define PSY_3GPP_THR_SPREAD_LOW 3.0f // spreading factor for hi-to-low threshold spreading (30 dB/Bark)
00045
00046 #define PSY_3GPP_EN_SPREAD_HI_L1 2.0f
00047
00048 #define PSY_3GPP_EN_SPREAD_HI_L2 1.5f
00049
00050 #define PSY_3GPP_EN_SPREAD_HI_S 1.5f
00051
00052 #define PSY_3GPP_EN_SPREAD_LOW_L 3.0f
00053
00054 #define PSY_3GPP_EN_SPREAD_LOW_S 2.0f
00055
00056 #define PSY_3GPP_RPEMIN 0.01f
00057 #define PSY_3GPP_RPELEV 2.0f
00058
00059 #define PSY_3GPP_C1 3.0f
00060 #define PSY_3GPP_C2 1.3219281f
00061 #define PSY_3GPP_C3 0.55935729f
00062
00063 #define PSY_SNR_1DB 7.9432821e-1f
00064 #define PSY_SNR_25DB 3.1622776e-3f
00065
00066 #define PSY_3GPP_SAVE_SLOPE_L -0.46666667f
00067 #define PSY_3GPP_SAVE_SLOPE_S -0.36363637f
00068 #define PSY_3GPP_SAVE_ADD_L -0.84285712f
00069 #define PSY_3GPP_SAVE_ADD_S -0.75f
00070 #define PSY_3GPP_SPEND_SLOPE_L 0.66666669f
00071 #define PSY_3GPP_SPEND_SLOPE_S 0.81818181f
00072 #define PSY_3GPP_SPEND_ADD_L -0.35f
00073 #define PSY_3GPP_SPEND_ADD_S -0.26111111f
00074 #define PSY_3GPP_CLIP_LO_L 0.2f
00075 #define PSY_3GPP_CLIP_LO_S 0.2f
00076 #define PSY_3GPP_CLIP_HI_L 0.95f
00077 #define PSY_3GPP_CLIP_HI_S 0.75f
00078
00079 #define PSY_3GPP_AH_THR_LONG 0.5f
00080 #define PSY_3GPP_AH_THR_SHORT 0.63f
00081
00082 enum {
00083 PSY_3GPP_AH_NONE,
00084 PSY_3GPP_AH_INACTIVE,
00085 PSY_3GPP_AH_ACTIVE
00086 };
00087
00088 #define PSY_3GPP_BITS_TO_PE(bits) ((bits) * 1.18f)
00089
00090
00091 #define PSY_LAME_FIR_LEN 21
00092 #define AAC_BLOCK_SIZE_LONG 1024
00093 #define AAC_BLOCK_SIZE_SHORT 128
00094 #define AAC_NUM_BLOCKS_SHORT 8
00095 #define PSY_LAME_NUM_SUBBLOCKS 3
00096
00097
00104 typedef struct AacPsyBand{
00105 float energy;
00106 float thr;
00107 float thr_quiet;
00108 float nz_lines;
00109 float active_lines;
00110 float pe;
00111 float pe_const;
00112 float norm_fac;
00113 int avoid_holes;
00114 }AacPsyBand;
00115
00119 typedef struct AacPsyChannel{
00120 AacPsyBand band[128];
00121 AacPsyBand prev_band[128];
00122
00123 float win_energy;
00124 float iir_state[2];
00125 uint8_t next_grouping;
00126 enum WindowSequence next_window_seq;
00127
00128 float attack_threshold;
00129 float prev_energy_subshort[AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
00130 int prev_attack;
00131 }AacPsyChannel;
00132
00136 typedef struct AacPsyCoeffs{
00137 float ath;
00138 float barks;
00139 float spread_low[2];
00140 float spread_hi [2];
00141 float min_snr;
00142 }AacPsyCoeffs;
00143
00147 typedef struct AacPsyContext{
00148 int chan_bitrate;
00149 int frame_bits;
00150 int fill_level;
00151 struct {
00152 float min;
00153 float max;
00154 float previous;
00155 float correction;
00156 } pe;
00157 AacPsyCoeffs psy_coef[2][64];
00158 AacPsyChannel *ch;
00159 }AacPsyContext;
00160
00164 typedef struct {
00165 int quality;
00166
00167
00168
00169 float st_lrm;
00170 } PsyLamePreset;
00171
00175 static const PsyLamePreset psy_abr_map[] = {
00176
00177
00178 { 8, 6.60},
00179 { 16, 6.60},
00180 { 24, 6.60},
00181 { 32, 6.60},
00182 { 40, 6.60},
00183 { 48, 6.60},
00184 { 56, 6.60},
00185 { 64, 6.40},
00186 { 80, 6.00},
00187 { 96, 5.60},
00188 {112, 5.20},
00189 {128, 5.20},
00190 {160, 5.20}
00191 };
00192
00196 static const PsyLamePreset psy_vbr_map[] = {
00197
00198 { 0, 4.20},
00199 { 1, 4.20},
00200 { 2, 4.20},
00201 { 3, 4.20},
00202 { 4, 4.20},
00203 { 5, 4.20},
00204 { 6, 4.20},
00205 { 7, 4.20},
00206 { 8, 4.20},
00207 { 9, 4.20},
00208 {10, 4.20}
00209 };
00210
00214 static const float psy_fir_coeffs[] = {
00215 -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
00216 -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
00217 -5.52212e-17 * 2, -0.313819 * 2
00218 };
00219
00223 static float lame_calc_attack_threshold(int bitrate)
00224 {
00225
00226 int lower_range = 12, upper_range = 12;
00227 int lower_range_kbps = psy_abr_map[12].quality;
00228 int upper_range_kbps = psy_abr_map[12].quality;
00229 int i;
00230
00231
00232
00233
00234 for (i = 1; i < 13; i++) {
00235 if (FFMAX(bitrate, psy_abr_map[i].quality) != bitrate) {
00236 upper_range = i;
00237 upper_range_kbps = psy_abr_map[i ].quality;
00238 lower_range = i - 1;
00239 lower_range_kbps = psy_abr_map[i - 1].quality;
00240 break;
00241 }
00242 }
00243
00244
00245 if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps))
00246 return psy_abr_map[lower_range].st_lrm;
00247 return psy_abr_map[upper_range].st_lrm;
00248 }
00249
00253 static void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx) {
00254 int i, j;
00255
00256 for (i = 0; i < avctx->channels; i++) {
00257 AacPsyChannel *pch = &ctx->ch[i];
00258
00259 if (avctx->flags & CODEC_FLAG_QSCALE)
00260 pch->attack_threshold = psy_vbr_map[avctx->global_quality / FF_QP2LAMBDA].st_lrm;
00261 else
00262 pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->channels / 1000);
00263
00264 for (j = 0; j < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; j++)
00265 pch->prev_energy_subshort[j] = 10.0f;
00266 }
00267 }
00268
00272 static av_cold float calc_bark(float f)
00273 {
00274 return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
00275 }
00276
00277 #define ATH_ADD 4
00278
00282 static av_cold float ath(float f, float add)
00283 {
00284 f /= 1000.0f;
00285 return 3.64 * pow(f, -0.8)
00286 - 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
00287 + 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
00288 + (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
00289 }
00290
00291 static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
00292 AacPsyContext *pctx;
00293 float bark;
00294 int i, j, g, start;
00295 float prev, minscale, minath, minsnr, pe_min;
00296 const int chan_bitrate = ctx->avctx->bit_rate / ctx->avctx->channels;
00297 const int bandwidth = ctx->avctx->cutoff ? ctx->avctx->cutoff : AAC_CUTOFF(ctx->avctx);
00298 const float num_bark = calc_bark((float)bandwidth);
00299
00300 ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
00301 pctx = (AacPsyContext*) ctx->model_priv_data;
00302
00303 pctx->chan_bitrate = chan_bitrate;
00304 pctx->frame_bits = chan_bitrate * AAC_BLOCK_SIZE_LONG / ctx->avctx->sample_rate;
00305 pctx->pe.min = 8.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
00306 pctx->pe.max = 12.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
00307 ctx->bitres.size = 6144 - pctx->frame_bits;
00308 ctx->bitres.size -= ctx->bitres.size % 8;
00309 pctx->fill_level = ctx->bitres.size;
00310 minath = ath(3410, ATH_ADD);
00311 for (j = 0; j < 2; j++) {
00312 AacPsyCoeffs *coeffs = pctx->psy_coef[j];
00313 const uint8_t *band_sizes = ctx->bands[j];
00314 float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
00315 float avg_chan_bits = chan_bitrate / ctx->avctx->sample_rate * (j ? 128.0f : 1024.0f);
00316
00317 float bark_pe = 0.024f * PSY_3GPP_BITS_TO_PE(avg_chan_bits) / num_bark;
00318 float en_spread_low = j ? PSY_3GPP_EN_SPREAD_LOW_S : PSY_3GPP_EN_SPREAD_LOW_L;
00319
00320 float en_spread_hi = (j || (chan_bitrate <= 22.0f)) ? PSY_3GPP_EN_SPREAD_HI_S : PSY_3GPP_EN_SPREAD_HI_L1;
00321
00322 i = 0;
00323 prev = 0.0;
00324 for (g = 0; g < ctx->num_bands[j]; g++) {
00325 i += band_sizes[g];
00326 bark = calc_bark((i-1) * line_to_frequency);
00327 coeffs[g].barks = (bark + prev) / 2.0;
00328 prev = bark;
00329 }
00330 for (g = 0; g < ctx->num_bands[j] - 1; g++) {
00331 AacPsyCoeffs *coeff = &coeffs[g];
00332 float bark_width = coeffs[g+1].barks - coeffs->barks;
00333 coeff->spread_low[0] = pow(10.0, -bark_width * PSY_3GPP_THR_SPREAD_LOW);
00334 coeff->spread_hi [0] = pow(10.0, -bark_width * PSY_3GPP_THR_SPREAD_HI);
00335 coeff->spread_low[1] = pow(10.0, -bark_width * en_spread_low);
00336 coeff->spread_hi [1] = pow(10.0, -bark_width * en_spread_hi);
00337 pe_min = bark_pe * bark_width;
00338 minsnr = exp2(pe_min / band_sizes[g]) - 1.5f;
00339 coeff->min_snr = av_clipf(1.0f / minsnr, PSY_SNR_25DB, PSY_SNR_1DB);
00340 }
00341 start = 0;
00342 for (g = 0; g < ctx->num_bands[j]; g++) {
00343 minscale = ath(start * line_to_frequency, ATH_ADD);
00344 for (i = 1; i < band_sizes[g]; i++)
00345 minscale = FFMIN(minscale, ath((start + i) * line_to_frequency, ATH_ADD));
00346 coeffs[g].ath = minscale - minath;
00347 start += band_sizes[g];
00348 }
00349 }
00350
00351 pctx->ch = av_mallocz(sizeof(AacPsyChannel) * ctx->avctx->channels);
00352
00353 lame_window_init(pctx, ctx->avctx);
00354
00355 return 0;
00356 }
00357
00361 static float iir_filter(int in, float state[2])
00362 {
00363 float ret;
00364
00365 ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
00366 state[0] = in;
00367 state[1] = ret;
00368 return ret;
00369 }
00370
00374 static const uint8_t window_grouping[9] = {
00375 0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
00376 };
00377
00382 static av_unused FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
00383 const int16_t *audio,
00384 const int16_t *la,
00385 int channel, int prev_type)
00386 {
00387 int i, j;
00388 int br = ctx->avctx->bit_rate / ctx->avctx->channels;
00389 int attack_ratio = br <= 16000 ? 18 : 10;
00390 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
00391 AacPsyChannel *pch = &pctx->ch[channel];
00392 uint8_t grouping = 0;
00393 int next_type = pch->next_window_seq;
00394 FFPsyWindowInfo wi = { { 0 } };
00395
00396 if (la) {
00397 float s[8], v;
00398 int switch_to_eight = 0;
00399 float sum = 0.0, sum2 = 0.0;
00400 int attack_n = 0;
00401 int stay_short = 0;
00402 for (i = 0; i < 8; i++) {
00403 for (j = 0; j < 128; j++) {
00404 v = iir_filter(la[i*128+j], pch->iir_state);
00405 sum += v*v;
00406 }
00407 s[i] = sum;
00408 sum2 += sum;
00409 }
00410 for (i = 0; i < 8; i++) {
00411 if (s[i] > pch->win_energy * attack_ratio) {
00412 attack_n = i + 1;
00413 switch_to_eight = 1;
00414 break;
00415 }
00416 }
00417 pch->win_energy = pch->win_energy*7/8 + sum2/64;
00418
00419 wi.window_type[1] = prev_type;
00420 switch (prev_type) {
00421 case ONLY_LONG_SEQUENCE:
00422 wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
00423 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
00424 break;
00425 case LONG_START_SEQUENCE:
00426 wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
00427 grouping = pch->next_grouping;
00428 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
00429 break;
00430 case LONG_STOP_SEQUENCE:
00431 wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
00432 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
00433 break;
00434 case EIGHT_SHORT_SEQUENCE:
00435 stay_short = next_type == EIGHT_SHORT_SEQUENCE || switch_to_eight;
00436 wi.window_type[0] = stay_short ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
00437 grouping = next_type == EIGHT_SHORT_SEQUENCE ? pch->next_grouping : 0;
00438 next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
00439 break;
00440 }
00441
00442 pch->next_grouping = window_grouping[attack_n];
00443 pch->next_window_seq = next_type;
00444 } else {
00445 for (i = 0; i < 3; i++)
00446 wi.window_type[i] = prev_type;
00447 grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
00448 }
00449
00450 wi.window_shape = 1;
00451 if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
00452 wi.num_windows = 1;
00453 wi.grouping[0] = 1;
00454 } else {
00455 int lastgrp = 0;
00456 wi.num_windows = 8;
00457 for (i = 0; i < 8; i++) {
00458 if (!((grouping >> i) & 1))
00459 lastgrp = i;
00460 wi.grouping[lastgrp]++;
00461 }
00462 }
00463
00464 return wi;
00465 }
00466
00467
00468 static int calc_bit_demand(AacPsyContext *ctx, float pe, int bits, int size,
00469 int short_window)
00470 {
00471 const float bitsave_slope = short_window ? PSY_3GPP_SAVE_SLOPE_S : PSY_3GPP_SAVE_SLOPE_L;
00472 const float bitsave_add = short_window ? PSY_3GPP_SAVE_ADD_S : PSY_3GPP_SAVE_ADD_L;
00473 const float bitspend_slope = short_window ? PSY_3GPP_SPEND_SLOPE_S : PSY_3GPP_SPEND_SLOPE_L;
00474 const float bitspend_add = short_window ? PSY_3GPP_SPEND_ADD_S : PSY_3GPP_SPEND_ADD_L;
00475 const float clip_low = short_window ? PSY_3GPP_CLIP_LO_S : PSY_3GPP_CLIP_LO_L;
00476 const float clip_high = short_window ? PSY_3GPP_CLIP_HI_S : PSY_3GPP_CLIP_HI_L;
00477 float clipped_pe, bit_save, bit_spend, bit_factor, fill_level;
00478
00479 ctx->fill_level += ctx->frame_bits - bits;
00480 ctx->fill_level = av_clip(ctx->fill_level, 0, size);
00481 fill_level = av_clipf((float)ctx->fill_level / size, clip_low, clip_high);
00482 clipped_pe = av_clipf(pe, ctx->pe.min, ctx->pe.max);
00483 bit_save = (fill_level + bitsave_add) * bitsave_slope;
00484 assert(bit_save <= 0.3f && bit_save >= -0.05000001f);
00485 bit_spend = (fill_level + bitspend_add) * bitspend_slope;
00486 assert(bit_spend <= 0.5f && bit_spend >= -0.1f);
00487
00488
00489
00490
00491
00492
00493 bit_factor = 1.0f - bit_save + ((bit_spend - bit_save) / (ctx->pe.max - ctx->pe.min)) * (clipped_pe - ctx->pe.min);
00494
00495 ctx->pe.max = FFMAX(pe, ctx->pe.max);
00496 ctx->pe.min = FFMIN(pe, ctx->pe.min);
00497
00498 return FFMIN(ctx->frame_bits * bit_factor, ctx->frame_bits + size - bits);
00499 }
00500
00501 static float calc_pe_3gpp(AacPsyBand *band)
00502 {
00503 float pe, a;
00504
00505 band->pe = 0.0f;
00506 band->pe_const = 0.0f;
00507 band->active_lines = 0.0f;
00508 if (band->energy > band->thr) {
00509 a = log2f(band->energy);
00510 pe = a - log2f(band->thr);
00511 band->active_lines = band->nz_lines;
00512 if (pe < PSY_3GPP_C1) {
00513 pe = pe * PSY_3GPP_C3 + PSY_3GPP_C2;
00514 a = a * PSY_3GPP_C3 + PSY_3GPP_C2;
00515 band->active_lines *= PSY_3GPP_C3;
00516 }
00517 band->pe = pe * band->nz_lines;
00518 band->pe_const = a * band->nz_lines;
00519 }
00520
00521 return band->pe;
00522 }
00523
00524 static float calc_reduction_3gpp(float a, float desired_pe, float pe,
00525 float active_lines)
00526 {
00527 float thr_avg, reduction;
00528
00529 if(active_lines == 0.0)
00530 return 0;
00531
00532 thr_avg = exp2f((a - pe) / (4.0f * active_lines));
00533 reduction = exp2f((a - desired_pe) / (4.0f * active_lines)) - thr_avg;
00534
00535 return FFMAX(reduction, 0.0f);
00536 }
00537
00538 static float calc_reduced_thr_3gpp(AacPsyBand *band, float min_snr,
00539 float reduction)
00540 {
00541 float thr = band->thr;
00542
00543 if (band->energy > thr) {
00544 thr = powf(thr, 0.25f) + reduction;
00545 thr = powf(thr, 4.0f);
00546
00547
00548
00549
00550
00551
00552 if (thr > band->energy * min_snr && band->avoid_holes != PSY_3GPP_AH_NONE) {
00553 thr = FFMAX(band->thr, band->energy * min_snr);
00554 band->avoid_holes = PSY_3GPP_AH_ACTIVE;
00555 }
00556 }
00557
00558 return thr;
00559 }
00560
00564 static void psy_3gpp_analyze_channel(FFPsyContext *ctx, int channel,
00565 const float *coefs, const FFPsyWindowInfo *wi)
00566 {
00567 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
00568 AacPsyChannel *pch = &pctx->ch[channel];
00569 int start = 0;
00570 int i, w, g;
00571 float desired_bits, desired_pe, delta_pe, reduction= NAN, spread_en[128] = {0};
00572 float a = 0.0f, active_lines = 0.0f, norm_fac = 0.0f;
00573 float pe = pctx->chan_bitrate > 32000 ? 0.0f : FFMAX(50.0f, 100.0f - pctx->chan_bitrate * 100.0f / 32000.0f);
00574 const int num_bands = ctx->num_bands[wi->num_windows == 8];
00575 const uint8_t *band_sizes = ctx->bands[wi->num_windows == 8];
00576 AacPsyCoeffs *coeffs = pctx->psy_coef[wi->num_windows == 8];
00577 const float avoid_hole_thr = wi->num_windows == 8 ? PSY_3GPP_AH_THR_SHORT : PSY_3GPP_AH_THR_LONG;
00578
00579
00580 for (w = 0; w < wi->num_windows*16; w += 16) {
00581 for (g = 0; g < num_bands; g++) {
00582 AacPsyBand *band = &pch->band[w+g];
00583
00584 float form_factor = 0.0f;
00585 band->energy = 0.0f;
00586 for (i = 0; i < band_sizes[g]; i++) {
00587 band->energy += coefs[start+i] * coefs[start+i];
00588 form_factor += sqrtf(fabs(coefs[start+i]));
00589 }
00590 band->thr = band->energy * 0.001258925f;
00591 band->nz_lines = band->energy>0 ? form_factor / powf(band->energy / band_sizes[g], 0.25f) : 0;
00592
00593 start += band_sizes[g];
00594 }
00595 }
00596
00597 for (w = 0; w < wi->num_windows*16; w += 16) {
00598 AacPsyBand *bands = &pch->band[w];
00599
00600
00601 spread_en[0] = bands[0].energy;
00602 for (g = 1; g < num_bands; g++) {
00603 bands[g].thr = FFMAX(bands[g].thr, bands[g-1].thr * coeffs[g].spread_hi[0]);
00604 spread_en[w+g] = FFMAX(bands[g].energy, spread_en[w+g-1] * coeffs[g].spread_hi[1]);
00605 }
00606 for (g = num_bands - 2; g >= 0; g--) {
00607 bands[g].thr = FFMAX(bands[g].thr, bands[g+1].thr * coeffs[g].spread_low[0]);
00608 spread_en[w+g] = FFMAX(spread_en[w+g], spread_en[w+g+1] * coeffs[g].spread_low[1]);
00609 }
00610
00611 for (g = 0; g < num_bands; g++) {
00612 AacPsyBand *band = &bands[g];
00613
00614 band->thr_quiet = band->thr = FFMAX(band->thr, coeffs[g].ath);
00615
00616 if (!(wi->window_type[0] == LONG_STOP_SEQUENCE || (wi->window_type[1] == LONG_START_SEQUENCE && !w)))
00617 band->thr = FFMAX(PSY_3GPP_RPEMIN*band->thr, FFMIN(band->thr,
00618 PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
00619
00620
00621 pe += calc_pe_3gpp(band);
00622 a += band->pe_const;
00623 active_lines += band->active_lines;
00624
00625
00626 if (spread_en[w+g] * avoid_hole_thr > band->energy || coeffs[g].min_snr > 1.0f)
00627 band->avoid_holes = PSY_3GPP_AH_NONE;
00628 else
00629 band->avoid_holes = PSY_3GPP_AH_INACTIVE;
00630 }
00631 }
00632
00633
00634 ctx->ch[channel].entropy = pe;
00635 desired_bits = calc_bit_demand(pctx, pe, ctx->bitres.bits, ctx->bitres.size, wi->num_windows == 8);
00636 desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits);
00637
00638
00639
00640
00641 if (ctx->bitres.bits > 0)
00642 desired_pe *= av_clipf(pctx->pe.previous / PSY_3GPP_BITS_TO_PE(ctx->bitres.bits),
00643 0.85f, 1.15f);
00644 pctx->pe.previous = PSY_3GPP_BITS_TO_PE(desired_bits);
00645
00646 if (desired_pe < pe) {
00647
00648 for (w = 0; w < wi->num_windows*16; w += 16) {
00649 reduction = calc_reduction_3gpp(a, desired_pe, pe, active_lines);
00650 pe = 0.0f;
00651 a = 0.0f;
00652 active_lines = 0.0f;
00653 for (g = 0; g < num_bands; g++) {
00654 AacPsyBand *band = &pch->band[w+g];
00655
00656 band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
00657
00658 pe += calc_pe_3gpp(band);
00659 a += band->pe_const;
00660 active_lines += band->active_lines;
00661 }
00662 }
00663
00664
00665 for (i = 0; i < 2; i++) {
00666 float pe_no_ah = 0.0f, desired_pe_no_ah;
00667 active_lines = a = 0.0f;
00668 for (w = 0; w < wi->num_windows*16; w += 16) {
00669 for (g = 0; g < num_bands; g++) {
00670 AacPsyBand *band = &pch->band[w+g];
00671
00672 if (band->avoid_holes != PSY_3GPP_AH_ACTIVE) {
00673 pe_no_ah += band->pe;
00674 a += band->pe_const;
00675 active_lines += band->active_lines;
00676 }
00677 }
00678 }
00679 desired_pe_no_ah = FFMAX(desired_pe - (pe - pe_no_ah), 0.0f);
00680 if (active_lines > 0.0f)
00681 reduction += calc_reduction_3gpp(a, desired_pe_no_ah, pe_no_ah, active_lines);
00682
00683 pe = 0.0f;
00684 for (w = 0; w < wi->num_windows*16; w += 16) {
00685 for (g = 0; g < num_bands; g++) {
00686 AacPsyBand *band = &pch->band[w+g];
00687
00688 if (active_lines > 0.0f)
00689 band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
00690 pe += calc_pe_3gpp(band);
00691 band->norm_fac = band->active_lines / band->thr;
00692 norm_fac += band->norm_fac;
00693 }
00694 }
00695 delta_pe = desired_pe - pe;
00696 if (fabs(delta_pe) > 0.05f * desired_pe)
00697 break;
00698 }
00699
00700 if (pe < 1.15f * desired_pe) {
00701
00702 norm_fac = 1.0f / norm_fac;
00703 for (w = 0; w < wi->num_windows*16; w += 16) {
00704 for (g = 0; g < num_bands; g++) {
00705 AacPsyBand *band = &pch->band[w+g];
00706
00707 if (band->active_lines > 0.5f) {
00708 float delta_sfb_pe = band->norm_fac * norm_fac * delta_pe;
00709 float thr = band->thr;
00710
00711 thr *= exp2f(delta_sfb_pe / band->active_lines);
00712 if (thr > coeffs[g].min_snr * band->energy && band->avoid_holes == PSY_3GPP_AH_INACTIVE)
00713 thr = FFMAX(band->thr, coeffs[g].min_snr * band->energy);
00714 band->thr = thr;
00715 }
00716 }
00717 }
00718 } else {
00719
00720 g = num_bands;
00721 while (pe > desired_pe && g--) {
00722 for (w = 0; w < wi->num_windows*16; w+= 16) {
00723 AacPsyBand *band = &pch->band[w+g];
00724 if (band->avoid_holes != PSY_3GPP_AH_NONE && coeffs[g].min_snr < PSY_SNR_1DB) {
00725 coeffs[g].min_snr = PSY_SNR_1DB;
00726 band->thr = band->energy * PSY_SNR_1DB;
00727 pe += band->active_lines * 1.5f - band->pe;
00728 }
00729 }
00730 }
00731
00732 }
00733 }
00734
00735 for (w = 0; w < wi->num_windows*16; w += 16) {
00736 for (g = 0; g < num_bands; g++) {
00737 AacPsyBand *band = &pch->band[w+g];
00738 FFPsyBand *psy_band = &ctx->ch[channel].psy_bands[w+g];
00739
00740 psy_band->threshold = band->thr;
00741 psy_band->energy = band->energy;
00742 }
00743 }
00744
00745 memcpy(pch->prev_band, pch->band, sizeof(pch->band));
00746 }
00747
00748 static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
00749 const float **coeffs, const FFPsyWindowInfo *wi)
00750 {
00751 int ch;
00752 FFPsyChannelGroup *group = ff_psy_find_group(ctx, channel);
00753
00754 for (ch = 0; ch < group->num_ch; ch++)
00755 psy_3gpp_analyze_channel(ctx, channel + ch, coeffs[ch], &wi[ch]);
00756 }
00757
00758 static av_cold void psy_3gpp_end(FFPsyContext *apc)
00759 {
00760 AacPsyContext *pctx = (AacPsyContext*) apc->model_priv_data;
00761 av_freep(&pctx->ch);
00762 av_freep(&apc->model_priv_data);
00763 }
00764
00765 static void lame_apply_block_type(AacPsyChannel *ctx, FFPsyWindowInfo *wi, int uselongblock)
00766 {
00767 int blocktype = ONLY_LONG_SEQUENCE;
00768 if (uselongblock) {
00769 if (ctx->next_window_seq == EIGHT_SHORT_SEQUENCE)
00770 blocktype = LONG_STOP_SEQUENCE;
00771 } else {
00772 blocktype = EIGHT_SHORT_SEQUENCE;
00773 if (ctx->next_window_seq == ONLY_LONG_SEQUENCE)
00774 ctx->next_window_seq = LONG_START_SEQUENCE;
00775 if (ctx->next_window_seq == LONG_STOP_SEQUENCE)
00776 ctx->next_window_seq = EIGHT_SHORT_SEQUENCE;
00777 }
00778
00779 wi->window_type[0] = ctx->next_window_seq;
00780 ctx->next_window_seq = blocktype;
00781 }
00782
00783 static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx, const float *audio,
00784 const float *la, int channel, int prev_type)
00785 {
00786 AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
00787 AacPsyChannel *pch = &pctx->ch[channel];
00788 int grouping = 0;
00789 int uselongblock = 1;
00790 int attacks[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
00791 int i;
00792 FFPsyWindowInfo wi = { { 0 } };
00793
00794 if (la) {
00795 float hpfsmpl[AAC_BLOCK_SIZE_LONG];
00796 float const *pf = hpfsmpl;
00797 float attack_intensity[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
00798 float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
00799 float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
00800 const float *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN);
00801 int j, att_sum = 0;
00802
00803
00804 for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
00805 float sum1, sum2;
00806 sum1 = firbuf[i + (PSY_LAME_FIR_LEN - 1) / 2];
00807 sum2 = 0.0;
00808 for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
00809 sum1 += psy_fir_coeffs[j] * (firbuf[i + j] + firbuf[i + PSY_LAME_FIR_LEN - j]);
00810 sum2 += psy_fir_coeffs[j + 1] * (firbuf[i + j + 1] + firbuf[i + PSY_LAME_FIR_LEN - j - 1]);
00811 }
00812
00813 hpfsmpl[i] = (sum1 + sum2) * 32768.0f;
00814 }
00815
00816
00817 for (i = 0; i < PSY_LAME_NUM_SUBBLOCKS; i++) {
00818 energy_subshort[i] = pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS)];
00819 assert(pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)] > 0);
00820 attack_intensity[i] = energy_subshort[i] / pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)];
00821 energy_short[0] += energy_subshort[i];
00822 }
00823
00824 for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
00825 float const *const pfe = pf + AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
00826 float p = 1.0f;
00827 for (; pf < pfe; pf++)
00828 p = FFMAX(p, fabsf(*pf));
00829 pch->prev_energy_subshort[i] = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS] = p;
00830 energy_short[1 + i / PSY_LAME_NUM_SUBBLOCKS] += p;
00831
00832
00833
00834
00835
00836
00837
00838 if (p > energy_subshort[i + 1])
00839 p = p / energy_subshort[i + 1];
00840 else if (energy_subshort[i + 1] > p * 10.0f)
00841 p = energy_subshort[i + 1] / (p * 10.0f);
00842 else
00843 p = 0.0;
00844 attack_intensity[i + PSY_LAME_NUM_SUBBLOCKS] = p;
00845 }
00846
00847
00848 for (i = 0; i < (AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS; i++)
00849 if (!attacks[i / PSY_LAME_NUM_SUBBLOCKS])
00850 if (attack_intensity[i] > pch->attack_threshold)
00851 attacks[i / PSY_LAME_NUM_SUBBLOCKS] = (i % PSY_LAME_NUM_SUBBLOCKS) + 1;
00852
00853
00854
00855
00856
00857 for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++) {
00858 float const u = energy_short[i - 1];
00859 float const v = energy_short[i];
00860 float const m = FFMAX(u, v);
00861 if (m < 40000) {
00862 if (u < 1.7f * v && v < 1.7f * u) {
00863 if (i == 1 && attacks[0] < attacks[i])
00864 attacks[0] = 0;
00865 attacks[i] = 0;
00866 }
00867 }
00868 att_sum += attacks[i];
00869 }
00870
00871 if (attacks[0] <= pch->prev_attack)
00872 attacks[0] = 0;
00873
00874 att_sum += attacks[0];
00875
00876 if (pch->prev_attack == 3 || att_sum) {
00877 uselongblock = 0;
00878
00879 for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++)
00880 if (attacks[i] && attacks[i-1])
00881 attacks[i] = 0;
00882 }
00883 } else {
00884
00885 uselongblock = !(prev_type == EIGHT_SHORT_SEQUENCE);
00886 }
00887
00888 lame_apply_block_type(pch, &wi, uselongblock);
00889
00890 wi.window_type[1] = prev_type;
00891 if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
00892 wi.num_windows = 1;
00893 wi.grouping[0] = 1;
00894 if (wi.window_type[0] == LONG_START_SEQUENCE)
00895 wi.window_shape = 0;
00896 else
00897 wi.window_shape = 1;
00898 } else {
00899 int lastgrp = 0;
00900
00901 wi.num_windows = 8;
00902 wi.window_shape = 0;
00903 for (i = 0; i < 8; i++) {
00904 if (!((pch->next_grouping >> i) & 1))
00905 lastgrp = i;
00906 wi.grouping[lastgrp]++;
00907 }
00908 }
00909
00910
00911
00912
00913
00914
00915
00916 for (i = 0; i < 9; i++) {
00917 if (attacks[i]) {
00918 grouping = i;
00919 break;
00920 }
00921 }
00922 pch->next_grouping = window_grouping[grouping];
00923
00924 pch->prev_attack = attacks[8];
00925
00926 return wi;
00927 }
00928
00929 const FFPsyModel ff_aac_psy_model =
00930 {
00931 .name = "3GPP TS 26.403-inspired model",
00932 .init = psy_3gpp_init,
00933 .window = psy_lame_window,
00934 .analyze = psy_3gpp_analyze,
00935 .end = psy_3gpp_end,
00936 };