2008-08-18 05:39:44 +00:00
|
|
|
/*
|
|
|
|
* AAC encoder psychoacoustic model
|
|
|
|
* Copyright (C) 2008 Konstantin Shishkov
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2008-08-18 05:39:44 +00:00
|
|
|
* AAC encoder psychoacoustic model
|
|
|
|
*/
|
|
|
|
|
2013-02-01 09:31:59 +00:00
|
|
|
#include "libavutil/attributes.h"
|
2012-09-09 10:59:55 +00:00
|
|
|
#include "libavutil/libm.h"
|
|
|
|
|
2008-08-18 05:39:44 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "aactab.h"
|
2009-07-08 20:01:31 +00:00
|
|
|
#include "psymodel.h"
|
2008-08-18 05:39:44 +00:00
|
|
|
|
|
|
|
/***********************************
|
|
|
|
* TODOs:
|
|
|
|
* try other bitrate controlling mechanism (maybe use ratecontrol.c?)
|
|
|
|
* control quality for quality-based output
|
|
|
|
**********************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constants for 3GPP AAC psychoacoustic model
|
|
|
|
* @{
|
|
|
|
*/
|
2011-03-17 03:35:39 +00:00
|
|
|
#define PSY_3GPP_THR_SPREAD_HI 1.5f // spreading factor for low-to-hi threshold spreading (15 dB/Bark)
|
|
|
|
#define PSY_3GPP_THR_SPREAD_LOW 3.0f // spreading factor for hi-to-low threshold spreading (30 dB/Bark)
|
2011-01-05 08:32:16 +00:00
|
|
|
/* spreading factor for low-to-hi energy spreading, long block, > 22kbps/channel (20dB/Bark) */
|
|
|
|
#define PSY_3GPP_EN_SPREAD_HI_L1 2.0f
|
|
|
|
/* spreading factor for low-to-hi energy spreading, long block, <= 22kbps/channel (15dB/Bark) */
|
|
|
|
#define PSY_3GPP_EN_SPREAD_HI_L2 1.5f
|
|
|
|
/* spreading factor for low-to-hi energy spreading, short block (15 dB/Bark) */
|
|
|
|
#define PSY_3GPP_EN_SPREAD_HI_S 1.5f
|
|
|
|
/* spreading factor for hi-to-low energy spreading, long block (30dB/Bark) */
|
|
|
|
#define PSY_3GPP_EN_SPREAD_LOW_L 3.0f
|
|
|
|
/* spreading factor for hi-to-low energy spreading, short block (20dB/Bark) */
|
|
|
|
#define PSY_3GPP_EN_SPREAD_LOW_S 2.0f
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
#define PSY_3GPP_RPEMIN 0.01f
|
|
|
|
#define PSY_3GPP_RPELEV 2.0f
|
2010-08-23 20:00:03 +00:00
|
|
|
|
2011-01-05 08:32:16 +00:00
|
|
|
#define PSY_3GPP_C1 3.0f /* log2(8) */
|
|
|
|
#define PSY_3GPP_C2 1.3219281f /* log2(2.5) */
|
|
|
|
#define PSY_3GPP_C3 0.55935729f /* 1 - C2 / C1 */
|
|
|
|
|
|
|
|
#define PSY_SNR_1DB 7.9432821e-1f /* -1dB */
|
|
|
|
#define PSY_SNR_25DB 3.1622776e-3f /* -25dB */
|
|
|
|
|
|
|
|
#define PSY_3GPP_SAVE_SLOPE_L -0.46666667f
|
|
|
|
#define PSY_3GPP_SAVE_SLOPE_S -0.36363637f
|
|
|
|
#define PSY_3GPP_SAVE_ADD_L -0.84285712f
|
|
|
|
#define PSY_3GPP_SAVE_ADD_S -0.75f
|
|
|
|
#define PSY_3GPP_SPEND_SLOPE_L 0.66666669f
|
|
|
|
#define PSY_3GPP_SPEND_SLOPE_S 0.81818181f
|
|
|
|
#define PSY_3GPP_SPEND_ADD_L -0.35f
|
|
|
|
#define PSY_3GPP_SPEND_ADD_S -0.26111111f
|
|
|
|
#define PSY_3GPP_CLIP_LO_L 0.2f
|
|
|
|
#define PSY_3GPP_CLIP_LO_S 0.2f
|
|
|
|
#define PSY_3GPP_CLIP_HI_L 0.95f
|
|
|
|
#define PSY_3GPP_CLIP_HI_S 0.75f
|
|
|
|
|
|
|
|
#define PSY_3GPP_AH_THR_LONG 0.5f
|
|
|
|
#define PSY_3GPP_AH_THR_SHORT 0.63f
|
|
|
|
|
2015-11-29 18:03:45 +00:00
|
|
|
#define PSY_PE_FORGET_SLOPE 511
|
|
|
|
|
2011-01-05 08:32:16 +00:00
|
|
|
enum {
|
|
|
|
PSY_3GPP_AH_NONE,
|
|
|
|
PSY_3GPP_AH_INACTIVE,
|
|
|
|
PSY_3GPP_AH_ACTIVE
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PSY_3GPP_BITS_TO_PE(bits) ((bits) * 1.18f)
|
2015-09-23 05:13:56 +00:00
|
|
|
#define PSY_3GPP_PE_TO_BITS(bits) ((bits) / 1.18f)
|
2011-01-05 08:32:16 +00:00
|
|
|
|
2010-08-23 20:00:03 +00:00
|
|
|
/* LAME psy model constants */
|
|
|
|
#define PSY_LAME_FIR_LEN 21 ///< LAME psy model FIR order
|
|
|
|
#define AAC_BLOCK_SIZE_LONG 1024 ///< long block size
|
|
|
|
#define AAC_BLOCK_SIZE_SHORT 128 ///< short block size
|
|
|
|
#define AAC_NUM_BLOCKS_SHORT 8 ///< number of blocks in a short sequence
|
|
|
|
#define PSY_LAME_NUM_SUBBLOCKS 3 ///< Number of sub-blocks in each short block
|
|
|
|
|
2008-08-18 05:39:44 +00:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* information for single band used by 3GPP TS26.403-inspired psychoacoustic model
|
|
|
|
*/
|
2010-08-23 19:27:24 +00:00
|
|
|
typedef struct AacPsyBand{
|
2011-04-05 07:05:24 +00:00
|
|
|
float energy; ///< band energy
|
|
|
|
float thr; ///< energy threshold
|
|
|
|
float thr_quiet; ///< threshold in quiet
|
2011-01-05 08:32:16 +00:00
|
|
|
float nz_lines; ///< number of non-zero spectral lines
|
|
|
|
float active_lines; ///< number of active spectral lines
|
|
|
|
float pe; ///< perceptual entropy
|
|
|
|
float pe_const; ///< constant part of the PE calculation
|
|
|
|
float norm_fac; ///< normalization factor for linearization
|
|
|
|
int avoid_holes; ///< hole avoidance flag
|
2010-08-23 19:27:24 +00:00
|
|
|
}AacPsyBand;
|
2008-08-18 05:39:44 +00:00
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
/**
|
|
|
|
* single/pair channel context for psychoacoustic model
|
|
|
|
*/
|
2010-08-23 19:27:24 +00:00
|
|
|
typedef struct AacPsyChannel{
|
|
|
|
AacPsyBand band[128]; ///< bands information
|
|
|
|
AacPsyBand prev_band[128]; ///< bands information from the previous frame
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
float win_energy; ///< sliding average of channel energy
|
|
|
|
float iir_state[2]; ///< hi-pass IIR filter state
|
|
|
|
uint8_t next_grouping; ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
|
|
|
|
enum WindowSequence next_window_seq; ///< window sequence to be used in the next frame
|
2010-08-23 20:00:03 +00:00
|
|
|
/* LAME psy model specific members */
|
|
|
|
float attack_threshold; ///< attack threshold for this channel
|
|
|
|
float prev_energy_subshort[AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS];
|
|
|
|
int prev_attack; ///< attack value for the last short block in the previous sequence
|
2010-08-23 19:27:24 +00:00
|
|
|
}AacPsyChannel;
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2008-08-18 05:39:44 +00:00
|
|
|
/**
|
|
|
|
* psychoacoustic model frame type-dependent coefficients
|
|
|
|
*/
|
2010-08-23 19:27:24 +00:00
|
|
|
typedef struct AacPsyCoeffs{
|
2011-03-17 03:34:12 +00:00
|
|
|
float ath; ///< absolute threshold of hearing per bands
|
|
|
|
float barks; ///< Bark value for each spectral band in long frame
|
|
|
|
float spread_low[2]; ///< spreading factor for low-to-high threshold spreading in long frame
|
|
|
|
float spread_hi [2]; ///< spreading factor for high-to-low threshold spreading in long frame
|
|
|
|
float min_snr; ///< minimal SNR
|
2010-08-23 19:27:24 +00:00
|
|
|
}AacPsyCoeffs;
|
2008-08-18 05:39:44 +00:00
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
/**
|
|
|
|
* 3GPP TS26.403-inspired psychoacoustic model specific data
|
|
|
|
*/
|
2010-08-23 19:27:24 +00:00
|
|
|
typedef struct AacPsyContext{
|
2011-01-05 08:32:16 +00:00
|
|
|
int chan_bitrate; ///< bitrate per channel
|
|
|
|
int frame_bits; ///< average bits per frame
|
|
|
|
int fill_level; ///< bit reservoir fill level
|
|
|
|
struct {
|
|
|
|
float min; ///< minimum allowed PE for bit factor calculation
|
|
|
|
float max; ///< maximum allowed PE for bit factor calculation
|
|
|
|
float previous; ///< allowed PE of the previous frame
|
|
|
|
float correction; ///< PE correction factor
|
|
|
|
} pe;
|
2011-03-17 03:34:12 +00:00
|
|
|
AacPsyCoeffs psy_coef[2][64];
|
2010-08-23 19:27:24 +00:00
|
|
|
AacPsyChannel *ch;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
float global_quality; ///< normalized global quality taken from avctx
|
2010-08-23 19:27:24 +00:00
|
|
|
}AacPsyContext;
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2010-08-23 20:00:03 +00:00
|
|
|
/**
|
|
|
|
* LAME psy model preset struct
|
|
|
|
*/
|
2014-09-22 09:01:31 +00:00
|
|
|
typedef struct PsyLamePreset {
|
2010-08-23 20:00:03 +00:00
|
|
|
int quality; ///< Quality to map the rest of the vaules to.
|
|
|
|
/* This is overloaded to be both kbps per channel in ABR mode, and
|
|
|
|
* requested quality in constant quality mode.
|
|
|
|
*/
|
|
|
|
float st_lrm; ///< short threshold for L, R, and M channels
|
|
|
|
} PsyLamePreset;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LAME psy model preset table for ABR
|
|
|
|
*/
|
|
|
|
static const PsyLamePreset psy_abr_map[] = {
|
|
|
|
/* TODO: Tuning. These were taken from LAME. */
|
|
|
|
/* kbps/ch st_lrm */
|
|
|
|
{ 8, 6.60},
|
|
|
|
{ 16, 6.60},
|
|
|
|
{ 24, 6.60},
|
|
|
|
{ 32, 6.60},
|
|
|
|
{ 40, 6.60},
|
|
|
|
{ 48, 6.60},
|
|
|
|
{ 56, 6.60},
|
|
|
|
{ 64, 6.40},
|
|
|
|
{ 80, 6.00},
|
|
|
|
{ 96, 5.60},
|
|
|
|
{112, 5.20},
|
|
|
|
{128, 5.20},
|
|
|
|
{160, 5.20}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LAME psy model preset table for constant quality
|
|
|
|
*/
|
|
|
|
static const PsyLamePreset psy_vbr_map[] = {
|
|
|
|
/* vbr_q st_lrm */
|
|
|
|
{ 0, 4.20},
|
|
|
|
{ 1, 4.20},
|
|
|
|
{ 2, 4.20},
|
|
|
|
{ 3, 4.20},
|
|
|
|
{ 4, 4.20},
|
|
|
|
{ 5, 4.20},
|
|
|
|
{ 6, 4.20},
|
|
|
|
{ 7, 4.20},
|
|
|
|
{ 8, 4.20},
|
|
|
|
{ 9, 4.20},
|
|
|
|
{10, 4.20}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LAME psy model FIR coefficient table
|
|
|
|
*/
|
|
|
|
static const float psy_fir_coeffs[] = {
|
|
|
|
-8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
|
|
|
|
-3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
|
|
|
|
-5.52212e-17 * 2, -0.313819 * 2
|
|
|
|
};
|
|
|
|
|
2013-03-06 13:55:04 +00:00
|
|
|
#if ARCH_MIPS
|
|
|
|
# include "mips/aacpsy_mips.h"
|
|
|
|
#endif /* ARCH_MIPS */
|
|
|
|
|
2010-08-23 20:00:03 +00:00
|
|
|
/**
|
2011-12-07 12:03:53 +00:00
|
|
|
* Calculate the ABR attack threshold from the above LAME psymodel table.
|
2010-08-23 20:00:03 +00:00
|
|
|
*/
|
|
|
|
static float lame_calc_attack_threshold(int bitrate)
|
|
|
|
{
|
|
|
|
/* Assume max bitrate to start with */
|
|
|
|
int lower_range = 12, upper_range = 12;
|
|
|
|
int lower_range_kbps = psy_abr_map[12].quality;
|
|
|
|
int upper_range_kbps = psy_abr_map[12].quality;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Determine which bitrates the value specified falls between.
|
|
|
|
* If the loop ends without breaking our above assumption of 320kbps was correct.
|
|
|
|
*/
|
|
|
|
for (i = 1; i < 13; i++) {
|
|
|
|
if (FFMAX(bitrate, psy_abr_map[i].quality) != bitrate) {
|
|
|
|
upper_range = i;
|
|
|
|
upper_range_kbps = psy_abr_map[i ].quality;
|
|
|
|
lower_range = i - 1;
|
|
|
|
lower_range_kbps = psy_abr_map[i - 1].quality;
|
|
|
|
break; /* Upper range found */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine which range the value specified is closer to */
|
|
|
|
if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps))
|
|
|
|
return psy_abr_map[lower_range].st_lrm;
|
|
|
|
return psy_abr_map[upper_range].st_lrm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LAME psy model specific initialization
|
|
|
|
*/
|
2013-02-01 09:31:59 +00:00
|
|
|
static av_cold void lame_window_init(AacPsyContext *ctx, AVCodecContext *avctx)
|
|
|
|
{
|
2010-10-25 06:15:21 +00:00
|
|
|
int i, j;
|
2010-08-23 20:00:03 +00:00
|
|
|
|
|
|
|
for (i = 0; i < avctx->channels; i++) {
|
|
|
|
AacPsyChannel *pch = &ctx->ch[i];
|
|
|
|
|
2015-06-29 19:59:37 +00:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_QSCALE)
|
2010-08-23 20:00:03 +00:00
|
|
|
pch->attack_threshold = psy_vbr_map[avctx->global_quality / FF_QP2LAMBDA].st_lrm;
|
|
|
|
else
|
|
|
|
pch->attack_threshold = lame_calc_attack_threshold(avctx->bit_rate / avctx->channels / 1000);
|
|
|
|
|
2010-10-25 06:15:21 +00:00
|
|
|
for (j = 0; j < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; j++)
|
|
|
|
pch->prev_energy_subshort[j] = 10.0f;
|
2010-08-23 20:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-18 05:39:44 +00:00
|
|
|
/**
|
|
|
|
* Calculate Bark value for given line.
|
|
|
|
*/
|
2009-07-08 20:01:31 +00:00
|
|
|
static av_cold float calc_bark(float f)
|
2008-08-18 05:39:44 +00:00
|
|
|
{
|
|
|
|
return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
|
|
|
|
}
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
#define ATH_ADD 4
|
|
|
|
/**
|
|
|
|
* Calculate ATH value for given frequency.
|
|
|
|
* Borrowed from Lame.
|
|
|
|
*/
|
|
|
|
static av_cold float ath(float f, float add)
|
|
|
|
{
|
|
|
|
f /= 1000.0f;
|
2009-07-08 21:16:06 +00:00
|
|
|
return 3.64 * pow(f, -0.8)
|
2009-07-08 20:01:31 +00:00
|
|
|
- 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
|
|
|
|
+ 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
|
|
|
|
+ (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
|
|
|
|
}
|
|
|
|
|
2009-07-08 20:36:45 +00:00
|
|
|
static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
|
2010-08-23 19:27:24 +00:00
|
|
|
AacPsyContext *pctx;
|
2010-07-14 04:10:31 +00:00
|
|
|
float bark;
|
2009-07-08 20:01:31 +00:00
|
|
|
int i, j, g, start;
|
2011-01-05 08:32:16 +00:00
|
|
|
float prev, minscale, minath, minsnr, pe_min;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
int chan_bitrate = ctx->avctx->bit_rate / ((ctx->avctx->flags & CODEC_FLAG_QSCALE) ? 2.0f : ctx->avctx->channels);
|
|
|
|
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
const int bandwidth = ctx->cutoff ? ctx->cutoff : AAC_CUTOFF(ctx->avctx);
|
2011-01-05 08:32:16 +00:00
|
|
|
const float num_bark = calc_bark((float)bandwidth);
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2010-08-23 19:27:24 +00:00
|
|
|
ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
|
2015-05-31 12:45:54 +00:00
|
|
|
if (!ctx->model_priv_data)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-08-23 19:27:24 +00:00
|
|
|
pctx = (AacPsyContext*) ctx->model_priv_data;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
pctx->global_quality = (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120) * 0.01f;
|
|
|
|
|
|
|
|
if (ctx->avctx->flags & CODEC_FLAG_QSCALE) {
|
|
|
|
/* Use the target average bitrate to compute spread parameters */
|
|
|
|
chan_bitrate = (int)(chan_bitrate / 120.0 * (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120));
|
|
|
|
}
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2011-01-05 08:32:16 +00:00
|
|
|
pctx->chan_bitrate = chan_bitrate;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
pctx->frame_bits = FFMIN(2560, chan_bitrate * AAC_BLOCK_SIZE_LONG / ctx->avctx->sample_rate);
|
2011-01-05 08:32:16 +00:00
|
|
|
pctx->pe.min = 8.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
|
|
|
|
pctx->pe.max = 12.0f * AAC_BLOCK_SIZE_LONG * bandwidth / (ctx->avctx->sample_rate * 2.0f);
|
|
|
|
ctx->bitres.size = 6144 - pctx->frame_bits;
|
|
|
|
ctx->bitres.size -= ctx->bitres.size % 8;
|
|
|
|
pctx->fill_level = ctx->bitres.size;
|
2015-04-21 16:43:55 +00:00
|
|
|
minath = ath(3410 - 0.733 * ATH_ADD, ATH_ADD);
|
2009-07-08 20:36:45 +00:00
|
|
|
for (j = 0; j < 2; j++) {
|
2011-03-17 03:34:12 +00:00
|
|
|
AacPsyCoeffs *coeffs = pctx->psy_coef[j];
|
|
|
|
const uint8_t *band_sizes = ctx->bands[j];
|
2010-07-14 04:10:52 +00:00
|
|
|
float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
|
2013-05-04 21:35:49 +00:00
|
|
|
float avg_chan_bits = chan_bitrate * (j ? 128.0f : 1024.0f) / ctx->avctx->sample_rate;
|
2011-01-05 08:32:16 +00:00
|
|
|
/* reference encoder uses 2.4% here instead of 60% like the spec says */
|
|
|
|
float bark_pe = 0.024f * PSY_3GPP_BITS_TO_PE(avg_chan_bits) / num_bark;
|
|
|
|
float en_spread_low = j ? PSY_3GPP_EN_SPREAD_LOW_S : PSY_3GPP_EN_SPREAD_LOW_L;
|
|
|
|
/* High energy spreading for long blocks <= 22kbps/channel and short blocks are the same. */
|
|
|
|
float en_spread_hi = (j || (chan_bitrate <= 22.0f)) ? PSY_3GPP_EN_SPREAD_HI_S : PSY_3GPP_EN_SPREAD_HI_L1;
|
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
i = 0;
|
|
|
|
prev = 0.0;
|
2009-07-08 20:36:45 +00:00
|
|
|
for (g = 0; g < ctx->num_bands[j]; g++) {
|
2011-03-17 03:34:12 +00:00
|
|
|
i += band_sizes[g];
|
2010-07-14 04:10:47 +00:00
|
|
|
bark = calc_bark((i-1) * line_to_frequency);
|
2011-03-17 03:34:12 +00:00
|
|
|
coeffs[g].barks = (bark + prev) / 2.0;
|
2010-07-14 04:10:31 +00:00
|
|
|
prev = bark;
|
2009-07-08 20:01:31 +00:00
|
|
|
}
|
2009-07-08 20:36:45 +00:00
|
|
|
for (g = 0; g < ctx->num_bands[j] - 1; g++) {
|
2011-03-17 03:34:12 +00:00
|
|
|
AacPsyCoeffs *coeff = &coeffs[g];
|
|
|
|
float bark_width = coeffs[g+1].barks - coeffs->barks;
|
2011-03-17 03:35:39 +00:00
|
|
|
coeff->spread_low[0] = pow(10.0, -bark_width * PSY_3GPP_THR_SPREAD_LOW);
|
|
|
|
coeff->spread_hi [0] = pow(10.0, -bark_width * PSY_3GPP_THR_SPREAD_HI);
|
2011-01-05 08:32:16 +00:00
|
|
|
coeff->spread_low[1] = pow(10.0, -bark_width * en_spread_low);
|
|
|
|
coeff->spread_hi [1] = pow(10.0, -bark_width * en_spread_hi);
|
|
|
|
pe_min = bark_pe * bark_width;
|
2012-09-09 10:59:55 +00:00
|
|
|
minsnr = exp2(pe_min / band_sizes[g]) - 1.5f;
|
2011-01-05 08:32:16 +00:00
|
|
|
coeff->min_snr = av_clipf(1.0f / minsnr, PSY_SNR_25DB, PSY_SNR_1DB);
|
2009-07-08 20:01:31 +00:00
|
|
|
}
|
|
|
|
start = 0;
|
2009-07-08 20:36:45 +00:00
|
|
|
for (g = 0; g < ctx->num_bands[j]; g++) {
|
2010-07-14 04:10:47 +00:00
|
|
|
minscale = ath(start * line_to_frequency, ATH_ADD);
|
2011-03-17 03:34:12 +00:00
|
|
|
for (i = 1; i < band_sizes[g]; i++)
|
2010-07-14 04:10:47 +00:00
|
|
|
minscale = FFMIN(minscale, ath((start + i) * line_to_frequency, ATH_ADD));
|
2011-03-17 03:34:12 +00:00
|
|
|
coeffs[g].ath = minscale - minath;
|
|
|
|
start += band_sizes[g];
|
2009-07-08 20:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 12:34:31 +00:00
|
|
|
pctx->ch = av_mallocz_array(ctx->avctx->channels, sizeof(AacPsyChannel));
|
2015-05-31 12:45:54 +00:00
|
|
|
if (!pctx->ch) {
|
2015-05-31 23:32:24 +00:00
|
|
|
av_freep(&ctx->model_priv_data);
|
2015-05-31 12:45:54 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2010-08-23 20:00:03 +00:00
|
|
|
|
|
|
|
lame_window_init(pctx, ctx->avctx);
|
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IIR filter used in block switching decision
|
|
|
|
*/
|
|
|
|
static float iir_filter(int in, float state[2])
|
|
|
|
{
|
|
|
|
float ret;
|
|
|
|
|
|
|
|
ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
|
|
|
|
state[0] = in;
|
|
|
|
state[1] = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* window grouping information stored as bits (0 - new group, 1 - group continues)
|
|
|
|
*/
|
|
|
|
static const uint8_t window_grouping[9] = {
|
|
|
|
0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tell encoder which window types to use.
|
|
|
|
* @see 3GPP TS26.403 5.4.1 "Blockswitching"
|
|
|
|
*/
|
2011-06-24 08:49:59 +00:00
|
|
|
static av_unused FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
|
|
|
|
const int16_t *audio,
|
|
|
|
const int16_t *la,
|
|
|
|
int channel, int prev_type)
|
2009-07-08 20:01:31 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
int br = ((AacPsyContext*)ctx->model_priv_data)->chan_bitrate;
|
2009-07-08 21:16:06 +00:00
|
|
|
int attack_ratio = br <= 16000 ? 18 : 10;
|
2010-08-23 19:27:24 +00:00
|
|
|
AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
|
|
|
|
AacPsyChannel *pch = &pctx->ch[channel];
|
2009-07-08 21:16:06 +00:00
|
|
|
uint8_t grouping = 0;
|
2010-06-28 21:49:26 +00:00
|
|
|
int next_type = pch->next_window_seq;
|
2011-06-06 12:13:02 +00:00
|
|
|
FFPsyWindowInfo wi = { { 0 } };
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2009-07-08 20:36:45 +00:00
|
|
|
if (la) {
|
2009-07-08 20:01:31 +00:00
|
|
|
float s[8], v;
|
|
|
|
int switch_to_eight = 0;
|
|
|
|
float sum = 0.0, sum2 = 0.0;
|
|
|
|
int attack_n = 0;
|
2010-06-28 21:49:26 +00:00
|
|
|
int stay_short = 0;
|
2009-07-08 20:36:45 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
for (j = 0; j < 128; j++) {
|
2011-12-15 02:43:56 +00:00
|
|
|
v = iir_filter(la[i*128+j], pch->iir_state);
|
2009-07-08 20:01:31 +00:00
|
|
|
sum += v*v;
|
|
|
|
}
|
2009-07-08 21:16:06 +00:00
|
|
|
s[i] = sum;
|
2009-07-08 20:01:31 +00:00
|
|
|
sum2 += sum;
|
|
|
|
}
|
2009-07-08 20:36:45 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (s[i] > pch->win_energy * attack_ratio) {
|
2009-07-08 21:16:06 +00:00
|
|
|
attack_n = i + 1;
|
2009-07-08 20:01:31 +00:00
|
|
|
switch_to_eight = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pch->win_energy = pch->win_energy*7/8 + sum2/64;
|
|
|
|
|
|
|
|
wi.window_type[1] = prev_type;
|
2009-07-08 20:36:45 +00:00
|
|
|
switch (prev_type) {
|
2009-07-08 20:01:31 +00:00
|
|
|
case ONLY_LONG_SEQUENCE:
|
|
|
|
wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
|
2010-06-28 21:49:26 +00:00
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
|
2009-07-08 20:01:31 +00:00
|
|
|
break;
|
|
|
|
case LONG_START_SEQUENCE:
|
|
|
|
wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
|
|
|
|
grouping = pch->next_grouping;
|
2010-06-28 21:49:26 +00:00
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
|
2009-07-08 20:01:31 +00:00
|
|
|
break;
|
|
|
|
case LONG_STOP_SEQUENCE:
|
2010-06-28 21:49:26 +00:00
|
|
|
wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
|
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
|
2009-07-08 20:01:31 +00:00
|
|
|
break;
|
|
|
|
case EIGHT_SHORT_SEQUENCE:
|
2010-06-28 21:49:26 +00:00
|
|
|
stay_short = next_type == EIGHT_SHORT_SEQUENCE || switch_to_eight;
|
|
|
|
wi.window_type[0] = stay_short ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
|
|
|
|
grouping = next_type == EIGHT_SHORT_SEQUENCE ? pch->next_grouping : 0;
|
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
|
2009-07-08 20:01:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-06-28 21:49:26 +00:00
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
pch->next_grouping = window_grouping[attack_n];
|
2010-06-28 21:49:26 +00:00
|
|
|
pch->next_window_seq = next_type;
|
2009-07-08 20:36:45 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < 3; i++)
|
2009-07-08 20:01:31 +00:00
|
|
|
wi.window_type[i] = prev_type;
|
|
|
|
grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wi.window_shape = 1;
|
2009-07-08 20:36:45 +00:00
|
|
|
if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
|
2009-07-08 20:01:31 +00:00
|
|
|
wi.num_windows = 1;
|
|
|
|
wi.grouping[0] = 1;
|
2009-07-08 20:36:45 +00:00
|
|
|
} else {
|
2009-07-08 20:01:31 +00:00
|
|
|
int lastgrp = 0;
|
|
|
|
wi.num_windows = 8;
|
2009-07-08 20:36:45 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (!((grouping >> i) & 1))
|
2009-07-08 20:01:31 +00:00
|
|
|
lastgrp = i;
|
|
|
|
wi.grouping[lastgrp]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wi;
|
|
|
|
}
|
|
|
|
|
2011-01-05 08:32:16 +00:00
|
|
|
/* 5.6.1.2 "Calculation of Bit Demand" */
|
|
|
|
static int calc_bit_demand(AacPsyContext *ctx, float pe, int bits, int size,
|
|
|
|
int short_window)
|
|
|
|
{
|
|
|
|
const float bitsave_slope = short_window ? PSY_3GPP_SAVE_SLOPE_S : PSY_3GPP_SAVE_SLOPE_L;
|
|
|
|
const float bitsave_add = short_window ? PSY_3GPP_SAVE_ADD_S : PSY_3GPP_SAVE_ADD_L;
|
|
|
|
const float bitspend_slope = short_window ? PSY_3GPP_SPEND_SLOPE_S : PSY_3GPP_SPEND_SLOPE_L;
|
|
|
|
const float bitspend_add = short_window ? PSY_3GPP_SPEND_ADD_S : PSY_3GPP_SPEND_ADD_L;
|
|
|
|
const float clip_low = short_window ? PSY_3GPP_CLIP_LO_S : PSY_3GPP_CLIP_LO_L;
|
|
|
|
const float clip_high = short_window ? PSY_3GPP_CLIP_HI_S : PSY_3GPP_CLIP_HI_L;
|
2015-11-29 18:03:45 +00:00
|
|
|
float clipped_pe, bit_save, bit_spend, bit_factor, fill_level, forgetful_min_pe;
|
2011-01-05 08:32:16 +00:00
|
|
|
|
|
|
|
ctx->fill_level += ctx->frame_bits - bits;
|
|
|
|
ctx->fill_level = av_clip(ctx->fill_level, 0, size);
|
|
|
|
fill_level = av_clipf((float)ctx->fill_level / size, clip_low, clip_high);
|
|
|
|
clipped_pe = av_clipf(pe, ctx->pe.min, ctx->pe.max);
|
|
|
|
bit_save = (fill_level + bitsave_add) * bitsave_slope;
|
|
|
|
assert(bit_save <= 0.3f && bit_save >= -0.05000001f);
|
|
|
|
bit_spend = (fill_level + bitspend_add) * bitspend_slope;
|
|
|
|
assert(bit_spend <= 0.5f && bit_spend >= -0.1f);
|
|
|
|
/* The bit factor graph in the spec is obviously incorrect.
|
|
|
|
* bit_spend + ((bit_spend - bit_spend))...
|
|
|
|
* The reference encoder subtracts everything from 1, but also seems incorrect.
|
|
|
|
* 1 - bit_save + ((bit_spend + bit_save))...
|
|
|
|
* Hopefully below is correct.
|
|
|
|
*/
|
|
|
|
bit_factor = 1.0f - bit_save + ((bit_spend - bit_save) / (ctx->pe.max - ctx->pe.min)) * (clipped_pe - ctx->pe.min);
|
2015-11-29 18:03:45 +00:00
|
|
|
/* NOTE: The reference encoder attempts to center pe max/min around the current pe.
|
|
|
|
* Here we do that by slowly forgetting pe.min when pe stays in a range that makes
|
|
|
|
* it unlikely (ie: above the mean)
|
|
|
|
*/
|
2011-01-05 08:32:16 +00:00
|
|
|
ctx->pe.max = FFMAX(pe, ctx->pe.max);
|
2015-11-29 18:03:45 +00:00
|
|
|
forgetful_min_pe = ((ctx->pe.min * PSY_PE_FORGET_SLOPE)
|
|
|
|
+ FFMAX(ctx->pe.min, pe * (pe / ctx->pe.max))) / (PSY_PE_FORGET_SLOPE + 1);
|
|
|
|
ctx->pe.min = FFMIN(pe, forgetful_min_pe);
|
2011-01-05 08:32:16 +00:00
|
|
|
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
/* NOTE: allocate a minimum of 1/8th average frame bits, to avoid
|
|
|
|
* reservoir starvation from producing zero-bit frames
|
|
|
|
*/
|
|
|
|
return FFMIN(
|
|
|
|
ctx->frame_bits * bit_factor,
|
|
|
|
FFMAX(ctx->frame_bits + size - bits, ctx->frame_bits / 8));
|
2011-01-05 08:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static float calc_pe_3gpp(AacPsyBand *band)
|
|
|
|
{
|
|
|
|
float pe, a;
|
|
|
|
|
|
|
|
band->pe = 0.0f;
|
|
|
|
band->pe_const = 0.0f;
|
|
|
|
band->active_lines = 0.0f;
|
|
|
|
if (band->energy > band->thr) {
|
|
|
|
a = log2f(band->energy);
|
|
|
|
pe = a - log2f(band->thr);
|
|
|
|
band->active_lines = band->nz_lines;
|
|
|
|
if (pe < PSY_3GPP_C1) {
|
|
|
|
pe = pe * PSY_3GPP_C3 + PSY_3GPP_C2;
|
|
|
|
a = a * PSY_3GPP_C3 + PSY_3GPP_C2;
|
|
|
|
band->active_lines *= PSY_3GPP_C3;
|
|
|
|
}
|
|
|
|
band->pe = pe * band->nz_lines;
|
|
|
|
band->pe_const = a * band->nz_lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
return band->pe;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float calc_reduction_3gpp(float a, float desired_pe, float pe,
|
|
|
|
float active_lines)
|
|
|
|
{
|
|
|
|
float thr_avg, reduction;
|
|
|
|
|
2012-09-14 01:26:19 +00:00
|
|
|
if(active_lines == 0.0)
|
|
|
|
return 0;
|
|
|
|
|
2012-09-09 10:59:55 +00:00
|
|
|
thr_avg = exp2f((a - pe) / (4.0f * active_lines));
|
|
|
|
reduction = exp2f((a - desired_pe) / (4.0f * active_lines)) - thr_avg;
|
2011-01-05 08:32:16 +00:00
|
|
|
|
|
|
|
return FFMAX(reduction, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static float calc_reduced_thr_3gpp(AacPsyBand *band, float min_snr,
|
|
|
|
float reduction)
|
|
|
|
{
|
|
|
|
float thr = band->thr;
|
|
|
|
|
|
|
|
if (band->energy > thr) {
|
2013-03-06 13:55:03 +00:00
|
|
|
thr = sqrtf(thr);
|
|
|
|
thr = sqrtf(thr) + reduction;
|
|
|
|
thr *= thr;
|
|
|
|
thr *= thr;
|
2011-01-05 08:32:16 +00:00
|
|
|
|
|
|
|
/* This deviates from the 3GPP spec to match the reference encoder.
|
|
|
|
* It performs min(thr_reduced, max(thr, energy/min_snr)) only for bands
|
|
|
|
* that have hole avoidance on (active or inactive). It always reduces the
|
|
|
|
* threshold of bands with hole avoidance off.
|
|
|
|
*/
|
|
|
|
if (thr > band->energy * min_snr && band->avoid_holes != PSY_3GPP_AH_NONE) {
|
|
|
|
thr = FFMAX(band->thr, band->energy * min_snr);
|
|
|
|
band->avoid_holes = PSY_3GPP_AH_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return thr;
|
|
|
|
}
|
|
|
|
|
2013-03-06 13:55:04 +00:00
|
|
|
#ifndef calc_thr_3gpp
|
|
|
|
static void calc_thr_3gpp(const FFPsyWindowInfo *wi, const int num_bands, AacPsyChannel *pch,
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
const uint8_t *band_sizes, const float *coefs, const int cutoff)
|
2009-07-08 20:01:31 +00:00
|
|
|
{
|
|
|
|
int i, w, g;
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
int start = 0, wstart = 0;
|
2009-07-08 20:36:45 +00:00
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
wstart = 0;
|
2009-07-08 20:36:45 +00:00
|
|
|
for (g = 0; g < num_bands; g++) {
|
2010-08-23 19:27:24 +00:00
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
2011-04-05 07:05:24 +00:00
|
|
|
|
2011-01-05 08:32:16 +00:00
|
|
|
float form_factor = 0.0f;
|
2013-03-06 13:55:03 +00:00
|
|
|
float Temp;
|
2009-07-08 20:01:31 +00:00
|
|
|
band->energy = 0.0f;
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
if (wstart < cutoff) {
|
|
|
|
for (i = 0; i < band_sizes[g]; i++) {
|
|
|
|
band->energy += coefs[start+i] * coefs[start+i];
|
|
|
|
form_factor += sqrtf(fabs(coefs[start+i]));
|
|
|
|
}
|
2011-01-05 08:32:16 +00:00
|
|
|
}
|
2013-03-06 13:55:03 +00:00
|
|
|
Temp = band->energy > 0 ? sqrtf((float)band_sizes[g] / band->energy) : 0;
|
2011-04-05 07:05:24 +00:00
|
|
|
band->thr = band->energy * 0.001258925f;
|
2013-03-06 13:55:03 +00:00
|
|
|
band->nz_lines = form_factor * sqrtf(Temp);
|
2011-01-05 08:32:16 +00:00
|
|
|
|
2011-04-05 07:05:24 +00:00
|
|
|
start += band_sizes[g];
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
wstart += band_sizes[g];
|
2009-07-08 20:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-06 13:55:04 +00:00
|
|
|
}
|
|
|
|
#endif /* calc_thr_3gpp */
|
|
|
|
|
|
|
|
#ifndef psy_hp_filter
|
|
|
|
static void psy_hp_filter(const float *firbuf, float *hpfsmpl, const float *psy_fir_coeffs)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
|
|
|
|
float sum1, sum2;
|
|
|
|
sum1 = firbuf[i + (PSY_LAME_FIR_LEN - 1) / 2];
|
|
|
|
sum2 = 0.0;
|
|
|
|
for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
|
|
|
|
sum1 += psy_fir_coeffs[j] * (firbuf[i + j] + firbuf[i + PSY_LAME_FIR_LEN - j]);
|
|
|
|
sum2 += psy_fir_coeffs[j + 1] * (firbuf[i + j + 1] + firbuf[i + PSY_LAME_FIR_LEN - j - 1]);
|
|
|
|
}
|
2013-05-04 08:17:52 +00:00
|
|
|
/* NOTE: The LAME psymodel expects it's input in the range -32768 to 32768.
|
|
|
|
* Tuning this for normalized floats would be difficult. */
|
2013-03-06 13:55:04 +00:00
|
|
|
hpfsmpl[i] = (sum1 + sum2) * 32768.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* psy_hp_filter */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate band thresholds as suggested in 3GPP TS26.403
|
|
|
|
*/
|
|
|
|
static void psy_3gpp_analyze_channel(FFPsyContext *ctx, int channel,
|
|
|
|
const float *coefs, const FFPsyWindowInfo *wi)
|
|
|
|
{
|
|
|
|
AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
|
|
|
|
AacPsyChannel *pch = &pctx->ch[channel];
|
|
|
|
int i, w, g;
|
|
|
|
float desired_bits, desired_pe, delta_pe, reduction= NAN, spread_en[128] = {0};
|
|
|
|
float a = 0.0f, active_lines = 0.0f, norm_fac = 0.0f;
|
|
|
|
float pe = pctx->chan_bitrate > 32000 ? 0.0f : FFMAX(50.0f, 100.0f - pctx->chan_bitrate * 100.0f / 32000.0f);
|
|
|
|
const int num_bands = ctx->num_bands[wi->num_windows == 8];
|
|
|
|
const uint8_t *band_sizes = ctx->bands[wi->num_windows == 8];
|
|
|
|
AacPsyCoeffs *coeffs = pctx->psy_coef[wi->num_windows == 8];
|
|
|
|
const float avoid_hole_thr = wi->num_windows == 8 ? PSY_3GPP_AH_THR_SHORT : PSY_3GPP_AH_THR_LONG;
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
const int bandwidth = ctx->cutoff ? ctx->cutoff : AAC_CUTOFF(ctx->avctx);
|
|
|
|
const int cutoff = bandwidth * 2048 / wi->num_windows / ctx->avctx->sample_rate;
|
2013-03-06 13:55:04 +00:00
|
|
|
|
|
|
|
//calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
|
AAC encoder: improve SF range utilization
This patch does 4 things, all of which interact and thus it
woudln't be possible to commit them separately without causing
either quality regressions or assertion failures.
Fate comparison targets don't all reflect improvements in
quality, yet listening tests show substantially improved quality
and stability.
1. Increase SF range utilization.
The spec requires SF delta values to be constrained within the
range -60..60. The previous code was applying that range to
the whole SF array and not only the deltas of consecutive values,
because doing so requires smarter code: zeroing or otherwise
skipping a band may invalidate lots of SF choices.
This patch implements that logic to allow the coders to utilize
the full dynamic range of scalefactors, increasing quality quite
considerably, and fixing delta-SF-related assertion failures,
since now the limitation is enforced rather than asserted.
2. PNS tweaks
The previous modification makes big improvements in twoloop's
efficiency, and every time that happens PNS logic needs to be
tweaked accordingly to avoid it from stepping all over twoloop's
decisions. This patch includes modifications of the sort.
3. Account for lowpass cutoff during PSY analysis
The closer PSY's allocation is to final allocation the better
the quality is, and given these modifications, twoloop is now
very efficient at avoiding holes. Thus, to compute accurate
thresholds, PSY needs to account for the lowpass applied
implicitly during twoloop (by zeroing high bands).
This patch makes twoloop set the cutoff in psymodel's context
the first time it runs, and makes PSY account for it during
threshold computation, making PE and threshold computations
closer to the final allocation and thus achieving better
subjective quality.
4. Tweaks to RC lambda tracking loop in relation to PNS
Without this tweak some corner cases cause quality regressions.
Basically, lambda needs to react faster to overall bitrate
efficiency changes since now PNS can be quite successful in
enforcing maximum bitrates, when PSY allocates too many bits
to the lower bands, suppressing the signals RC logic uses to
lower lambda in those cases and causing aggressive PNS.
This tweak makes PNS much less aggressive, though it can still
use some further tweaks.
Also update MIPS specializations and adjust fuzz
Also in lavc/mips/aacpsy_mips.h: remove trailing whitespace
2015-12-01 06:28:36 +00:00
|
|
|
calc_thr_3gpp(wi, num_bands, pch, band_sizes, coefs, cutoff);
|
2013-03-06 13:55:04 +00:00
|
|
|
|
2011-03-17 03:35:39 +00:00
|
|
|
//modify thresholds and energies - spread, threshold in quiet, pre-echo control
|
2009-07-08 20:36:45 +00:00
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
2011-03-17 03:34:12 +00:00
|
|
|
AacPsyBand *bands = &pch->band[w];
|
2011-04-05 07:05:24 +00:00
|
|
|
|
2012-12-19 17:48:21 +00:00
|
|
|
/* 5.4.2.3 "Spreading" & 5.4.3 "Spread Energy Calculation" */
|
2011-01-05 08:32:16 +00:00
|
|
|
spread_en[0] = bands[0].energy;
|
|
|
|
for (g = 1; g < num_bands; g++) {
|
2011-04-05 07:05:24 +00:00
|
|
|
bands[g].thr = FFMAX(bands[g].thr, bands[g-1].thr * coeffs[g].spread_hi[0]);
|
2011-01-05 08:32:16 +00:00
|
|
|
spread_en[w+g] = FFMAX(bands[g].energy, spread_en[w+g-1] * coeffs[g].spread_hi[1]);
|
|
|
|
}
|
|
|
|
for (g = num_bands - 2; g >= 0; g--) {
|
2011-04-05 07:05:24 +00:00
|
|
|
bands[g].thr = FFMAX(bands[g].thr, bands[g+1].thr * coeffs[g].spread_low[0]);
|
2011-01-05 08:32:16 +00:00
|
|
|
spread_en[w+g] = FFMAX(spread_en[w+g], spread_en[w+g+1] * coeffs[g].spread_low[1]);
|
|
|
|
}
|
2011-03-17 03:35:39 +00:00
|
|
|
//5.4.2.4 "Threshold in quiet"
|
2009-07-08 20:36:45 +00:00
|
|
|
for (g = 0; g < num_bands; g++) {
|
2011-03-17 03:34:12 +00:00
|
|
|
AacPsyBand *band = &bands[g];
|
2011-04-05 07:05:24 +00:00
|
|
|
|
2011-03-17 03:34:12 +00:00
|
|
|
band->thr_quiet = band->thr = FFMAX(band->thr, coeffs[g].ath);
|
2011-03-17 03:35:39 +00:00
|
|
|
//5.4.2.5 "Pre-echo control"
|
2010-10-27 05:54:41 +00:00
|
|
|
if (!(wi->window_type[0] == LONG_STOP_SEQUENCE || (wi->window_type[1] == LONG_START_SEQUENCE && !w)))
|
2011-03-17 03:34:12 +00:00
|
|
|
band->thr = FFMAX(PSY_3GPP_RPEMIN*band->thr, FFMIN(band->thr,
|
|
|
|
PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
|
2011-01-05 08:32:16 +00:00
|
|
|
|
2012-12-19 17:48:21 +00:00
|
|
|
/* 5.6.1.3.1 "Preparatory steps of the perceptual entropy calculation" */
|
2011-01-05 08:32:16 +00:00
|
|
|
pe += calc_pe_3gpp(band);
|
|
|
|
a += band->pe_const;
|
|
|
|
active_lines += band->active_lines;
|
|
|
|
|
|
|
|
/* 5.6.1.3.3 "Selection of the bands for avoidance of holes" */
|
|
|
|
if (spread_en[w+g] * avoid_hole_thr > band->energy || coeffs[g].min_snr > 1.0f)
|
|
|
|
band->avoid_holes = PSY_3GPP_AH_NONE;
|
|
|
|
else
|
|
|
|
band->avoid_holes = PSY_3GPP_AH_INACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5.6.1.3.2 "Calculation of the desired perceptual entropy" */
|
2011-06-15 08:50:25 +00:00
|
|
|
ctx->ch[channel].entropy = pe;
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
if (ctx->avctx->flags & CODEC_FLAG_QSCALE) {
|
|
|
|
/* (2.5 * 120) achieves almost transparent rate, and we want to give
|
|
|
|
* ample room downwards, so we make that equivalent to QSCALE=2.4
|
|
|
|
*/
|
|
|
|
desired_pe = pe * (ctx->avctx->global_quality ? ctx->avctx->global_quality : 120) / (2 * 2.5f * 120.0f);
|
|
|
|
desired_bits = FFMIN(2560, PSY_3GPP_PE_TO_BITS(desired_pe));
|
|
|
|
desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits); // reflect clipping
|
|
|
|
|
|
|
|
/* PE slope smoothing */
|
|
|
|
if (ctx->bitres.bits > 0) {
|
|
|
|
desired_bits = FFMIN(2560, PSY_3GPP_PE_TO_BITS(desired_pe));
|
|
|
|
desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits); // reflect clipping
|
|
|
|
}
|
|
|
|
|
|
|
|
pctx->pe.max = FFMAX(pe, pctx->pe.max);
|
|
|
|
pctx->pe.min = FFMIN(pe, pctx->pe.min);
|
|
|
|
} else {
|
2015-10-11 21:06:02 +00:00
|
|
|
desired_bits = calc_bit_demand(pctx, pe, ctx->bitres.bits, ctx->bitres.size, wi->num_windows == 8);
|
|
|
|
desired_pe = PSY_3GPP_BITS_TO_PE(desired_bits);
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
|
2015-10-11 21:06:02 +00:00
|
|
|
/* NOTE: PE correction is kept simple. During initial testing it had very
|
|
|
|
* little effect on the final bitrate. Probably a good idea to come
|
|
|
|
* back and do more testing later.
|
|
|
|
*/
|
|
|
|
if (ctx->bitres.bits > 0)
|
|
|
|
desired_pe *= av_clipf(pctx->pe.previous / PSY_3GPP_BITS_TO_PE(ctx->bitres.bits),
|
|
|
|
0.85f, 1.15f);
|
AAC encoder: Extensive improvements
This finalizes merging of the work in the patches in ticket #2686.
Improvements to twoloop and RC logic are extensive.
The non-exhaustive list of twoloop improvments includes:
- Tweaks to distortion limits on the RD optimization phase of twoloop
- Deeper search in twoloop
- PNS information marking to let twoloop decide when to use it
(turned out having the decision made separately wasn't working)
- Tonal band detection and priorization
- Better band energy conservation rules
- Strict hole avoidance
For rate control:
- Use psymodel's bit allocation to allow proper use of the bit
reservoir. Don't work against the bit reservoir by moving lambda
in the opposite direction when psymodel decides to allocate more/less
bits to a frame.
- Retry the encode if the effective rate lies outside a reasonable
margin of psymodel's allocation or the selected ABR.
- Log average lambda at the end. Useful info for everyone, but especially
for tuning of the various encoder constants that relate to lambda
feedback.
Psy:
- Do not apply lowpass with a FIR filter, instead just let the coder
zero bands above the cutoff. The FIR filter induces group delay,
and while zeroing bands causes ripple, it's lost in the quantization
noise.
- Experimental VBR bit allocation code
- Tweak automatic lowpass filter threshold to maximize audio bandwidth
at all bitrates while still providing acceptable, stable quality.
I/S:
- Phase decision fixes. Unrelated to #2686, but the bugs only surfaced
when the merge was finalized. Measure I/S band energy accounting for
phase, and prevent I/S and M/S from being applied both.
PNS:
- Avoid marking short bands with PNS when they're part of a window
group in which there's a large variation of energy from one window
to the next. PNS can't preserve those and the effect is extremely
noticeable.
M/S:
- Implement BMLD protection similar to the specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Since M/S decision
doesn't conform to section 6.1, a different method had to be
implemented, but should provide equivalent protection.
- Move the decision logic closer to the method specified in
ISO-IEC/13818:7-2003, Appendix C Section 6.1. Specifically,
make sure M/S needs less bits than dual stereo.
- Don't apply M/S in bands that are using I/S
Now, this of course needed adjustments in the compare targets and
fuzz factors of the AAC encoder's fate tests, but if wondering why
the targets go up (more distortion), consider the previous coder
was using too many bits on LF content (far more than required by
psy), and thus those signals will now be more distorted, not less.
The extra distortion isn't audible though, I carried extensive
ABX testing to make sure.
A very similar patch was also extensively tested by Kamendo2 in
the context of #2686.
2015-10-11 20:29:50 +00:00
|
|
|
}
|
2011-01-05 08:32:16 +00:00
|
|
|
pctx->pe.previous = PSY_3GPP_BITS_TO_PE(desired_bits);
|
2015-09-23 05:13:56 +00:00
|
|
|
ctx->bitres.alloc = desired_bits;
|
2011-01-05 08:32:16 +00:00
|
|
|
|
|
|
|
if (desired_pe < pe) {
|
|
|
|
/* 5.6.1.3.4 "First Estimation of the reduction value" */
|
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
|
|
|
reduction = calc_reduction_3gpp(a, desired_pe, pe, active_lines);
|
|
|
|
pe = 0.0f;
|
|
|
|
a = 0.0f;
|
|
|
|
active_lines = 0.0f;
|
|
|
|
for (g = 0; g < num_bands; g++) {
|
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
|
|
|
|
|
|
|
band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
|
|
|
|
/* recalculate PE */
|
|
|
|
pe += calc_pe_3gpp(band);
|
|
|
|
a += band->pe_const;
|
|
|
|
active_lines += band->active_lines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5.6.1.3.5 "Second Estimation of the reduction value" */
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
float pe_no_ah = 0.0f, desired_pe_no_ah;
|
|
|
|
active_lines = a = 0.0f;
|
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
|
|
|
for (g = 0; g < num_bands; g++) {
|
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
|
|
|
|
|
|
|
if (band->avoid_holes != PSY_3GPP_AH_ACTIVE) {
|
|
|
|
pe_no_ah += band->pe;
|
|
|
|
a += band->pe_const;
|
|
|
|
active_lines += band->active_lines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
desired_pe_no_ah = FFMAX(desired_pe - (pe - pe_no_ah), 0.0f);
|
|
|
|
if (active_lines > 0.0f)
|
2015-03-01 23:11:12 +00:00
|
|
|
reduction = calc_reduction_3gpp(a, desired_pe_no_ah, pe_no_ah, active_lines);
|
2011-01-05 08:32:16 +00:00
|
|
|
|
|
|
|
pe = 0.0f;
|
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
|
|
|
for (g = 0; g < num_bands; g++) {
|
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
|
|
|
|
|
|
|
if (active_lines > 0.0f)
|
|
|
|
band->thr = calc_reduced_thr_3gpp(band, coeffs[g].min_snr, reduction);
|
|
|
|
pe += calc_pe_3gpp(band);
|
2015-04-16 18:04:54 +00:00
|
|
|
if (band->thr > 0.0f)
|
|
|
|
band->norm_fac = band->active_lines / band->thr;
|
|
|
|
else
|
|
|
|
band->norm_fac = 0.0f;
|
2011-01-05 08:32:16 +00:00
|
|
|
norm_fac += band->norm_fac;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delta_pe = desired_pe - pe;
|
|
|
|
if (fabs(delta_pe) > 0.05f * desired_pe)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pe < 1.15f * desired_pe) {
|
|
|
|
/* 6.6.1.3.6 "Final threshold modification by linearization" */
|
|
|
|
norm_fac = 1.0f / norm_fac;
|
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
|
|
|
for (g = 0; g < num_bands; g++) {
|
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
|
|
|
|
|
|
|
if (band->active_lines > 0.5f) {
|
|
|
|
float delta_sfb_pe = band->norm_fac * norm_fac * delta_pe;
|
|
|
|
float thr = band->thr;
|
|
|
|
|
2012-09-09 10:59:55 +00:00
|
|
|
thr *= exp2f(delta_sfb_pe / band->active_lines);
|
2011-01-05 08:32:16 +00:00
|
|
|
if (thr > coeffs[g].min_snr * band->energy && band->avoid_holes == PSY_3GPP_AH_INACTIVE)
|
|
|
|
thr = FFMAX(band->thr, coeffs[g].min_snr * band->energy);
|
|
|
|
band->thr = thr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 5.6.1.3.7 "Further perceptual entropy reduction" */
|
|
|
|
g = num_bands;
|
|
|
|
while (pe > desired_pe && g--) {
|
|
|
|
for (w = 0; w < wi->num_windows*16; w+= 16) {
|
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
|
|
|
if (band->avoid_holes != PSY_3GPP_AH_NONE && coeffs[g].min_snr < PSY_SNR_1DB) {
|
|
|
|
coeffs[g].min_snr = PSY_SNR_1DB;
|
|
|
|
band->thr = band->energy * PSY_SNR_1DB;
|
|
|
|
pe += band->active_lines * 1.5f - band->pe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* TODO: allow more holes (unused without mid/side) */
|
2011-03-17 03:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2011-03-17 03:34:12 +00:00
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
|
|
|
for (g = 0; g < num_bands; g++) {
|
|
|
|
AacPsyBand *band = &pch->band[w+g];
|
2011-06-15 08:50:25 +00:00
|
|
|
FFPsyBand *psy_band = &ctx->ch[channel].psy_bands[w+g];
|
2011-03-17 03:34:12 +00:00
|
|
|
|
|
|
|
psy_band->threshold = band->thr;
|
|
|
|
psy_band->energy = band->energy;
|
2015-07-02 18:13:00 +00:00
|
|
|
psy_band->spread = band->active_lines * 2.0f / band_sizes[g];
|
2015-09-23 05:13:56 +00:00
|
|
|
psy_band->bits = PSY_3GPP_PE_TO_BITS(band->pe);
|
2009-07-08 20:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-17 03:35:39 +00:00
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
memcpy(pch->prev_band, pch->band, sizeof(pch->band));
|
|
|
|
}
|
|
|
|
|
2011-05-19 05:23:22 +00:00
|
|
|
static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
|
2011-05-19 05:14:59 +00:00
|
|
|
const float **coeffs, const FFPsyWindowInfo *wi)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
FFPsyChannelGroup *group = ff_psy_find_group(ctx, channel);
|
|
|
|
|
|
|
|
for (ch = 0; ch < group->num_ch; ch++)
|
2011-05-19 05:23:22 +00:00
|
|
|
psy_3gpp_analyze_channel(ctx, channel + ch, coeffs[ch], &wi[ch]);
|
2011-05-19 05:14:59 +00:00
|
|
|
}
|
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
static av_cold void psy_3gpp_end(FFPsyContext *apc)
|
|
|
|
{
|
2010-08-23 19:27:24 +00:00
|
|
|
AacPsyContext *pctx = (AacPsyContext*) apc->model_priv_data;
|
2009-07-08 20:01:31 +00:00
|
|
|
av_freep(&pctx->ch);
|
|
|
|
av_freep(&apc->model_priv_data);
|
|
|
|
}
|
|
|
|
|
2010-08-23 20:00:03 +00:00
|
|
|
static void lame_apply_block_type(AacPsyChannel *ctx, FFPsyWindowInfo *wi, int uselongblock)
|
|
|
|
{
|
|
|
|
int blocktype = ONLY_LONG_SEQUENCE;
|
|
|
|
if (uselongblock) {
|
|
|
|
if (ctx->next_window_seq == EIGHT_SHORT_SEQUENCE)
|
|
|
|
blocktype = LONG_STOP_SEQUENCE;
|
|
|
|
} else {
|
|
|
|
blocktype = EIGHT_SHORT_SEQUENCE;
|
|
|
|
if (ctx->next_window_seq == ONLY_LONG_SEQUENCE)
|
|
|
|
ctx->next_window_seq = LONG_START_SEQUENCE;
|
|
|
|
if (ctx->next_window_seq == LONG_STOP_SEQUENCE)
|
|
|
|
ctx->next_window_seq = EIGHT_SHORT_SEQUENCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wi->window_type[0] = ctx->next_window_seq;
|
|
|
|
ctx->next_window_seq = blocktype;
|
|
|
|
}
|
|
|
|
|
2011-12-18 01:45:55 +00:00
|
|
|
static FFPsyWindowInfo psy_lame_window(FFPsyContext *ctx, const float *audio,
|
|
|
|
const float *la, int channel, int prev_type)
|
2010-08-23 20:00:03 +00:00
|
|
|
{
|
|
|
|
AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
|
|
|
|
AacPsyChannel *pch = &pctx->ch[channel];
|
|
|
|
int grouping = 0;
|
|
|
|
int uselongblock = 1;
|
|
|
|
int attacks[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
|
2015-07-21 01:53:24 +00:00
|
|
|
float clippings[AAC_NUM_BLOCKS_SHORT];
|
2010-08-23 20:00:03 +00:00
|
|
|
int i;
|
2011-06-06 12:13:02 +00:00
|
|
|
FFPsyWindowInfo wi = { { 0 } };
|
2010-08-23 20:00:03 +00:00
|
|
|
|
|
|
|
if (la) {
|
|
|
|
float hpfsmpl[AAC_BLOCK_SIZE_LONG];
|
|
|
|
float const *pf = hpfsmpl;
|
|
|
|
float attack_intensity[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
|
|
|
|
float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
|
|
|
|
float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
|
2011-12-15 02:43:56 +00:00
|
|
|
const float *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN);
|
2013-03-06 13:55:04 +00:00
|
|
|
int att_sum = 0;
|
2010-08-23 20:00:03 +00:00
|
|
|
|
|
|
|
/* LAME comment: apply high pass filter of fs/4 */
|
2013-03-06 13:55:04 +00:00
|
|
|
psy_hp_filter(firbuf, hpfsmpl, psy_fir_coeffs);
|
2010-08-23 20:00:03 +00:00
|
|
|
|
|
|
|
/* Calculate the energies of each sub-shortblock */
|
|
|
|
for (i = 0; i < PSY_LAME_NUM_SUBBLOCKS; i++) {
|
|
|
|
energy_subshort[i] = pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 1) * PSY_LAME_NUM_SUBBLOCKS)];
|
|
|
|
assert(pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)] > 0);
|
|
|
|
attack_intensity[i] = energy_subshort[i] / pch->prev_energy_subshort[i + ((AAC_NUM_BLOCKS_SHORT - 2) * PSY_LAME_NUM_SUBBLOCKS + 1)];
|
|
|
|
energy_short[0] += energy_subshort[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS; i++) {
|
|
|
|
float const *const pfe = pf + AAC_BLOCK_SIZE_LONG / (AAC_NUM_BLOCKS_SHORT * PSY_LAME_NUM_SUBBLOCKS);
|
|
|
|
float p = 1.0f;
|
|
|
|
for (; pf < pfe; pf++)
|
2012-01-07 21:21:35 +00:00
|
|
|
p = FFMAX(p, fabsf(*pf));
|
2010-08-23 20:00:03 +00:00
|
|
|
pch->prev_energy_subshort[i] = energy_subshort[i + PSY_LAME_NUM_SUBBLOCKS] = p;
|
|
|
|
energy_short[1 + i / PSY_LAME_NUM_SUBBLOCKS] += p;
|
2012-01-07 21:16:10 +00:00
|
|
|
/* NOTE: The indexes below are [i + 3 - 2] in the LAME source.
|
|
|
|
* Obviously the 3 and 2 have some significance, or this would be just [i + 1]
|
|
|
|
* (which is what we use here). What the 3 stands for is ambiguous, as it is both
|
|
|
|
* number of short blocks, and the number of sub-short blocks.
|
|
|
|
* It seems that LAME is comparing each sub-block to sub-block + 1 in the
|
|
|
|
* previous block.
|
2010-08-23 20:00:03 +00:00
|
|
|
*/
|
|
|
|
if (p > energy_subshort[i + 1])
|
|
|
|
p = p / energy_subshort[i + 1];
|
|
|
|
else if (energy_subshort[i + 1] > p * 10.0f)
|
|
|
|
p = energy_subshort[i + 1] / (p * 10.0f);
|
|
|
|
else
|
|
|
|
p = 0.0;
|
|
|
|
attack_intensity[i + PSY_LAME_NUM_SUBBLOCKS] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compare energy between sub-short blocks */
|
|
|
|
for (i = 0; i < (AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS; i++)
|
|
|
|
if (!attacks[i / PSY_LAME_NUM_SUBBLOCKS])
|
|
|
|
if (attack_intensity[i] > pch->attack_threshold)
|
|
|
|
attacks[i / PSY_LAME_NUM_SUBBLOCKS] = (i % PSY_LAME_NUM_SUBBLOCKS) + 1;
|
|
|
|
|
|
|
|
/* should have energy change between short blocks, in order to avoid periodic signals */
|
|
|
|
/* Good samples to show the effect are Trumpet test songs */
|
|
|
|
/* GB: tuned (1) to avoid too many short blocks for test sample TRUMPET */
|
|
|
|
/* RH: tuned (2) to let enough short blocks through for test sample FSOL and SNAPS */
|
|
|
|
for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++) {
|
|
|
|
float const u = energy_short[i - 1];
|
|
|
|
float const v = energy_short[i];
|
|
|
|
float const m = FFMAX(u, v);
|
|
|
|
if (m < 40000) { /* (2) */
|
|
|
|
if (u < 1.7f * v && v < 1.7f * u) { /* (1) */
|
|
|
|
if (i == 1 && attacks[0] < attacks[i])
|
|
|
|
attacks[0] = 0;
|
|
|
|
attacks[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
att_sum += attacks[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attacks[0] <= pch->prev_attack)
|
|
|
|
attacks[0] = 0;
|
|
|
|
|
|
|
|
att_sum += attacks[0];
|
|
|
|
/* 3 below indicates the previous attack happened in the last sub-block of the previous sequence */
|
|
|
|
if (pch->prev_attack == 3 || att_sum) {
|
|
|
|
uselongblock = 0;
|
|
|
|
|
2011-03-17 03:34:12 +00:00
|
|
|
for (i = 1; i < AAC_NUM_BLOCKS_SHORT + 1; i++)
|
|
|
|
if (attacks[i] && attacks[i-1])
|
|
|
|
attacks[i] = 0;
|
2010-08-23 20:00:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We have no lookahead info, so just use same type as the previous sequence. */
|
|
|
|
uselongblock = !(prev_type == EIGHT_SHORT_SEQUENCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lame_apply_block_type(pch, &wi, uselongblock);
|
|
|
|
|
2015-07-21 01:53:24 +00:00
|
|
|
/* Calculate input sample maximums and evaluate clipping risk */
|
|
|
|
if (audio) {
|
|
|
|
for (i = 0; i < AAC_NUM_BLOCKS_SHORT; i++) {
|
|
|
|
const float *wbuf = audio + i * AAC_BLOCK_SIZE_SHORT;
|
|
|
|
float max = 0;
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < AAC_BLOCK_SIZE_SHORT; j++)
|
|
|
|
max = FFMAX(max, fabsf(wbuf[j]));
|
|
|
|
clippings[i] = max;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
clippings[i] = 0;
|
|
|
|
}
|
|
|
|
|
2010-08-23 20:00:03 +00:00
|
|
|
wi.window_type[1] = prev_type;
|
|
|
|
if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
|
2015-07-21 01:53:24 +00:00
|
|
|
float clipping = 0.0f;
|
|
|
|
|
2010-08-23 20:00:03 +00:00
|
|
|
wi.num_windows = 1;
|
|
|
|
wi.grouping[0] = 1;
|
|
|
|
if (wi.window_type[0] == LONG_START_SEQUENCE)
|
|
|
|
wi.window_shape = 0;
|
|
|
|
else
|
|
|
|
wi.window_shape = 1;
|
2015-07-21 01:53:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
clipping = FFMAX(clipping, clippings[i]);
|
|
|
|
wi.clipping[0] = clipping;
|
2010-08-23 20:00:03 +00:00
|
|
|
} else {
|
|
|
|
int lastgrp = 0;
|
|
|
|
|
|
|
|
wi.num_windows = 8;
|
|
|
|
wi.window_shape = 0;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (!((pch->next_grouping >> i) & 1))
|
|
|
|
lastgrp = i;
|
|
|
|
wi.grouping[lastgrp]++;
|
|
|
|
}
|
2015-07-21 01:53:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i += wi.grouping[i]) {
|
|
|
|
int w;
|
|
|
|
float clipping = 0.0f;
|
|
|
|
for (w = 0; w < wi.grouping[i] && !clipping; w++)
|
|
|
|
clipping = FFMAX(clipping, clippings[i+w]);
|
|
|
|
wi.clipping[i] = clipping;
|
|
|
|
}
|
2010-08-23 20:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine grouping, based on the location of the first attack, and save for
|
|
|
|
* the next frame.
|
|
|
|
* FIXME: Move this to analysis.
|
|
|
|
* TODO: Tune groupings depending on attack location
|
|
|
|
* TODO: Handle more than one attack in a group
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 9; i++) {
|
|
|
|
if (attacks[i]) {
|
|
|
|
grouping = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pch->next_grouping = window_grouping[grouping];
|
|
|
|
|
|
|
|
pch->prev_attack = attacks[8];
|
|
|
|
|
|
|
|
return wi;
|
|
|
|
}
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
const FFPsyModel ff_aac_psy_model =
|
|
|
|
{
|
|
|
|
.name = "3GPP TS 26.403-inspired model",
|
|
|
|
.init = psy_3gpp_init,
|
2010-08-23 20:00:03 +00:00
|
|
|
.window = psy_lame_window,
|
2009-07-08 20:01:31 +00:00
|
|
|
.analyze = psy_3gpp_analyze,
|
|
|
|
.end = psy_3gpp_end,
|
|
|
|
};
|