FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
d3d12va_decode.h
Go to the documentation of this file.
1 /*
2  * Direct3D 12 HW acceleration video decoder
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 AVCODEC_D3D12VA_DECODE_H
24 #define AVCODEC_D3D12VA_DECODE_H
25 
26 #include "libavutil/fifo.h"
27 #include "libavutil/hwcontext.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "hwaccel_internal.h"
32 
33 /**
34  * @brief This structure is used to provide the necessary configurations and data
35  * to the FFmpeg Direct3D 12 HWAccel implementation for video decoder.
36  */
37 typedef struct D3D12VADecodeContext {
39 
40  /**
41  * D3D12 video decoder
42  */
43  ID3D12VideoDecoder *decoder;
44 
45  /**
46  * D3D12 video decoder heap
47  */
48  ID3D12VideoDecoderHeap *decoder_heap;
49 
50  /**
51  * D3D12 configuration used to create the decoder
52  *
53  * Specified by decoders
54  */
55  D3D12_VIDEO_DECODE_CONFIGURATION cfg;
56 
57  /**
58  * A cached queue for reusing the D3D12 command allocators and upload buffers
59  *
60  * @see https://learn.microsoft.com/en-us/windows/win32/direct3d12/recording-command-lists-and-bundles#id3d12commandallocator
61  */
63 
64  /**
65  * D3D12 command queue
66  */
67  ID3D12CommandQueue *command_queue;
68 
69  /**
70  * D3D12 video decode command list
71  */
72  ID3D12VideoDecodeCommandList *command_list;
73 
74  /**
75  * The array of resources used for reference frames
76  *
77  * The ref_resources.length is the same as D3D12VADecodeContext.max_num_ref
78  */
79  ID3D12Resource **ref_resources;
80 
81  /**
82  * The array of subresources used for reference frames
83  *
84  * The ref_subresources.length is the same as D3D12VADecodeContext.max_num_ref
85  */
87 
88  /**
89  * Maximum number of reference frames
90  */
92 
93  /**
94  * Used mask used to record reference frames indices
95  */
96  UINT used_mask;
97 
98  /**
99  * Bitstream size for each frame
100  */
102 
103  /**
104  * The sync context used to sync command queue
105  */
107 
108  /**
109  * A pointer to AVD3D12VADeviceContext used to create D3D12 objects
110  */
112 
113  /**
114  * Pixel format
115  */
117 
118  /**
119  * Private to the FFmpeg AVHWAccel implementation
120  */
121  unsigned report_id;
122 
123  /**
124  * The Reference-Only feature in DirectX 12 is a memory optimization
125  * technique designed for video decoding/encoding scenarios.
126  * This feature requires that reference resources must be allocated
127  * with the `D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY` resource flag.
128  * Reference textures must also be separated from output textures.
129  * reference_only_map used as a storage for reference only frames
130  * ref_only_resources used as a shadow for ref_resources
131  */
133  ID3D12Resource **ref_only_resources;
134 
136 
137 /**
138  * @}
139  */
140 #define D3D12VA_VIDEO_DEC_ASYNC_DEPTH 36
141 #define D3D12VA_DECODE_CONTEXT(avctx) ((D3D12VADecodeContext *)((avctx)->internal->hwaccel_priv_data))
142 #define D3D12VA_FRAMES_CONTEXT(avctx) ((AVHWFramesContext *)(avctx)->hw_frames_ctx->data)
143 
144 /**
145  * @brief Get a suitable maximum bitstream size
146  *
147  * Creating and destroying a resource on d3d12 needs sync and reallocation, so use this function
148  * to help allocate a big enough bitstream buffer to avoid recreating resources when decoding.
149  *
150  * @return the suitable size
151  */
153 
154 /**
155  * @brief init D3D12VADecodeContext
156  *
157  * @return Error code (ret < 0 if failed)
158  */
160 
161 /**
162  * @brief uninit D3D12VADecodeContext
163  *
164  * @return Error code (ret < 0 if failed)
165  */
167 
168 /**
169  * @brief d3d12va common frame params
170  *
171  * @return Error code (ret < 0 if failed)
172  */
173 int ff_d3d12va_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
174 
175 /**
176  * @brief d3d12va common end frame
177  *
178  * @param avctx codec context
179  * @param frame current output frame
180  * @param pp picture parameters
181  * @param pp_size the size of the picture parameters
182  * @param qm quantization matrix
183  * @param qm_size the size of the quantization matrix
184  * @param callback update decoder-specified input stream arguments
185  * @return Error code (ret < 0 if failed)
186  */
188  const void *pp, unsigned pp_size,
189  const void *qm, unsigned qm_size,
190  int(*)(AVCodecContext *, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *, ID3D12Resource *));
191 
192 #endif /* AVCODEC_D3D12VA_DEC_H */
D3D12VADecodeContext::ref_only_resources
ID3D12Resource ** ref_only_resources
Definition: d3d12va_decode.h:133
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
D3D12VADecodeContext::cfg
D3D12_VIDEO_DECODE_CONFIGURATION cfg
D3D12 configuration used to create the decoder.
Definition: d3d12va_decode.h:55
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
internal.h
D3D12VADecodeContext::ref_resources
ID3D12Resource ** ref_resources
The array of resources used for reference frames.
Definition: d3d12va_decode.h:79
D3D12VADecodeContext::max_num_ref
UINT max_num_ref
Maximum number of reference frames.
Definition: d3d12va_decode.h:91
ff_d3d12va_common_end_frame
int ff_d3d12va_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*)(AVCodecContext *, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *, ID3D12Resource *))
d3d12va common end frame
Definition: d3d12va_decode.c:543
D3D12VADecodeContext::sync_ctx
AVD3D12VASyncContext sync_ctx
The sync context used to sync command queue.
Definition: d3d12va_decode.h:106
fifo.h
D3D12VADecodeContext::device_ctx
AVD3D12VADeviceContext * device_ctx
A pointer to AVD3D12VADeviceContext used to create D3D12 objects.
Definition: d3d12va_decode.h:111
D3D12VADecodeContext::decoder_heap
ID3D12VideoDecoderHeap * decoder_heap
D3D12 video decoder heap.
Definition: d3d12va_decode.h:48
AVD3D12VASyncContext
This struct is used to sync d3d12 execution.
Definition: hwcontext_d3d12va.h:84
D3D12VADecodeContext::bitstream_size
UINT bitstream_size
Bitstream size for each frame.
Definition: d3d12va_decode.h:101
hwaccel_internal.h
D3D12VADecodeContext::used_mask
UINT used_mask
Used mask used to record reference frames indices.
Definition: d3d12va_decode.h:96
hwcontext_d3d12va.h
D3D12VADecodeContext::decoder
ID3D12VideoDecoder * decoder
D3D12 video decoder.
Definition: d3d12va_decode.h:43
ff_d3d12va_decode_init
int ff_d3d12va_decode_init(AVCodecContext *avctx)
init D3D12VADecodeContext
Definition: d3d12va_decode.c:390
AVFifo
Definition: fifo.c:35
ff_d3d12va_decode_uninit
int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
uninit D3D12VADecodeContext
Definition: d3d12va_decode.c:480
D3D12VADecodeContext::decoder_ref
AVBufferRef * decoder_ref
Definition: d3d12va_decode.h:38
D3D12VADecodeContext::objects_queue
AVFifo * objects_queue
A cached queue for reusing the D3D12 command allocators and upload buffers.
Definition: d3d12va_decode.h:62
AVD3D12VADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_d3d12va.h:43
D3D12VADecodeContext::reference_only_map
void * reference_only_map
The Reference-Only feature in DirectX 12 is a memory optimization technique designed for video decodi...
Definition: d3d12va_decode.h:132
D3D12VADecodeContext::command_queue
ID3D12CommandQueue * command_queue
D3D12 command queue.
Definition: d3d12va_decode.h:67
avcodec.h
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:431
D3D12VADecodeContext::report_id
unsigned report_id
Private to the FFmpeg AVHWAccel implementation.
Definition: d3d12va_decode.h:121
D3D12VADecodeContext::command_list
ID3D12VideoDecodeCommandList * command_list
D3D12 video decode command list.
Definition: d3d12va_decode.h:72
D3D12VADecodeContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format.
Definition: d3d12va_decode.h:116
ff_d3d12va_get_suitable_max_bitstream_size
int ff_d3d12va_get_suitable_max_bitstream_size(AVCodecContext *avctx)
Get a suitable maximum bitstream size.
Definition: d3d12va_decode.c:139
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
D3D12VADecodeContext
This structure is used to provide the necessary configurations and data to the FFmpeg Direct3D 12 HWA...
Definition: d3d12va_decode.h:37
D3D12VADecodeContext::ref_subresources
UINT * ref_subresources
The array of subresources used for reference frames.
Definition: d3d12va_decode.h:86
hwcontext.h
ff_d3d12va_common_frame_params
int ff_d3d12va_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
d3d12va common frame params
Definition: d3d12va_decode.c:378