[FFmpeg-devel] [PATCH v2 3/9] avcodec: add D3D12VA hardware accelerated HEVC decoding
Tong Wu
tong1.wu at intel.com
Wed May 31 06:07:16 EEST 2023
From: Wu Jianhua <toqsxw at outlook.com>
The command below is how to enable d3d12va:
ffmpeg -hwaccel d3d12va -i input.mp4 output.mp4
Signed-off-by: Wu Jianhua <toqsxw at outlook.com>
---
configure | 2 +
libavcodec/Makefile | 1 +
libavcodec/d3d12va_hevc.c | 211 ++++++++++++++++++++++++++++++++++++
libavcodec/dxva2_hevc.c | 10 +-
libavcodec/dxva2_internal.h | 6 +
libavcodec/hevcdec.c | 10 ++
libavcodec/hwaccels.h | 1 +
7 files changed, 237 insertions(+), 4 deletions(-)
create mode 100644 libavcodec/d3d12va_hevc.c
diff --git a/configure b/configure
index f5dad4653f..3d25e5fdea 100755
--- a/configure
+++ b/configure
@@ -3051,6 +3051,8 @@ hevc_d3d11va_hwaccel_deps="d3d11va DXVA_PicParams_HEVC"
hevc_d3d11va_hwaccel_select="hevc_decoder"
hevc_d3d11va2_hwaccel_deps="d3d11va DXVA_PicParams_HEVC"
hevc_d3d11va2_hwaccel_select="hevc_decoder"
+hevc_d3d12va_hwaccel_deps="d3d12va DXVA_PicParams_HEVC"
+hevc_d3d12va_hwaccel_select="hevc_decoder"
hevc_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_HEVC"
hevc_dxva2_hwaccel_select="hevc_decoder"
hevc_nvdec_hwaccel_deps="nvdec"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ae143d8821..6cc28f2fd0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1004,6 +1004,7 @@ OBJS-$(CONFIG_H264_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
OBJS-$(CONFIG_H264_VULKAN_HWACCEL) += vulkan_decode.o vulkan_h264.o
OBJS-$(CONFIG_HEVC_D3D11VA_HWACCEL) += dxva2_hevc.o
OBJS-$(CONFIG_HEVC_DXVA2_HWACCEL) += dxva2_hevc.o
+OBJS-$(CONFIG_HEVC_D3D12VA_HWACCEL) += dxva2_hevc.o d3d12va_hevc.o
OBJS-$(CONFIG_HEVC_NVDEC_HWACCEL) += nvdec_hevc.o
OBJS-$(CONFIG_HEVC_QSV_HWACCEL) += qsvdec.o
OBJS-$(CONFIG_HEVC_VAAPI_HWACCEL) += vaapi_hevc.o h265_profile_level.o
diff --git a/libavcodec/d3d12va_hevc.c b/libavcodec/d3d12va_hevc.c
new file mode 100644
index 0000000000..1d94831e01
--- /dev/null
+++ b/libavcodec/d3d12va_hevc.c
@@ -0,0 +1,211 @@
+/*
+ * Direct3D 12 HEVC HW acceleration
+ *
+ * copyright (c) 2022-2023 Wu Jianhua <toqsxw at 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 "hevc_data.h"
+#include "hevcdec.h"
+#include "dxva2_internal.h"
+#include "d3d12va.h"
+#include <dxva.h>
+
+#define MAX_SLICES 256
+
+typedef struct HEVCDecodePictureContext {
+ DXVA_PicParams_HEVC pp;
+ DXVA_Qmatrix_HEVC qm;
+ unsigned slice_count;
+ DXVA_Slice_HEVC_Short slice_short[MAX_SLICES];
+ const uint8_t *bitstream;
+ unsigned bitstream_size;
+} HEVCDecodePictureContext;
+
+static void fill_slice_short(DXVA_Slice_HEVC_Short *slice, unsigned position, unsigned size)
+{
+ memset(slice, 0, sizeof(*slice));
+ slice->BSNALunitDataLocation = position;
+ slice->SliceBytesInBuffer = size;
+ slice->wBadSliceChopping = 0;
+}
+
+static int d3d12va_hevc_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
+{
+ const HEVCContext *h = avctx->priv_data;
+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
+ HEVCDecodePictureContext *ctx_pic = h->ref->hwaccel_picture_private;
+
+ if (!ctx)
+ return -1;
+
+ av_assert0(ctx_pic);
+
+ ff_dxva2_hevc_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp);
+
+ ff_dxva2_hevc_fill_scaling_lists(avctx, (AVDXVAContext *)ctx, &ctx_pic->qm);
+
+ ctx_pic->slice_count = 0;
+ ctx_pic->bitstream_size = 0;
+ ctx_pic->bitstream = NULL;
+
+ return 0;
+}
+
+static int d3d12va_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
+{
+ const HEVCContext *h = avctx->priv_data;
+ const HEVCFrame *current_picture = h->ref;
+ HEVCDecodePictureContext *ctx_pic = current_picture->hwaccel_picture_private;
+ unsigned position;
+
+ if (ctx_pic->slice_count >= MAX_SLICES)
+ return AVERROR(ERANGE);
+
+ if (!ctx_pic->bitstream)
+ ctx_pic->bitstream = buffer;
+ ctx_pic->bitstream_size += size;
+
+ position = buffer - ctx_pic->bitstream;
+ fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count], position, size);
+ ctx_pic->slice_count++;
+
+ return 0;
+}
+
+#define START_CODE 65536
+#define START_CODE_SIZE 3
+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 HEVCContext *h = avctx->priv_data;
+ const HEVCFrame *current_picture = h->ref;
+ HEVCDecodePictureContext *ctx_pic = current_picture->hwaccel_picture_private;
+
+ int i, index;
+ uint8_t *mapped_data, *mapped_ptr;
+ DXVA_Slice_HEVC_Short *slice;
+ 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);
+ }
+
+ mapped_ptr = mapped_data;
+ for (i = 0; i < ctx_pic->slice_count; i++) {
+ UINT position, size;
+ slice = &ctx_pic->slice_short[i];
+
+ position = slice->BSNALunitDataLocation;
+ size = slice->SliceBytesInBuffer;
+
+ slice->SliceBytesInBuffer += START_CODE_SIZE;
+ slice->BSNALunitDataLocation = mapped_ptr - mapped_data;
+
+ *(uint32_t *)mapped_ptr = START_CODE;
+ mapped_ptr += START_CODE_SIZE;
+
+ memcpy(mapped_ptr, &ctx_pic->bitstream[position], size);
+ mapped_ptr += size;
+ }
+
+ ID3D12Resource_Unmap(buffer, 0, NULL);
+
+ input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
+ .pBuffer = buffer,
+ .Offset = 0,
+ .Size = mapped_ptr - mapped_data,
+ };
+
+ args = &input_args->FrameArguments[input_args->NumFrameArguments++];
+ args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
+ args->Size = sizeof(DXVA_Slice_HEVC_Short) * ctx_pic->slice_count;
+ args->pData = ctx_pic->slice_short;
+
+ index = ctx_pic->pp.CurrPic.Index7Bits;
+ ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture;
+ for (i = 0; i < FF_ARRAY_ELEMS(ctx_pic->pp.RefPicList); i++) {
+ index = ctx_pic->pp.RefPicList[i].Index7Bits;
+ if (index != 0x7f)
+ ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture;
+ }
+
+ return 0;
+}
+
+static int d3d12va_hevc_end_frame(AVCodecContext *avctx)
+{
+ HEVCContext *h = avctx->priv_data;
+ HEVCDecodePictureContext *ctx_pic = h->ref->hwaccel_picture_private;
+
+ int scale = ctx_pic->pp.dwCodingParamToolFlags & 1;
+
+ if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
+ return -1;
+
+ return ff_d3d12va_common_end_frame(avctx, h->ref->frame, &ctx_pic->pp, sizeof(ctx_pic->pp),
+ scale ? &ctx_pic->qm : NULL, scale ? sizeof(ctx_pic->qm) : 0, update_input_arguments);
+}
+
+static int d3d12va_hevc_decode_init(AVCodecContext *avctx)
+{
+ HEVCContext *h = avctx->priv_data;
+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
+
+ switch (avctx->profile) {
+ case FF_PROFILE_HEVC_MAIN_10:
+ ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_HEVC_MAIN10;
+ break;
+
+ case FF_PROFILE_HEVC_MAIN_STILL_PICTURE:
+ av_log(avctx, AV_LOG_ERROR, "D3D12 doesn't support PROFILE_HEVC_MAIN_STILL_PICTURE!\n");
+ return AVERROR(EINVAL);
+
+ case FF_PROFILE_HEVC_MAIN:
+ default:
+ ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_HEVC_MAIN;
+ break;
+ };
+
+ return ff_d3d12va_decode_init(avctx);
+}
+
+#if CONFIG_HEVC_D3D12VA_HWACCEL
+const AVHWAccel ff_hevc_d3d12va_hwaccel = {
+ .name = "hevc_d3d12va",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = AV_CODEC_ID_HEVC,
+ .pix_fmt = AV_PIX_FMT_D3D12,
+ .init = d3d12va_hevc_decode_init,
+ .uninit = ff_d3d12va_decode_uninit,
+ .start_frame = d3d12va_hevc_start_frame,
+ .decode_slice = d3d12va_hevc_decode_slice,
+ .end_frame = d3d12va_hevc_end_frame,
+ .frame_params = ff_d3d12va_common_frame_params,
+ .frame_priv_data_size = sizeof(HEVCDecodePictureContext),
+ .priv_data_size = sizeof(D3D12VADecodeContext),
+};
+#endif
diff --git a/libavcodec/dxva2_hevc.c b/libavcodec/dxva2_hevc.c
index 6b239d9917..2b8c0e2a9a 100644
--- a/libavcodec/dxva2_hevc.c
+++ b/libavcodec/dxva2_hevc.c
@@ -56,9 +56,10 @@ static int get_refpic_index(const DXVA_PicParams_HEVC *pp, int surface_index)
return 0xff;
}
-static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const HEVCContext *h,
+void ff_dxva2_hevc_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx,
DXVA_PicParams_HEVC *pp)
{
+ const HEVCContext *h = avctx->priv_data;
const HEVCFrame *current_picture = h->ref;
const HEVCSPS *sps = h->ps.sps;
const HEVCPPS *pps = h->ps.pps;
@@ -199,8 +200,9 @@ static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *
pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
}
-static void fill_scaling_lists(AVDXVAContext *ctx, const HEVCContext *h, DXVA_Qmatrix_HEVC *qm)
+void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_HEVC *qm)
{
+ const HEVCContext *h = avctx->priv_data;
unsigned i, j, pos;
const ScalingList *sl = h->ps.pps->scaling_list_data_present_flag ?
&h->ps.pps->scaling_list : &h->ps.sps->scaling_list;
@@ -368,10 +370,10 @@ static int dxva2_hevc_start_frame(AVCodecContext *avctx,
av_assert0(ctx_pic);
/* Fill up DXVA_PicParams_HEVC */
- fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp);
+ ff_dxva2_hevc_fill_picture_parameters(avctx, ctx, &ctx_pic->pp);
/* Fill up DXVA_Qmatrix_HEVC */
- fill_scaling_lists(ctx, h, &ctx_pic->qm);
+ ff_dxva2_hevc_fill_scaling_lists(avctx, ctx, &ctx_pic->qm);
ctx_pic->slice_count = 0;
ctx_pic->bitstream_size = 0;
diff --git a/libavcodec/dxva2_internal.h b/libavcodec/dxva2_internal.h
index 03f4003c67..f6106d80da 100644
--- a/libavcodec/dxva2_internal.h
+++ b/libavcodec/dxva2_internal.h
@@ -168,4 +168,10 @@ void ff_dxva2_h264_fill_picture_parameters(const AVCodecContext *avctx, AVDXVACo
void ff_dxva2_h264_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_H264 *qm);
#endif
+#if CONFIG_HEVC_D3D12VA_HWACCEL
+void ff_dxva2_hevc_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_HEVC *pp);
+
+void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_HEVC *qm);
+#endif
+
#endif /* AVCODEC_DXVA2_INTERNAL_H */
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index eee77ec4db..f515725aa7 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -402,6 +402,7 @@ static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
{
#define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
+ CONFIG_HEVC_D3D12VA_HWACCEL + \
CONFIG_HEVC_NVDEC_HWACCEL + \
CONFIG_HEVC_VAAPI_HWACCEL + \
CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
@@ -419,6 +420,9 @@ static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
*fmt++ = AV_PIX_FMT_D3D11VA_VLD;
*fmt++ = AV_PIX_FMT_D3D11;
#endif
+#if CONFIG_HEVC_D3D12VA_HWACCEL
+ *fmt++ = AV_PIX_FMT_D3D12;
+#endif
#if CONFIG_HEVC_VAAPI_HWACCEL
*fmt++ = AV_PIX_FMT_VAAPI;
#endif
@@ -443,6 +447,9 @@ static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
*fmt++ = AV_PIX_FMT_D3D11VA_VLD;
*fmt++ = AV_PIX_FMT_D3D11;
#endif
+#if CONFIG_HEVC_D3D12VA_HWACCEL
+ *fmt++ = AV_PIX_FMT_D3D12;
+#endif
#if CONFIG_HEVC_VAAPI_HWACCEL
*fmt++ = AV_PIX_FMT_VAAPI;
#endif
@@ -3778,6 +3785,9 @@ const FFCodec ff_hevc_decoder = {
#if CONFIG_HEVC_D3D11VA2_HWACCEL
HWACCEL_D3D11VA2(hevc),
#endif
+#if CONFIG_HEVC_D3D12VA_HWACCEL
+ HWACCEL_D3D12VA(hevc),
+#endif
#if CONFIG_HEVC_NVDEC_HWACCEL
HWACCEL_NVDEC(hevc),
#endif
diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
index be54604b81..70e115f78a 100644
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
@@ -41,6 +41,7 @@ extern const AVHWAccel ff_h264_videotoolbox_hwaccel;
extern const AVHWAccel ff_h264_vulkan_hwaccel;
extern const AVHWAccel ff_hevc_d3d11va_hwaccel;
extern const AVHWAccel ff_hevc_d3d11va2_hwaccel;
+extern const AVHWAccel ff_hevc_d3d12va_hwaccel;
extern const AVHWAccel ff_hevc_dxva2_hwaccel;
extern const AVHWAccel ff_hevc_nvdec_hwaccel;
extern const AVHWAccel ff_hevc_vaapi_hwaccel;
--
2.35.1.windows.2
More information about the ffmpeg-devel
mailing list