If options are set then the user definitely intends to create a new
device, so we shouldn't attempt to return an existing one.
---
 libavutil/hwcontext.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
index ab9ad3703e..c4e01e0e78 100644
--- a/libavutil/hwcontext.c
+++ b/libavutil/hwcontext.c
@@ -653,18 +653,24 @@ int av_hwdevice_ctx_create_derived_opts(AVBufferRef 
**dst_ref_ptr,
     AVHWDeviceContext *dst_ctx, *tmp_ctx;
     int ret = 0;

-    tmp_ref = src_ref;
-    while (tmp_ref) {
-        tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
-        if (tmp_ctx->type == type) {
-            dst_ref = av_buffer_ref(tmp_ref);
-            if (!dst_ref) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
+    // If we were derived (possibly transitively) from a device of the
+    // target type then we want to return that original device, unless
+    // options are set in which case we can skip this check because it
+    // is definitely intended to create a new device.
+    if (!options) {
+        tmp_ref = src_ref;
+        while (tmp_ref) {
+            tmp_ctx = (AVHWDeviceContext*)tmp_ref->data;
+            if (tmp_ctx->type == type) {
+                dst_ref = av_buffer_ref(tmp_ref);
+                if (!dst_ref) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+                goto done;
             }
-            goto done;
+            tmp_ref = tmp_ctx->internal->source_device;
         }
-        tmp_ref = tmp_ctx->internal->source_device;
     }

     dst_ref = av_hwdevice_ctx_alloc(type);
--
2.35.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to