PR #20979 opened by ArazIusubov
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20979
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20979.patch

The current behavior allows creating a second AMF hw_device_ctx, which may lead 
to incorrect behavior or failures.
This patch fixes that assumption and prevents multiple hardware device contexts 
from being created.


>From cc26a2ff1054987bd0647fa4e698e85605c984be Mon Sep 17 00:00:00 2001
From: Araz Iusubov <[email protected]>
Date: Thu, 20 Nov 2025 13:34:21 +0100
Subject: [PATCH] avcodec/amf: fix hw_device_ctx handling

---
 libavcodec/amfdec.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libavcodec/amfdec.c b/libavcodec/amfdec.c
index 1a2eb9392c..eb182114fa 100644
--- a/libavcodec/amfdec.c
+++ b/libavcodec/amfdec.c
@@ -274,15 +274,17 @@ static int amf_decode_init(AVCodecContext *avctx)
     if (!ctx->in_pkt)
         return AVERROR(ENOMEM);
 
-    if  (avctx->hw_device_ctx && !avctx->hw_frames_ctx) {
+    if  (avctx->hw_device_ctx) {
         AVHWDeviceContext   *hwdev_ctx;
         hwdev_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
         if (hwdev_ctx->type == AV_HWDEVICE_TYPE_AMF)
         {
             ctx->device_ctx_ref = av_buffer_ref(avctx->hw_device_ctx);
-            avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
-
-            AMF_GOTO_FAIL_IF_FALSE(avctx, !!avctx->hw_frames_ctx, 
AVERROR(ENOMEM), "av_hwframe_ctx_alloc failed\n");
+            if (!avctx->hw_frames_ctx)
+            {
+                avctx->hw_frames_ctx = 
av_hwframe_ctx_alloc(avctx->hw_device_ctx);
+                AMF_GOTO_FAIL_IF_FALSE(avctx, !!avctx->hw_frames_ctx, 
AVERROR(ENOMEM), "av_hwframe_ctx_alloc failed\n");
+            }
         } else {
             ret = av_hwdevice_ctx_create_derived(&ctx->device_ctx_ref, 
AV_HWDEVICE_TYPE_AMF, avctx->hw_device_ctx, 0);
             AMF_GOTO_FAIL_IF_FALSE(avctx, ret == 0, ret, "Failed to create 
derived AMF device context: %s\n", av_err2str(ret));
-- 
2.49.1

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

Reply via email to