avfilter/af_astats: fix calculations involving last sample
authorPaul B Mahol <onemda@gmail.com>
Sun, 12 May 2019 15:10:40 +0000 (17:10 +0200)
committerPaul B Mahol <onemda@gmail.com>
Sun, 12 May 2019 15:12:06 +0000 (17:12 +0200)
libavfilter/af_astats.c

index 9c673b2..7707f31 100644 (file)
@@ -196,6 +196,7 @@ static void reset_stats(AudioStatsContext *s)
         p->nb_nans = 0;
         p->nb_infs = 0;
         p->nb_denormals = 0;
+        p->last = NAN;
     }
 }
 
@@ -285,10 +286,12 @@ static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d,
     p->sigma_x += nd;
     p->sigma_x2 += nd * nd;
     p->avg_sigma_x2 = p->avg_sigma_x2 * s->mult + (1.0 - s->mult) * nd * nd;
-    p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
-    p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
-    p->diff1_sum += fabs(d - p->last);
-    p->diff1_sum_x2 += (d - p->last) * (d - p->last);
+    if (!isnan(p->last)) {
+        p->min_diff = FFMIN(p->min_diff, fabs(d - p->last));
+        p->max_diff = FFMAX(p->max_diff, fabs(d - p->last));
+        p->diff1_sum += fabs(d - p->last);
+        p->diff1_sum_x2 += (d - p->last) * (d - p->last);
+    }
     p->last = d;
     p->mask |= i;
     p->imask &= i;