[FFmpeg-cvslog] avfilter, swresample, swscale: use fabs, fabsf instead of FFABS

Ganesh Ajjanagadde git at videolan.org
Thu Oct 22 22:15:54 CEST 2015


ffmpeg | branch: master | Ganesh Ajjanagadde <gajjanagadde at gmail.com> | Mon Oct 12 01:30:22 2015 -0400| [8507b98c10d948653375400e2b0a3d4389f74be4] | committer: Ganesh Ajjanagadde

avfilter,swresample,swscale: use fabs, fabsf instead of FFABS

It is well known that fabs and fabsf are at least as fast and sometimes
faster than the FFABS macro, at least on the gcc+glibc combination.
For instance, see the reference:
http://patchwork.sourceware.org/patch/6735/.
This was a patch to glibc in order to remove their usages of a macro.

The reason essentially boils down to fabs using the __builtin_fabs of
the compiler, while FFABS needs to infer to not use a branch and to
simply change the sign bit. Usually the inference works, but sometimes
it does not. This may be easily checked by looking at the asm.

This also has the added benefit of reducing macro usage, which has
problems with side-effects.

Note that avcodec is not handled here, as it is huge and
most things there are integer arithmetic anyway.

Tested with FATE.

Reviewed-by: Clément Bœsch <u at pkh.me>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>

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

 libavfilter/af_stereotools.c    |    2 +-
 libavfilter/avf_avectorscope.c  |    4 ++--
 libavfilter/avf_showcqt.c       |    2 +-
 libavfilter/avf_showfreqs.c     |    4 ++--
 libavfilter/f_ebur128.c         |    6 +++---
 libavfilter/vf_blend.c          |    4 ++--
 libavfilter/vf_dctdnoiz.c       |    4 ++--
 libavfilter/vf_framerate.c      |    4 ++--
 libavfilter/vf_hqdn3d.c         |    2 +-
 libavformat/mux.c               |    2 +-
 libswresample/swresample-test.c |    4 ++--
 11 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/libavfilter/af_stereotools.c b/libavfilter/af_stereotools.c
index e19ada4..a22efb0 100644
--- a/libavfilter/af_stereotools.c
+++ b/libavfilter/af_stereotools.c
@@ -146,7 +146,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     double *buffer = s->buffer;
     AVFrame *out;
     double *dst;
-    int nbuf = inlink->sample_rate * (FFABS(delay) / 1000.);
+    int nbuf = inlink->sample_rate * (fabs(delay) / 1000.);
     int n;
 
     nbuf -= nbuf % 2;
diff --git a/libavfilter/avf_avectorscope.c b/libavfilter/avf_avectorscope.c
index 30985f3..38dd97e 100644
--- a/libavfilter/avf_avectorscope.c
+++ b/libavfilter/avf_avectorscope.c
@@ -220,7 +220,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
                 cx = sx * sqrtf(1 - 0.5*sy*sy);
                 cy = sy * sqrtf(1 - 0.5*sx*sx);
                 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
-                y = s->h - s->h * FFABS(cx + cy) * .7;
+                y = s->h - s->h * fabsf(cx + cy) * .7;
             }
 
             draw_dot(s, x, y);
@@ -244,7 +244,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
                 cx = sx * sqrtf(1 - 0.5 * sy * sy);
                 cy = sy * sqrtf(1 - 0.5 * sx * sx);
                 x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
-                y = s->h - s->h * FFABS(cx + cy) * .7;
+                y = s->h - s->h * fabsf(cx + cy) * .7;
             }
 
             draw_dot(s, x, y);
diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c
index ce42cd6..2bd772e 100644
--- a/libavfilter/avf_showcqt.c
+++ b/libavfilter/avf_showcqt.c
@@ -371,7 +371,7 @@ static int config_output(AVFilterLink *outlink)
             tlength = s->timeclamp;
         }
 
-        volume = FFABS(av_expr_eval(volume_expr, expr_vars_val, NULL));
+        volume = fabs(av_expr_eval(volume_expr, expr_vars_val, NULL));
         if (isnan(volume)) {
             av_log(ctx, AV_LOG_WARNING, "at freq %g: volume is nan, setting it to 0\n", freq);
             volume = VOLUME_MIN;
diff --git a/libavfilter/avf_showfreqs.c b/libavfilter/avf_showfreqs.c
index 0f2ae22..a3665ef 100644
--- a/libavfilter/avf_showfreqs.c
+++ b/libavfilter/avf_showfreqs.c
@@ -163,7 +163,7 @@ static void generate_window_func(float *lut, int N, int win_func, float *overlap
         break;
     case WFUNC_BARTLETT:
         for (n = 0; n < N; n++)
-            lut[n] = 1.-FFABS((n-(N-1)/2.)/((N-1)/2.));
+            lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
         *overlap = 0.5;
         break;
     case WFUNC_HANNING:
@@ -207,7 +207,7 @@ static void generate_window_func(float *lut, int N, int win_func, float *overlap
         break;
     case WFUNC_BHANN:
         for (n = 0; n < N; n++)
-            lut[n] = 0.62-0.48*FFABS(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
+            lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
         *overlap = 0.5;
         break;
     case WFUNC_SINE:
diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
index b9ea11b..9e115fc 100644
--- a/libavfilter/f_ebur128.c
+++ b/libavfilter/f_ebur128.c
@@ -558,9 +558,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
             ebur128->true_peaks_per_frame[ch] = 0.0;
         for (idx_insample = 0; idx_insample < ret; idx_insample++) {
             for (ch = 0; ch < nb_channels; ch++) {
-                ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], FFABS(*swr_samples));
+                ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], fabs(*swr_samples));
                 ebur128->true_peaks_per_frame[ch] = FFMAX(ebur128->true_peaks_per_frame[ch],
-                                                          FFABS(*swr_samples));
+                                                          fabs(*swr_samples));
                 swr_samples++;
             }
         }
@@ -586,7 +586,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
             double bin;
 
             if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS)
-                ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], FFABS(*samples));
+                ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], fabs(*samples));
 
             ebur128->x[ch * 3] = *samples++; // set X[i]
 
diff --git a/libavfilter/vf_blend.c b/libavfilter/vf_blend.c
index 7b5e51b..f2c4b84 100644
--- a/libavfilter/vf_blend.c
+++ b/libavfilter/vf_blend.c
@@ -241,7 +241,7 @@ DEFINE_BLEND8(lighten,    FFMAX(A, B))
 DEFINE_BLEND8(divide,     av_clip_uint8(((float)A / ((float)B) * 255)))
 DEFINE_BLEND8(dodge,      DODGE(A, B))
 DEFINE_BLEND8(burn,       BURN(A, B))
-DEFINE_BLEND8(softlight,  (A > 127) ? B + (255 - B) * (A - 127.5) / 127.5 * (0.5 - FFABS(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - FFABS(B - 127.5)/255))
+DEFINE_BLEND8(softlight,  (A > 127) ? B + (255 - B) * (A - 127.5) / 127.5 * (0.5 - fabs(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - fabs(B - 127.5)/255))
 DEFINE_BLEND8(exclusion,  A + B - 2 * A * B / 255)
 DEFINE_BLEND8(pinlight,   (B < 128) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 128)))
 DEFINE_BLEND8(phoenix,    FFMIN(A, B) - FFMAX(A, B) + 255)
@@ -280,7 +280,7 @@ DEFINE_BLEND16(lighten,    FFMAX(A, B))
 DEFINE_BLEND16(divide,     av_clip_uint16(((float)A / ((float)B) * 65535)))
 DEFINE_BLEND16(dodge,      DODGE(A, B))
 DEFINE_BLEND16(burn,       BURN(A, B))
-DEFINE_BLEND16(softlight,  (A > 32767) ? B + (65535 - B) * (A - 32767.5) / 32767.5 * (0.5 - FFABS(B - 32767.5) / 65535): B - B * ((32767.5 - A) / 32767.5) * (0.5 - FFABS(B - 32767.5)/65535))
+DEFINE_BLEND16(softlight,  (A > 32767) ? B + (65535 - B) * (A - 32767.5) / 32767.5 * (0.5 - fabs(B - 32767.5) / 65535): B - B * ((32767.5 - A) / 32767.5) * (0.5 - fabs(B - 32767.5)/65535))
 DEFINE_BLEND16(exclusion,  A + B - 2 * A * B / 65535)
 DEFINE_BLEND16(pinlight,   (B < 32768) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 32768)))
 DEFINE_BLEND16(phoenix,    FFMIN(A, B) - FFMAX(A, B) + 65535)
diff --git a/libavfilter/vf_dctdnoiz.c b/libavfilter/vf_dctdnoiz.c
index 37306bb..6957f19 100644
--- a/libavfilter/vf_dctdnoiz.c
+++ b/libavfilter/vf_dctdnoiz.c
@@ -367,10 +367,10 @@ static av_always_inline void filter_freq_##bsize(const float *src, int src_lines
         float *b = &tmp_block2[i];                                                          \
         /* frequency filtering */                                                           \
         if (expr) {                                                                         \
-            var_values[VAR_C] = FFABS(*b);                                                  \
+            var_values[VAR_C] = fabsf(*b);                                                  \
             *b *= av_expr_eval(expr, var_values, NULL);                                     \
         } else {                                                                            \
-            if (FFABS(*b) < sigma_th)                                                       \
+            if (fabsf(*b) < sigma_th)                                                       \
                 *b = 0;                                                                     \
         }                                                                                   \
     }                                                                                       \
diff --git a/libavfilter/vf_framerate.c b/libavfilter/vf_framerate.c
index e8fba28..237a487 100644
--- a/libavfilter/vf_framerate.c
+++ b/libavfilter/vf_framerate.c
@@ -223,7 +223,7 @@ static int blend_frames16(AVFilterContext *ctx, float interpolate,
     }
     // decide if the shot-change detection allows us to blend two frames
     if (interpolate_scene_score < s->scene_score && copy_src2) {
-        uint16_t src2_factor = FFABS(interpolate) * (1 << (s->bitdepth - 8));
+        uint16_t src2_factor = fabsf(interpolate) * (1 << (s->bitdepth - 8));
         uint16_t src1_factor = s->max - src2_factor;
         const int half = s->max / 2;
         const int uv = (s->max + 1) * half;
@@ -287,7 +287,7 @@ static int blend_frames8(AVFilterContext *ctx, float interpolate,
     }
     // decide if the shot-change detection allows us to blend two frames
     if (interpolate_scene_score < s->scene_score && copy_src2) {
-        uint16_t src2_factor = FFABS(interpolate);
+        uint16_t src2_factor = fabsf(interpolate);
         uint16_t src1_factor = 256 - src2_factor;
         int plane, line, pixel;
 
diff --git a/libavfilter/vf_hqdn3d.c b/libavfilter/vf_hqdn3d.c
index 6c76c5c..5b367ff 100644
--- a/libavfilter/vf_hqdn3d.c
+++ b/libavfilter/vf_hqdn3d.c
@@ -182,7 +182,7 @@ static int16_t *precalc_coefs(double dist25, int depth)
 
     for (i = -256<<LUT_BITS; i < 256<<LUT_BITS; i++) {
         double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
-        simil = FFMAX(0, 1.0 - FFABS(f) / 255.0);
+        simil = FFMAX(0, 1.0 - fabs(f) / 255.0);
         C = pow(simil, gamma) * 256.0 * f;
         ct[(256<<LUT_BITS)+i] = lrint(C);
     }
diff --git a/libavformat/mux.c b/libavformat/mux.c
index c9ef490..c60cbda 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -317,7 +317,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 goto fail;
             }
             if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
-                && FFABS(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
+                && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
             ) {
                 if (st->sample_aspect_ratio.num != 0 &&
                     st->sample_aspect_ratio.den != 0 &&
diff --git a/libswresample/swresample-test.c b/libswresample/swresample-test.c
index 47c54a1..9caa750 100644
--- a/libswresample/swresample-test.c
+++ b/libswresample/swresample-test.c
@@ -374,7 +374,7 @@ int main(int argc, char **argv){
                 sum_aa+= a*a;
                 sum_bb+= b*b;
                 sum_ab+= a*b;
-                maxdiff= FFMAX(maxdiff, FFABS(a-b));
+                maxdiff= FFMAX(maxdiff, fabs(a-b));
             }
             sse= sum_aa + sum_bb - 2*sum_ab;
             if(sse < 0 && sse > -0.00001) sse=0; //fix rounding error
@@ -404,7 +404,7 @@ int main(int argc, char **argv){
                     sum_aa+= a*a;
                     sum_bb+= b*b;
                     sum_ab+= a*b;
-                    maxdiff= FFMAX(maxdiff, FFABS(a-b));
+                    maxdiff= FFMAX(maxdiff, fabs(a-b));
                 }
                 sse= sum_aa + sum_bb - 2*sum_ab;
                 if(sse < 0 && sse > -0.00001) sse=0; //fix rounding error



More information about the ffmpeg-cvslog mailing list