From 1acdbf34921e0fcc25c502da2cbe994554d540d9 Mon Sep 17 00:00:00 2001
From: Dom Cobley <popcornmix@gmail.com>
Date: Tue, 11 Mar 2025 17:15:42 +0000
Subject: [PATCH 13/13] DVDVideoCodecDRMPRIME: Set hwctx for v4l2 deinterlace

---
 .../DVDCodecs/Video/DVDVideoCodecDRMPRIME.cpp  | 18 +++++++++++++++++-
 .../DVDCodecs/Video/DVDVideoCodecDRMPRIME.h    |  2 ++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.cpp b/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.cpp
index 60f9bf28bf66..c8385f64d83e 100644
--- a/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.cpp
+++ b/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.cpp
@@ -775,7 +775,21 @@ bool CDVDVideoCodecDRMPRIME::FilterOpen(const std::string& filters, AVPixelForma
 
   memset(par, 0, sizeof(*par));
   par->format = AV_PIX_FMT_NONE;
-  par->hw_frames_ctx = m_pFrame->hw_frames_ctx;
+
+  if (pix_fmt == AV_PIX_FMT_DRM_PRIME)
+  {
+    if (av_hwdevice_ctx_create(&m_hw_device_ref, AV_HWDEVICE_TYPE_DRM, NULL, NULL, 0) < 0) {
+        CLog::Log(LOGERROR, "Failed to create DRM device");
+        return false;
+    }
+    m_hw_frames_ref = av_hwframe_ctx_alloc(m_hw_device_ref);
+    if (!m_hw_frames_ref) {
+      CLog::Log(LOGERROR, "Failed to allocate hwframe context");
+        av_buffer_unref(&m_hw_device_ref);
+        return false;
+    }
+    par->hw_frames_ctx = av_buffer_ref(m_hw_frames_ref);
+  }
 
   result = av_buffersrc_parameters_set(m_pFilterIn, par);
   if (result < 0)
@@ -876,6 +890,8 @@ void CDVDVideoCodecDRMPRIME::FilterClose()
     m_pFilterIn = nullptr;
     m_pFilterOut = nullptr;
     m_pFilterGraph = nullptr;
+    av_buffer_unref(&m_hw_frames_ref);
+    av_buffer_unref(&m_hw_device_ref);
   }
 }
 
diff --git a/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.h b/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.h
index 55675c3c2ea8..64c6c029f3d4 100644
--- a/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.h
+++ b/xbmc/cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodecDRMPRIME.h
@@ -64,4 +64,6 @@ protected:
   AVFilterContext* m_pFilterIn = nullptr;
   AVFilterContext* m_pFilterOut = nullptr;
   std::shared_ptr<IVideoBufferPool> m_videoBufferPool;
+  AVBufferRef *m_hw_device_ref = nullptr;
+  AVBufferRef *m_hw_frames_ref = nullptr;
 };
-- 
2.39.5

