From 7551012b4e1c854c1dc36483c893f90b1c236977 Mon Sep 17 00:00:00 2001
From: Aadhu23 <aadhavansalthivel5@gmail.com>
Date: Thu, 16 Jul 2026 09:21:37 +0000
Subject: [PATCH] videoio: support FFmpeg after AVCodec::pix_fmts removal

---
 modules/videoio/src/cap_ffmpeg_hw.hpp | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/modules/videoio/src/cap_ffmpeg_hw.hpp b/modules/videoio/src/cap_ffmpeg_hw.hpp
index 65045a2bc79e..bb5e4c108819 100644
--- a/modules/videoio/src/cap_ffmpeg_hw.hpp
+++ b/modules/videoio/src/cap_ffmpeg_hw.hpp
@@ -757,6 +757,30 @@ AVCodec *hw_find_codec(AVCodecID id, AVHWDeviceType hw_type, int (*check_categor
 #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 LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100)
+            if (av_codec_is_encoder(c) && hw_native_fmt != AV_PIX_FMT_NONE) {
+                const AVPixelFormat *pix_fmts = NULL;
+                int num_pix_fmts = 0;
+
+                int 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 && num_pix_fmts > 0) {
+                    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;
+                        }
+                    }
+                }
+            }
+#else
             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) {
@@ -766,6 +790,7 @@ AVCodec *hw_find_codec(AVCodecID id, AVHWDeviceType hw_type, int (*check_categor
                     }
                 }
             }
+#endif
             for (int i = 0;; i++) {
                 const AVCodecHWConfig *hw_config = avcodec_get_hw_config(c, i);
                 if (!hw_config)
