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