From 6ba5ada049662b3f03a9da63aa83724ec08dc09e Mon Sep 17 00:00:00 2001
From: Wu Jianhua <toqsxw@outlook.com>
Date: Sat, 17 Dec 2022 02:14:16 +0800
Subject: [PATCH 3/6] avcodec: add D3D12VA hardware accelerated HEVC 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>
---
 configure                  |   2 +
 libavcodec/Makefile        |   1 +
 libavcodec/d3d12dec_hevc.c | 398 +++++++++++++++++++++++++++++++++++++
 libavcodec/hevcdec.c       |  10 +
 libavcodec/hwaccels.h      |   1 +
 5 files changed, 412 insertions(+)
 create mode 100644 libavcodec/d3d12dec_hevc.c

diff --git a/configure b/configure
index 823d8e448d..4014f15c96 100755
--- a/configure
+++ b/configure
@@ -3042,6 +3042,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_d3d12dec_hwaccel_deps="d3d12va DXVA_PicParams_HEVC"
+hevc_d3d12dec_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 2cfc473875..e55a9a5e68 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -985,6 +985,7 @@ OBJS-$(CONFIG_H264_VDPAU_HWACCEL)         += vdpau_h264.o
 OBJS-$(CONFIG_H264_VIDEOTOOLBOX_HWACCEL)  += videotoolbox.o
 OBJS-$(CONFIG_HEVC_D3D11VA_HWACCEL)       += dxva2_hevc.o
 OBJS-$(CONFIG_HEVC_DXVA2_HWACCEL)         += dxva2_hevc.o
+OBJS-$(CONFIG_HEVC_D3D12DEC_HWACCEL)      += d3d12dec_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/d3d12dec_hevc.c b/libavcodec/d3d12dec_hevc.c
new file mode 100644
index 0000000000..6053ed4ebc
--- /dev/null
+++ b/libavcodec/d3d12dec_hevc.c
@@ -0,0 +1,398 @@
+/*
+ * Direct3D 12 HEVC HW acceleration
+ *
+ * copyright (c) 2022 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 "hevc_data.h"
+#include "hevcdec.h"
+#include "d3d12dec.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_picture_entry(DXVA_PicEntry_HEVC *pic,
+                               unsigned index, unsigned flag)
+{
+    av_assert0((index & 0x7f) == index && (flag & 0x01) == flag);
+    pic->bPicEntry = index | (flag << 7);
+}
+
+static int get_refpic_index(const DXVA_PicParams_HEVC *pp, int surface_index)
+{
+    int i;
+    for (i = 0; i < FF_ARRAY_ELEMS(pp->RefPicList); i++) {
+        if ((pp->RefPicList[i].bPicEntry & 0x7f) == surface_index)
+          return i;
+    }
+    return 0xff;
+}
+
+static void fill_picture_parameters(AVCodecContext *avctx, const HEVCContext *h,
+                                    DXVA_PicParams_HEVC *pp)
+{
+    D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
+
+    const HEVCFrame *current_picture = h->ref;
+    const HEVCSPS *sps = h->ps.sps;
+    const HEVCPPS *pps = h->ps.pps;
+    int i, j;
+
+    memset(pp, 0, sizeof(*pp));
+
+    pp->PicWidthInMinCbsY  = sps->min_cb_width;
+    pp->PicHeightInMinCbsY = sps->min_cb_height;
+
+    pp->wFormatAndSequenceInfoFlags = (sps->chroma_format_idc             <<  0) |
+                                      (sps->separate_colour_plane_flag    <<  2) |
+                                      ((sps->bit_depth - 8)               <<  3) |
+                                      ((sps->bit_depth - 8)               <<  6) |
+                                      ((sps->log2_max_poc_lsb - 4)        <<  9) |
+                                      (0                                  << 13) |
+                                      (0                                  << 14) |
+                                      (0                                  << 15);
+
+    fill_picture_entry(&pp->CurrPic, ff_d3d12dec_get_surface_index(avctx, current_picture->frame), 0);
+
+    pp->sps_max_dec_pic_buffering_minus1         = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
+    pp->log2_min_luma_coding_block_size_minus3   = sps->log2_min_cb_size - 3;
+    pp->log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size;
+    pp->log2_min_transform_block_size_minus2     = sps->log2_min_tb_size - 2;
+    pp->log2_diff_max_min_transform_block_size   = sps->log2_max_trafo_size  - sps->log2_min_tb_size;
+    pp->max_transform_hierarchy_depth_inter      = sps->max_transform_hierarchy_depth_inter;
+    pp->max_transform_hierarchy_depth_intra      = sps->max_transform_hierarchy_depth_intra;
+    pp->num_short_term_ref_pic_sets              = sps->nb_st_rps;
+    pp->num_long_term_ref_pics_sps               = sps->num_long_term_ref_pics_sps;
+
+    pp->num_ref_idx_l0_default_active_minus1     = pps->num_ref_idx_l0_default_active - 1;
+    pp->num_ref_idx_l1_default_active_minus1     = pps->num_ref_idx_l1_default_active - 1;
+    pp->init_qp_minus26                          = pps->pic_init_qp_minus26;
+
+    if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
+        pp->ucNumDeltaPocsOfRefRpsIdx            = h->sh.short_term_rps->rps_idx_num_delta_pocs;
+        pp->wNumBitsForShortTermRPSInSlice       = h->sh.short_term_ref_pic_set_size;
+    }
+
+    pp->dwCodingParamToolFlags = (sps->scaling_list_enable_flag                  <<  0) |
+                                 (sps->amp_enabled_flag                          <<  1) |
+                                 (sps->sao_enabled                               <<  2) |
+                                 (sps->pcm_enabled_flag                          <<  3) |
+                                 ((sps->pcm_enabled_flag ? (sps->pcm.bit_depth - 1) : 0)            <<  4) |
+                                 ((sps->pcm_enabled_flag ? (sps->pcm.bit_depth_chroma - 1) : 0)     <<  8) |
+                                 ((sps->pcm_enabled_flag ? (sps->pcm.log2_min_pcm_cb_size - 3) : 0) << 12) |
+                                 ((sps->pcm_enabled_flag ? (sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size) : 0) << 14) |
+                                 (sps->pcm.loop_filter_disable_flag              << 16) |
+                                 (sps->long_term_ref_pics_present_flag           << 17) |
+                                 (sps->sps_temporal_mvp_enabled_flag             << 18) |
+                                 (sps->sps_strong_intra_smoothing_enable_flag    << 19) |
+                                 (pps->dependent_slice_segments_enabled_flag     << 20) |
+                                 (pps->output_flag_present_flag                  << 21) |
+                                 (pps->num_extra_slice_header_bits               << 22) |
+                                 (pps->sign_data_hiding_flag                     << 25) |
+                                 (pps->cabac_init_present_flag                   << 26) |
+                                 (0                                              << 27);
+
+    pp->dwCodingSettingPicturePropertyFlags = (pps->constrained_intra_pred_flag                   <<  0) |
+                                              (pps->transform_skip_enabled_flag                   <<  1) |
+                                              (pps->cu_qp_delta_enabled_flag                      <<  2) |
+                                              (pps->pic_slice_level_chroma_qp_offsets_present_flag <<  3) |
+                                              (pps->weighted_pred_flag                            <<  4) |
+                                              (pps->weighted_bipred_flag                          <<  5) |
+                                              (pps->transquant_bypass_enable_flag                 <<  6) |
+                                              (pps->tiles_enabled_flag                            <<  7) |
+                                              (pps->entropy_coding_sync_enabled_flag              <<  8) |
+                                              (pps->uniform_spacing_flag                          <<  9) |
+                                              ((pps->tiles_enabled_flag ? pps->loop_filter_across_tiles_enabled_flag : 0) << 10) |
+                                              (pps->seq_loop_filter_across_slices_enabled_flag    << 11) |
+                                              (pps->deblocking_filter_override_enabled_flag       << 12) |
+                                              (pps->disable_dbf                                   << 13) |
+                                              (pps->lists_modification_present_flag               << 14) |
+                                              (pps->slice_header_extension_present_flag           << 15) |
+                                              (IS_IRAP(h)                                         << 16) |
+                                              (IS_IDR(h)                                          << 17) |
+                                              /* IntraPicFlag */
+                                              (IS_IRAP(h)                                         << 18) |
+                                              (0                                                  << 19);
+    pp->pps_cb_qp_offset            = pps->cb_qp_offset;
+    pp->pps_cr_qp_offset            = pps->cr_qp_offset;
+    if (pps->tiles_enabled_flag) {
+        pp->num_tile_columns_minus1 = pps->num_tile_columns - 1;
+        pp->num_tile_rows_minus1    = pps->num_tile_rows - 1;
+
+        if (!pps->uniform_spacing_flag) {
+            for (i = 0; i < pps->num_tile_columns; i++)
+                pp->column_width_minus1[i] = pps->column_width[i] - 1;
+
+            for (i = 0; i < pps->num_tile_rows; i++)
+                pp->row_height_minus1[i] = pps->row_height[i] - 1;
+        }
+    }
+
+    pp->diff_cu_qp_delta_depth           = pps->diff_cu_qp_delta_depth;
+    pp->pps_beta_offset_div2             = pps->beta_offset / 2;
+    pp->pps_tc_offset_div2               = pps->tc_offset / 2;
+    pp->log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2;
+    pp->CurrPicOrderCntVal               = h->poc;
+
+    // fill RefPicList from the DPB
+    for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->RefPicList); i++) {
+        const HEVCFrame *frame = NULL;
+        while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
+            if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
+                frame = &h->DPB[j];
+            j++;
+        }
+
+        if (frame) {
+            fill_picture_entry(&pp->RefPicList[i], ff_d3d12dec_get_surface_index(avctx, frame->frame), !!(frame->flags & HEVC_FRAME_FLAG_LONG_REF));
+            pp->PicOrderCntValList[i] = frame->poc;
+        } else {
+            pp->RefPicList[i].bPicEntry = 0xff;
+            pp->PicOrderCntValList[i]   = 0;
+        }
+    }
+
+    #define DO_REF_LIST(ref_idx, ref_list) { \
+        const RefPicList *rpl = &h->rps[ref_idx]; \
+        for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ref_list); i++) { \
+            const HEVCFrame *frame = NULL; \
+            while (!frame && j < rpl->nb_refs) \
+                frame = rpl->ref[j++]; \
+            if (frame && frame->flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)) \
+                pp->ref_list[i] = get_refpic_index(pp, ff_d3d12dec_get_surface_index(avctx, frame->frame)); \
+            else \
+                pp->ref_list[i] = 0xff; \
+        } \
+    }
+
+    // Fill short term and long term lists
+    DO_REF_LIST(ST_CURR_BEF, RefPicSetStCurrBefore);
+    DO_REF_LIST(ST_CURR_AFT, RefPicSetStCurrAfter);
+    DO_REF_LIST(LT_CURR, RefPicSetLtCurr);
+
+    pp->StatusReportFeedbackNumber = ctx->report_id++;
+}
+
+static void fill_scaling_lists(const AVCodecContext *avctx, const HEVCContext *h, DXVA_Qmatrix_HEVC *qm)
+{
+    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;
+
+    memset(qm, 0, sizeof(*qm));
+    for (i = 0; i < 6; i++) {
+        for (j = 0; j < 16; j++) {
+            pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
+            qm->ucScalingLists0[i][j] = sl->sl[0][i][pos];
+        }
+
+        for (j = 0; j < 64; j++) {
+            pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
+            qm->ucScalingLists1[i][j] = sl->sl[1][i][pos];
+            qm->ucScalingLists2[i][j] = sl->sl[2][i][pos];
+
+            if (i < 2)
+                qm->ucScalingLists3[i][j] = sl->sl[3][i * 3][pos];
+        }
+
+        qm->ucScalingListDCCoefSizeID2[i] = sl->sl_dc[0][i];
+        if (i < 2)
+            qm->ucScalingListDCCoefSizeID3[i] = sl->sl_dc[1][i * 3];
+    }
+}
+
+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 d3d12dec_hevc_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
+{
+    const HEVCContext        *h       = avctx->priv_data;
+    HEVCDecodePictureContext *ctx_pic = h->ref->hwaccel_picture_private;
+
+    if (!D3D12VA_DECODE_CONTEXT(avctx))
+        return -1;
+
+    av_assert0(ctx_pic);
+
+    fill_picture_parameters(avctx, h, &ctx_pic->pp);
+
+    fill_scaling_lists(avctx, h, &ctx_pic->qm);
+
+    ctx_pic->slice_count    = 0;
+    ctx_pic->bitstream_size = 0;
+    ctx_pic->bitstream      = NULL;
+
+    return 0;
+}
+
+static int d3d12dec_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 d3d12dec_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_d3d12dec_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 d3d12dec_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_d3d12dec_decode_init(avctx);
+}
+
+#if CONFIG_HEVC_D3D12DEC_HWACCEL
+const AVHWAccel ff_hevc_d3d12dec_hwaccel = {
+    .name                 = "hevc_d3d12dec",
+    .type                 = AVMEDIA_TYPE_VIDEO,
+    .id                   = AV_CODEC_ID_HEVC,
+    .pix_fmt              = AV_PIX_FMT_D3D12,
+    .init                 = d3d12dec_hevc_decode_init,
+    .uninit               = ff_d3d12dec_decode_uninit,
+    .start_frame          = d3d12dec_hevc_start_frame,
+    .decode_slice         = d3d12dec_hevc_decode_slice,
+    .end_frame            = d3d12dec_hevc_end_frame,
+    .frame_params         = ff_d3d12dec_common_frame_params,
+    .frame_priv_data_size = sizeof(HEVCDecodePictureContext),
+    .priv_data_size       = sizeof(D3D12VADecodeContext),
+};
+#endif
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 567e8d81d4..a673d129fa 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_D3D12DEC_HWACCEL + \
                      CONFIG_HEVC_NVDEC_HWACCEL + \
                      CONFIG_HEVC_VAAPI_HWACCEL + \
                      CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
@@ -418,6 +419,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_D3D12DEC_HWACCEL
+        *fmt++ = AV_PIX_FMT_D3D12;
+#endif
 #if CONFIG_HEVC_VAAPI_HWACCEL
         *fmt++ = AV_PIX_FMT_VAAPI;
 #endif
@@ -439,6 +443,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_D3D12DEC_HWACCEL
+        *fmt++ = AV_PIX_FMT_D3D12;
+#endif
 #if CONFIG_HEVC_VAAPI_HWACCEL
         *fmt++ = AV_PIX_FMT_VAAPI;
 #endif
@@ -3728,6 +3735,9 @@ const FFCodec ff_hevc_decoder = {
 #if CONFIG_HEVC_D3D11VA2_HWACCEL
                                HWACCEL_D3D11VA2(hevc),
 #endif
+#if CONFIG_HEVC_D3D12DEC_HWACCEL
+                               HWACCEL_D3D12DEC(hevc),
+#endif
 #if CONFIG_HEVC_NVDEC_HWACCEL
                                HWACCEL_NVDEC(hevc),
 #endif
diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
index 607a2cd0f9..bc27023c8f 100644
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
@@ -39,6 +39,7 @@ extern const AVHWAccel ff_h264_vdpau_hwaccel;
 extern const AVHWAccel ff_h264_videotoolbox_hwaccel;
 extern const AVHWAccel ff_hevc_d3d11va_hwaccel;
 extern const AVHWAccel ff_hevc_d3d11va2_hwaccel;
+extern const AVHWAccel ff_hevc_d3d12dec_hwaccel;
 extern const AVHWAccel ff_hevc_dxva2_hwaccel;
 extern const AVHWAccel ff_hevc_nvdec_hwaccel;
 extern const AVHWAccel ff_hevc_vaapi_hwaccel;
-- 
2.32.0

