dxva2_internal.h (8111B)
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" 39 #include "libavutil/hwcontext_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" 47 #include "libavutil/hwcontext_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 #include "libavutil/mem.h" 69 70 #include "avcodec.h" 71 #include "internal.h" 72 73 typedef void DECODER_BUFFER_DESC; 74 75 typedef union { 76 #if CONFIG_D3D11VA 77 struct AVD3D11VAContext d3d11va; 78 #endif 79 #if CONFIG_DXVA2 80 struct dxva_context dxva2; 81 #endif 82 #if CONFIG_D3D12VA 83 struct D3D12VADecodeContext d3d12va; 84 #endif 85 } AVDXVAContext; 86 87 typedef struct FFDXVASharedContext { 88 AVBufferRef *decoder_ref; 89 90 // FF_DXVA2_WORKAROUND_* flags 91 uint64_t workaround; 92 93 // E.g. AV_PIX_FMT_D3D11 (same as AVCodecContext.pix_fmt, except during init) 94 enum AVPixelFormat pix_fmt; 95 96 AVHWDeviceContext *device_ctx; 97 98 #if CONFIG_D3D11VA 99 ID3D11VideoDecoder *d3d11_decoder; 100 D3D11_VIDEO_DECODER_CONFIG d3d11_config; 101 ID3D11VideoDecoderOutputView **d3d11_views; 102 int nb_d3d11_views; 103 ID3D11Texture2D *d3d11_texture; 104 #endif 105 106 #if CONFIG_DXVA2 107 IDirectXVideoDecoder *dxva2_decoder; 108 IDirectXVideoDecoderService *dxva2_service; 109 DXVA2_ConfigPictureDecode dxva2_config; 110 #endif 111 112 // Legacy (but used by code outside of setup) 113 // In generic mode, DXVA_CONTEXT() will return a pointer to this. 114 AVDXVAContext ctx; 115 } FFDXVASharedContext; 116 117 #define DXVA_SHARED_CONTEXT(avctx) ((FFDXVASharedContext *)((avctx)->internal->hwaccel_priv_data)) 118 119 #define DXVA_CONTEXT(avctx) (AVDXVAContext *)((avctx)->hwaccel_context ? (avctx)->hwaccel_context : (&(DXVA_SHARED_CONTEXT(avctx)->ctx))) 120 121 #define D3D11VA_CONTEXT(ctx) (&ctx->d3d11va) 122 #define DXVA2_CONTEXT(ctx) (&ctx->dxva2) 123 124 #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))) 125 126 #define DXVA_CONTEXT_REPORT_ID(avctx, ctx) (*ff_dxva2_get_report_id(avctx, ctx)) 127 #define DXVA_CONTEXT_WORKAROUND(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, workaround) 128 #define DXVA_CONTEXT_COUNT(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, surface_count) 129 #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))) 130 #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))) 131 #define DXVA_CONTEXT_CFG_BITSTREAM(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, cfg->ConfigBitstreamRaw) 132 #define DXVA_CONTEXT_CFG_INTRARESID(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, cfg->ConfigIntraResidUnsigned) 133 #define DXVA_CONTEXT_CFG_RESIDACCEL(avctx, ctx) DXVA2_CONTEXT_VAR(avctx, ctx, cfg->ConfigResidDiffAccelerator) 134 #define DXVA_CONTEXT_VALID(avctx, ctx) (DXVA_CONTEXT_DECODER(avctx, ctx) && \ 135 DXVA_CONTEXT_CFG(avctx, ctx) && \ 136 (ff_dxva2_is_d3d11(avctx) || DXVA2_VAR(ctx, surface_count))) 137 138 #if CONFIG_D3D12VA 139 unsigned ff_d3d12va_get_surface_index(const AVCodecContext *avctx, 140 D3D12VADecodeContext *ctx, const AVFrame *frame, 141 int curr); 142 #endif 143 144 unsigned ff_dxva2_get_surface_index(const AVCodecContext *avctx, 145 AVDXVAContext *, const AVFrame *frame, int curr); 146 147 int ff_dxva2_commit_buffer(AVCodecContext *, AVDXVAContext *, 148 DECODER_BUFFER_DESC *, 149 unsigned type, const void *data, unsigned size, 150 unsigned mb_count); 151 152 153 int ff_dxva2_common_end_frame(AVCodecContext *, AVFrame *, 154 const void *pp, unsigned pp_size, 155 const void *qm, unsigned qm_size, 156 int (*commit_bs_si)(AVCodecContext *, 157 DECODER_BUFFER_DESC *bs, 158 DECODER_BUFFER_DESC *slice)); 159 160 int ff_dxva2_decode_init(AVCodecContext *avctx); 161 162 int ff_dxva2_decode_uninit(AVCodecContext *avctx); 163 164 int ff_dxva2_common_frame_params(AVCodecContext *avctx, 165 AVBufferRef *hw_frames_ctx); 166 167 int ff_dxva2_is_d3d11(const AVCodecContext *avctx); 168 169 unsigned *ff_dxva2_get_report_id(const AVCodecContext *avctx, AVDXVAContext *ctx); 170 171 void ff_dxva2_h264_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_H264 *pp); 172 173 void ff_dxva2_h264_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_H264 *qm); 174 175 #if CONFIG_HEVC_D3D12VA_HWACCEL || CONFIG_HEVC_D3D11VA_HWACCEL || CONFIG_HEVC_D3D11VA2_HWACCEL || CONFIG_HEVC_DXVA2_HWACCEL 176 void ff_dxva2_hevc_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_HEVC *pp); 177 178 void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_HEVC *qm); 179 #endif 180 181 #if CONFIG_VP9_D3D12VA_HWACCEL || CONFIG_VP9_D3D11VA_HWACCEL || CONFIG_VP9_D3D11VA2_HWACCEL || CONFIG_VP9_DXVA2_HWACCEL 182 int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp); 183 #endif 184 185 #if CONFIG_AV1_D3D12VA_HWACCEL || CONFIG_AV1_D3D11VA_HWACCEL || CONFIG_AV1_D3D11VA2_HWACCEL || CONFIG_AV1_DXVA2_HWACCEL 186 int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp); 187 #endif 188 189 void ff_dxva2_mpeg2_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp); 190 191 void ff_dxva2_mpeg2_fill_quantization_matrices(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_QmatrixData *qm); 192 193 void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size); 194 195 void ff_dxva2_vc1_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp); 196 197 void ff_dxva2_vc1_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, unsigned size); 198 199 #endif /* AVCODEC_DXVA2_INTERNAL_H */