Merge commit 'a1a143adb0fd11c474221431417cff25db7d920f'
[ffmpeg.git] / libavcodec / aacenc_tns.c
index 43fa974..2ffe1f8 100644 (file)
@@ -25,6 +25,7 @@
  * @author Rostislav Pehlivanov ( atomnuker gmail com )
  */
 
+#include "libavutil/libm.h"
 #include "aacenc.h"
 #include "aacenc_tns.h"
 #include "aactab.h"
@@ -170,13 +171,18 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
     const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
     const int slant = sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE  ? 1 :
                       sce->ics.window_sequence[0] == LONG_START_SEQUENCE ? 0 : 2;
+    const int sfb_len = sfb_end - sfb_start;
+    const int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
+
+    if (coef_len <= 0 || sfb_len <= 0) {
+        sce->tns.present = 0;
+        return;
+    }
 
     for (w = 0; w < sce->ics.num_windows; w++) {
         float en[2] = {0.0f, 0.0f};
         int oc_start = 0, os_start = 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];
-        const int sfb_len = sfb_end - sfb_start;
+        int coef_start = sce->ics.swb_offset[sfb_start];
 
         for (g = sfb_start; g < sce->ics.num_swb && g <= sfb_end; g++) {
             FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[w*16+g];
@@ -186,14 +192,11 @@ void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
                 en[0] += band->energy;
         }
 
-        if (coef_len <= 0 || sfb_len <= 0)
-            continue;
-
         /* LPC */
         gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[w*128 + coef_start],
                                        coef_len, order, coefs);
 
-        if (!order || gain < TNS_GAIN_THRESHOLD_LOW || gain > TNS_GAIN_THRESHOLD_HIGH)
+        if (!order || !isfinite(gain) || gain < TNS_GAIN_THRESHOLD_LOW || gain > TNS_GAIN_THRESHOLD_HIGH)
             continue;
 
         tns->n_filt[w] = is8 ? 1 : order != TNS_MAX_ORDER ? 2 : 3;