diff mbox series

[FFmpeg-devel,v2,5/9] avcodec: add D3D12VA hardware accelerated AV1 decoding

Message ID 20230531030722.1307-5-tong1.wu@intel.com
State New
Headers show
Series [FFmpeg-devel,v2,1/9] libavutil: add hwcontext_d3d12va and AV_PIX_FMT_D3D12 | expand

Checks

Context Check Description
yinshiyou/make_fate_loongarch64 success Make fate finished
yinshiyou/make_loongarch64 warning New warnings during build
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Wu, Tong1 May 31, 2023, 3:07 a.m. UTC
From: Wu Jianhua <toqsxw@outlook.com>

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

Signed-off-by: Wu Jianhua <toqsxw@outlook.com>
---
 configure                   |   2 +
 libavcodec/Makefile         |   1 +
 libavcodec/av1dec.c         |  10 ++
 libavcodec/d3d12va_av1.c    | 220 ++++++++++++++++++++++++++++++++++++
 libavcodec/dxva2_av1.c      |   5 +-
 libavcodec/dxva2_internal.h |   4 +
 libavcodec/hwaccels.h       |   1 +
 7 files changed, 241 insertions(+), 2 deletions(-)
 create mode 100644 libavcodec/d3d12va_av1.c
diff mbox series

Patch

diff --git a/configure b/configure
index fa2548f3e2..1d05c898fb 100755
--- a/configure
+++ b/configure
@@ -3015,6 +3015,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"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index d5a1bfef7a..d2940aad4c 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -987,6 +987,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
diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 5cc5d87c64..bbf13d4717 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -448,6 +448,7 @@  static int get_pixel_format(AVCodecContext *avctx)
     enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
 #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 + \
@@ -523,6 +524,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
@@ -544,6 +548,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
@@ -1541,6 +1548,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
diff --git a/libavcodec/d3d12va_av1.c b/libavcodec/d3d12va_av1.c
new file mode 100644
index 0000000000..44ae689341
--- /dev/null
+++ b/libavcodec/d3d12va_av1.c
@@ -0,0 +1,220 @@ 
+/*
+ * 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.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);
+
+    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;
+
+    int index;
+    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);
+
+    index = ctx_pic->pp.CurrPicTextureIndex;
+    ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(ctx_pic->pp.RefFrameMapTextureIndex); i++) {
+        index = ctx_pic->pp.RefFrameMapTextureIndex[i];
+        if (index != 0xFF)
+            ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture;
+    }
+
+    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;
+
+    int ret;
+
+    if (avctx->profile != FF_PROFILE_AV1_MAIN)
+        return AVERROR(EINVAL);
+
+    ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE0;
+
+    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 AVHWAccel ff_av1_d3d12va_hwaccel = {
+    .name                 = "av1_d3d12va",
+    .type                 = AVMEDIA_TYPE_VIDEO,
+    .id                   = AV_CODEC_ID_AV1,
+    .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
diff --git a/libavcodec/dxva2_av1.c b/libavcodec/dxva2_av1.c
index 228f72ba18..7cb1c74fee 100644
--- a/libavcodec/dxva2_av1.c
+++ b/libavcodec/dxva2_av1.c
@@ -55,10 +55,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;
@@ -280,7 +281,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;
diff --git a/libavcodec/dxva2_internal.h b/libavcodec/dxva2_internal.h
index d13b3c1e1f..0084e837b7 100644
--- a/libavcodec/dxva2_internal.h
+++ b/libavcodec/dxva2_internal.h
@@ -178,4 +178,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);
 #endif
 
+#if CONFIG_AV1_D3D12VA_HWACCEL
+int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp);
+#endif
+
 #endif /* AVCODEC_DXVA2_INTERNAL_H */
diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
index 1059b6db70..517e70d5c4 100644
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
@@ -23,6 +23,7 @@ 
 
 extern const AVHWAccel ff_av1_d3d11va_hwaccel;
 extern const AVHWAccel ff_av1_d3d11va2_hwaccel;
+extern const AVHWAccel ff_av1_d3d12va_hwaccel;
 extern const AVHWAccel ff_av1_dxva2_hwaccel;
 extern const AVHWAccel ff_av1_nvdec_hwaccel;
 extern const AVHWAccel ff_av1_vaapi_hwaccel;