FFmpeg
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;
123 
124 /**
125  * @}
126  */
127 #define D3D12VA_VIDEO_DEC_ASYNC_DEPTH 36
128 #define D3D12VA_DECODE_CONTEXT(avctx) ((D3D12VADecodeContext *)((avctx)->internal->hwaccel_priv_data))
129 #define D3D12VA_FRAMES_CONTEXT(avctx) ((AVHWFramesContext *)(avctx)->hw_frames_ctx->data)
130 
131 /**
132  * @brief Get a suitable maximum bitstream size
133  *
134  * Creating and destroying a resource on d3d12 needs sync and reallocation, so use this function
135  * to help allocate a big enough bitstream buffer to avoid recreating resources when decoding.
136  *
137  * @return the suitable size
138  */
140 
141 /**
142  * @brief init D3D12VADecodeContext
143  *
144  * @return Error code (ret < 0 if failed)
145  */
147 
148 /**
149  * @brief uninit D3D12VADecodeContext
150  *
151  * @return Error code (ret < 0 if failed)
152  */
154 
155 /**
156  * @brief d3d12va common frame params
157  *
158  * @return Error code (ret < 0 if failed)
159  */
160 int ff_d3d12va_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
161 
162 /**
163  * @brief d3d12va common end frame
164  *
165  * @param avctx codec context
166  * @param frame current output frame
167  * @param pp picture parameters
168  * @param pp_size the size of the picture parameters
169  * @param qm quantization matrix
170  * @param qm_size the size of the quantization matrix
171  * @param callback update decoder-specified input stream arguments
172  * @return Error code (ret < 0 if failed)
173  */
175  const void *pp, unsigned pp_size,
176  const void *qm, unsigned qm_size,
177  int(*)(AVCodecContext *, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *, ID3D12Resource *));
178 
179 #endif /* AVCODEC_D3D12VA_DEC_H */
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:374
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:434
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:283
AVFifo
Definition: fifo.c:35
ff_d3d12va_decode_uninit
int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
uninit D3D12VADecodeContext
Definition: d3d12va_decode.c:373
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::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:445
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:44
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:271