PR #22417 opened by Deng-zewen
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22417
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22417.patch

libavutil/riscv: Add timer support using rdtime

This adds a RISC-V-specific high-resolution timer implementation in
libavutil/riscv/timer.h, using the 'rdtime' instruction for real-time
tick counting. This is analogous to architecture-specific timers on ARM, x86, 
and others, providing better performance and lower overhead than the generic 
clock_gettime(CLOCK_MONOTONIC) fallback.

Signed-off-by: deng.zewen [email protected]


>From bd56555669c75bf37b5396121f4e6740b6410e09 Mon Sep 17 00:00:00 2001
From: "deng.zewen" <[email protected]>
Date: Fri, 6 Mar 2026 15:56:59 +0800
Subject: [PATCH] libavutil/riscv: Add timer support using rdtime

---
 libavutil/riscv/timer.h | 38 ++++++++++++++++++++++++++++++++++++++
 libavutil/timer.h       |  2 ++
 2 files changed, 40 insertions(+)
 create mode 100644 libavutil/riscv/timer.h

diff --git a/libavutil/riscv/timer.h b/libavutil/riscv/timer.h
new file mode 100644
index 0000000000..796d23d366
--- /dev/null
+++ b/libavutil/riscv/timer.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2026 Deng Zewen <[email protected]>
+ *
+ * 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 AVUTIL_RISCV_TIMER_H
+#define AVUTIL_RISCV_TIMER_H
+
+#include <stdint.h>
+#include "config.h"
+
+#if HAVE_INLINE_ASM
+
+#define AV_READ_TIME read_time
+static inline uint64_t read_time(void)
+{
+    uint64_t cycle_counter;
+    __asm__ volatile("rdtime %0" : "=r" (cycle_counter) : : "memory");
+    return cycle_counter;
+}
+#endif /* HAVE_INLINE_ASM */
+
+#endif /* AVUTIL_RISCV_TIMER_H */
\ No newline at end of file
diff --git a/libavutil/timer.h b/libavutil/timer.h
index 03706b0d10..d61ffe7da2 100644
--- a/libavutil/timer.h
+++ b/libavutil/timer.h
@@ -65,6 +65,8 @@
 #   include "x86/timer.h"
 #elif ARCH_LOONGARCH
 #   include "loongarch/timer.h"
+#elif ARCH_RISCV
+#   include "riscv/timer.h"
 #endif
 
 #if !defined(AV_READ_TIME)
-- 
2.52.0

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

Reply via email to