From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id 9D2444BFE7
	for <ffmpegdev@gitmailbox.com>; Tue,  1 Apr 2025 14:35:46 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 48101687D16;
	Tue,  1 Apr 2025 17:35:42 +0300 (EEST)
Received: from SY8PR01CU002.outbound.protection.outlook.com
 (mail-australiaeastazolkn19010020.outbound.protection.outlook.com
 [52.103.72.20])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2B730687A92
 for <ffmpeg-devel@ffmpeg.org>; Tue,  1 Apr 2025 17:35:35 +0300 (EEST)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
 b=ZTzQvccorSjCnT4DHYpmB/wX6VS6ATrnEm+yh3pMspJfrJScNCkhwOC134I88YHUhBqXLEi/YSDRZLvAdwwsr34q1LkfULNqf/4EiIpm3YvuUCwZY8utovdbtrRy3r8Nn9qJ5KxhTtEYdBPyeARk0VwhbE10M1o6S5rVmYaj3/4KGvkSLIarxllScfX1R0hnRmQbfaad1Z7NAnYaq51UInxsL64E8KersM5qCYtFHsBpLMfLztz+35z1+TqFDXh1KfUo5GCqURnXUDWDXTuhFKiqT+c8KsCH9lIicETnhxXPxb11NU03ktF9+HUbQ8GSNvTqmkTAXn9eyZwQp3u00A==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; 
 s=arcselector10001;
 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=qni9OldPQX1NAVu62i6kpyehIo30Oa6yFlA2U9kAKqo=;
 b=NS/GgMTk1SkX7ei/eop5hlIRr8SvCuLQIV1E7B1pNvU5l3bliRqX/h20XSKq5b000Y80d3xqCYqzAdjWpALxHKpOFKxl4D8R+/0K1sG6Qeh63lBoVOtFEuXf7Oe5mCUfVP6vmoXDstBqdGjdpY5oLJpM0QJFba3xha+S/prxuNhK1ziZACwfc+WFeVrcW2NeiT0HiGGLblPI/CzwcCIVYYB7/AAlG/u2/ZrRFOqXBOYH7xkWu30yDn+FkEde7CfTfRrzCTvBF+6hRbMkckiqldMCiNyXz9BE2wRdptJsP2XUEDAS1pdouM/hhutu54STkMdRn/HY2LGnk6zPXh/HZw==
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=outlook.com;
 s=selector1;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=qni9OldPQX1NAVu62i6kpyehIo30Oa6yFlA2U9kAKqo=;
 b=RXwNDbra0SChU2iHEmCIB6U/7PVHN4xgtD7BhG+eFy+KyltfnNMWKJNFBDaWT+8Dx/h160WFwhhdYY7YXhLyp2zYdyLLg3Fh8CT/cjB3qk5B2SGdPaheU7lymZDFeycHwLiDgqZmXvDHNm5QYk/npWS2EhxjWWqvvxhwmB6/gaFZHZC3/bevyq7iQMGODmExqe57sFkcu1Nu4mOtHWXTcwQN0HL+xbCf1UfqkbUh/maoVkntHCNwAZmVgjvMrJF3GaXxvawNNXh1sAHRolbgw0+eS/3m3fTWdGFnyGwIaS9kxmpMOguqGSn6q18U54M3c9rfGOQ8oFRqYmN5l1evUg==
Received: from SY7P300MB0845.AUSP300.PROD.OUTLOOK.COM (2603:10c6:10:288::12)
 by SY0P300MB0109.AUSP300.PROD.OUTLOOK.COM (2603:10c6:10:24c::12) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.54; Tue, 1 Apr
 2025 14:35:29 +0000
Received: from SY7P300MB0845.AUSP300.PROD.OUTLOOK.COM
 ([fe80::eaec:d3ce:9584:3e10]) by SY7P300MB0845.AUSP300.PROD.OUTLOOK.COM
 ([fe80::eaec:d3ce:9584:3e10%2]) with mapi id 15.20.8534.048; Tue, 1 Apr 2025
 14:35:29 +0000
From: Tong Wu <wutong1208@outlook.com>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Thread-Topic: [FFmpeg-devel] [PATCH, v2] avcodec/d3d12va_decode: enable
 reference-only mode
Thread-Index: AQHbmDs+ovqx/zB94kG7oC0MpWB1q7OO9fuA
Date: Tue, 1 Apr 2025 14:35:29 +0000
Message-ID: <SY7P300MB0845D287675B2015C5FD0F72C7AC2@SY7P300MB0845.AUSP300.PROD.OUTLOOK.COM>
References: <20250318192304.987-1-Primeadvice@gmail.com>
In-Reply-To: <20250318192304.987-1-Primeadvice@gmail.com>
Accept-Language: en-US, zh-CN
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-ms-exchange-messagesentrepresentingtype: 1
x-ms-publictraffictype: Email
x-ms-traffictypediagnostic: SY7P300MB0845:EE_|SY0P300MB0109:EE_
x-ms-office365-filtering-correlation-id: b6f4c495-4c47-48d0-8d7a-08dd712a7388
x-ms-exchange-slblob-mailprops: EgT5Wr3QDKyfA/kqhNtxhLoDzMvzACColEVj2VCM/wyCBorkwOzXDwieBZhU3NG+M2QQN9+tt0YTMpdIM2h7Nyd9MMZZF0vCgsyhIDGyvaFVVNCJScb8EsBJsxGqg3AgL3/Y9RRgQ3DK+/WPYi5giVARbRZTgm+IoGaq5owMFNxpw8FG54zd3Bqb8SFo0F+Qwo2c5FIiFRtarKBkgiQnB2JyoK3JihBwfqL2fQTO95GD45lqrroRjpQ+b6nEvDZJkw1bS+RxnOV/jzHt0cDP2eYeTU1Y9zQ1/zF6X/WRlNUERt5lci3HYBCXVkjwMU8MxiNnZScQABsxVqCFE0cHO6TvilqsCgyU/WLzjIb9FhWHI7lnejnyC0DYIIextAS/6eij+h9zYoVQ3bpBc9wCs0rYUicLVJ5xwKkQT+cb3p4Mz4tK4CY19HV1YsM9xX5xxk/Gn51KyfhqEir7mDVfaHrStR2MYBLO6FwqRB6bGKO2IfzqOpHYw0Qf7xRdEhkvBni6UYog1g2DQGqdph/cQ68KZoTAg+z5vEOaSaKNbv09mXtk1TWRaPTsCBpJF5tuxWdKzcaI5DDviJfEwf+w+/R609+zfFak4qhqksj5+kLM9Y2P60LUuhqpmbwUfioU8UzKRcvnmkRDP1NGmKEY5ywDljIgMRnlw7KBJVFTCayFNpDRQAIHllVg6HN0FAv++GIIte+az3Qo22KxbiMUbzWTRVesGCUWLQAuw2rdhYw=
x-microsoft-antispam: BCL:0;
 ARA:14566002|8060799006|461199028|8062599003|19110799003|15080799006|7092599003|440099028|3412199025|102099032|12091999003|41001999003|56899033;
x-microsoft-antispam-message-info: =?us-ascii?Q?jsjqkx8I3YqdV5lFi5bLHH/KrpeY0se3V31C4L0KxQ1LZiTovZQ2Xw2AGc69?=
 =?us-ascii?Q?LOyfD4toyvVQkbojU232rbcbhsbMkJEvDoJpLM6nGeuSDWVYpI0VfRv9qkDL?=
 =?us-ascii?Q?a4ciCVj50++dxHVRkbLTy4+phTHm10v9RC6rT/GTOih1CXxTTb6B6yKH1UmF?=
 =?us-ascii?Q?IW/5dt9EbDYiX7uGTVBB+mUIT+hCAsydeb/dqRnGnnchl3LTI7vx1ixBiv8w?=
 =?us-ascii?Q?pClOllxc9HG7Q4T3T4fPRirwbv3ZGT83YgCVQLiuFXHr7FrHcUgdm48QcMKM?=
 =?us-ascii?Q?YZFvlnKujHUC1LogkmpEydqmW2uHmEqqI6l/x4IRstV4J0J2DEbL0Yl1ro0V?=
 =?us-ascii?Q?66fWBmGHC+Js9AmH7BSErSYBSiPjM6jTFk44KwTbF26l0QKwRUPJPLuUdMPg?=
 =?us-ascii?Q?iW+0/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Imlxw1oz?=
 =?us-ascii?Q?y8jt58CkzsXLjUoVclXx0hiUmItjYv26Wk7I9IvO67m8KaumRdz4XZ3YCsdu?=
 =?us-ascii?Q?ZJNm9DpcwGbIO/aXO0gqLUC7CoWsg3lh/rHPWuIwmMhBFzWjsyYj2dcN9sW5?=
 =?us-ascii?Q?Y29ejmVlngRoNXdLT0yTGshKHttKWFFuE4lzfImozys4mohXrCLda2MNYajT?=
 =?us-ascii?Q?bw+5WxIBTyvPZu+3Yo2Sv2TbnQWLlHJKWI9NvjKwcKJKrHJ/I+zN/UMnRKpP?=
 =?us-ascii?Q?fpeo+rQnOawjovd34Q1E8BzxiM7rtcmeCMjU0/1jni7OwcgyfK222zh8Hxy3?=
 =?us-ascii?Q?EmZ06GB/am4518Y59TAlokZ6EZiLz4s28r0ulKM0QkB1dDvqww+C0Zw6Hv8?=
 =?us-ascii?Q?=3D?=
MIME-Version: 1.0
X-OriginatorOrg: outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-AuthSource: SY7P300MB0845.AUSP300.PROD.OUTLOOK.COM
X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000
X-MS-Exchange-CrossTenant-Network-Message-Id: b6f4c495-4c47-48d0-8d7a-08dd712a7388
X-MS-Exchange-CrossTenant-originalarrivaltime: 01 Apr 2025 14:35:29.7157 (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: SY0P300MB0109
Subject: Re: [FFmpeg-devel] [PATCH,
 v2] avcodec/d3d12va_decode: enable reference-only mode
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Araz Iusubov <Primeadvice@gmail.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/SY7P300MB0845D287675B2015C5FD0F72C7AC2@SY7P300MB0845.AUSP300.PROD.OUTLOOK.COM/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

Araz Iusubov: 
>Subject: [FFmpeg-devel] [PATCH, v2] avcodec/d3d12va_decode: enable
>reference-only mode
>
>The Reference-Only feature in DirectX 12 is a memory optimization technique
>designed for video decoding scenarios.
>This feature requires that reference resources must be allocated with the
>D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY resource flag.
>Reference textures must also be separated from output textures.
>This feature is not supported in the current version of ffmpeg.
>Since AMD GPU uses this feature in Direct 12 decoder, ffmpeg does not support
>AMD GPU Direct 12 decoding.
>
>---
> libavcodec/d3d12va_decode.c | 165 +++++++++++++++++++++++++++++++++---
> libavcodec/d3d12va_decode.h |  13 +++
> 2 files changed, 166 insertions(+), 12 deletions(-)
>
>diff --git a/libavcodec/d3d12va_decode.c b/libavcodec/d3d12va_decode.c index
>3b8978635e..b9df266e37 100644
>--- a/libavcodec/d3d12va_decode.c
>+++ b/libavcodec/d3d12va_decode.c
>@@ -41,6 +41,102 @@ typedef struct HelperObjects {
>     uint64_t fence_value;
> } HelperObjects;
>
>+typedef struct ReferenceFrame {
>+    ID3D12Resource *resource;
>+    int            used;
>+    ID3D12Resource *output_resource;
>+} ReferenceFrame;
>+
>+static ID3D12Resource *get_reference_only_resource(AVCodecContext
>+*avctx, ID3D12Resource *output_resource) {
>+    D3D12VADecodeContext   *ctx          = D3D12VA_DECODE_CONTEXT(avctx);
>+    AVHWFramesContext      *frames_ctx   = D3D12VA_FRAMES_CONTEXT(avctx);
>+    AVD3D12VADeviceContext *device_hwctx = ctx->device_ctx;
>+    AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx;
>+    int i = 0;
>+    ID3D12Resource *resource = NULL;
>+    ReferenceFrame *reference_only_map = ctx->reference_only_map;
>+    if (reference_only_map == NULL) {
>+        av_log(avctx, AV_LOG_ERROR, "Reference frames are not allocated!\n");
>+        return NULL;
>+    }
>+
>+    // find unused resource
>+    for (i = 0; i < ctx->max_num_ref; i++) {
>+        if (!reference_only_map[i].used && reference_only_map[i].resource !=
>NULL) {
>+            reference_only_map[i].used = 1;
>+            resource = reference_only_map[i].resource;
>+            reference_only_map[i].output_resource = output_resource;
>+            return resource;
>+        }
>+    }
>+
>+    // find space to allocate
>+    for (i = 0; i < ctx->max_num_ref; i++) {
>+        if (reference_only_map[i].resource == NULL)
>+            break;
>+    }
>+
>+    if (i == ctx->max_num_ref) {
>+        av_log(avctx, AV_LOG_ERROR, "No space for new Reference frame!\n");
>+        return NULL;
>+    }
>+
>+    // allocate frame
>+    D3D12_HEAP_PROPERTIES props = { .Type = D3D12_HEAP_TYPE_DEFAULT };
>+    D3D12_RESOURCE_DESC desc;
>+    output_resource->lpVtbl->GetDesc(output_resource, &desc);
>+    desc.Flags = D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY |
>+ D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
>+
>+    if (FAILED(ID3D12Device_CreateCommittedResource(device_hwctx->device,
>&props, D3D12_HEAP_FLAG_NONE, &desc,
>+        D3D12_RESOURCE_STATE_COMMON, NULL, &IID_ID3D12Resource, (void
>**)&reference_only_map[i].resource))) {
>+        av_log(ctx, AV_LOG_ERROR, "Failed to create D3D12 Reference
>Resource!\n");
>+        return NULL;
>+    }
>+
>+    reference_only_map[i].used = 1;
>+    resource = reference_only_map[i].resource;
>+    reference_only_map[i].output_resource = output_resource;
>+
>+    return resource;
>+}
>+
>+static void free_reference_only_resources(AVCodecContext *avctx) {
>+    D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
>+    int i;
>+    ReferenceFrame *reference_only_map = ctx->reference_only_map;
>+    if (reference_only_map != NULL) {
>+        for (i = 0; i < ctx->max_num_ref; i++) {
>+            if (reference_only_map[i].resource != NULL) {
>+                D3D12_OBJECT_RELEASE(reference_only_map[i].resource);
>+            }
>+        }
>+        av_freep(&ctx->reference_only_map);
>+        av_freep(&ctx->ref_only_resources);
>+    }
>+}
>+
>+static void prepare_reference_only_resources(AVCodecContext *avctx) {
>+    D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
>+    int i, j;
>+    ReferenceFrame *reference_only_map = ctx->reference_only_map;
>+    if (reference_only_map == NULL)
>+        return;
>+    memset(ctx->ref_only_resources, 0, ctx->max_num_ref * sizeof(*(ctx-
>>ref_only_resources)));
>+    for (j = 0; j < ctx->max_num_ref; j++) {
>+        for (i = 0; i < ctx->max_num_ref; i++) {
>+            if (reference_only_map[j].used &&
>reference_only_map[j].output_resource == ctx->ref_resources[i]) {
>+                ctx->ref_only_resources[i] = reference_only_map[j].resource;
>+                break;
>+            }
>+        }
>+        if (i == ctx->max_num_ref)
>+            reference_only_map[j].used = 0;
>+    }
>+}
>+
> int ff_d3d12va_get_suitable_max_bitstream_size(AVCodecContext *avctx)  {
>     AVHWFramesContext *frames_ctx = D3D12VA_FRAMES_CONTEXT(avctx); @@
>-250,6 +346,18 @@ static int d3d12va_create_decoder(AVCodecContext *avctx)
>         return AVERROR_PATCHWELCOME;
>     }
>
>+    ctx->reference_only_map = NULL;
>+    ctx->ref_only_resources = NULL;
>+    if (feature.ConfigurationFlags &
>D3D12_VIDEO_DECODE_CONFIGURATION_FLAG_REFERENCE_ONLY_ALLOCATI
>ONS_REQUIRED) {
>+        av_log(avctx, AV_LOG_VERBOSE, "Reference-Only Allocations are required
>for this D3D12 decoder configuration.\n");
>+        ctx->reference_only_map = av_calloc(ctx->max_num_ref + 1,
>sizeof(ReferenceFrame));
>+            if (!ctx->reference_only_map)
>+                return AVERROR(ENOMEM);
>+        ctx->ref_only_resources = av_calloc(ctx->max_num_ref, sizeof(*ctx-
>>ref_only_resources));
>+            if (!ctx->ref_only_resources)
>+                return AVERROR(ENOMEM);
>+    }
>+
>     desc = (D3D12_VIDEO_DECODER_DESC) {
>         .NodeMask = 0,
>         .Configuration = ctx->cfg,
>@@ -394,6 +502,7 @@ int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
>
>         av_log(avctx, AV_LOG_VERBOSE, "Total number of command allocators
>reused: %d\n", num_allocator);
>     }
>+    free_reference_only_resources(avctx);
>
>     av_fifo_freep2(&ctx->objects_queue);
>
>@@ -412,14 +521,15 @@ static inline int
>d3d12va_update_reference_frames_state(AVCodecContext *avctx, D
>                                                         ID3D12Resource *current_resource, int
>state_before, int state_end)  {
>     D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
>+    ID3D12Resource **ref_resources = ctx->ref_only_resources ?
>+ ctx->ref_only_resources : ctx->ref_resources;
>
>     int num_barrier = 0;
>     for (int i = 0; i < ctx->max_num_ref; i++) {
>-        if (((ctx->used_mask >> i) & 0x1) && ctx->ref_resources[i] && ctx-
>>ref_resources[i] != current_resource) {
>+        if (((ctx->used_mask >> i) & 0x1) && ref_resources[i] &&
>+ ref_resources[i] != current_resource) {
>             barriers[num_barrier].Type  =
>D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
>             barriers[num_barrier].Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
>-            barriers[num_barrier].Transition =
>(D3D12_RESOURCE_TRANSITION_BARRIER){
>-                .pResource   = ctx->ref_resources[i],
>+            barriers[num_barrier].Transition =
>(D3D12_RESOURCE_TRANSITION_BARRIER) {
>+                .pResource   = ref_resources[i],
>                 .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
>                 .StateBefore = state_before,
>                 .StateAfter  = state_end, @@ -440,8 +550,9 @@ int
>ff_d3d12va_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
>     D3D12VADecodeContext   *ctx               = D3D12VA_DECODE_CONTEXT(avctx);
>     ID3D12Resource         *buffer            = NULL;
>     ID3D12CommandAllocator *command_allocator = NULL;
>-    AVD3D12VAFrame         *f                 = (AVD3D12VAFrame *)frame->data[0];
>-    ID3D12Resource         *resource          = (ID3D12Resource *)f->texture;
>+    AVD3D12VAFrame         *f                 = (AVD3D12VAFrame*)frame->data[0];
>+    ID3D12Resource         *output_resource   = (ID3D12Resource*)f->texture;
>+    ID3D12Resource         *ref_resource      = NULL;
>
>     ID3D12VideoDecodeCommandList *cmd_list = ctx->command_list;
>     D3D12_RESOURCE_BARRIER barriers[32] = { 0 }; @@ -466,25 +577,55 @@ int
>ff_d3d12va_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
>     D3D12_VIDEO_DECODE_OUTPUT_STREAM_ARGUMENTS output_args = {
>         .ConversionArguments = { 0 },
>         .OutputSubresource   = 0,
>-        .pOutputTexture2D    = resource,
>+        .pOutputTexture2D    = output_resource,
>     };
>
>+    memset(ctx->ref_subresources, 0, sizeof(UINT) * ctx->max_num_ref);
>+    input_args.ReferenceFrames.NumTexture2Ds = ctx->max_num_ref;
>+    input_args.ReferenceFrames.pSubresources = ctx->ref_subresources;
>+
>+    if (ctx->reference_only_map) {
>+        ref_resource = get_reference_only_resource(avctx, output_resource);
>+        if (ref_resource == NULL) {
>+            av_log(avctx, AV_LOG_ERROR, "Failed to get reference frame!\n");
>+            goto fail;
>+        }
>+        prepare_reference_only_resources(avctx);
>+
>+        output_args.ConversionArguments.Enable               = 1;
>+        input_args.ReferenceFrames.ppTexture2Ds  = ctx->ref_only_resources;
>+        output_args.ConversionArguments.pReferenceTexture2D  = ref_resource;
>+        output_args.ConversionArguments.ReferenceSubresource = 0;
>+    } else {
>+        ref_resource = output_resource;
>+        input_args.ReferenceFrames.ppTexture2Ds  = ctx->ref_resources;
>+    }
>+
>     UINT num_barrier = 1;
>     barriers[0] = (D3D12_RESOURCE_BARRIER) {
>         .Type  = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
>         .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
>         .Transition = {
>-            .pResource   = resource,
>+            .pResource   = output_resource,
>             .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
>             .StateBefore = D3D12_RESOURCE_STATE_COMMON,
>             .StateAfter  = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
>         },
>     };
>
>-    memset(ctx->ref_subresources, 0, sizeof(UINT) * ctx->max_num_ref);
>-    input_args.ReferenceFrames.NumTexture2Ds = ctx->max_num_ref;
>-    input_args.ReferenceFrames.ppTexture2Ds  = ctx->ref_resources;
>-    input_args.ReferenceFrames.pSubresources = ctx->ref_subresources;
>+    if (ctx->reference_only_map) {
>+        barriers[1] = (D3D12_RESOURCE_BARRIER) {
>+            .Type  = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
>+            .Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
>+            .Transition = {
>+                .pResource   = ref_resource,
>+                .Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
>+                .StateBefore = D3D12_RESOURCE_STATE_COMMON,
>+                .StateAfter  = D3D12_RESOURCE_STATE_VIDEO_DECODE_WRITE,
>+            },
>+        };
>+        num_barrier++;
>+    }
>
>     ret = d3d12va_fence_completion(&f->sync_ctx);
>     if (ret < 0)
>@@ -505,7 +646,7 @@ int ff_d3d12va_common_end_frame(AVCodecContext
>*avctx, AVFrame *frame,
>
>     DX_CHECK(ID3D12VideoDecodeCommandList_Reset(cmd_list,
>command_allocator));
>
>-    num_barrier += d3d12va_update_reference_frames_state(avctx, &barriers[1],
>resource, D3D12_RESOURCE_STATE_COMMON,
>D3D12_RESOURCE_STATE_VIDEO_DECODE_READ);
>+    num_barrier += d3d12va_update_reference_frames_state(avctx,
>+ &barriers[num_barrier], ref_resource, D3D12_RESOURCE_STATE_COMMON,
>+ D3D12_RESOURCE_STATE_VIDEO_DECODE_READ);
>
>     ID3D12VideoDecodeCommandList_ResourceBarrier(cmd_list, num_barrier,
>barriers);
>
>diff --git a/libavcodec/d3d12va_decode.h b/libavcodec/d3d12va_decode.h index
>b64994760a..74991fe853 100644
>--- a/libavcodec/d3d12va_decode.h
>+++ b/libavcodec/d3d12va_decode.h
>@@ -119,6 +119,19 @@ typedef struct D3D12VADecodeContext {
>      * Private to the FFmpeg AVHWAccel implementation
>      */
>     unsigned report_id;
>+
>+    /**
>+     * The Reference-Only feature in DirectX 12 is a memory optimization
>+     * technique designed for video decoding/encoding scenarios.
>+     * This feature requires that reference resources must be allocated
>+     * with the `D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY`
>resource flag.
>+     * Reference textures must also be separated from output textures.
>+     * reference_only_map used as a storage for reference only frames
>+     * ref_only_resources used as a shadow for  ref_resources
>+     */
>+    void *reference_only_map;
>+    ID3D12Resource **ref_only_resources;
>+
> } D3D12VADecodeContext;
>
> /**
>--
>2.47.1.windows.1

LGTM, will apply if no objections

Tong

_______________________________________________
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".