| 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; |
| 98 | } AVD3D12VADeviceContext; |
| 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; |
| 120 | } AVD3D12VASyncContext; |
| 121 |
| 122 | /** |
| 123 | * Define the behaviours of frame allocation. |
| 124 | */ |
| 125 | typedef enum AVD3D12VAFrameFlags { |
| 126 | AV_D3D12VA_FRAME_FLAG_NONE = 0, |
| 127 |
| 128 | /** |
| 129 | * Indicates that frame data should be allocated using a texture array resource. |
| 130 | */ |
| 131 | AV_D3D12VA_FRAME_FLAG_TEXTURE_ARRAY = (1 << 1), |
| 132 | } AVD3D12VAFrameFlags; |
| 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 | */ |
| 152 | int subresource_index; |
| 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 | */ |
| 159 | AVD3D12VASyncContext sync_ctx; |
| 160 |
| 161 | /** |
| 162 | * A combination of AVD3D12VAFrameFlags. |
| 163 | * Set by AVD3D12VAFramesContext. |
| 164 | */ |
| 165 | AVD3D12VAFrameFlags flags; |
| 166 | } AVD3D12VAFrame; |
| 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 | */ |
| 206 | AVD3D12VAFrameFlags flags; |
| 207 | } AVD3D12VAFramesContext; |
| 208 |
| 209 | #endif /* AVUTIL_HWCONTEXT_D3D12VA_H */ |
| 210 |