[FFmpeg-cvslog] aacenc_tns: adjust coefficient calculation, add double filter support

Rostislav Pehlivanov git at videolan.org
Sat Sep 5 10:14:01 CEST 2015


ffmpeg | branch: master | Rostislav Pehlivanov <atomnuker at gmail.com> | Sat Sep  5 09:08:30 2015 +0100| [e3faad811e429002d549562db4e0fc30c08dc6a4] | committer: Rostislav Pehlivanov

aacenc_tns: adjust coefficient calculation, add double filter support

This commit improves the TNS implementation to the point where it's
actually usable and very rarely results in nastyness (in all bitrates
except extremely low bitrates it's increasing the quality and prevents
some distortions from the coder being audiable).

Also adds a double filter support which is only used if the energy
difference between the top and bottom of the SFBs is above the
thresholds defined in the header file. Looking at the bitstream
that fdk_aac generates it sometimes used a double filter despite
the specs stating that a single filter should be enough for almost
all cases and purposes.

Unlike FAAC or fdk_aac we sometimes use a reverse filter in case
the energy difference isn't enought to use a double filter. This
actually works better.

Signed-off-by: Rostislav Pehlivanov <atomnuker at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e3faad811e429002d549562db4e0fc30c08dc6a4
---

 libavcodec/aacenc_tns.c |   48 +++++++++++++++++++++++++++++------------------
 libavcodec/aacenc_tns.h |   10 ++++++----
 2 files changed, 36 insertions(+), 22 deletions(-)

diff --git a/libavcodec/aacenc_tns.c b/libavcodec/aacenc_tns.c
index 3c442e8..637b813 100644
--- a/libavcodec/aacenc_tns.c
+++ b/libavcodec/aacenc_tns.c
@@ -67,7 +67,7 @@ void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
     }
 }
 
-static int quantize_coefs(double *coef, int *idx, float *lpc, int order)
+static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order)
 {
     int i;
     uint8_t u_coef;
@@ -79,7 +79,6 @@ static int quantize_coefs(double *coef, int *idx, float *lpc, int order)
         u_coef = (idx[i])&(~(~0<<TNS_Q_BITS));
         lpc[i] = quant_arr[u_coef];
     }
-    return order;
 }
 
 /* Apply TNS filter */
@@ -129,15 +128,14 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
     int w, w2, g, count = 0;
     const int mmm = FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb);
     const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
-    int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
+    const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
 
     int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm);
     int sfb_end   = av_clip(sce->ics.num_swb, 0, mmm);
 
     for (w = 0; w < sce->ics.num_windows; w++) {
-        float en_low = 0.0f, en_high = 0.0f, threshold = 0.0f, spread = 0.0f;
+        float e_ratio = 0.0f, threshold = 0.0f, spread = 0.0f, en[2] = {0.0, 0.0f};
         double gain = 0.0f, coefs[MAX_LPC_ORDER] = {0};
-
         int coef_start = w*sce->ics.num_swb + sce->ics.swb_offset[sfb_start];
         int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
 
@@ -147,9 +145,9 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
                 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
                 if ((w+w2)*16+g > sfb_start + ((sfb_end - sfb_start)/2))
-                    en_high += band->energy;
+                    en[1] += band->energy;
                 else
-                    en_low  += band->energy;
+                    en[0] += band->energy;
                 threshold += band->threshold;
                 spread += band->spread;
             }
@@ -157,22 +155,36 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
 
         if (coef_len <= 0 || (sfb_end - sfb_start) <= 0)
             continue;
+        else
+            e_ratio = en[0]/en[1];
 
         /* LPC */
         gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[coef_start],
                                        coef_len, order, coefs);
 
-        gain *= s->lambda/110.0f;
-
-        if (gain > TNS_GAIN_THRESHOLD_LOW && gain*0 < TNS_GAIN_THRESHOLD_HIGH &&
-            (en_low+en_high) > TNS_GAIN_THRESHOLD_LOW*threshold &&
-            spread > TNS_SPREAD_THRESHOLD) {
-            tns->n_filt[w] = 1;
-            for (g = 0; g < tns->n_filt[w]; g++) {
-                tns->length[w][g] = sfb_end - sfb_start;
-                tns->direction[w][g] = en_low < en_high && TNS_DIRECTION_VARY;
-                tns->order[w][g] = quantize_coefs(coefs, tns->coef_idx[w][g],
-                                                  tns->coef[w][g], order);
+        if (gain > TNS_GAIN_THRESHOLD_LOW && gain < TNS_GAIN_THRESHOLD_HIGH &&
+            (en[0]+en[1]) > TNS_GAIN_THRESHOLD_LOW*threshold &&
+            spread < TNS_SPREAD_THRESHOLD && order) {
+            if (is8 || order < 2 || (e_ratio > TNS_E_RATIO_LOW && e_ratio < TNS_E_RATIO_HIGH)) {
+                tns->n_filt[w] = 1;
+                for (g = 0; g < tns->n_filt[w]; g++) {
+                    tns->length[w][g] = sfb_end - sfb_start;
+                    tns->direction[w][g] = en[0] < en[1];
+                    tns->order[w][g] = order;
+                    quantize_coefs(coefs, tns->coef_idx[w][g], tns->coef[w][g],
+                                   order);
+                }
+            } else {  /* 2 filters due to energy disbalance */
+                tns->n_filt[w] = 2;
+                for (g = 0; g < tns->n_filt[w]; g++) {
+                    tns->direction[w][g] = en[g] < en[!g];
+                    tns->order[w][g] = !g ? order/2 : order - tns->order[w][g-1];
+                    tns->length[w][g] = !g ? (sfb_end - sfb_start)/2 : \
+                                    (sfb_end - sfb_start) - tns->length[w][g-1];
+                    quantize_coefs(&coefs[!g ? 0 : order - tns->order[w][g-1]],
+                                   tns->coef_idx[w][g], tns->coef[w][g],
+                                   tns->order[w][g]);
+                }
             }
             count++;
         }
diff --git a/libavcodec/aacenc_tns.h b/libavcodec/aacenc_tns.h
index 812deea..f0e7556 100644
--- a/libavcodec/aacenc_tns.h
+++ b/libavcodec/aacenc_tns.h
@@ -37,11 +37,13 @@
 #define TNS_GAIN_THRESHOLD_LOW  1.395f
 #define TNS_GAIN_THRESHOLD_HIGH 11.19f
 
-/* Do not use TNS if the psy band spread is below this value */
-#define TNS_SPREAD_THRESHOLD 20.081512f
+/* If the energy ratio between the low SFBs vs the high SFBs is not between
+ * those two values, use 2 filters instead */
+#define TNS_E_RATIO_LOW  0.77
+#define TNS_E_RATIO_HIGH 1.23
 
-/* Allows to reverse the filter direction if the band energy is uneven */
-#define TNS_DIRECTION_VARY 1
+/* Do not use TNS if the psy band spread is below this value */
+#define TNS_SPREAD_THRESHOLD 37.081512f
 
 void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce);
 void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce);



More information about the ffmpeg-cvslog mailing list