This commit extends the trellis quantizer (used by the default twoloop coder)
to accept and correctly encode codebooks needed for intensity stereo and
perceptual noise substitution.
---
libavcodec/aaccoder.c | 81 +--
1 file changed, 46 insertions(+), 35 deletions(-)
diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c
index a6e4cc4..f069a3b 100644
--- a/libavcodec/aaccoder.c
+++ b/libavcodec/aaccoder.c
@@ -43,8 +43,23 @@
/** Frequency in Hz for lower limit of noise substitution **/
#define NOISE_LOW_LIMIT 4000
+/* Energy spread threshold value below which no PNS is used, this corresponds
to
+ * typically around 17Khz, after which PNS usage decays ending at 19Khz */
+#define NOISE_SPREAD_THRESHOLD 152234544.0f
+
+/* Above ~1.26*threshold all normally-zeroed values are PNS'd. Lambda divides
+ * the defined value below as to try to get a ~1.26 multiplier so that there is
+ * a balance between noise and zero bands leaving more bits for actual signal
*/
+#define NOISE_LAMBDA_NUMERATOR 252.1f
+
+/** Frequency in Hz for lower limit of intensity stereo **/
+#define INT_STEREO_LOW_LIMIT 6000
+
/** Total number of usable codebooks **/
-#define CB_TOT 13
+#define CB_TOT 12
+
+/** Total number of codebooks, including special ones **/
+#define CB_TOT_ALL 15
/** bits needed to code codebook run value for long windows */
static const uint8_t run_value_bits_long[64] = {
@@ -64,9 +79,9 @@ static const uint8_t * const run_value_bits[2] = {
};
/** Map to convert values from BandCodingPath index to a codebook index **/
-static const uint8_t aac_cb_out_map[CB_TOT] = {0,1,2,3,4,5,6,7,8,9,10,11,13};
+static const uint8_t aac_cb_out_map[CB_TOT_ALL] =
{0,1,2,3,4,5,6,7,8,9,10,11,13,14,15};
/** Inverse map to convert from codebooks to BandCodingPath indices **/
-static const uint8_t aac_cb_in_map[CB_TOT+1] =
{0,1,2,3,4,5,6,7,8,9,10,11,0,12};
+static const uint8_t aac_cb_in_map[CB_TOT_ALL+1] =
{0,1,2,3,4,5,6,7,8,9,10,11,0,12,13,14};
/**
* Quantize one coefficient.
@@ -118,7 +133,7 @@ static av_always_inline float
quantize_and_encode_band_cost_template(
const float *scaled, int size, int scale_idx,
int cb, const float lambda, const float uplim,
int *bits, int BT_ZERO, int BT_UNSIGNED,
-int BT_PAIR, int BT_ESC, int BT_NOISE)
+int BT_PAIR, int BT_ESC, int BT_NOISE, int
BT_STEREO)
{
const int q_idx = POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512;
const float Q = ff_aac_pow2sf_tab [q_idx];
@@ -131,14 +146,7 @@ static av_always_inline float
quantize_and_encode_band_cost_template(
int resbits = 0;
int off;
-if (BT_ZERO) {
-for (i = 0; i size; i++)
-cost += in[i]*in[i];
-if (bits)
-*bits = 0;
-return cost * lambda;
-}
-if (BT_NOISE) {
+if (BT_ZERO || BT_NOISE || BT_STEREO) {
for (i = 0; i size; i++)
cost += in[i]*in[i];
if (bits)
@@ -231,26 +239,27 @@ static float quantize_and_encode_band_cost_NONE(struct
AACEncContext *s, PutBitC
return 0.0f;
}
-#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED,
BT_PAIR, BT_ESC, BT_NOISE) \
-static float quantize_and_encode_band_cost_ ## NAME(
\
-struct AACEncContext *s,
\
-PutBitContext *pb, const float *in,
\
-const float *scaled, int size, int scale_idx,
\
-int cb, const float lambda, const float uplim,
\
-int *bits) {
\
-return quantize_and_encode_band_cost_template(
\
-s, pb, in, scaled, size, scale_idx,
\
-BT_ESC ? ESC_BT : cb, lambda, uplim, bits,
\
-BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC,
BT_NOISE); \
+#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED,
BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO) \
+static float quantize_and_encode_band_cost_ ## NAME(
\
+struct AACEncContext *s,
\
+PutBitContext *pb, const float *in,
\
+const float *scaled, int size, int scale_idx,
\
+int cb, const float lambda, const float uplim,
\
+int *bits) {
\
+return