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;
79 
80 /**
81  * @brief This struct is used to sync d3d12 execution
82  *
83  */
84 typedef struct AVD3D12VASyncContext {
85  /**
86  * D3D12 fence object
87  */
88  ID3D12Fence *fence;
89 
90  /**
91  * A handle to the event object that's raised when the fence
92  * reaches a certain value.
93  */
94  HANDLE event;
95 
96  /**
97  * The fence value used for sync
98  */
99  uint64_t fence_value;
101 
102 /**
103  * Define the behaviours of frame allocation.
104  */
105 typedef enum AVD3D12VAFrameFlags {
107 
108  /**
109  * Indicates that frame data should be allocated using a texture array resource.
110  */
113 
114 /**
115  * @brief D3D12VA frame descriptor for pool allocation.
116  *
117  */
118 typedef struct AVD3D12VAFrame {
119  /**
120  * The texture in which the frame is located. The reference count is
121  * managed by the AVBufferRef, and destroying the reference will release
122  * the interface.
123  */
124  ID3D12Resource *texture;
125 
126  /**
127  * Index of the subresource within the texture.
128  *
129  * In texture array mode, this specifies the array slice index.
130  * When texture array mode is not used, this value is always 0.
131  */
133 
134  /**
135  * The sync context for the texture
136  *
137  * @see: https://learn.microsoft.com/en-us/windows/win32/medfound/direct3d-12-video-overview#directx-12-fences
138  */
140 
141  /**
142  * A combination of AVD3D12VAFrameFlags.
143  * Set by AVD3D12VAFramesContext.
144  */
147 
148 /**
149  * @brief This struct is allocated as AVHWFramesContext.hwctx
150  *
151  */
152 typedef struct AVD3D12VAFramesContext {
153  /**
154  * DXGI_FORMAT format. MUST be compatible with the pixel format.
155  * If unset, will be automatically set.
156  */
157  DXGI_FORMAT format;
158 
159  /**
160  * Options for working with resources.
161  * If unset, this will be D3D12_RESOURCE_FLAG_NONE.
162  *
163  * @see https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_resource_flags
164  */
165  D3D12_RESOURCE_FLAGS resource_flags;
166 
167  /**
168  * In texture array mode, the D3D12 uses the same texture array (resource)for all
169  * pictures.
170  */
171  ID3D12Resource *texture_array;
172 
173  /**
174  * A combination of AVD3D12VAFrameFlags. Unless AV_D3D12VA_FRAME_FLAG_NONE is set,
175  * autodetected flags will be OR'd based on the device and frame features during
176  * av_hwframe_ctx_init().
177  */
180 
181 #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:178
AVD3D12VADeviceContext::lock_ctx
void * lock_ctx
Definition: hwcontext_d3d12va.h:77
AVD3D12VAFrameFlags
AVD3D12VAFrameFlags
Define the behaviours of frame allocation.
Definition: hwcontext_d3d12va.h:105
AVD3D12VAFrame::flags
AVD3D12VAFrameFlags flags
A combination of AVD3D12VAFrameFlags.
Definition: hwcontext_d3d12va.h:145
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:171
AVD3D12VAFrame::sync_ctx
AVD3D12VASyncContext sync_ctx
The sync context for the texture.
Definition: hwcontext_d3d12va.h:139
AVD3D12VASyncContext
This struct is used to sync d3d12 execution.
Definition: hwcontext_d3d12va.h:84
AVD3D12VASyncContext::fence
ID3D12Fence * fence
D3D12 fence object.
Definition: hwcontext_d3d12va.h:88
AVD3D12VAFramesContext
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_d3d12va.h:152
AVD3D12VAFrame::texture
ID3D12Resource * texture
The texture in which the frame is located.
Definition: hwcontext_d3d12va.h:124
AVD3D12VAFramesContext::resource_flags
D3D12_RESOURCE_FLAGS resource_flags
Options for working with resources.
Definition: hwcontext_d3d12va.h:165
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:118
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:111
AVD3D12VAFramesContext::format
DXGI_FORMAT format
DXGI_FORMAT format.
Definition: hwcontext_d3d12va.h:157
AV_D3D12VA_FRAME_FLAG_NONE
@ AV_D3D12VA_FRAME_FLAG_NONE
Definition: hwcontext_d3d12va.h:106
AVD3D12VASyncContext::event
HANDLE event
A handle to the event object that's raised when the fence reaches a certain value.
Definition: hwcontext_d3d12va.h:94
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:99
AVD3D12VAFrame::subresource_index
int subresource_index
Index of the subresource within the texture.
Definition: hwcontext_d3d12va.h:132