1
0
Fork 0

avcodec: add D3D12VA hardware accelerated VP9 decoding

The command below is how to enable d3d12va:
ffmpeg -hwaccel d3d12va -i input.mp4 output.mp4

Signed-off-by: Wu Jianhua <toqsxw@outlook.com>
Signed-off-by: Tong Wu <tong1.wu@intel.com>
This commit is contained in:
Wu Jianhua 2023-12-05 14:46:46 +08:00 committed by Haihao Xiang
parent cbb93c4ff6
commit 326288c70a
7 changed files with 190 additions and 5 deletions

2
configure vendored
View File

@ -3170,6 +3170,8 @@ vp9_d3d11va_hwaccel_deps="d3d11va DXVA_PicParams_VP9"
vp9_d3d11va_hwaccel_select="vp9_decoder"
vp9_d3d11va2_hwaccel_deps="d3d11va DXVA_PicParams_VP9"
vp9_d3d11va2_hwaccel_select="vp9_decoder"
vp9_d3d12va_hwaccel_deps="d3d12va DXVA_PicParams_VP9"
vp9_d3d12va_hwaccel_select="vp9_decoder"
vp9_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_VP9"
vp9_dxva2_hwaccel_select="vp9_decoder"
vp9_nvdec_hwaccel_deps="nvdec"

View File

@ -1045,6 +1045,7 @@ OBJS-$(CONFIG_VP8_NVDEC_HWACCEL) += nvdec_vp8.o
OBJS-$(CONFIG_VP8_VAAPI_HWACCEL) += vaapi_vp8.o
OBJS-$(CONFIG_VP9_D3D11VA_HWACCEL) += dxva2_vp9.o
OBJS-$(CONFIG_VP9_DXVA2_HWACCEL) += dxva2_vp9.o
OBJS-$(CONFIG_VP9_D3D12VA_HWACCEL) += dxva2_vp9.o d3d12va_vp9.o
OBJS-$(CONFIG_VP9_NVDEC_HWACCEL) += nvdec_vp9.o
OBJS-$(CONFIG_VP9_VAAPI_HWACCEL) += vaapi_vp9.o
OBJS-$(CONFIG_VP9_VDPAU_HWACCEL) += vdpau_vp9.o

171
libavcodec/d3d12va_vp9.c Normal file
View File

@ -0,0 +1,171 @@
/*
* Direct3D 12 VP9 HW acceleration
*
* copyright (c) 2022-2023 Wu Jianhua <toqsxw@outlook.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config_components.h"
#include "libavutil/avassert.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hwcontext_d3d12va_internal.h"
#include "vp9shared.h"
#include "dxva2_internal.h"
#include "d3d12va_decode.h"
typedef struct VP9DecodePictureContext {
DXVA_PicParams_VP9 pp;
DXVA_Slice_VPx_Short slice;
const uint8_t *bitstream;
unsigned bitstream_size;
} VP9DecodePictureContext;
static void fill_slice_short(DXVA_Slice_VPx_Short *slice, unsigned position, unsigned size)
{
memset(slice, 0, sizeof(*slice));
slice->BSNALunitDataLocation = position;
slice->SliceBytesInBuffer = size;
slice->wBadSliceChopping = 0;
}
static int d3d12va_vp9_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
{
const VP9SharedContext *h = avctx->priv_data;
D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
if (!ctx)
return -1;
av_assert0(ctx_pic);
ctx->used_mask = 0;
if (ff_dxva2_vp9_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp) < 0)
return -1;
ctx_pic->bitstream_size = 0;
ctx_pic->bitstream = NULL;
return 0;
}
static int d3d12va_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
{
const VP9SharedContext *h = avctx->priv_data;
VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
unsigned position;
if (!ctx_pic->bitstream)
ctx_pic->bitstream = buffer;
ctx_pic->bitstream_size += size;
position = buffer - ctx_pic->bitstream;
fill_slice_short(&ctx_pic->slice, position, size);
return 0;
}
static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
{
D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
AVHWFramesContext *frames_ctx = D3D12VA_FRAMES_CONTEXT(avctx);
AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx;
const VP9SharedContext *h = avctx->priv_data;
VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
uint8_t *mapped_data;
D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args;
if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) {
av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
return AVERROR(EINVAL);
}
args = &input_args->FrameArguments[input_args->NumFrameArguments++];
args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
args->Size = sizeof(ctx_pic->slice);
args->pData = &ctx_pic->slice;
memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->slice.SliceBytesInBuffer);
ID3D12Resource_Unmap(buffer, 0, NULL);
input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
.pBuffer = buffer,
.Offset = 0,
.Size = ctx_pic->slice.SliceBytesInBuffer,
};
return 0;
}
static int d3d12va_vp9_end_frame(AVCodecContext *avctx)
{
VP9SharedContext *h = avctx->priv_data;
VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
if (ctx_pic->bitstream_size <= 0)
return -1;
return ff_d3d12va_common_end_frame(avctx, h->frames[CUR_FRAME].tf.f,
&ctx_pic->pp, sizeof(ctx_pic->pp), NULL, 0, update_input_arguments);
}
static int d3d12va_vp9_decode_init(AVCodecContext *avctx)
{
D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
DXVA_PicParams_VP9 pp;
switch (avctx->profile) {
case FF_PROFILE_VP9_2:
case FF_PROFILE_VP9_3:
ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_VP9_10BIT_PROFILE2;
break;
case FF_PROFILE_VP9_0:
case FF_PROFILE_VP9_1:
default:
ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_VP9;
break;
};
ctx->max_num_ref = FF_ARRAY_ELEMS(pp.frame_refs) + 1;
return ff_d3d12va_decode_init(avctx);
}
#if CONFIG_VP9_D3D12VA_HWACCEL
const FFHWAccel ff_vp9_d3d12va_hwaccel = {
.p.name = "vp9_d3d12va",
.p.type = AVMEDIA_TYPE_VIDEO,
.p.id = AV_CODEC_ID_VP9,
.p.pix_fmt = AV_PIX_FMT_D3D12,
.init = d3d12va_vp9_decode_init,
.uninit = ff_d3d12va_decode_uninit,
.start_frame = d3d12va_vp9_start_frame,
.decode_slice = d3d12va_vp9_decode_slice,
.end_frame = d3d12va_vp9_end_frame,
.frame_params = ff_d3d12va_common_frame_params,
.frame_priv_data_size = sizeof(VP9DecodePictureContext),
.priv_data_size = sizeof(D3D12VADecodeContext),
};
#endif

View File

@ -175,4 +175,6 @@ void ff_dxva2_hevc_fill_picture_parameters(const AVCodecContext *avctx, AVDXVACo
void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_HEVC *qm);
int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp);
#endif /* AVCODEC_DXVA2_INTERNAL_H */

View File

@ -43,19 +43,18 @@ static void fill_picture_entry(DXVA_PicEntry_VPx *pic,
pic->bPicEntry = index | (flag << 7);
}
static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const VP9SharedContext *h,
int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx,
DXVA_PicParams_VP9 *pp)
{
const VP9SharedContext *h = avctx->priv_data;
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
int i;
const AVPixFmtDescriptor * pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
if (!pixdesc)
return -1;
memset(pp, 0, sizeof(*pp));
fill_picture_entry(&pp->CurrPic, ff_dxva2_get_surface_index(avctx, ctx, h->frames[CUR_FRAME].tf.f, 1), 0);
pp->profile = h->h.profile;
pp->wFormatAndPictureInfoFlags = ((h->h.keyframe == 0) << 0) |
((h->h.invisible == 0) << 1) |
@ -98,6 +97,8 @@ static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *c
pp->ref_frame_sign_bias[i + 1] = h->h.signbias[i];
}
fill_picture_entry(&pp->CurrPic, ff_dxva2_get_surface_index(avctx, ctx, h->frames[CUR_FRAME].tf.f, 1), 0);
pp->filter_level = h->h.filter.level;
pp->sharpness_level = h->h.filter.sharpness;
@ -265,7 +266,7 @@ static int dxva2_vp9_start_frame(AVCodecContext *avctx,
av_assert0(ctx_pic);
/* Fill up DXVA_PicParams_VP9 */
if (fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp) < 0)
if (ff_dxva2_vp9_fill_picture_parameters(avctx, ctx, &ctx_pic->pp) < 0)
return -1;
ctx_pic->bitstream_size = 0;

View File

@ -73,6 +73,7 @@ extern const struct FFHWAccel ff_vp8_nvdec_hwaccel;
extern const struct FFHWAccel ff_vp8_vaapi_hwaccel;
extern const struct FFHWAccel ff_vp9_d3d11va_hwaccel;
extern const struct FFHWAccel ff_vp9_d3d11va2_hwaccel;
extern const struct FFHWAccel ff_vp9_d3d12va_hwaccel;
extern const struct FFHWAccel ff_vp9_dxva2_hwaccel;
extern const struct FFHWAccel ff_vp9_nvdec_hwaccel;
extern const struct FFHWAccel ff_vp9_vaapi_hwaccel;

View File

@ -170,6 +170,7 @@ static int update_size(AVCodecContext *avctx, int w, int h)
{
#define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + \
CONFIG_VP9_D3D11VA_HWACCEL * 2 + \
CONFIG_VP9_D3D12VA_HWACCEL + \
CONFIG_VP9_NVDEC_HWACCEL + \
CONFIG_VP9_VAAPI_HWACCEL + \
CONFIG_VP9_VDPAU_HWACCEL + \
@ -196,6 +197,9 @@ static int update_size(AVCodecContext *avctx, int w, int h)
*fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
*fmtp++ = AV_PIX_FMT_D3D11;
#endif
#if CONFIG_VP9_D3D12VA_HWACCEL
*fmtp++ = AV_PIX_FMT_D3D12;
#endif
#if CONFIG_VP9_NVDEC_HWACCEL
*fmtp++ = AV_PIX_FMT_CUDA;
#endif
@ -1899,6 +1903,9 @@ const FFCodec ff_vp9_decoder = {
#if CONFIG_VP9_D3D11VA2_HWACCEL
HWACCEL_D3D11VA2(vp9),
#endif
#if CONFIG_VP9_D3D12VA_HWACCEL
HWACCEL_D3D12VA(vp9),
#endif
#if CONFIG_VP9_NVDEC_HWACCEL
HWACCEL_NVDEC(vp9),
#endif