PR #21207 opened by Rémi Denis-Courmont (Courmisch)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21207
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21207.patch


From 87750fc41d88c6fd80f7cfe7485e9cc640e76367 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <[email protected]>
Date: Sun, 14 Dec 2025 11:06:13 +0200
Subject: [PATCH 1/2] lavc/mathops: simplify mid_pred()

This reduces mid_pred() (i.e. median of 3) down to the minimum:
- 3 comparisons and 4 conditional moves, or
- 4 min/max,
whilst eliminating all branches.

The same algorithm is already implemented via inline assembler for some
architectures such as x86 and Arm, but notably not Arm64 and RVA22.
Besides, using C code allows the compiler to schedule instruction
properly.

Even on architectures with neither conditional moves nor min/max, this
leads to a visible performance improvement for C code, as seen here for
RVA20 code running on SiFive-U74:

Before:
sub_median_pred_c:                                    1657.5 ( 1.00x)
sub_median_pred_rvb_b:                                 875.9 ( 1.89x)

After:
sub_median_pred_c:                                    1331.9 ( 1.00x)
sub_median_pred_rvb_b:                                 881.8 ( 1.51x)
---
 libavcodec/mathops.h | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/libavcodec/mathops.h b/libavcodec/mathops.h
index aa0bdfe956..4411d138b4 100644
--- a/libavcodec/mathops.h
+++ b/libavcodec/mathops.h
@@ -93,23 +93,24 @@ static av_always_inline unsigned UMULH(unsigned a, unsigned 
b){
 #endif
 
 /* median of 3 */
-#ifndef mid_pred
-#define mid_pred mid_pred
-static inline av_const int mid_pred(int a, int b, int c)
+static inline av_const int median3_c(int a, int b, int c)
 {
-    if(a>b){
-        if(c>b){
-            if(c>a) b=a;
-            else    b=c;
-        }
-    }else{
-        if(b>c){
-            if(c>a) b=c;
-            else    b=a;
-        }
+    int max2, min2, m;
+
+    if (a >= b) {
+        max2 = a;
+        min2 = b;
+    } else {
+        max2 = b;
+        min2 = a;
     }
-    return b;
+    m = (c >= max2) ? max2 : c;
+
+    return (m >= min2) ? m : min2;
 }
+
+#ifndef mid_pred
+#define mid_pred median3_c
 #endif
 
 #ifndef median4
-- 
2.49.1


From 2cf5e492568bd06ac829f0f743477f42c665fcc4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <[email protected]>
Date: Sun, 14 Dec 2025 14:45:16 +0200
Subject: [PATCH 2/2] lavc/mathops: R-V B optimisation for mid_pred

If Zbb is enabled at compilation (e.g. Ubuntu), the compiler should
compile the new C mid_pred() function correctly. But if Zbb is *not*
enabled (e.g. Debian), then we can at least fallback at run-time.

On SiFive-U74, before:
sub_median_pred_c:                                    1331.9 ( 1.00x)
sub_median_pred_rvb_b:                                 881.8 ( 1.51x)

After:
sub_median_pred_c:                                    1133.1 ( 1.00x)
sub_median_pred_rvb_b:                                 875.7 ( 1.29x)
---
 libavcodec/mathops.h       |  2 ++
 libavcodec/riscv/mathops.h | 54 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 libavcodec/riscv/mathops.h

diff --git a/libavcodec/mathops.h b/libavcodec/mathops.h
index 4411d138b4..64431b8a15 100644
--- a/libavcodec/mathops.h
+++ b/libavcodec/mathops.h
@@ -44,6 +44,8 @@ extern const uint8_t ff_zigzag_scan[16+1];
 #   include "mips/mathops.h"
 #elif ARCH_PPC
 #   include "ppc/mathops.h"
+#elif ARCH_RISCV
+#   include "riscv/mathops.h"
 #elif ARCH_X86
 #   include "x86/mathops.h"
 #endif
diff --git a/libavcodec/riscv/mathops.h b/libavcodec/riscv/mathops.h
new file mode 100644
index 0000000000..c2258f49d7
--- /dev/null
+++ b/libavcodec/riscv/mathops.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright © 2025 Rémi Denis-Courmont.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_RISCV_MATHOPS_H
+#define AVCODEC_RISCV_MATHOPS_H
+
+#include "config.h"
+#include <stdbool.h>
+#include "libavutil/attributes_internal.h"
+#include "libavutil/riscv/cpu.h"
+
+#if HAVE_RV && !defined(__riscv_zbb)
+static inline int median3_c(int a, int b, int c);
+
+static inline av_const int median3_rv(int a, int b, int c)
+{
+    if (__builtin_expect(ff_rv_zbb_support(), true)) {
+        int min2, max2;
+
+        __asm__ (
+           ".option push\n"
+           ".option arch, +zbb\n"
+           "max     %1, %2, %3\n"
+           "min     %0, %2, %3\n"
+           "min     %1, %4, %1\n"
+           "max     %0, %0, %1\n"
+           ".option pop\n"
+           : "=&r" (min2), "=&r" (max2) : "r" (a), "r" (b), "r" (c));
+
+        return min2;
+    }
+    return median3_c(a, b, c);
+}
+#define mid_pred median3_rv
+#endif
+
+#endif /* HAVE_RVV */
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to