From patchwork Fri Mar 17 12:42:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40708 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp526771pzb; Fri, 17 Mar 2023 05:42:49 -0700 (PDT) X-Google-Smtp-Source: AK7set/ITBVrp255C+DSB+pRbyDCksQ7/8fvURiCWhSQrxbb9B7rZrhXax4MyQHbCqdOX+ca9/Cy X-Received: by 2002:a17:906:e093:b0:8b1:7e88:c20f with SMTP id gh19-20020a170906e09300b008b17e88c20fmr16142867ejb.15.1679056969797; Fri, 17 Mar 2023 05:42:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679056969; cv=none; d=google.com; s=arc-20160816; b=qXviBC3ygwBfa57QJXTpDy4jNfNR4ruk3wdC4JFH6fpjC6NX7dSFEYAB0As60qavWv caVFmXJBOp8XjC1JLv+VfPeaRFjQJMNQaO76ce4oW6bU1idtoP8EFHgIMqQsoQUBi6eX Zf5E5ShcSIkxvfKd+uJdrlGxoPhUv2G7a/wka3NshxONsxbWAI5VzmalQaMSvM3OxE3S mWY8++6UVqPA4kpRvrMKcdQQKBtPQE9dblwbfG8hbGp/6HrfonAstXElJ/+jAGGS8fdC Bg4nJnCA4WN9qdFVT5sHGkJ4n8bUWbhtTRoz3fbGMMHQvIXULXNimrspEskIlycoogc/ IHmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=qDnNNUJHLttah874WSpa9IP+p/4GYSX2E1O4qkkhS+U=; b=opyh1j3SKvrPHcqrnylk8aV0GCYvrAtgL+k2eKGP+f22LA0av8icLiHeeliHN+QO9K U6DimA9QF8EKMyGOiCLQjHn401QBfBPuRUvWJaP//vBUXiRMD89A+H4fFu07xT1RANMS Pb5+cr+WOIGkoYhm7IkyQZ0QwNvfLbXXPqv/0AMEL8oZ1T2/DJk0usxXpfHpaRsXUZ20 Dhg0dgC9h/FovoF+V0zU40HrFH64gTp+3kofCTFsoqk3alrL4rheGhCxN1y2uPdawXnB y7v5soIWD5BRn9ltDv4zos6LAkys2QOamSv7v2zw6SnBtm/gyHReyJ3ESGb5wSqpq1Ab scvA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a3-20020a1709062b0300b0093105432da9si2147122ejg.151.2023.03.17.05.42.49; Fri, 17 Mar 2023 05:42:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4426068C1CD; Fri, 17 Mar 2023 14:42:46 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DFD3268C1AD for ; Fri, 17 Mar 2023 14:42:39 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 915A62404EE for ; Fri, 17 Mar 2023 13:42:39 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id N6BTuE2MQF-i for ; Fri, 17 Mar 2023 13:42:38 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 777CA240178 for ; Fri, 17 Mar 2023 13:42:38 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 355633A039B for ; Fri, 17 Mar 2023 13:42:32 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 Mar 2023 13:42:30 +0100 Message-Id: <20230317124230.8835-2-anton@khirnov.net> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230317124230.8835-1-anton@khirnov.net> References: <20230317124230.8835-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] fftools/ffmpeg: supply hw_device_ctx to filters before initializing them X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: x7t8WkEYvTIZ 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; }