From patchwork Thu Nov 25 02:41:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 31651 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp11555206iob; Wed, 24 Nov 2021 18:41:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxNDMmsFyEx9RBPu3XLlhaLNhPlq+hbSScpVqtTtDLQyR0aWDSxVWB39NNRHJvtlynDeFsq X-Received: by 2002:a05:6402:514d:: with SMTP id n13mr35321476edd.380.1637808105777; Wed, 24 Nov 2021 18:41:45 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g18si5152017edb.117.2021.11.24.18.41.45; Wed, 24 Nov 2021 18:41:45 -0800 (PST) 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=UZAAI6cj; 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 58D9A689B7F; Thu, 25 Nov 2021 04:41:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02olkn2038.outbound.protection.outlook.com [40.92.44.38]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1BE0368A13F for ; Thu, 25 Nov 2021 04:41:35 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X+BZk4xwScrDwHR854UYRC7yxN2QDFfVyHV7ZCvMXuQFIep5/w/pD+FRK8u3LtvuWOzlLFwxskejTrDhoVgMsWK1Pk6PNwyP+Uzh5y20ZcbAyrFPSKaZ+cCdsBivCzbNqXCaUq4ELIRIQ+MoMjayotX1bCA3UBJqcUrGniCXhGieaaUrQD1UvyyTPqBpe3d7dZMXEFg0DGmI99AYMxFtuYa5ttkWMZAJmXxZxnroucStDzc7hax4n/Ae9eRgjKfFAG2hBdSMp+vs8XqQTvRS0r8WgE5/fAJBOrbbf8FVCHo0h7xdUtOAlQXKiXMHLqmsfkMbwcV4DRAwQiff9yYR4Q== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PioSj6X9ivg0HsBOPw09AYaGfT7IbkAx8AvUlEK+N0o=; b=QCOUTQCK+yVseAMfTQjoLSuiy4xvFCbrz3ggA9IEcn1jiPF619Zt74CJrTHAAd9cb0H2s2lWcf7NQxNm7hd4RvATvZH3SF+hmb2jw7Co80eTod68Boum/Sl7C5kuot3ihbFVEMkUCAoKr1on/TV8NfR4VZG6ThTKi6W5I2yWY/bVdF0uuqlex/raB/7zQ+/SRlbjUlTFaRxKNM0w6vtMwDLngxb0CVUZ2TzZeVYqA2shMnMV5rK6yo5aOQg7u9EHFgS28dlp3lo6MrPmWqNY013AlC2MxA8hzwzotzMeSp8lO04ZxAc503wooSef1dFv/6X42W8LnQfQLtAPdh37pA== 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=PioSj6X9ivg0HsBOPw09AYaGfT7IbkAx8AvUlEK+N0o=; b=UZAAI6cjg4uMZmRxJDdIgLG2tltVzozVwdEwlDnQPVMu+xjJnhiAiHBcJ1MFV6t8q1kHloUHj8haVKZlIaOjlbiQKn4aqn9iPBLEif8oIBQTwjbVLjCfMj2xFCOyiNhA0vG/LxwDOIJ0OoHjTSBsABl+h/m8W5U5Ks1DyIqYWP3gF9/+WnQKtgMUAgyBCDM0f+Wcar43m8eGFuizCMhV0lvORlntWGc/bWLZ8Nc6YuLGMOsmCc7FSyM6iW7H3JiVRhk0Nspr76zPbP2st9UQVRr0vrf2J7+hm5QRuGZD5qeNcYwd5sAYxT0h0jwaMsuLF1yTG1cGq1xAhKMOg9QYVw== Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:b::20) by DM8P223MB0079.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4734.21; Thu, 25 Nov 2021 02:41:32 +0000 Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::8c05:4078:21a:53fb]) by DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::8c05:4078:21a:53fb%5]) with mapi id 15.20.4734.020; Thu, 25 Nov 2021 02:41:32 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v4 1/1] avutils/hwcontext: When deriving a hwdevice, search for existing device in both directions Thread-Index: AQGnQV+GaP/9mUjFEoNhMBDgx+itSwJHT6uB Date: Thu, 25 Nov 2021 02:41:32 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [gMxhQnZ2tTdM998eBMmgrhIXyrLiOfDm] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 2d70d5a9-9a93-4228-00df-08d9afbd1759 x-ms-traffictypediagnostic: DM8P223MB0079: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Vv2eB9nKioVjDxdNi2XYJt6ONDsmIL2NTG/7V2ZeYGzY3Rm0UkcHBhPlwpiW9ht5gQB59vTJAlUamaY4vjETk2Rm1uHTam6n6UyHeujOJyyAOSbx4X7lvh4SDznKNwZEwSDiRqHcn1k093CjYV4gzXPIppv489HJjVlcDA45H78GDk8T9NW4dMCFe8fKVqp2xEINVF3uR6x96IP1whppXI1+bA8nImBKtIo97DpmdWDjOCbffoN4lN/yFSjDJGesJgrIb+3yczZjI/sd1AbO3y+bFFtif5C98IXGuor7hJlRKhnRhkeUV7/oQyWiAbJLNMYevr6Wugz7l2EHV1CuqTK3OtsWDV5Edout53Zx0eWXu4MxQAXAINoOnpsWuK0Q19SfbFQ/C6ie4xI5neUzfiCBSbdwtOohnSZ08hQCekh27Ga2pOgNRVhS7aAROLrnPjyU9kl4HPbdtIiFtSobJQnB6F4hzDcQLaAVAYSbJFP8IKnBMlz18PiJUJsNKNc6I/qNudHGwwZG5yrE2Hc4aESYoBSzY4gKbfyyqxXxempwgnhqfUbVusX19VcoYMjM984x8v/FfXcDkLCkNXLvvQ== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: Arq5FV1LFjJDUv/epE5UWs0VrO5JCpdyEMnDrGl2Z/s+E45zQ63CBLRUOADJeQFX1JGqojmPYIPGOGMbef/M8A1RE1DLe/T8CnaaH9kvkhrHdHTQeZh+6qUkzWAP7wPerbF40tlimnaYeSMshrESWfhpGjJovdHoGEfJ9CZC8BoDqHRQA9l3b5jqjl9kj9fc7uxXcJDze9ZBCoJq2u2VHcIQUV9dxaQWYqHXnw0bOozJP1mQKYBKwU6kduPvn+X/afE1xOfKBXDLxxAlXRCkXS7smycnwNyDSifjperQ5bpQz183fzFHai0wCqt/T3rGI6dUZgDS0vhzke75WFgybX2ib15fsiidL2UIO8BBbEgT20ZzYJZOKw7srEJORkToqPaydGu0kEOD2SIi0W8AuSmCzQ+R2Ry4xm/Erey++q2GkfHfDnIVArFnU4+WtoTxcOoNj85Ek6sOKM5wzhsLwjYSvIwJBDzmR/kXnReLrqidiKf9LeCBVJD4XWgA7S5MIsmLj/oU6apVNbz1MewbBbRveg0L1gA+PFNA8SpKEqWGo9/7dwfNkQrOfnCGSgUAzNXBn88hx7Z18Om1zVEtFpJB+Ai7NI0/7ZOUHsvhYuw1eZStlf/LN+KsgZyE9F6FNIC7rLZpeQNjlCARlHv/6hs8tV9K+7dLPXpPZd68Td1Y0QDyG8FYGHEWidHyQpxNLcVWmd/hGgTiizo90OoovQ== MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-3912-16-msonline-outlook-1fc6d.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 2d70d5a9-9a93-4228-00df-08d9afbd1759 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Nov 2021 02:41:32.3620 (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: DM8P223MB0079 Subject: [FFmpeg-devel] [PATCH v4 1/1] 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: TlGFt+izrXSZ 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 --- v4: Fixed braces code style libavutil/hwcontext.c | 38 ++++++++++++++++++++++++++++++++++ libavutil/hwcontext.h | 1 + libavutil/hwcontext_internal.h | 6 ++++++ libavutil/hwcontext_qsv.c | 13 +++++++++--- 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index 31c7840dba..1a50635018 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 NULL; +} + 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 268be9f8a1..3c140e4c8b 100644 --- a/libavutil/hwcontext_qsv.c +++ b/libavutil/hwcontext_qsv.c @@ -223,7 +223,7 @@ static void qsv_frames_uninit(AVHWFramesContext *ctx) av_buffer_unref(&s->child_frames_ref); } -static void qsv_pool_release_dummy(void *opaque, uint8_t *data) +static void qsv_release_dummy(void *opaque, uint8_t *data) { } @@ -236,7 +236,7 @@ static AVBufferRef *qsv_pool_alloc(void *opaque, size_t size) if (s->nb_surfaces_used < hwctx->nb_surfaces) { s->nb_surfaces_used++; return av_buffer_create((uint8_t*)(s->surfaces_internal + s->nb_surfaces_used - 1), - sizeof(*hwctx->surfaces), qsv_pool_release_dummy, NULL, 0); + sizeof(*hwctx->surfaces), qsv_release_dummy, NULL, 0); } return NULL; @@ -1526,8 +1526,15 @@ static int qsv_device_create(AVHWDeviceContext *ctx, const char *device, child_device = (AVHWDeviceContext*)priv->child_device_ctx->data; impl = choose_implementation(device, child_device_type); + 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), qsv_release_dummy, 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 = {