42
41
#define PSY_3GPP_THR_SPREAD_HI 1.5f // spreading factor for low-to-hi threshold spreading (15 dB/Bark)
43
42
#define PSY_3GPP_THR_SPREAD_LOW 3.0f // spreading factor for hi-to-low threshold spreading (30 dB/Bark)
43
/* spreading factor for low-to-hi energy spreading, long block, > 22kbps/channel (20dB/Bark) */
44
#define PSY_3GPP_EN_SPREAD_HI_L1 2.0f
45
/* spreading factor for low-to-hi energy spreading, long block, <= 22kbps/channel (15dB/Bark) */
46
#define PSY_3GPP_EN_SPREAD_HI_L2 1.5f
47
/* spreading factor for low-to-hi energy spreading, short block (15 dB/Bark) */
48
#define PSY_3GPP_EN_SPREAD_HI_S 1.5f
49
/* spreading factor for hi-to-low energy spreading, long block (30dB/Bark) */
50
#define PSY_3GPP_EN_SPREAD_LOW_L 3.0f
51
/* spreading factor for hi-to-low energy spreading, short block (20dB/Bark) */
52
#define PSY_3GPP_EN_SPREAD_LOW_S 2.0f
45
54
#define PSY_3GPP_RPEMIN 0.01f
46
55
#define PSY_3GPP_RPELEV 2.0f
57
#define PSY_3GPP_C1 3.0f /* log2(8) */
58
#define PSY_3GPP_C2 1.3219281f /* log2(2.5) */
59
#define PSY_3GPP_C3 0.55935729f /* 1 - C2 / C1 */
61
#define PSY_SNR_1DB 7.9432821e-1f /* -1dB */
62
#define PSY_SNR_25DB 3.1622776e-3f /* -25dB */
64
#define PSY_3GPP_SAVE_SLOPE_L -0.46666667f
65
#define PSY_3GPP_SAVE_SLOPE_S -0.36363637f
66
#define PSY_3GPP_SAVE_ADD_L -0.84285712f
67
#define PSY_3GPP_SAVE_ADD_S -0.75f
68
#define PSY_3GPP_SPEND_SLOPE_L 0.66666669f
69
#define PSY_3GPP_SPEND_SLOPE_S 0.81818181f
70
#define PSY_3GPP_SPEND_ADD_L -0.35f
71
#define PSY_3GPP_SPEND_ADD_S -0.26111111f
72
#define PSY_3GPP_CLIP_LO_L 0.2f
73
#define PSY_3GPP_CLIP_LO_S 0.2f
74
#define PSY_3GPP_CLIP_HI_L 0.95f
75
#define PSY_3GPP_CLIP_HI_S 0.75f
77
#define PSY_3GPP_AH_THR_LONG 0.5f
78
#define PSY_3GPP_AH_THR_SHORT 0.63f
86
#define PSY_3GPP_BITS_TO_PE(bits) ((bits) * 1.18f)
48
88
/* LAME psy model constants */
49
89
#define PSY_LAME_FIR_LEN 21 ///< LAME psy model FIR order
50
90
#define AAC_BLOCK_SIZE_LONG 1024 ///< long block size
235
290
AacPsyContext *pctx;
237
292
int i, j, g, start;
238
float prev, minscale, minath;
293
float prev, minscale, minath, minsnr, pe_min;
294
const int chan_bitrate = ctx->avctx->bit_rate / ctx->avctx->channels;
295
const int bandwidth = ctx->avctx->cutoff ? ctx->avctx->cutoff : ctx->avctx->sample_rate / 2;
296
const float num_bark = calc_bark((float)bandwidth);
240
298
ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
241
299
pctx = (AacPsyContext*) ctx->model_priv_data;
301
pctx->chan_bitrate = chan_bitrate;
302
pctx->frame_bits = chan_bitrate * AAC_BLOCK_SIZE_LONG / ctx->avctx->sample_rate;
303
pctx->pe.min = 8.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
304
pctx->pe.max = 12.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
305
ctx->bitres.size = 6144 - pctx->frame_bits;
306
ctx->bitres.size -= ctx->bitres.size % 8;
307
pctx->fill_level = ctx->bitres.size;
243
308
minath = ath(3410, ATH_ADD);
244
309
for (j = 0; j < 2; j++) {
245
310
AacPsyCoeffs *coeffs = pctx->psy_coef[j];
246
311
const uint8_t *band_sizes = ctx->bands[j];
247
312
float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
313
float avg_chan_bits = chan_bitrate / ctx->avctx->sample_rate * (j ? 128.0f : 1024.0f);
314
/* reference encoder uses 2.4% here instead of 60% like the spec says */
315
float bark_pe = 0.024f * PSY_3GPP_BITS_TO_PE(avg_chan_bits) / num_bark;
316
float en_spread_low = j ? PSY_3GPP_EN_SPREAD_LOW_S : PSY_3GPP_EN_SPREAD_LOW_L;
317
/* High energy spreading for long blocks <= 22kbps/channel and short blocks are the same. */
318
float en_spread_hi = (j || (chan_bitrate <= 22.0f)) ? PSY_3GPP_EN_SPREAD_HI_S : PSY_3GPP_EN_SPREAD_HI_L1;
250
322
for (g = 0; g < ctx->num_bands[j]; g++) {
465
/* 5.6.1.2 "Calculation of Bit Demand" */
466
static int calc_bit_demand(AacPsyContext *ctx, float pe, int bits, int size,
469
const float bitsave_slope = short_window ? PSY_3GPP_SAVE_SLOPE_S : PSY_3GPP_SAVE_SLOPE_L;
470
const float bitsave_add = short_window ? PSY_3GPP_SAVE_ADD_S : PSY_3GPP_SAVE_ADD_L;
471
const float bitspend_slope = short_window ? PSY_3GPP_SPEND_SLOPE_S : PSY_3GPP_SPEND_SLOPE_L;
472
const float bitspend_add = short_window ? PSY_3GPP_SPEND_ADD_S : PSY_3GPP_SPEND_ADD_L;
473
const float clip_low = short_window ? PSY_3GPP_CLIP_LO_S : PSY_3GPP_CLIP_LO_L;
474
const float clip_high = short_window ? PSY_3GPP_CLIP_HI_S : PSY_3GPP_CLIP_HI_L;
475
float clipped_pe, bit_save, bit_spend, bit_factor, fill_level;
477
ctx->fill_level += ctx->frame_bits - bits;
478
ctx->fill_level = av_clip(ctx->fill_level, 0, size);
479
fill_level = av_clipf((float)ctx->fill_level / size, clip_low, clip_high);
480
clipped_pe = av_clipf(pe, ctx->pe.min, ctx->pe.max);
481
bit_save = (fill_level + bitsave_add) * bitsave_slope;
482
assert(bit_save <= 0.3f && bit_save >= -0.05000001f);
483
bit_spend = (fill_level + bitspend_add) * bitspend_slope;
484
assert(bit_spend <= 0.5f && bit_spend >= -0.1f);
485
/* The bit factor graph in the spec is obviously incorrect.
486
* bit_spend + ((bit_spend - bit_spend))...
487
* The reference encoder subtracts everything from 1, but also seems incorrect.
488
* 1 - bit_save + ((bit_spend + bit_save))...
489
* Hopefully below is correct.
491
bit_factor = 1.0f - bit_save + ((bit_spend - bit_save) / (ctx->pe.max - ctx->pe.min)) * (clipped_pe - ctx->pe.min);
492
/* NOTE: The reference encoder attempts to center pe max/min around the current pe. */
493
ctx->pe.max = FFMAX(pe, ctx->pe.max);
494
ctx->pe.min = FFMIN(pe, ctx->pe.min);
496
return FFMIN(ctx->frame_bits * bit_factor, ctx->frame_bits + size - bits);
499
static float calc_pe_3gpp(AacPsyBand *band)
504
band->pe_const = 0.0f;
505
band->active_lines = 0.0f;
506
if (band->energy > band->thr) {
507
a = log2f(band->energy);
508
pe = a - log2f(band->thr);
509
band->active_lines = band->nz_lines;
510
if (pe < PSY_3GPP_C1) {
511
pe = pe * PSY_3GPP_C3 + PSY_3GPP_C2;
512
a = a * PSY_3GPP_C3 + PSY_3GPP_C2;
513
band->active_lines *= PSY_3GPP_C3;
515
band->pe = pe * band->nz_lines;
516
band->pe_const = a * band->nz_lines;
522
static float calc_reduction_3gpp(float a, float desired_pe, float pe,
525
float thr_avg, reduction;
527
thr_avg = powf(2.0f, (a - pe) / (4.0f * active_lines));
528
reduction = powf(2.0f, (a - desired_pe) / (4.0f * active_lines)) - thr_avg;
530
return FFMAX(reduction, 0.0f);
533
static float calc_reduced_thr_3gpp(AacPsyBand *band, float min_snr,
536
float thr = band->thr;
538
if (band->energy > thr) {
539
thr = powf(thr, 0.25f) + reduction;
540
thr = powf(thr, 4.0f);
542
/* This deviates from the 3GPP spec to match the reference encoder.
543
* It performs min(thr_reduced, max(thr, energy/min_snr)) only for bands
544
* that have hole avoidance on (active or inactive). It always reduces the
545
* threshold of bands with hole avoidance off.
547
if (thr > band->energy * min_snr && band->avoid_holes != PSY_3GPP_AH_NONE) {
548
thr = FFMAX(band->thr, band->energy * min_snr);
549
band->avoid_holes = PSY_3GPP_AH_ACTIVE;
389
557
* Calculate band thresholds as suggested in 3GPP TS26.403
395
563
AacPsyChannel *pch = &pctx->ch[channel];
398
const int num_bands = ctx->num_bands[wi->num_windows == 8];
399
const uint8_t *band_sizes = ctx->bands[wi->num_windows == 8];
400
AacPsyCoeffs *coeffs = pctx->psy_coef[wi->num_windows == 8];
566
float desired_bits, desired_pe, delta_pe, reduction, spread_en[128] = {0};
567
float a = 0.0f, active_lines = 0.0f, norm_fac = 0.0f;
568
float pe = pctx->chan_bitrate > 32000 ? 0.0f : FFMAX(50.0f, 100.0f - pctx->chan_bitrate * 100.0f / 32000.0f);
569
const int num_bands = ctx->num_bands[wi->num_windows == 8];
570
const uint8_t *band_sizes = ctx->bands[wi->num_windows == 8];
571
AacPsyCoeffs *coeffs = pctx->psy_coef[wi->num_windows == 8];
572
const float avoid_hole_thr = wi->num_windows == 8 ? PSY_3GPP_AH_THR_SHORT : PSY_3GPP_AH_THR_LONG;
402
574
//calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
403
575
for (w = 0; w < wi->num_windows*16; w += 16) {
404
576
for (g = 0; g < num_bands; g++) {
405
577
AacPsyBand *band = &pch->band[w+g];
579
float form_factor = 0.0f;
406
580
band->energy = 0.0f;
407
for (i = 0; i < band_sizes[g]; i++)
581
for (i = 0; i < band_sizes[g]; i++) {
408
582
band->energy += coefs[start+i] * coefs[start+i];
409
band->thr = band->energy * 0.001258925f;
410
start += band_sizes[g];
583
form_factor += sqrtf(fabs(coefs[start+i]));
585
band->thr = band->energy * 0.001258925f;
586
band->nz_lines = form_factor / powf(band->energy / band_sizes[g], 0.25f);
588
start += band_sizes[g];
413
591
//modify thresholds and energies - spread, threshold in quiet, pre-echo control
414
592
for (w = 0; w < wi->num_windows*16; w += 16) {
415
593
AacPsyBand *bands = &pch->band[w];
416
595
//5.4.2.3 "Spreading" & 5.4.3 "Spreaded Energy Calculation"
417
for (g = 1; g < num_bands; g++)
418
bands[g].thr = FFMAX(bands[g].thr, bands[g-1].thr * coeffs[g].spread_hi[0]);
419
for (g = num_bands - 2; g >= 0; g--)
420
bands[g].thr = FFMAX(bands[g].thr, bands[g+1].thr * coeffs[g].spread_low[0]);
596
spread_en[0] = bands[0].energy;
597
for (g = 1; g < num_bands; g++) {
598
bands[g].thr = FFMAX(bands[g].thr, bands[g-1].thr * coeffs[g].spread_hi[0]);
599
spread_en[w+g] = FFMAX(bands[g].energy, spread_en[w+g-1] * coeffs[g].spread_hi[1]);
601
for (g = num_bands - 2; g >= 0; g--) {
602
bands[g].thr = FFMAX(bands[g].thr, bands[g+1].thr * coeffs[g].spread_low[0]);
603
spread_en[w+g] = FFMAX(spread_en[w+g], spread_en[w+g+1] * coeffs[g].spread_low[1]);
421
605
//5.4.2.4 "Threshold in quiet"
422
606
for (g = 0; g < num_bands; g++) {
423
607
AacPsyBand *band = &bands[g];
424
609
band->thr_quiet = band->thr = FFMAX(band->thr, coeffs[g].ath);
425
610
//5.4.2.5 "Pre-echo control"
426
611
if (!(wi->window_type[0] == LONG_STOP_SEQUENCE || (wi->window_type[1] == LONG_START_SEQUENCE && !w)))
427
612
band->thr = FFMAX(PSY_3GPP_RPEMIN*band->thr, FFMIN(band->thr,
428
613
PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
615
/* 5.6.1.3.1 "Prepatory steps of the perceptual entropy calculation" */
616
pe += calc_pe_3gpp(band);
618
active_lines += band->active_lines;
620
/* 5.6.1.3.3 "Selection of the bands for avoidance of holes" */
621
if (spread_en[w+g] * avoid_hole_thr > band->energy || coeffs[g].min_snr > 1.0f)
622
band->avoid_holes = PSY_3GPP_AH_NONE;
624
band->avoid_holes = PSY_3GPP_AH_INACTIVE;
628
/* 5.6.1.3.2 "Calculation of the desired perceptual entropy" */
629
ctx->pe[channel] = pe;
630
desired_bits = calc_bit_demand(pctx, pe, ctx->bitres.bits, ctx->bitres.size, wi->num_windows == 8);
631
desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits);
632
/* NOTE: PE correction is kept simple. During initial testing it had very
633
* little effect on the final bitrate. Probably a good idea to come
634
* back and do more testing later.
636
if (ctx->bitres.bits > 0)
637
desired_pe *= av_clipf(pctx->pe.previous / PSY_3GPP_BITS_TO_PE(ctx->bitres.bits),
639
pctx->pe.previous = PSY_3GPP_BITS_TO_PE(desired_bits);
641
if (desired_pe < pe) {
642
/* 5.6.1.3.4 "First Estimation of the reduction value" */
643
for (w = 0; w < wi->num_windows*16; w += 16) {
644
reduction = calc_reduction_3gpp(a, desired_pe, pe, active_lines);
648
for (g = 0; g < num_bands; g++) {
649
AacPsyBand *band = &pch->band[w+g];
651
band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
653
pe += calc_pe_3gpp(band);
655
active_lines += band->active_lines;
659
/* 5.6.1.3.5 "Second Estimation of the reduction value" */
660
for (i = 0; i < 2; i++) {
661
float pe_no_ah = 0.0f, desired_pe_no_ah;
662
active_lines = a = 0.0f;
663
for (w = 0; w < wi->num_windows*16; w += 16) {
664
for (g = 0; g < num_bands; g++) {
665
AacPsyBand *band = &pch->band[w+g];
667
if (band->avoid_holes != PSY_3GPP_AH_ACTIVE) {
668
pe_no_ah += band->pe;
670
active_lines += band->active_lines;
674
desired_pe_no_ah = FFMAX(desired_pe - (pe - pe_no_ah), 0.0f);
675
if (active_lines > 0.0f)
676
reduction += calc_reduction_3gpp(a, desired_pe_no_ah, pe_no_ah, active_lines);
679
for (w = 0; w < wi->num_windows*16; w += 16) {
680
for (g = 0; g < num_bands; g++) {
681
AacPsyBand *band = &pch->band[w+g];
683
if (active_lines > 0.0f)
684
band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
685
pe += calc_pe_3gpp(band);
686
band->norm_fac = band->active_lines / band->thr;
687
norm_fac += band->norm_fac;
690
delta_pe = desired_pe - pe;
691
if (fabs(delta_pe) > 0.05f * desired_pe)
695
if (pe < 1.15f * desired_pe) {
696
/* 6.6.1.3.6 "Final threshold modification by linearization" */
697
norm_fac = 1.0f / norm_fac;
698
for (w = 0; w < wi->num_windows*16; w += 16) {
699
for (g = 0; g < num_bands; g++) {
700
AacPsyBand *band = &pch->band[w+g];
702
if (band->active_lines > 0.5f) {
703
float delta_sfb_pe = band->norm_fac * norm_fac * delta_pe;
704
float thr = band->thr;
706
thr *= powf(2.0f, delta_sfb_pe / band->active_lines);
707
if (thr > coeffs[g].min_snr * band->energy && band->avoid_holes == PSY_3GPP_AH_INACTIVE)
708
thr = FFMAX(band->thr, coeffs[g].min_snr * band->energy);
714
/* 5.6.1.3.7 "Further perceptual entropy reduction" */
716
while (pe > desired_pe && g--) {
717
for (w = 0; w < wi->num_windows*16; w+= 16) {
718
AacPsyBand *band = &pch->band[w+g];
719
if (band->avoid_holes != PSY_3GPP_AH_NONE && coeffs[g].min_snr < PSY_SNR_1DB) {
720
coeffs[g].min_snr = PSY_SNR_1DB;
721
band->thr = band->energy * PSY_SNR_1DB;
722
pe += band->active_lines * 1.5f - band->pe;
726
/* TODO: allow more holes (unused without mid/side) */