[PR] swscale/rational64: implement av_gcd_q64 (PR #23623)

Zhao Zhili via ffmpeg-devel <[email protected]>
Newsgroups gmane.comp.video.ffmpeg.devel
Message-ID <178262892725.59.17824871201949205747@29965ddac10e>
PR #23623 opened by Zhao Zhili (quink)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23623
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23623.patch

av_gcd_q64 was declared in rational64.h but never defined. Mirror the
32-bit av_gcd_q semantics.

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From 06169124bba761f9bbd1668b9745ecadf2b08481 Mon Sep 17 00:00:00 2001
From: Zhao Zhili <[email protected]>
Date: Sun, 28 Jun 2026 14:22:01 +0800
Subject: [PATCH] swscale/rational64: implement av_gcd_q64

av_gcd_q64 was declared in rational64.h but never defined. Mirror the
32-bit av_gcd_q semantics.
---
 libswscale/rational64.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libswscale/rational64.c b/libswscale/rational64.c
index 998e46c572..9c1bcdbc6d 100644
--- a/libswscale/rational64.c
+++ b/libswscale/rational64.c
@@ -29,6 +29,7 @@
 #include <limits.h>
 
 #include "libavutil/int128.h"
+#include "libavutil/mathematics.h"
 #include "rational64.h"
 
 static av_int128 gcd128(av_int128 a, av_int128 b)
@@ -144,3 +145,14 @@ AVRational64 av_sub_q64(AVRational64 b, AVRational64 c)
                               av_mul128(av_to128i(c.num), av_to128i(b.den))),
                     av_mul128(av_to128i(b.den), av_to128i(c.den)));
 }
+
+AVRational64 av_gcd_q64(AVRational64 a, AVRational64 b, int max_den, AVRational64 def)
+{
+    int64_t gcd = av_gcd(a.den, b.den);
+    /* lcm can exceed INT64_MAX, so keep it in 128-bit until the comparison */
+    av_int128 lcm = av_mul128(av_to128i(a.den / gcd), av_to128i(b.den));
+
+    if (av_cmp128(lcm, av_to128i(max_den)) < 0)
+        return av_make_q64(av_gcd(a.num, b.num), av_from128i(lcm));
+    return def;
+}
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.