[FFmpeg-devel] [PATCH 04/24] lavfi/vf_hwupload: validate the hw device in init

Anton Khirnov anton at khirnov.net
Sat Oct 12 20:02:19 EEST 2024


Rather than query_formats(). Init is a more appropriate place, as
query_formats() is supposed to be free of side-effects.
---
 libavfilter/vf_hwupload.c | 49 ++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 21 deletions(-)

diff --git a/libavfilter/vf_hwupload.c b/libavfilter/vf_hwupload.c
index 3afd67012f..70ee262c09 100644
--- a/libavfilter/vf_hwupload.c
+++ b/libavfilter/vf_hwupload.c
@@ -39,6 +39,33 @@ typedef struct HWUploadContext {
     char *device_type;
 } HWUploadContext;
 
+static int hwupload_init(AVFilterContext *avctx)
+{
+    HWUploadContext *ctx = avctx->priv;
+    int err;
+
+    if (!avctx->hw_device_ctx) {
+        av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
+               "to upload frames to.\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (ctx->device_type) {
+        err = av_hwdevice_ctx_create_derived(
+            &ctx->hwdevice_ref,
+            av_hwdevice_find_type_by_name(ctx->device_type),
+            avctx->hw_device_ctx, 0);
+        if (err < 0)
+            return err;
+    } else {
+        ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
+        if (!ctx->hwdevice_ref)
+            return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
 static int hwupload_query_formats(AVFilterContext *avctx)
 {
     HWUploadContext *ctx = avctx->priv;
@@ -47,27 +74,6 @@ static int hwupload_query_formats(AVFilterContext *avctx)
     AVFilterFormats *input_formats = NULL;
     int err, i;
 
-    if (ctx->hwdevice_ref) {
-        /* We already have a specified device. */
-    } else if (avctx->hw_device_ctx) {
-        if (ctx->device_type) {
-            err = av_hwdevice_ctx_create_derived(
-                &ctx->hwdevice_ref,
-                av_hwdevice_find_type_by_name(ctx->device_type),
-                avctx->hw_device_ctx, 0);
-            if (err < 0)
-                return err;
-        } else {
-            ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
-            if (!ctx->hwdevice_ref)
-                return AVERROR(ENOMEM);
-        }
-    } else {
-        av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
-               "to upload frames to.\n");
-        return AVERROR(EINVAL);
-    }
-
     constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
     if (!constraints) {
         err = AVERROR(EINVAL);
@@ -253,6 +259,7 @@ static const AVFilterPad hwupload_outputs[] = {
 const AVFilter ff_vf_hwupload = {
     .name          = "hwupload",
     .description   = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
+    .init          = hwupload_init,
     .uninit        = hwupload_uninit,
     .priv_size     = sizeof(HWUploadContext),
     .priv_class    = &hwupload_class,
-- 
2.43.0



More information about the ffmpeg-devel mailing list