[FFmpeg-devel] [PATCH v10 6/9] avcodec: add D3D12VA hardware accelerated MPEG-2 decoding
Tong Wu
tong1.wu at intel.com
Sat Dec 2 12:12:46 EET 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>
Signed-off-by: Tong Wu <tong1.wu at intel.com>
---
configure | 2 +
libavcodec/Makefile | 1 +
libavcodec/d3d12va_mpeg2.c | 191 ++++++++++++++++++++++++++++++++++++
libavcodec/dxva2_internal.h | 6 ++
libavcodec/dxva2_mpeg2.c | 20 ++--
libavcodec/hwaccels.h | 1 +
libavcodec/mpeg12dec.c | 6 ++
7 files changed, 217 insertions(+), 10 deletions(-)
create mode 100644 libavcodec/d3d12va_mpeg2.c
diff --git a/configure b/configure
index 7feeeddaf5..000ea76c34 100755
--- a/configure
+++ b/configure
@@ -3130,6 +3130,8 @@ mpeg2_d3d11va_hwaccel_deps="d3d11va"
mpeg2_d3d11va_hwaccel_select="mpeg2video_decoder"
mpeg2_d3d11va2_hwaccel_deps="d3d11va"
mpeg2_d3d11va2_hwaccel_select="mpeg2video_decoder"
+mpeg2_d3d12va_hwaccel_deps="d3d12va"
+mpeg2_d3d12va_hwaccel_select="mpeg2video_decoder"
mpeg2_dxva2_hwaccel_deps="dxva2"
mpeg2_dxva2_hwaccel_select="mpeg2video_decoder"
mpeg2_nvdec_hwaccel_deps="nvdec"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0f152df17c..00c1c01cbd 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1027,6 +1027,7 @@ OBJS-$(CONFIG_MPEG1_VDPAU_HWACCEL) += vdpau_mpeg12.o
OBJS-$(CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
OBJS-$(CONFIG_MPEG2_D3D11VA_HWACCEL) += dxva2_mpeg2.o
OBJS-$(CONFIG_MPEG2_DXVA2_HWACCEL) += dxva2_mpeg2.o
+OBJS-$(CONFIG_MPEG2_D3D12VA_HWACCEL) += dxva2_mpeg2.o d3d12va_mpeg2.o
OBJS-$(CONFIG_MPEG2_NVDEC_HWACCEL) += nvdec_mpeg12.o
OBJS-$(CONFIG_MPEG2_QSV_HWACCEL) += qsvdec.o
OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL) += vaapi_mpeg2.o
diff --git a/libavcodec/d3d12va_mpeg2.c b/libavcodec/d3d12va_mpeg2.c
new file mode 100644
index 0000000000..91bf3f8b75
--- /dev/null
+++ b/libavcodec/d3d12va_mpeg2.c
@@ -0,0 +1,191 @@
+/*
+ * Direct3D12 MPEG-2 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 "mpegutils.h"
+#include "mpegvideodec.h"
+#include "d3d12va_decode.h"
+#include "dxva2_internal.h"
+
+#define MAX_SLICES 1024
+#define INVALID_REF 0xffff
+
+#define REF_RESOURCE(index) if (index != INVALID_REF) { \
+ ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture; \
+}
+
+typedef struct D3D12DecodePictureContext {
+ DXVA_PictureParameters pp;
+ DXVA_QmatrixData qm;
+ unsigned slice_count;
+ DXVA_SliceInfo slices[MAX_SLICES];
+ const uint8_t *bitstream;
+ unsigned bitstream_size;
+} D3D12DecodePictureContext;
+
+static int d3d12va_mpeg2_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
+{
+ const MpegEncContext *s = avctx->priv_data;
+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
+ D3D12DecodePictureContext *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
+ DXVA_QmatrixData *qm = &ctx_pic->qm;
+
+ if (!ctx)
+ return -1;
+
+ av_assert0(ctx_pic);
+
+ ctx->used_mask = 0;
+
+ ff_dxva2_mpeg2_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp);
+ ff_dxva2_mpeg2_fill_quantization_matrices(avctx, (AVDXVAContext *)ctx, &ctx_pic->qm);
+
+ // Post processing operations are not supported in D3D12 Video
+ ctx_pic->pp.wDeblockedPictureIndex = INVALID_REF;
+
+ ctx_pic->bitstream = NULL;
+ ctx_pic->bitstream_size = 0;
+ ctx_pic->slice_count = 0;
+
+ return 0;
+}
+
+static int d3d12va_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
+{
+ const MpegEncContext *s = avctx->priv_data;
+ D3D12DecodePictureContext *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
+
+ int is_field = s->picture_structure != PICT_FRAME;
+
+ if (ctx_pic->slice_count >= MAX_SLICES) {
+ return AVERROR(ERANGE);
+ }
+
+ if (!ctx_pic->bitstream)
+ ctx_pic->bitstream = buffer;
+ ctx_pic->bitstream_size += size;
+
+ ff_dxva2_mpeg2_fill_slice(avctx, &ctx_pic->slices[ctx_pic->slice_count++],
+ buffer - ctx_pic->bitstream, buffer, 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 MpegEncContext *s = avctx->priv_data;
+ D3D12DecodePictureContext *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
+
+ const int is_field = s->picture_structure != PICT_FRAME;
+ const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
+
+ int i;
+ uint8_t *mapped_data = NULL;
+ D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++];
+
+ D3D12_RANGE range = {
+ .Begin = 0,
+ .End = ctx_pic->bitstream_size,
+ };
+
+ if (FAILED(ID3D12Resource_Map(buffer, 0, &range, &mapped_data))) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
+ return AVERROR(EINVAL);
+ }
+
+ for (i = 0; i < ctx_pic->slice_count; i++) {
+ DXVA_SliceInfo *slice = &ctx_pic->slices[i];
+
+ if (i < ctx_pic->slice_count - 1)
+ slice->wNumberMBsInSlice = slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
+ else
+ slice->wNumberMBsInSlice = mb_count - slice[0].wNumberMBsInSlice;
+ }
+
+ memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->bitstream_size);
+
+ ID3D12Resource_Unmap(buffer, 0, &range);
+
+ args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
+ args->Size = sizeof(DXVA_SliceInfo) * ctx_pic->slice_count;
+ args->pData = ctx_pic->slices;
+
+ input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
+ .pBuffer = buffer,
+ .Offset = 0,
+ .Size = ctx_pic->bitstream_size,
+ };
+
+ return 0;
+}
+
+static int d3d12va_mpeg2_end_frame(AVCodecContext *avctx)
+{
+ int ret;
+ MpegEncContext *s = avctx->priv_data;
+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
+ D3D12DecodePictureContext *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
+
+ if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
+ return -1;
+
+ ret = ff_d3d12va_common_end_frame(avctx, s->current_picture_ptr->f, &ctx_pic->pp, sizeof(ctx_pic->pp),
+ &ctx_pic->qm, sizeof(ctx_pic->qm), update_input_arguments);
+ if (!ret)
+ ff_mpeg_draw_horiz_band(s, 0, avctx->height);
+
+ return ret;
+}
+
+static int d3d12va_mpeg2_decode_init(AVCodecContext *avctx)
+{
+ const MpegEncContext *s = avctx->priv_data;
+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
+
+ ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_MPEG2;
+
+ ctx->max_num_ref = 3;
+
+ return ff_d3d12va_decode_init(avctx);
+}
+
+#if CONFIG_MPEG2_D3D12VA_HWACCEL
+const FFHWAccel ff_mpeg2_d3d12va_hwaccel = {
+ .p.name = "mpeg2_d3d12va",
+ .p.type = AVMEDIA_TYPE_VIDEO,
+ .p.id = AV_CODEC_ID_MPEG2VIDEO,
+ .p.pix_fmt = AV_PIX_FMT_D3D12,
+ .init = d3d12va_mpeg2_decode_init,
+ .uninit = ff_d3d12va_decode_uninit,
+ .start_frame = d3d12va_mpeg2_start_frame,
+ .decode_slice = d3d12va_mpeg2_decode_slice,
+ .end_frame = d3d12va_mpeg2_end_frame,
+ .frame_params = ff_d3d12va_common_frame_params,
+ .frame_priv_data_size = sizeof(D3D12DecodePictureContext),
+ .priv_data_size = sizeof(D3D12VADecodeContext),
+};
+#endif
diff --git a/libavcodec/dxva2_internal.h b/libavcodec/dxva2_internal.h
index 1a7ebd6e8e..7f84b0d2fa 100644
--- a/libavcodec/dxva2_internal.h
+++ b/libavcodec/dxva2_internal.h
@@ -179,4 +179,10 @@ int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVACont
int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp);
#endif
+void ff_dxva2_mpeg2_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp);
+
+void ff_dxva2_mpeg2_fill_quantization_matrices(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_QmatrixData *qm);
+
+void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size);
+
#endif /* AVCODEC_DXVA2_INTERNAL_H */
diff --git a/libavcodec/dxva2_mpeg2.c b/libavcodec/dxva2_mpeg2.c
index b739dc1e54..4c9ed1d761 100644
--- a/libavcodec/dxva2_mpeg2.c
+++ b/libavcodec/dxva2_mpeg2.c
@@ -40,16 +40,15 @@ struct dxva2_picture_context {
unsigned bitstream_size;
};
-static void fill_picture_parameters(AVCodecContext *avctx,
+void ff_dxva2_mpeg2_fill_picture_parameters(AVCodecContext *avctx,
AVDXVAContext *ctx,
- const struct MpegEncContext *s,
DXVA_PictureParameters *pp)
{
+ const struct MpegEncContext *s = avctx->priv_data;
const Picture *current_picture = s->current_picture_ptr;
int is_field = s->picture_structure != PICT_FRAME;
memset(pp, 0, sizeof(*pp));
- pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f, 1);
pp->wDeblockedPictureIndex = 0;
if (s->pict_type != AV_PICTURE_TYPE_I)
pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f, 0);
@@ -59,6 +58,7 @@ static void fill_picture_parameters(AVCodecContext *avctx,
pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f, 0);
else
pp->wBackwardRefPictureIndex = 0xffff;
+ pp->wDecodedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f, 1);
pp->wPicWidthInMBminus1 = s->mb_width - 1;
pp->wPicHeightInMBminus1 = (s->mb_height >> is_field) - 1;
pp->bMacroblockWidthMinus1 = 15;
@@ -106,11 +106,11 @@ static void fill_picture_parameters(AVCodecContext *avctx,
pp->bBitstreamConcealmentMethod = 0;
}
-static void fill_quantization_matrices(AVCodecContext *avctx,
+void ff_dxva2_mpeg2_fill_quantization_matrices(AVCodecContext *avctx,
AVDXVAContext *ctx,
- const struct MpegEncContext *s,
DXVA_QmatrixData *qm)
{
+ const struct MpegEncContext *s = avctx->priv_data;
int i;
for (i = 0; i < 4; i++)
qm->bNewQmatrix[i] = 1;
@@ -123,12 +123,12 @@ static void fill_quantization_matrices(AVCodecContext *avctx,
}
}
-static void fill_slice(AVCodecContext *avctx,
- const struct MpegEncContext *s,
+void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx,
DXVA_SliceInfo *slice,
unsigned position,
const uint8_t *buffer, unsigned size)
{
+ const struct MpegEncContext *s = avctx->priv_data;
int is_field = s->picture_structure != PICT_FRAME;
GetBitContext gb;
@@ -266,8 +266,8 @@ static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
return -1;
assert(ctx_pic);
- fill_picture_parameters(avctx, ctx, s, &ctx_pic->pp);
- fill_quantization_matrices(avctx, ctx, s, &ctx_pic->qm);
+ ff_dxva2_mpeg2_fill_picture_parameters(avctx, ctx, &ctx_pic->pp);
+ ff_dxva2_mpeg2_fill_quantization_matrices(avctx, ctx, &ctx_pic->qm);
ctx_pic->slice_count = 0;
ctx_pic->bitstream_size = 0;
@@ -293,7 +293,7 @@ static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
ctx_pic->bitstream_size += size;
position = buffer - ctx_pic->bitstream;
- fill_slice(avctx, s, &ctx_pic->slice[ctx_pic->slice_count++], position,
+ ff_dxva2_mpeg2_fill_slice(avctx, &ctx_pic->slice[ctx_pic->slice_count++], position,
buffer, size);
return 0;
}
diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
index 1c4825f2fb..927314c5e3 100644
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
@@ -54,6 +54,7 @@ extern const struct FFHWAccel ff_mpeg1_vdpau_hwaccel;
extern const struct FFHWAccel ff_mpeg1_videotoolbox_hwaccel;
extern const struct FFHWAccel ff_mpeg2_d3d11va_hwaccel;
extern const struct FFHWAccel ff_mpeg2_d3d11va2_hwaccel;
+extern const struct FFHWAccel ff_mpeg2_d3d12va_hwaccel;
extern const struct FFHWAccel ff_mpeg2_dxva2_hwaccel;
extern const struct FFHWAccel ff_mpeg2_nvdec_hwaccel;
extern const struct FFHWAccel ff_mpeg2_vaapi_hwaccel;
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 020b7b7564..d07eed8744 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -850,6 +850,9 @@ static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
AV_PIX_FMT_D3D11VA_VLD,
AV_PIX_FMT_D3D11,
#endif
+#if CONFIG_MPEG2_D3D12VA_HWACCEL
+ AV_PIX_FMT_D3D12,
+#endif
#if CONFIG_MPEG2_VAAPI_HWACCEL
AV_PIX_FMT_VAAPI,
#endif
@@ -2620,6 +2623,9 @@ const FFCodec ff_mpeg2video_decoder = {
#if CONFIG_MPEG2_D3D11VA2_HWACCEL
HWACCEL_D3D11VA2(mpeg2),
#endif
+#if CONFIG_MPEG2_D3D12VA_HWACCEL
+ HWACCEL_D3D12VA(mpeg2),
+#endif
#if CONFIG_MPEG2_NVDEC_HWACCEL
HWACCEL_NVDEC(mpeg2),
#endif
--
2.41.0.windows.1
More information about the ffmpeg-devel
mailing list