From patchwork Tue Aug 10 09:52:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 29389 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:8e8b:0:0:0:0:0 with SMTP id q133csp286110iod; Tue, 10 Aug 2021 02:52:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzlWEuAGAah0SQx4VIrf0u+Ck7tv5o9NQE2CLfSg+63fr1U+VyStvda+ZA1wkFJ/w379WQ X-Received: by 2002:aa7:cd77:: with SMTP id ca23mr3867290edb.92.1628589167847; Tue, 10 Aug 2021 02:52:47 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id dk3si14013484edb.202.2021.08.10.02.52.47; Tue, 10 Aug 2021 02:52:47 -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; dkim=neutral (body hash did not verify) header.i=@hotmail.com header.s=selector1 header.b=nLQ0OEOf; arc=fail (body hash mismatch); 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hotmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BA99468A3B0; Tue, 10 Aug 2021 12:52:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11olkn2060.outbound.protection.outlook.com [40.92.19.60]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 614C968085C for ; Tue, 10 Aug 2021 12:52:37 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QgJLjhcunyJWPIyBkvbHsC/fuZgDS+YKNpK0H7fXgmcgvd20chDAYOrqu+08HwpqenL/OUC3j6Hx6m3bkxOvNyf01kzs12nKk74C0Iyxjo9obavhrqKCfFChdBv/Wu+P44eULKzUqJflPEgBZinGt4Ld+ha0/++yqr8xO2LGFUb1U+V0jhgxSltAegk9KQ5MIRfc4HcX+IZCM2nKtYZfsToUt+TqPXE6Rks2qrGEMF4xU6v7BI+dG52rqeIdDq1QDKvGvzeduvTA8pafh+YTDJH5yvbyBbLM5oLhT2WL72og44SBzylKlHrDMdQx1gXAzgFBHJtBAnwAuaFB2JNeAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qVnJVn+FcrQ4qDhfjYzDIuE5j6lImO3hyvxlxGm+aT4=; b=DiCeSpPey0oZwjzn9fWntNxJZILCmQtSKHujZt0H4H6CBnKh81NjtadA2qxnxqwNHC3A2HLpEydUV7IvMBRvCnLiFof+VML7dOis2UEWlPRyMkJr6DjbbOmbjxeq8mJMyF3nUhG4bwTy0xExlQdJL7L0QAd+1UDfdc3mbuX7k3C6JSfL6MZqdcg9pv24XfYzX5RgdnE6mqgLDLPWsccUHcQ8oA5qMtvvBrm4lKcTchQ4hPC0xAjSaMkWlqgk9G/lcl3Fd458xv3DUbVcZBzAaXusob1LUj9+6T1BxlVSkLGZJGKePN5iDSD+msPhHwpous7SZIfwRNAzsmnzzCKaBg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qVnJVn+FcrQ4qDhfjYzDIuE5j6lImO3hyvxlxGm+aT4=; b=nLQ0OEOf4Z58Zh91ZHbFfOc9I2v2W51qULVF8WqNiza3M59GZCkiJtnm6n2yif76maE4zQ85maXK8JDnqYwvdpfXlKxsUKrEgnKgHcAYjWMmuZR7Z8P5MHQwt83PRmCAlAO4zeSqqzAqaORjtDNQvcrl7h90hoaEshOUIFAwsGjza6s2+M6B0wJQM13Qu8u0kJDqi0xO9551Fm496OETa3bmi4VV4zDEHL8FrhjDJLoR7Rm/aLmYe+q4hf8yBKrysSHDUasuc7lfPJm66MXkmL6wZXGJ4sfuYCBhVaITNlxYap81lxQ0nG9i7s1P20aXM7IBiGlxwzT4Q8ZzIQ5jbw== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5503.namprd04.prod.outlook.com (2603:10b6:208:e4::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.21; Tue, 10 Aug 2021 09:52:34 +0000 Received: from MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::5d83:1c26:c2b1:3a30]) by MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::5d83:1c26:c2b1:3a30%6]) with mapi id 15.20.4394.023; Tue, 10 Aug 2021 09:52:34 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v2] avutils/hwcontext: When deriving a hwdevice, search for existing device in both directions Thread-Index: AdeNvRYcH4Yz1suZS0OVMhk1r4SI/Q== Date: Tue, 10 Aug 2021 09:52:34 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [JsMV12hlOEAiQR1aHjosPJQVXebic1LF] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 1eabf677-1487-47eb-8ea3-08d95be49422 x-ms-traffictypediagnostic: MN2PR04MB5503: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: AzHwUo2Z6jpK/szCuTFc28N4FrAnrQ/VOs8CcYHtrwKFLdZProklPZmF//+1DgIl4V9U+Q+1z1TeIbxAmf+YB5mIvfjLv5e3CtVvGfTtoplOCm/f/sHXu/ujhYBbdfKAFkAYWNq2vGxAT5LGKpnSv/7p4AsySRjEoYPRaLp83OCmEx64ydbouRkiy5xpEr8Dkjv41oB1QADoA/9dofS0EkdcsP9Cw2WQI54ru0hngvhG6kMh67igFiwNT1RufOnKB/Pif3qYGEF7OiC1YL+of07vY6seOBQI4ttsphRQWij0BaLcHWXguXOr/YB885dgxm28jBLs0Ai2UNQYjEFvdmvuw/kMPUiyBGXa7REyxKk4KyBo/pNHSo4uJ0/0K7Oyfkvzz8dWrGQR+b1QRkoWhi8kgOO2fygpx5WI5/3JwKoALvMaQdpAr+DKZ4A/kS1g x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: /7XzrVtfPMrTLl8BeD1lxErs2cLfxPWlzIGy23msmi/OclhQj33X7Kju5syY6PNSptZ8Tnkh2aN0oDVY9yDdU0Ie0COk01HxH0A9PlfC9/fhSzzrQNmE1ECy2Fv+IoMZijwVnfPRG5RQpukY8JmB9g== x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-3174-20-msonline-outlook-529c7.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN2PR04MB5981.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 1eabf677-1487-47eb-8ea3-08d95be49422 X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Aug 2021 09:52:34.4820 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5503 Subject: [FFmpeg-devel] [PATCH v2] avutils/hwcontext: When deriving a hwdevice, search for existing device in both directions 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: fgMXuWpK/N6M The test /libavutil/tests/hwdevice checks that when deriving a device from a source device and then deriving back to the type of the source device, the result is matching the original source device, i.e. the derivation mechanism doesn't create a new device in this case. Previously, this test was usually passed, but only due to two different kind of flaws: 1. The test covers only a single level of derivation (and back) It derives device Y from device X and then Y back to the type of X and checks whether the result matches X. What it doesn't check for, are longer chains of derivation like: CUDA1 > OpenCL2 > CUDA3 and then back to OpenCL4 In that case, the second derivation returns the first device (CUDA3 == CUDA1), but when deriving OpenCL4, hwcontext.c was creating a new OpenCL4 context instead of returning OpenCL2, because there was no link from CUDA1 to OpenCL2 (only backwards from OpenCL2 to CUDA1) If the test would check for two levels of derivation, it would have failed. This patch fixes those (yet untested) cases by introducing forward references (derived_device) in addition to the existing back references (source_device). 2. hwcontext_qsv didn't properly set the source_device In case of QSV, hwcontext_qsv creates a source context internally (vaapi, dxva2 or d3d11va) without calling av_hwdevice_ctx_create_derived and without setting source_device. This way, the hwcontext test ran successful, but what practically happened, was that - for example - deriving vaapi from qsv didn't return the original underlying vaapi device and a new one was created instead: Exactly what the test is intended to detect and prevent. It just couldn't do so, because the original device was hidden (= not set as the source_device of the QSV device). This patch properly makes these setting and fixes all derivation scenarios. (at a later stage, /libavutil/tests/hwdevice should be extended to check longer derivation chains as well) Signed-off-by: softworkz --- v2: allow storing multiple derived devices in a device ctx; add checks for oom libavutil/hwcontext.c | 38 ++++++++++++++++++++++++++++++++++ libavutil/hwcontext.h | 1 + libavutil/hwcontext_internal.h | 6 ++++++ libavutil/hwcontext_qsv.c | 10 ++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index d13d0f7c9b..7f4e541553 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -122,6 +122,7 @@ static const AVClass hwdevice_ctx_class = { static void hwdevice_ctx_free(void *opaque, uint8_t *data) { AVHWDeviceContext *ctx = (AVHWDeviceContext*)data; + int i; /* uninit might still want access the hw context and the user * free() callback might destroy it, so uninit has to be called first */ @@ -132,6 +133,8 @@ static void hwdevice_ctx_free(void *opaque, uint8_t *data) ctx->free(ctx); av_buffer_unref(&ctx->internal->source_device); + for (i = 0; i < AV_HWDEVICE_TYPE_NB; i++) + av_buffer_unref(&ctx->internal->derived_devices[i]); av_freep(&ctx->hwctx); av_freep(&ctx->internal->priv); @@ -643,6 +646,26 @@ fail: return ret; } +static AVBufferRef* find_derived_hwdevice_ctx(AVBufferRef *src_ref, enum AVHWDeviceType type) +{ + AVBufferRef *tmp_ref; + AVHWDeviceContext *src_ctx; + int i; + + src_ctx = (AVHWDeviceContext*)src_ref->data; + if (src_ctx->type == type) + return src_ref; + + for (i = 0; i < AV_HWDEVICE_TYPE_NB; i++) + if (src_ctx->internal->derived_devices[i]) { + tmp_ref = find_derived_hwdevice_ctx(src_ctx->internal->derived_devices[i], type); + if (tmp_ref) + return tmp_ref; + } + + return 0; +} + int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr, enum AVHWDeviceType type, AVBufferRef *src_ref, @@ -666,6 +689,16 @@ int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr, tmp_ref = tmp_ctx->internal->source_device; } + tmp_ref = find_derived_hwdevice_ctx(src_ref, type); + if (tmp_ref) { + dst_ref = av_buffer_ref(tmp_ref); + if (!dst_ref) { + ret = AVERROR(ENOMEM); + goto fail; + } + goto done; + } + dst_ref = av_hwdevice_ctx_alloc(type); if (!dst_ref) { ret = AVERROR(ENOMEM); @@ -687,6 +720,11 @@ int av_hwdevice_ctx_create_derived_opts(AVBufferRef **dst_ref_ptr, ret = AVERROR(ENOMEM); goto fail; } + tmp_ctx->internal->derived_devices[type] = av_buffer_ref(dst_ref); + if (!tmp_ctx->internal->derived_devices[type]) { + ret = AVERROR(ENOMEM); + goto fail; + } ret = av_hwdevice_ctx_init(dst_ref); if (ret < 0) goto fail; diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h index 04d19d89c2..56077963e6 100644 --- a/libavutil/hwcontext.h +++ b/libavutil/hwcontext.h @@ -37,6 +37,7 @@ enum AVHWDeviceType { AV_HWDEVICE_TYPE_OPENCL, AV_HWDEVICE_TYPE_MEDIACODEC, AV_HWDEVICE_TYPE_VULKAN, + AV_HWDEVICE_TYPE_NB, ///< number of hw device types }; typedef struct AVHWDeviceInternal AVHWDeviceInternal; diff --git a/libavutil/hwcontext_internal.h b/libavutil/hwcontext_internal.h index e6266494ac..f6fb67c491 100644 --- a/libavutil/hwcontext_internal.h +++ b/libavutil/hwcontext_internal.h @@ -109,6 +109,12 @@ struct AVHWDeviceInternal { * context it was derived from. */ AVBufferRef *source_device; + + /** + * An array of reference to device contexts which + * were derived from this device. + */ + AVBufferRef *derived_devices[AV_HWDEVICE_TYPE_NB]; }; struct AVHWFramesInternal { diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c index 08a6e0ee1c..b26910f95d 100644 --- a/libavutil/hwcontext_qsv.c +++ b/libavutil/hwcontext_qsv.c @@ -1268,8 +1268,16 @@ static int qsv_device_create(AVHWDeviceContext *ctx, const char *device, child_device = (AVHWDeviceContext*)priv->child_device_ctx->data; impl = choose_implementation(device); + ret = qsv_device_derive_from_child(ctx, impl, child_device, 0); + if (ret >= 0) { + ctx->internal->source_device = av_buffer_ref(priv->child_device_ctx); + child_device->internal->derived_devices[ctx->type] = av_buffer_create((uint8_t*)ctx, sizeof(*ctx), 0, ctx, 0); + if (!child_device->internal->derived_devices[ctx->type]) { + return AVERROR(ENOMEM); + } + } - return qsv_device_derive_from_child(ctx, impl, child_device, 0); + return ret; } const HWContextType ff_hwcontext_type_qsv = {