Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 2/2] fftools/ffmpeg: supply hw_device_ctx to filters before initializing them
Date: Fri, 17 Mar 2023 13:42:30 +0100
Message-ID: <20230317124230.8835-2-anton@khirnov.net> (raw)
In-Reply-To: <20230317124230.8835-1-anton@khirnov.net>

This is more correct, but was not possible before the recently-added
filtergraph parsing API.

Also, only pass hw devices to filters that are flagged as capable of
using them.
---
 fftools/ffmpeg.h        |  7 ++++++-
 fftools/ffmpeg_filter.c | 24 ++++++++++++++++++------
 fftools/ffmpeg_hw.c     | 21 +++++----------------
 3 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 4d4433f5bad..2020770312f 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -802,7 +802,12 @@ void hw_device_free_all(void);
 
 int hw_device_setup_for_decode(InputStream *ist);
 int hw_device_setup_for_encode(OutputStream *ost);
-int hw_device_setup_for_filter(FilterGraph *fg);
+/**
+ * Get a hardware device to be used with this filtergraph.
+ * The returned reference is owned by the callee, the caller
+ * must ref it explicitly for long-term use.
+ */
+AVBufferRef *hw_device_for_filter(FilterGraph *fg);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 3504a3cc0ae..8f924a74538 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -440,7 +440,8 @@ static int graph_opts_apply(AVFilterGraphSegment *seg)
 }
 
 static int graph_parse(AVFilterGraph *graph, const char *desc,
-                       AVFilterInOut **inputs, AVFilterInOut **outputs)
+                       AVFilterInOut **inputs, AVFilterInOut **outputs,
+                       AVBufferRef *hw_device)
 {
     AVFilterGraphSegment *seg;
     int ret;
@@ -453,6 +454,18 @@ static int graph_parse(AVFilterGraph *graph, const char *desc,
     if (ret < 0)
         goto fail;
 
+    if (hw_device) {
+        for (int i = 0; i < graph->nb_filters; i++) {
+            AVFilterContext *f = graph->filters[i];
+
+            if (!(f->filter->flags & AVFILTER_FLAG_HWDEVICE))
+                continue;
+            f->hw_device_ctx = av_buffer_ref(hw_device);
+            if (!f->hw_device_ctx)
+                return AVERROR(ENOMEM);
+        }
+    }
+
     ret = graph_opts_apply(seg);
     if (ret < 0)
         goto fail;
@@ -477,7 +490,7 @@ int init_complex_filtergraph(FilterGraph *fg)
         return AVERROR(ENOMEM);
     graph->nb_threads = 1;
 
-    ret = graph_parse(graph, fg->graph_desc, &inputs, &outputs);
+    ret = graph_parse(graph, fg->graph_desc, &inputs, &outputs, NULL);
     if (ret < 0)
         goto fail;
 
@@ -1111,6 +1124,7 @@ static int graph_is_meta(AVFilterGraph *graph)
 
 int configure_filtergraph(FilterGraph *fg)
 {
+    AVBufferRef *hw_device;
     AVFilterInOut *inputs, *outputs, *cur;
     int ret, i, simple = filtergraph_is_simple(fg);
     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
@@ -1154,11 +1168,9 @@ int configure_filtergraph(FilterGraph *fg)
         fg->graph->nb_threads = filter_complex_nbthreads;
     }
 
-    if ((ret = graph_parse(fg->graph, graph_desc, &inputs, &outputs)) < 0)
-        goto fail;
+    hw_device = hw_device_for_filter(fg);
 
-    ret = hw_device_setup_for_filter(fg);
-    if (ret < 0)
+    if ((ret = graph_parse(fg->graph, graph_desc, &inputs, &outputs, hw_device)) < 0)
         goto fail;
 
     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c
index 88fa7824701..a3351d98b59 100644
--- a/fftools/ffmpeg_hw.c
+++ b/fftools/ffmpeg_hw.c
@@ -548,17 +548,14 @@ int hwaccel_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
-int hw_device_setup_for_filter(FilterGraph *fg)
+AVBufferRef *hw_device_for_filter(FilterGraph *fg)
 {
-    HWDevice *dev;
-    int i;
-
     // Pick the last hardware device if the user doesn't pick the device for
     // filters explicitly with the filter_hw_device option.
     if (filter_hw_device)
-        dev = filter_hw_device;
+        return filter_hw_device->device_ref;
     else if (nb_hw_devices > 0) {
-        dev = hw_devices[nb_hw_devices - 1];
+        HWDevice *dev = hw_devices[nb_hw_devices - 1];
 
         if (nb_hw_devices > 1)
             av_log(NULL, AV_LOG_WARNING, "There are %d hardware devices. device "
@@ -567,17 +564,9 @@ int hw_device_setup_for_filter(FilterGraph *fg)
                    "%s is not usable for filters.\n",
                    nb_hw_devices, dev->name,
                    av_hwdevice_get_type_name(dev->type), dev->name);
-    } else
-        dev = NULL;
 
-    if (dev) {
-        for (i = 0; i < fg->graph->nb_filters; i++) {
-            fg->graph->filters[i]->hw_device_ctx =
-                av_buffer_ref(dev->device_ref);
-            if (!fg->graph->filters[i]->hw_device_ctx)
-                return AVERROR(ENOMEM);
-        }
+        return dev->device_ref;
     }
 
-    return 0;
+    return NULL;
 }
-- 
2.39.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-request@ffmpeg.org with subject "unsubscribe".

      reply	other threads:[~2023-03-17 12:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-17 12:42 [FFmpeg-devel] [PATCH 1/2] lavfi: add a flag for filters able to work with hw_device_ctx Anton Khirnov
2023-03-17 12:42 ` Anton Khirnov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230317124230.8835-2-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git