Index | Thread | Search

From:
Kirill A. Korinsky <kirill@korins.ky>
Subject:
graphics/opencv: compatibility with ffmpeg 9.0
To:
Rafael Sadowski <rsadowski@openbsd.org>
Cc:
OpenBSD ports <ports@openbsd.org>
Date:
Mon, 10 Aug 2026 12:37:17 +0200

Download raw body.

Thread
Rafael, ports@,

I'd like to commit a fix for graphics/opencv which switches it away from
deprecated API in ffmpeg which prevents it to build against ffmpeg 9.0

Build tested against ffmpeg-9.0 and ffmpeg-8.1.2

Ok?

Index: Makefile
===================================================================
RCS file: /cvs/ports/graphics/opencv/Makefile,v
diff -u -p -r1.112 Makefile
--- Makefile	22 Jun 2026 17:49:21 -0000	1.112
+++ Makefile	10 Aug 2026 10:35:18 -0000
@@ -8,7 +8,7 @@ V =			4.13.0
 GH_ACCOUNT =		opencv
 GH_PROJECT =		opencv
 GH_TAGNAME =		${V}
-REVISION-main =		0
+REVISION-main =		1
 REVISION-java =		0
 
 PKGNAME-main =		opencv-${V}
Index: patches/patch-modules_videoio_src_cap_ffmpeg_hw_hpp
===================================================================
RCS file: patches/patch-modules_videoio_src_cap_ffmpeg_hw_hpp
diff -N patches/patch-modules_videoio_src_cap_ffmpeg_hw_hpp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-modules_videoio_src_cap_ffmpeg_hw_hpp	10 Aug 2026 10:35:18 -0000
@@ -0,0 +1,36 @@
+Index: modules/videoio/src/cap_ffmpeg_hw.hpp
+--- modules/videoio/src/cap_ffmpeg_hw.hpp.orig
++++ modules/videoio/src/cap_ffmpeg_hw.hpp
+@@ -739,7 +739,9 @@ bool hw_check_codec(AVCodec* codec, AVHWDeviceType hw_
+ static
+ AVCodec *hw_find_codec(AVCodecID id, AVHWDeviceType hw_type, int (*check_category)(const AVCodec *), const char *disabled_codecs, AVPixelFormat *hw_pix_fmt) {
+     AVCodec *c = 0;
++    const enum AVPixelFormat *pix_fmts;
+     void *opaque = 0;
++    int ret, num_pix_fmts;
+ 
+     while (NULL != (c = (AVCodec*)av_codec_iterate(&opaque)))
+     {
+@@ -757,12 +759,16 @@ AVCodec *hw_find_codec(AVCodecID id, AVHWDeviceType hw
+ #endif
+             if (hw_type == AV_HWDEVICE_TYPE_CUDA) // CUDA encoders don't support avcodec_get_hw_config()
+                 hw_native_fmt = AV_PIX_FMT_CUDA;
+-            if (av_codec_is_encoder(c) && hw_native_fmt != AV_PIX_FMT_NONE && c->pix_fmts) {
+-                for (int i = 0; c->pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
+-                    if (c->pix_fmts[i] == hw_native_fmt) {
+-                        *hw_pix_fmt = hw_native_fmt;
+-                        if (hw_check_codec(c, hw_type, disabled_codecs))
+-                            return c;
++            if (av_codec_is_encoder(c) && hw_native_fmt != AV_PIX_FMT_NONE) {
++                ret = avcodec_get_supported_config(NULL, c, AV_CODEC_CONFIG_PIX_FORMAT,
++                                                   0, (const void **) &pix_fmts, &num_pix_fmts);
++                if (ret >= 0 && pix_fmts) {
++                    for (int i = 0; i < num_pix_fmts; i++) {
++                        if (pix_fmts[i] == hw_native_fmt) {
++                            *hw_pix_fmt = hw_native_fmt;
++                            if (hw_check_codec(c, hw_type, disabled_codecs))
++                                return c;
++                        }
+                     }
+                 }
+             }
Index: patches/patch-modules_videoio_src_cap_ffmpeg_impl_hpp
===================================================================
RCS file: patches/patch-modules_videoio_src_cap_ffmpeg_impl_hpp
diff -N patches/patch-modules_videoio_src_cap_ffmpeg_impl_hpp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-modules_videoio_src_cap_ffmpeg_impl_hpp	10 Aug 2026 10:35:18 -0000
@@ -0,0 +1,51 @@
+Index: modules/videoio/src/cap_ffmpeg_impl.hpp
+--- modules/videoio/src/cap_ffmpeg_impl.hpp.orig
++++ modules/videoio/src/cap_ffmpeg_impl.hpp
+@@ -2539,10 +2539,11 @@ static AVCodecContext * icv_configure_video_stream_FFM
+ #else
+     AVCodecContext *c = st->codec;
+ #endif
++    const AVRational *supported_framerates;
++    int frame_rate, frame_rate_base, num_supported_framerates, ret;
++
+     CV_Assert(c);
+ 
+-    int frame_rate, frame_rate_base;
+-
+     c->codec_id = codec ? codec->id : codec_id;
+ 
+ #ifndef CV_FFMPEG_CODECPAR
+@@ -2583,17 +2584,28 @@ static AVCodecContext * icv_configure_video_stream_FFM
+     c->time_base.den = frame_rate;
+     c->time_base.num = frame_rate_base;
+     /* adjust time base for supported framerates */
+-    if(codec && codec->supported_framerates){
+-        const AVRational *p= codec->supported_framerates;
++    if(codec){
++        ret = avcodec_get_supported_config(c, NULL, AV_CODEC_CONFIG_FRAME_RATE, 0,
++                                           (const void **) &supported_framerates,
++                                           &num_supported_framerates);
++        if (ret < 0)
++        {
++#ifdef CV_FFMPEG_CODECPAR
++            avcodec_free_context(&c);
++#endif
++            return NULL;
++        }
++    }
++    if(codec && supported_framerates){
+         AVRational req = {frame_rate, frame_rate_base};
+         const AVRational *best=NULL;
+         AVRational best_error= {INT_MAX, 1};
+-        for(; p->den!=0; p++){
+-            AVRational error= av_sub_q(req, *p);
++        for(int i = 0; i < num_supported_framerates; i++){
++            AVRational error= av_sub_q(req, supported_framerates[i]);
+             if(error.num <0) error.num *= -1;
+             if(av_cmp_q(error, best_error) < 0){
+                 best_error= error;
+-                best= p;
++                best= &supported_framerates[i];
+             }
+         }
+         if (best == NULL)


-- 
wbr, Kirill