FFmpeg
hwcontext_d3d12va.h
Go to the documentation of this file.
1 /*
2  * Direct3D 12 HW acceleration.
3  *
4  * copyright (c) 2022-2023 Wu Jianhua <toqsxw@outlook.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef AVUTIL_HWCONTEXT_D3D12VA_H
24 #define AVUTIL_HWCONTEXT_D3D12VA_H
25 
26 /**
27  * @file
28  * An API-specific header for AV_HWDEVICE_TYPE_D3D12VA.
29  *
30  * AVHWFramesContext.pool must contain AVBufferRefs whose
31  * data pointer points to an AVD3D12VAFrame struct.
32  */
33 #include <stdint.h>
34 #include <initguid.h>
35 #include <d3d12.h>
36 #include <d3d12sdklayers.h>
37 #include <d3d12video.h>
38 
39 /**
40  * @brief This struct is allocated as AVHWDeviceContext.hwctx
41  *
42  */
43 typedef struct AVD3D12VADeviceContext {
44  /**
45  * Device used for objects creation and access. This can also be
46  * used to set the libavcodec decoding device.
47  *
48  * Can be set by the user. This is the only mandatory field - the other
49  * device context fields are set from this and are available for convenience.
50  *
51  * Deallocating the AVHWDeviceContext will always release this interface,
52  * and it does not matter whether it was user-allocated.
53  */
54  ID3D12Device *device;
55 
56  /**
57  * If unset, this will be set from the device field on init.
58  *
59  * Deallocating the AVHWDeviceContext will always release this interface,
60  * and it does not matter whether it was user-allocated.
61  */
62  ID3D12VideoDevice *video_device;
63 
64  /**
65  * Callbacks for locking. They protect access to the internal staging
66  * texture (for av_hwframe_transfer_data() calls). They do NOT protect
67  * access to hwcontext or decoder state in general.
68  *
69  * If unset on init, the hwcontext implementation will set them to use an
70  * internal mutex.
71  *
72  * The underlying lock must be recursive. lock_ctx is for free use by the
73  * locking implementation.
74  */
75  void (*lock)(void *lock_ctx);
76  void (*unlock)(void *lock_ctx);
77  void *lock_ctx;
78 
79  /**
80  * Resource flags to be applied to D3D12 resources allocated
81  * for frames using this device context.
82  *
83  * If unset, this will be D3D12_RESOURCE_FLAG_NONE.
84  *
85  * It applies globally to all AVD3D12VAFramesContext allocated from this device context.
86  */
87  D3D12_RESOURCE_FLAGS resource_flags;
88 
89  /**
90  * Heap flags to be applied to D3D12 resources allocated
91  * for frames using this device context.
92  *
93  * If unset, this will be D3D12_HEAP_FLAG_NONE.
94  *
95  * It applies globally to all AVD3D12VAFramesContext allocated from this device context.
96  */
97  D3D12_HEAP_FLAGS heap_flags;
99 
100 /**
101  * @brief This struct is used to sync d3d12 execution
102  *
103  */
104 typedef struct AVD3D12VASyncContext {
105  /**
106  * D3D12 fence object
107  */
108  ID3D12Fence *fence;
109 
110  /**
111  * A handle to the event object that's raised when the fence
112  * reaches a certain value.
113  */
114  HANDLE event;
115 
116  /**
117  * The fence value used for sync
118  */
119  uint64_t fence_value;
121 
122 /**
123  * Define the behaviours of frame allocation.
124  */
125 typedef enum AVD3D12VAFrameFlags {
127 
128  /**
129  * Indicates that frame data should be allocated using a texture array resource.
130  */
133 
134 /**
135  * @brief D3D12VA frame descriptor for pool allocation.
136  *
137  */
138 typedef struct AVD3D12VAFrame {
139  /**
140  * The texture in which the frame is located. The reference count is
141  * managed by the AVBufferRef, and destroying the reference will release
142  * the interface.
143  */
144  ID3D12Resource *texture;
145 
146  /**
147  * Index of the subresource within the texture.
148  *
149  * In texture array mode, this specifies the array slice index.
150  * When texture array mode is not used, this value is always 0.
151  */
153 
154  /**
155  * The sync context for the texture
156  *
157  * @see: https://learn.microsoft.com/en-us/windows/win32/medfound/direct3d-12-video-overview#directx-12-fences
158  */
160 
161  /**
162  * A combination of AVD3D12VAFrameFlags.
163  * Set by AVD3D12VAFramesContext.
164  */
167 
168 /**
169  * @brief This struct is allocated as AVHWFramesContext.hwctx
170  *
171  */
172 typedef struct AVD3D12VAFramesContext {
173  /**
174  * DXGI_FORMAT format. MUST be compatible with the pixel format.
175  * If unset, will be automatically set.
176  */
177  DXGI_FORMAT format;
178 
179  /**
180  * Options for working with resources.
181  * If unset, this will be D3D12_RESOURCE_FLAG_NONE.
182  *
183  * @see https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_resource_flags
184  */
185  D3D12_RESOURCE_FLAGS resource_flags;
186 
187  /**
188  * Options for working with heaps allocation when creating resources.
189  * If unset, this will be D3D12_HEAP_FLAG_NONE.
190  *
191  * @see https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_heap_flags
192  */
193  D3D12_HEAP_FLAGS heap_flags;
194 
195  /**
196  * In texture array mode, the D3D12 uses the same texture array (resource)for all
197  * pictures.
198  */
199  ID3D12Resource *texture_array;
200 
201  /**
202  * A combination of AVD3D12VAFrameFlags. Unless AV_D3D12VA_FRAME_FLAG_NONE is set,
203  * autodetected flags will be OR'd based on the device and frame features during
204  * av_hwframe_ctx_init().
205  */
208 
209 #endif /* AVUTIL_HWCONTEXT_D3D12VA_H */
AVD3D12VADeviceContext::device
ID3D12Device * device
Device used for objects creation and access.
Definition: hwcontext_d3d12va.h:54
AVD3D12VAFramesContext::flags
AVD3D12VAFrameFlags flags
A combination of AVD3D12VAFrameFlags.
Definition: hwcontext_d3d12va.h:206
AVD3D12VADeviceContext::lock_ctx
void * lock_ctx
Definition: hwcontext_d3d12va.h:77
AVD3D12VAFrameFlags
AVD3D12VAFrameFlags
Define the behaviours of frame allocation.
Definition: hwcontext_d3d12va.h:125
AVD3D12VADeviceContext::heap_flags
D3D12_HEAP_FLAGS heap_flags
Heap flags to be applied to D3D12 resources allocated for frames using this device context.
Definition: hwcontext_d3d12va.h:97
AVD3D12VAFrame::flags
AVD3D12VAFrameFlags flags
A combination of AVD3D12VAFrameFlags.
Definition: hwcontext_d3d12va.h:165
AVD3D12VADeviceContext::unlock
void(* unlock)(void *lock_ctx)
Definition: hwcontext_d3d12va.h:76
AVD3D12VAFramesContext::texture_array
ID3D12Resource * texture_array
In texture array mode, the D3D12 uses the same texture array (resource)for all pictures.
Definition: hwcontext_d3d12va.h:199
AVD3D12VAFrame::sync_ctx
AVD3D12VASyncContext sync_ctx
The sync context for the texture.
Definition: hwcontext_d3d12va.h:159
AVD3D12VASyncContext
This struct is used to sync d3d12 execution.
Definition: hwcontext_d3d12va.h:104
AVD3D12VASyncContext::fence
ID3D12Fence * fence
D3D12 fence object.
Definition: hwcontext_d3d12va.h:108
AVD3D12VAFramesContext::heap_flags
D3D12_HEAP_FLAGS heap_flags
Options for working with heaps allocation when creating resources.
Definition: hwcontext_d3d12va.h:193
AVD3D12VAFramesContext
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_d3d12va.h:172
AVD3D12VAFrame::texture
ID3D12Resource * texture
The texture in which the frame is located.
Definition: hwcontext_d3d12va.h:144
AVD3D12VAFramesContext::resource_flags
D3D12_RESOURCE_FLAGS resource_flags
Options for working with resources.
Definition: hwcontext_d3d12va.h:185
AVD3D12VADeviceContext::video_device
ID3D12VideoDevice * video_device
If unset, this will be set from the device field on init.
Definition: hwcontext_d3d12va.h:62
AVD3D12VAFrame
D3D12VA frame descriptor for pool allocation.
Definition: hwcontext_d3d12va.h:138
AVD3D12VADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_d3d12va.h:43
AV_D3D12VA_FRAME_FLAG_TEXTURE_ARRAY
@ AV_D3D12VA_FRAME_FLAG_TEXTURE_ARRAY
Indicates that frame data should be allocated using a texture array resource.
Definition: hwcontext_d3d12va.h:131
AVD3D12VAFramesContext::format
DXGI_FORMAT format
DXGI_FORMAT format.
Definition: hwcontext_d3d12va.h:177
AV_D3D12VA_FRAME_FLAG_NONE
@ AV_D3D12VA_FRAME_FLAG_NONE
Definition: hwcontext_d3d12va.h:126
AVD3D12VASyncContext::event
HANDLE event
A handle to the event object that's raised when the fence reaches a certain value.
Definition: hwcontext_d3d12va.h:114
AVD3D12VADeviceContext::lock
void(* lock)(void *lock_ctx)
Callbacks for locking.
Definition: hwcontext_d3d12va.h:75
AVD3D12VASyncContext::fence_value
uint64_t fence_value
The fence value used for sync.
Definition: hwcontext_d3d12va.h:119
AVD3D12VAFrame::subresource_index
int subresource_index
Index of the subresource within the texture.
Definition: hwcontext_d3d12va.h:152
AVD3D12VADeviceContext::resource_flags
D3D12_RESOURCE_FLAGS resource_flags
Resource flags to be applied to D3D12 resources allocated for frames using this device context.
Definition: hwcontext_d3d12va.h:87