From: Wu Jianhua <toq...@outlook.com>

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

Signed-off-by: Wu Jianhua <toq...@outlook.com>
Signed-off-by: Tong Wu <tong1...@intel.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 --git a/configure b/configure
index 756be03af6..80e9e91b81 100755
--- a/configure
+++ b/configure
@@ -3069,6 +3069,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 9ef49a8da5..fb5ec2ee2d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -992,6 +992,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 03283ab064..96e2c8febb 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -449,6 +449,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 + \
@@ -524,6 +525,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
@@ -545,6 +549,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
@@ -1534,6 +1541,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..4476f5bd8a
--- /dev/null
+++ b/libavcodec/d3d12va_av1.c
@@ -0,0 +1,220 @@
+/*
+ * Direct3D 12 AV1 HW acceleration
+ *
+ * copyright (c) 2022-2023 Wu Jianhua <toq...@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);
+
+    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 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
diff --git a/libavcodec/dxva2_av1.c b/libavcodec/dxva2_av1.c
index ab118a4356..9b4c8f3875 100644
--- a/libavcodec/dxva2_av1.c
+++ b/libavcodec/dxva2_av1.c
@@ -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;
@@ -281,7 +282,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 1c361929f1..8329db2a81 100644
--- a/libavcodec/dxva2_internal.h
+++ b/libavcodec/dxva2_internal.h
@@ -172,4 +172,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 */
diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
index 1344b84326..1c4825f2fb 100644
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
@@ -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;
-- 
2.41.0.windows.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to