From 0cc4d9659aaa35c8a21af0e7a8927bf9132315bf Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Thu, 1 Jun 2017 03:14:13 -0400 Subject: Adding leakage modelling to boost bands We boost bands that either cause leakage or are filled with leakage --- celt/celt.h | 4 ++++ celt/celt_encoder.c | 24 ++++++++++++++++++------ src/analysis.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/celt/celt.h b/celt/celt.h index d69cd44c..70175301 100644 --- a/celt/celt.h +++ b/celt/celt.h @@ -50,6 +50,8 @@ extern "C" { #define CELTDecoder OpusCustomDecoder #define CELTMode OpusCustomMode +#define LEAK_BANDS 19 + typedef struct { int valid; float tonality; @@ -60,6 +62,8 @@ typedef struct { float vad_prob; int bandwidth; float activity_probability; + /* Store as Q6 char to save space. */ + unsigned char leak_boost[LEAK_BANDS]; } AnalysisInfo; typedef struct { diff --git a/celt/celt_encoder.c b/celt/celt_encoder.c index 187cccce..5729dc35 100644 --- a/celt/celt_encoder.c +++ b/celt/celt_encoder.c @@ -964,7 +964,7 @@ static opus_val16 median_of_3(const opus_val16 *x) static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2, int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN, int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM, - int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_dynalloc) + int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_dynalloc, AnalysisInfo *analysis) { int i, c; opus_int32 tot_boost=0; @@ -1054,14 +1054,26 @@ static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 } for (i=start;i=12) follower[i] = HALF16(follower[i]); + } +#ifdef DISABLE_FLOAT_API + (void)analysis; +#else + if (analysis->valid) + { + for (i=start;ileak_boost[i]; + } +#endif + for (i=start;ilsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr, - eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc); + eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis); /* For LFE, everything interesting is in the first band */ if (st->lfe) offsets[0] = IMIN(8, effectiveBytes/3); diff --git a/src/analysis.c b/src/analysis.c index 867b7fdd..e3c93f89 100644 --- a/src/analysis.c +++ b/src/analysis.c @@ -290,6 +290,9 @@ static const float std_feature_bias[9] = { 2.163313f, 1.260756f, 1.116868f, 1.918795f }; +#define LEAKAGE_OFFSET 2.5f +#define LEAKAGE_SLOPE 2.f + #ifdef FIXED_POINT /* For fixed-point, the input is +/-2^15 shifted up by SIG_SHIFT, so we need to compensate for that in the energy. */ @@ -336,6 +339,9 @@ static void tonality_analysis(TonalityAnalysisState *tonal, const CELTMode *celt float tonality2[240]; float midE[8]; float spec_variability=0; + float band_log2[NB_TBANDS+1]; + float leakage_from[NB_TBANDS+1]; + float leakage_to[NB_TBANDS+1]; SAVE_STACK; alpha = 1.f/IMIN(10, 1+tonal->count); @@ -461,6 +467,22 @@ static void tonality_analysis(TonalityAnalysisState *tonal, const CELTMode *celt } relativeE = 0; frame_loudness = 0; + /* The energy of the very first band is special because of DC. */ + { + float E = 0; + float X1r, X2r; + X1r = 2*(float)out[0].r; + X2r = 2*(float)out[0].i; + E = X1r*X1r + X2r*X2r; + for (i=1;i<4;i++) + { + float binE = out[i].r*(float)out[i].r + out[N-i].r*(float)out[N-i].r + + out[i].i*(float)out[i].i + out[N-i].i*(float)out[N-i].i; + E += binE; + } + E = SCALE_ENER(E); + band_log2[0] = (float).5*log2(E+1e-10f); + } for (b=0;blogE[tonal->E_count][b] = logE[b]; if (tonal->count==0) tonal->highE[b] = tonal->lowE[b] = logE[b]; @@ -541,6 +564,35 @@ static void tonality_analysis(TonalityAnalysisState *tonal, const CELTMode *celt tonal->prev_band_tonality[b] = band_tonality[b]; } + leakage_from[0] = band_log2[0]; + leakage_to[0] = band_log2[0] - LEAKAGE_OFFSET; + for (b=1;b=0;b--) + { + float leak_slope = LEAKAGE_SLOPE*(tbands[b+1]-tbands[b])/4; + leakage_from[b] = MIN16(leakage_from[b+1]+leak_slope, leakage_from[b]); + leakage_to[b] = MAX16(leakage_to[b+1]-leak_slope, leakage_to[b]); + } + celt_assert(NB_TBANDS+1 <= LEAK_BANDS); + for (b=0;bleak_boost[b] = IMIN(255, (int)floor(.5 + 64.f*boost)); + } + for (;bleak_boost[b] = 0; + for (i=0;i