FFmpeg
dxva2_internal.h
Go to the documentation of this file.
1 /*
2  * DXVA2 HW acceleration
3  *
4  * copyright (c) 2010 Laurent Aimar
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_DXVA2_INTERNAL_H
24 #define AVCODEC_DXVA2_INTERNAL_H
25 
26 #define COBJMACROS
27 
28 #include "config.h"
29 #include "config_components.h"
30 
31 /* define the proper COM entries before forcing desktop APIs */
32 #include <objbase.h>
33 
34 #define FF_DXVA2_WORKAROUND_SCALING_LIST_ZIGZAG 1 ///< Work around for DXVA2/Direct3D11 and old UVD/UVD+ ATI video cards
35 #define FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO 2 ///< Work around for DXVA2/Direct3D11 and old Intel GPUs with ClearVideo interface
36 
37 #if CONFIG_DXVA2
38 #include "dxva2.h"
40 #define DXVA2_VAR(ctx, var) ctx->dxva2.var
41 #else
42 #define DXVA2_VAR(ctx, var) 0
43 #endif
44 
45 #if CONFIG_D3D11VA
46 #include "d3d11va.h"
48 #define D3D11VA_VAR(ctx, var) ctx->d3d11va.var
49 #else
50 #define D3D11VA_VAR(ctx, var) 0
51 #endif
52 
53 #if CONFIG_D3D12VA
54 #include "d3d12va_decode.h"
55 #endif
56 
57 #if HAVE_DXVA_H
58 /* When targeting WINAPI_FAMILY_PHONE_APP or WINAPI_FAMILY_APP, dxva.h
59  * defines nothing. Force the struct definitions to be visible. */
60 #undef WINAPI_FAMILY
61 #define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
62 #undef _CRT_BUILD_DESKTOP_APP
63 #define _CRT_BUILD_DESKTOP_APP 0
64 #include <dxva.h>
65 #endif
66 
67 #include "libavutil/hwcontext.h"
68 
69 #include "avcodec.h"
70 #include "internal.h"
71 
72 typedef void DECODER_BUFFER_DESC;
73 
74 typedef union {
75 #if CONFIG_D3D11VA
76  struct AVD3D11VAContext d3d11va;
77 #endif
78 #if CONFIG_DXVA2
79  struct dxva_context dxva2;
80 #endif
81 #if CONFIG_D3D12VA
82  struct D3D12VADecodeContext d3d12va;
83 #endif
85 
86 typedef struct FFDXVASharedContext {
88 
89  // FF_DXVA2_WORKAROUND_* flags
90  uint64_t workaround;
91 
92  // E.g. AV_PIX_FMT_D3D11 (same as AVCodecContext.pix_fmt, except during init)
94 
96 
97 #if CONFIG_D3D11VA
98  ID3D11VideoDecoder *d3d11_decoder;
99  D3D11_VIDEO_DECODER_CONFIG d3d11_config;
100  ID3D11VideoDecoderOutputView **d3d11_views;
101  int nb_d3d11_views;
102  ID3D11Texture2D *d3d11_texture;
103 #endif
104 
105 #if CONFIG_DXVA2
106  IDirectXVideoDecoder *dxva2_decoder;
107  IDirectXVideoDecoderService *dxva2_service;
108  DXVA2_ConfigPictureDecode dxva2_config;
109 #endif
110 
111  // Legacy (but used by code outside of setup)
112  // In generic mode, DXVA_CONTEXT() will return a pointer to this.
115 
116 #define DXVA_SHARED_CONTEXT(avctx) ((FFDXVASharedContext *)((avctx)->internal->hwaccel_priv_data))
117 
118 #define DXVA_CONTEXT(avctx) (AVDXVAContext *)((avctx)->hwaccel_context ? (avctx)->hwaccel_context : (&(DXVA_SHARED_CONTEXT(avctx)->ctx)))
119 
120 #define D3D11VA_CONTEXT(ctx) (&ctx->d3d11va)
121 #define DXVA2_CONTEXT(ctx) (&ctx->dxva2)
122 
123 #define DXVA2_CONTEXT_VAR(avctx, ctx, var) (avctx->pix_fmt == AV_PIX_FMT_D3D12 ? 0 : (ff_dxva2_is_d3d11(avctx) ? D3D11VA_VAR(ctx, var) : DXVA2_VAR(ctx, var)))
124 
125 #define DXVA_CONTEXT_REPORT_ID(avctx, ctx) (*ff_dxva2_get_report_id(avctx, ctx))
126 #define DXVA_CONTEXT_WORKAROUND(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, workaround)
127 #define DXVA_CONTEXT_COUNT(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, surface_count)
128 #define DXVA_CONTEXT_DECODER(avctx, ctx) (avctx->pix_fmt == AV_PIX_FMT_D3D12 ? 0 : (ff_dxva2_is_d3d11(avctx) ? (void *)D3D11VA_VAR(ctx, decoder) : (void *)DXVA2_VAR(ctx, decoder)))
129 #define DXVA_CONTEXT_CFG(avctx, ctx) (avctx->pix_fmt == AV_PIX_FMT_D3D12 ? 0 : (ff_dxva2_is_d3d11(avctx) ? (void *)D3D11VA_VAR(ctx, cfg) : (void *)DXVA2_VAR(ctx, cfg)))
130 #define DXVA_CONTEXT_CFG_BITSTREAM(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, cfg->ConfigBitstreamRaw)
131 #define DXVA_CONTEXT_CFG_INTRARESID(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, cfg->ConfigIntraResidUnsigned)
132 #define DXVA_CONTEXT_CFG_RESIDACCEL(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, cfg->ConfigResidDiffAccelerator)
133 #define DXVA_CONTEXT_VALID(avctx, ctx) (DXVA_CONTEXT_DECODER(avctx, ctx) && \
134  DXVA_CONTEXT_CFG(avctx, ctx) && \
135  (ff_dxva2_is_d3d11(avctx) || DXVA2_VAR(ctx, surface_count)))
136 
137 #if CONFIG_D3D12VA
138 unsigned ff_d3d12va_get_surface_index(const AVCodecContext *avctx,
140  int curr);
141 #endif
142 
143 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx,
144  AVDXVAContext *, const AVFrame *frame, int curr);
145 
148  unsigned type, const void *data, unsigned size,
149  unsigned mb_count);
150 
151 
153  const void *pp, unsigned pp_size,
154  const void *qm, unsigned qm_size,
155  int (*commit_bs_si)(AVCodecContext *,
157  DECODER_BUFFER_DESC *slice));
158 
160 
162 
164  AVBufferRef *hw_frames_ctx);
165 
166 int ff_dxva2_is_d3d11(const AVCodecContext *avctx);
167 
168 unsigned *ff_dxva2_get_report_id(const AVCodecContext *avctx, AVDXVAContext *ctx);
169 
170 void ff_dxva2_h264_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_H264 *pp);
171 
172 void ff_dxva2_h264_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_H264 *qm);
173 
174 #if CONFIG_HEVC_D3D12VA_HWACCEL || CONFIG_HEVC_D3D11VA_HWACCEL || CONFIG_HEVC_D3D11VA2_HWACCEL || CONFIG_HEVC_DXVA2_HWACCEL
175 void ff_dxva2_hevc_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_HEVC *pp);
176 
177 void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_HEVC *qm);
178 #endif
179 
180 #if CONFIG_VP9_D3D12VA_HWACCEL || CONFIG_VP9_D3D11VA_HWACCEL || CONFIG_VP9_D3D11VA2_HWACCEL || CONFIG_VP9_DXVA2_HWACCEL
181 int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp);
182 #endif
183 
184 #if CONFIG_AV1_D3D12VA_HWACCEL || CONFIG_AV1_D3D11VA_HWACCEL || CONFIG_AV1_D3D11VA2_HWACCEL || CONFIG_AV1_DXVA2_HWACCEL
185 int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp);
186 #endif
187 
188 void ff_dxva2_mpeg2_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp);
189 
191 
192 void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size);
193 
194 void ff_dxva2_vc1_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp);
195 
196 void ff_dxva2_vc1_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, unsigned size);
197 
198 #endif /* AVCODEC_DXVA2_INTERNAL_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
ff_dxva2_hevc_fill_picture_parameters
void ff_dxva2_hevc_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_HEVC *pp)
Definition: dxva2_hevc.c:60
FFDXVASharedContext::pix_fmt
enum AVPixelFormat pix_fmt
Definition: dxva2_internal.h:93
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
ff_dxva2_get_report_id
unsigned * ff_dxva2_get_report_id(const AVCodecContext *avctx, AVDXVAContext *ctx)
Definition: dxva2.c:1066
internal.h
ff_dxva2_common_end_frame
int ff_dxva2_common_end_frame(AVCodecContext *, AVFrame *, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*commit_bs_si)(AVCodecContext *, DECODER_BUFFER_DESC *bs, DECODER_BUFFER_DESC *slice))
Definition: dxva2.c:892
data
const char data[16]
Definition: mxf.c:149
ff_d3d12va_get_surface_index
unsigned ff_d3d12va_get_surface_index(const AVCodecContext *avctx, D3D12VADecodeContext *ctx, const AVFrame *frame, int curr)
Definition: d3d12va_decode.c:50
ff_dxva2_common_frame_params
int ff_dxva2_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Definition: dxva2.c:593
FFDXVASharedContext::workaround
uint64_t workaround
Definition: dxva2_internal.h:90
FFDXVASharedContext::device_ctx
AVHWDeviceContext * device_ctx
Definition: dxva2_internal.h:95
ff_dxva2_mpeg2_fill_quantization_matrices
void ff_dxva2_mpeg2_fill_quantization_matrices(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_QmatrixData *qm)
Definition: dxva2_mpeg2.c:109
AVD3D11VAContext
This structure is used to provides the necessary configurations and data to the Direct3D11 FFmpeg HWA...
Definition: d3d11va.h:56
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_dxva2_mpeg2_fill_picture_parameters
void ff_dxva2_mpeg2_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp)
Definition: dxva2_mpeg2.c:43
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
ff_dxva2_vc1_fill_picture_parameters
void ff_dxva2_vc1_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp)
Definition: dxva2_vc1.c:43
ff_dxva2_h264_fill_scaling_lists
void ff_dxva2_h264_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_H264 *qm)
Definition: dxva2_h264.c:168
FFDXVASharedContext::ctx
AVDXVAContext ctx
Definition: dxva2_internal.h:113
ctx
AVFormatContext * ctx
Definition: movenc.c:49
ff_dxva2_get_surface_index
unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx, AVDXVAContext *, const AVFrame *frame, int curr)
Definition: dxva2.c:771
ff_dxva2_decode_init
int ff_dxva2_decode_init(AVCodecContext *avctx)
Definition: dxva2.c:656
FFDXVASharedContext
Definition: dxva2_internal.h:86
ff_dxva2_hevc_fill_scaling_lists
void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_HEVC *qm)
Definition: dxva2_hevc.c:205
dxva_context
This structure is used to provides the necessary configurations and data to the DXVA2 FFmpeg HWAccel ...
Definition: dxva2.h:54
hwcontext_dxva2.h
ff_dxva2_vc1_fill_slice
void ff_dxva2_vc1_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, unsigned size)
Definition: dxva2_vc1.c:167
size
int size
Definition: twinvq_data.h:10344
AVDXVAContext
Definition: dxva2_internal.h:74
ff_dxva2_mpeg2_fill_slice
void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size)
Definition: dxva2_mpeg2.c:126
DECODER_BUFFER_DESC
void DECODER_BUFFER_DESC
Definition: dxva2_internal.h:72
ff_dxva2_is_d3d11
int ff_dxva2_is_d3d11(const AVCodecContext *avctx)
Definition: dxva2.c:1057
d3d11va.h
ff_dxva2_commit_buffer
int ff_dxva2_commit_buffer(AVCodecContext *, AVDXVAContext *, DECODER_BUFFER_DESC *, unsigned type, const void *data, unsigned size, unsigned mb_count)
Definition: dxva2.c:803
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
ff_dxva2_vp9_fill_picture_parameters
int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp)
Definition: dxva2_vp9.c:46
dxva2.h
AVCodecContext
main external API structure.
Definition: avcodec.h:451
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
ff_dxva2_h264_fill_picture_parameters
void ff_dxva2_h264_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_H264 *pp)
Definition: dxva2_h264.c:51
ff_dxva2_av1_fill_picture_parameters
int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp)
Definition: dxva2_av1.c:60
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
hwcontext.h
FFDXVASharedContext::decoder_ref
AVBufferRef * decoder_ref
Definition: dxva2_internal.h:87
hwcontext_d3d11va.h
d3d12va_decode.h
ff_dxva2_decode_uninit
int ff_dxva2_decode_uninit(AVCodecContext *avctx)
Definition: dxva2.c:731