1
0

avcodec: add D3D12VA hardware accelerated AV1 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:47 +08:00 committed by Haihao Xiang
parent 326288c70a
commit b16fd96c5f
7 changed files with 237 additions and 3 deletions

2
configure vendored
View File

@ -3066,6 +3066,8 @@ av1_d3d11va_hwaccel_deps="d3d11va DXVA_PicParams_AV1"
av1_d3d11va_hwaccel_select="av1_decoder"
av1_d3d11va2_hwaccel_deps="d3d11va DXVA_PicParams_AV1"
av1_d3d11va2_hwaccel_select="av1_decoder"
av1_d3d12va_hwaccel_deps="d3d12va DXVA_PicParams_AV1"
av1_d3d12va_hwaccel_select="av1_decoder"
av1_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_AV1"
av1_dxva2_hwaccel_select="av1_decoder"
av1_nvdec_hwaccel_deps="nvdec CUVIDAV1PICPARAMS"

View File

@ -996,6 +996,7 @@ OBJS-$(CONFIG_VULKAN) += vulkan.o vulkan_video.o
OBJS-$(CONFIG_AV1_D3D11VA_HWACCEL) += dxva2_av1.o
OBJS-$(CONFIG_AV1_DXVA2_HWACCEL) += dxva2_av1.o
OBJS-$(CONFIG_AV1_D3D12VA_HWACCEL) += dxva2_av1.o d3d12va_av1.o
OBJS-$(CONFIG_AV1_NVDEC_HWACCEL) += nvdec_av1.o
OBJS-$(CONFIG_AV1_VAAPI_HWACCEL) += vaapi_av1.o
OBJS-$(CONFIG_AV1_VDPAU_HWACCEL) += vdpau_av1.o

View File

@ -511,6 +511,7 @@ static int get_pixel_format(AVCodecContext *avctx)
enum AVPixelFormat pix_fmt = get_sw_pixel_format(avctx, seq);
#define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
CONFIG_AV1_D3D12VA_HWACCEL + \
CONFIG_AV1_NVDEC_HWACCEL + \
CONFIG_AV1_VAAPI_HWACCEL + \
CONFIG_AV1_VDPAU_HWACCEL + \
@ -529,6 +530,9 @@ static int get_pixel_format(AVCodecContext *avctx)
*fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
*fmtp++ = AV_PIX_FMT_D3D11;
#endif
#if CONFIG_AV1_D3D12VA_HWACCEL
*fmtp++ = AV_PIX_FMT_D3D12;
#endif
#if CONFIG_AV1_NVDEC_HWACCEL
*fmtp++ = AV_PIX_FMT_CUDA;
#endif
@ -550,6 +554,9 @@ static int get_pixel_format(AVCodecContext *avctx)
*fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
*fmtp++ = AV_PIX_FMT_D3D11;
#endif
#if CONFIG_AV1_D3D12VA_HWACCEL
*fmtp++ = AV_PIX_FMT_D3D12;
#endif
#if CONFIG_AV1_NVDEC_HWACCEL
*fmtp++ = AV_PIX_FMT_CUDA;
#endif
@ -1507,6 +1514,9 @@ const FFCodec ff_av1_decoder = {
#if CONFIG_AV1_D3D11VA2_HWACCEL
HWACCEL_D3D11VA2(av1),
#endif
#if CONFIG_AV1_D3D12VA_HWACCEL
HWACCEL_D3D12VA(av1),
#endif
#if CONFIG_AV1_NVDEC_HWACCEL
HWACCEL_NVDEC(av1),
#endif

214
libavcodec/d3d12va_av1.c Normal file
View File

@ -0,0 +1,214 @@
/*
* Direct3D 12 AV1 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/hwcontext_d3d12va_internal.h"
#include "av1dec.h"
#include "dxva2_internal.h"
#include "d3d12va_decode.h"
#define MAX_TILES 256
typedef struct D3D12AV1DecodeContext {
D3D12VADecodeContext ctx;
uint8_t *bitstream_buffer;
} D3D12AV1DecodeContext;
#define D3D12_AV1_DECODE_CONTEXT(avctx) ((D3D12AV1DecodeContext *)D3D12VA_DECODE_CONTEXT(avctx))
typedef struct AV1DecodePictureContext {
DXVA_PicParams_AV1 pp;
unsigned tile_count;
DXVA_Tile_AV1 tiles[MAX_TILES];
uint8_t *bitstream;
unsigned bitstream_size;
} AV1DecodePictureContext;
static int d3d12va_av1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
{
const AV1DecContext *h = avctx->priv_data;
AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
if (!ctx)
return -1;
av_assert0(ctx_pic);
ctx->used_mask = 0;
if (ff_dxva2_av1_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp) < 0)
return -1;
ctx_pic->bitstream = NULL;
ctx_pic->bitstream_size = 0;
ctx_pic->tile_count = 0;
return 0;
}
static int d3d12va_av1_decode_slice(AVCodecContext *avctx,
const uint8_t *buffer,
uint32_t size)
{
const AV1DecContext *h = avctx->priv_data;
const AV1RawFrameHeader *frame_header = h->raw_frame_header;
AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
int offset = 0;
uint32_t tg_start, tg_end;
ctx_pic->tile_count = frame_header->tile_cols * frame_header->tile_rows;
if (ctx_pic->tile_count > MAX_TILES)
return AVERROR(ENOSYS);
if (ctx_pic->tile_count == h->tg_end - h->tg_start + 1) {
tg_start = 0;
tg_end = ctx_pic->tile_count - 1;
ctx_pic->bitstream = (uint8_t *)buffer;
ctx_pic->bitstream_size = size;
} else {
ctx_pic->bitstream = D3D12_AV1_DECODE_CONTEXT(avctx)->bitstream_buffer;
memcpy(ctx_pic->bitstream + ctx_pic->bitstream_size, buffer, size);
tg_start = h->tg_start;
tg_end = h->tg_end;
offset = ctx_pic->bitstream_size;
ctx_pic->bitstream_size += size;
}
for (uint32_t tile_num = tg_start; tile_num <= tg_end; tile_num++) {
ctx_pic->tiles[tile_num].DataOffset = offset + h->tile_group_info[tile_num].tile_offset;
ctx_pic->tiles[tile_num].DataSize = h->tile_group_info[tile_num].tile_size;
ctx_pic->tiles[tile_num].row = h->tile_group_info[tile_num].tile_row;
ctx_pic->tiles[tile_num].column = h->tile_group_info[tile_num].tile_column;
ctx_pic->tiles[tile_num].anchor_frame = 0xFF;
}
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 AV1DecContext *h = avctx->priv_data;
AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
uint8_t *mapped_data;
D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++];
args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
args->Size = sizeof(DXVA_Tile_AV1) * ctx_pic->tile_count;
args->pData = ctx_pic->tiles;
input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
.pBuffer = buffer,
.Offset = 0,
.Size = ctx_pic->bitstream_size,
};
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);
}
memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->bitstream_size);
ID3D12Resource_Unmap(buffer, 0, NULL);
return 0;
}
static int d3d12va_av1_end_frame(AVCodecContext *avctx)
{
int ret;
const AV1DecContext *h = avctx->priv_data;
D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
if (ctx_pic->tiles <= 0 || ctx_pic->bitstream_size <= 0)
return -1;
ret = ff_d3d12va_common_end_frame(avctx, h->cur_frame.f, &ctx_pic->pp, sizeof(ctx_pic->pp),
NULL, 0, update_input_arguments);
return ret;
}
static int d3d12va_av1_decode_init(AVCodecContext *avctx)
{
const AV1DecContext *h = avctx->priv_data;
D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
D3D12AV1DecodeContext *av1_ctx = D3D12_AV1_DECODE_CONTEXT(avctx);
AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
DXVA_PicParams_AV1 pp;
int ret;
if (avctx->profile != FF_PROFILE_AV1_MAIN)
return AVERROR(EINVAL);
ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE0;
ctx->max_num_ref = FF_ARRAY_ELEMS(pp.RefFrameMapTextureIndex) + 1;
ret = ff_d3d12va_decode_init(avctx);
if (ret < 0)
return ret;
if (!av1_ctx->bitstream_buffer) {
av1_ctx->bitstream_buffer = av_malloc(ff_d3d12va_get_suitable_max_bitstream_size(avctx));
if (!av1_ctx->bitstream_buffer)
return AVERROR(ENOMEM);
}
return 0;
}
static int d3d12va_av1_decode_uninit(AVCodecContext *avctx)
{
const AV1DecContext *h = avctx->priv_data;
D3D12AV1DecodeContext *ctx = D3D12_AV1_DECODE_CONTEXT(avctx);
AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
if (ctx->bitstream_buffer)
av_freep(&ctx->bitstream_buffer);
return ff_d3d12va_decode_uninit(avctx);
}
#if CONFIG_AV1_D3D12VA_HWACCEL
const FFHWAccel ff_av1_d3d12va_hwaccel = {
.p.name = "av1_d3d12va",
.p.type = AVMEDIA_TYPE_VIDEO,
.p.id = AV_CODEC_ID_AV1,
.p.pix_fmt = AV_PIX_FMT_D3D12,
.init = d3d12va_av1_decode_init,
.uninit = d3d12va_av1_decode_uninit,
.start_frame = d3d12va_av1_start_frame,
.decode_slice = d3d12va_av1_decode_slice,
.end_frame = d3d12va_av1_end_frame,
.frame_params = ff_d3d12va_common_frame_params,
.frame_priv_data_size = sizeof(AV1DecodePictureContext),
.priv_data_size = sizeof(D3D12AV1DecodeContext),
};
#endif

View File

@ -56,10 +56,11 @@ static int get_bit_depth_from_seq(const AV1RawSequenceHeader *seq)
return 8;
}
static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const AV1DecContext *h,
int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx,
DXVA_PicParams_AV1 *pp)
{
int i,j, uses_lr;
const AV1DecContext *h = avctx->priv_data;
const AV1RawSequenceHeader *seq = h->raw_seq;
const AV1RawFrameHeader *frame_header = h->raw_frame_header;
const AV1RawFilmGrainParams *film_grain = &h->cur_frame.film_grain;
@ -75,7 +76,6 @@ static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *c
pp->max_width = seq->max_frame_width_minus_1 + 1;
pp->max_height = seq->max_frame_height_minus_1 + 1;
pp->CurrPicTextureIndex = ff_dxva2_get_surface_index(avctx, ctx, h->cur_frame.f, 1);
pp->superres_denom = frame_header->use_superres ? frame_header->coded_denom + AV1_SUPERRES_DENOM_MIN : AV1_SUPERRES_NUM;
pp->bitdepth = get_bit_depth_from_seq(seq);
pp->seq_profile = seq->seq_profile;
@ -154,6 +154,8 @@ static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *c
pp->RefFrameMapTextureIndex[i] = ff_dxva2_get_surface_index(avctx, ctx, ref_frame, 0);
}
pp->CurrPicTextureIndex = ff_dxva2_get_surface_index(avctx, ctx, h->cur_frame.f, 1);
/* Loop filter parameters */
pp->loop_filter.filter_level[0] = frame_header->loop_filter_level[0];
pp->loop_filter.filter_level[1] = frame_header->loop_filter_level[1];
@ -281,7 +283,7 @@ static int dxva2_av1_start_frame(AVCodecContext *avctx,
av_assert0(ctx_pic);
/* Fill up DXVA_PicParams_AV1 */
if (fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp) < 0)
if (ff_dxva2_av1_fill_picture_parameters(avctx, ctx, &ctx_pic->pp) < 0)
return -1;
ctx_pic->bitstream_size = 0;

View File

@ -177,4 +177,8 @@ void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext
int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp);
#if CONFIG_AV1_D3D12VA_HWACCEL || CONFIG_AV1_D3D11VA_HWACCEL || CONFIG_AV1_D3D11VA2_HWACCEL || CONFIG_AV1_DXVA2_HWACCEL
int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp);
#endif
#endif /* AVCODEC_DXVA2_INTERNAL_H */

View File

@ -21,6 +21,7 @@
extern const struct FFHWAccel ff_av1_d3d11va_hwaccel;
extern const struct FFHWAccel ff_av1_d3d11va2_hwaccel;
extern const struct FFHWAccel ff_av1_d3d12va_hwaccel;
extern const struct FFHWAccel ff_av1_dxva2_hwaccel;
extern const struct FFHWAccel ff_av1_nvdec_hwaccel;
extern const struct FFHWAccel ff_av1_vaapi_hwaccel;