* [FFmpeg-devel] [PATCH] avcodec/d3d12va_encode: texture array support for H264 (PR #20475)
@ 2025-09-09 8:26 ArazIusubov via ffmpeg-devel
0 siblings, 0 replies; only message in thread
From: ArazIusubov via ffmpeg-devel @ 2025-09-09 8:26 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: ArazIusubov
PR #20475 opened by ArazIusubov
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20475
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20475.patch
This patch adds support for the texture array feature
used by AMD boards in the D3D12 H264 encoder.
In texture array mode, a single texture array is shared for all
reference and reconstructed pictures using different subresources.
The implementation ensures compatibility
and has been successfully tested on AMD, Intel, and NVIDIA GPUs.
>From beba116469ea714484d80892bafdb643a817f9b6 Mon Sep 17 00:00:00 2001
From: Araz Iusubov <Primeadvice@gmail.com>
Date: Mon, 8 Sep 2025 23:52:50 +0200
Subject: [PATCH] avcodec/d3d12va_encode: texture array support for H264
This patch adds support for the texture array feature
used by AMD boards in the D3D12 H264 encoder.
In texture array mode, a single texture array is shared for all
reference and reconstructed pictures using different subresources.
The implementation ensures compatibility
and has been successfully tested on AMD, Intel, and NVIDIA GPUs.
---
configure | 1 +
libavcodec/Makefile | 2 +
libavcodec/allcodecs.c | 1 +
libavcodec/d3d12va_encode_h264.c | 634 +++++++++++++++++++++++++++++++
4 files changed, 638 insertions(+)
create mode 100644 libavcodec/d3d12va_encode_h264.c
diff --git a/configure b/configure
index c6afcd7494..4094e9852f 100755
--- a/configure
+++ b/configure
@@ -3421,6 +3421,7 @@ h264_amf_encoder_deps="amf"
h264_amf_decoder_deps="amf"
h264_cuvid_decoder_deps="cuvid"
h264_cuvid_decoder_select="h264_mp4toannexb_bsf"
+h264_d3d12va_encoder_select="cbs_h264 d3d12va_encode"
h264_mediacodec_decoder_deps="mediacodec"
h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_parser"
h264_mediacodec_encoder_deps="mediacodec"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ac143f4103..1ebd7b4dde 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -431,6 +431,8 @@ OBJS-$(CONFIG_H264_DECODER) += h264dec.o h264_cabac.o h264_cavlc.o \
OBJS-$(CONFIG_H264_AMF_ENCODER) += amfenc_h264.o
OBJS-$(CONFIG_H264_AMF_DECODER) += amfdec.o
OBJS-$(CONFIG_H264_CUVID_DECODER) += cuviddec.o
+OBJS-$(CONFIG_H264_D3D12VA_ENCODER) += d3d12va_encode_h264.o h264_levels.o \
+ h2645data.o hw_base_encode_h264.o
OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec.o
OBJS-$(CONFIG_H264_MEDIACODEC_ENCODER) += mediacodecenc.o
OBJS-$(CONFIG_H264_MF_ENCODER) += mfenc.o mf_utils.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index f5ec2e01e8..2980921cc3 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -860,6 +860,7 @@ extern const FFCodec ff_libopenh264_decoder;
extern const FFCodec ff_h264_amf_encoder;
extern const FFCodec ff_h264_amf_decoder;
extern const FFCodec ff_h264_cuvid_decoder;
+extern const FFCodec ff_h264_d3d12va_encoder;
extern const FFCodec ff_h264_mf_encoder;
extern const FFCodec ff_h264_nvenc_encoder;
extern const FFCodec ff_h264_oh_decoder;
diff --git a/libavcodec/d3d12va_encode_h264.c b/libavcodec/d3d12va_encode_h264.c
new file mode 100644
index 0000000000..6e68b5122b
--- /dev/null
+++ b/libavcodec/d3d12va_encode_h264.c
@@ -0,0 +1,634 @@
+/*
+ * Direct3D 12 HW acceleration video encoder
+ *
+ * Copyright (c) 2024 Intel Corporation
+ *
+ * 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 "libavutil/opt.h"
+#include "libavutil/common.h"
+#include "libavutil/mem.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/hwcontext_d3d12va_internal.h"
+
+#include "avcodec.h"
+#include "cbs.h"
+#include "cbs_h264.h"
+#include "hw_base_encode_h264.h"
+#include "h2645data.h"
+#include "h264_levels.h"
+#include "codec_internal.h"
+#include "d3d12va_encode.h"
+
+typedef struct D3D12VAEncodeH264Picture {
+ int pic_order_cnt;
+ int64_t last_idr_frame;
+} D3D12VAEncodeH264Picture;
+
+typedef struct D3D12VAEncodeH264Context {
+ D3D12VAEncodeContext common;
+
+ // User options.
+ int qp;
+ int profile;
+ int level;
+ int idr_pic_id;
+
+ // Writer structures.
+ FFHWBaseEncodeH264 units;
+ FFHWBaseEncodeH264Opts unit_opts;
+
+ CodedBitstreamContext *cbc;
+ CodedBitstreamFragment current_access_unit;
+} D3D12VAEncodeH264Context;
+
+typedef struct D3D12VAEncodeH264Level {
+ int level;
+ D3D12_VIDEO_ENCODER_LEVELS_H264 d3d12_level;
+} D3D12VAEncodeH264Level;
+
+static const D3D12VAEncodeH264Level h264_levels[] = {
+ { 10, D3D12_VIDEO_ENCODER_LEVELS_H264_1 },
+ { 11, D3D12_VIDEO_ENCODER_LEVELS_H264_11 },
+ { 12, D3D12_VIDEO_ENCODER_LEVELS_H264_12 },
+ { 13, D3D12_VIDEO_ENCODER_LEVELS_H264_13 },
+ { 20, D3D12_VIDEO_ENCODER_LEVELS_H264_2 },
+ { 21, D3D12_VIDEO_ENCODER_LEVELS_H264_21 },
+ { 22, D3D12_VIDEO_ENCODER_LEVELS_H264_22 },
+ { 30, D3D12_VIDEO_ENCODER_LEVELS_H264_3 },
+ { 31, D3D12_VIDEO_ENCODER_LEVELS_H264_31 },
+ { 32, D3D12_VIDEO_ENCODER_LEVELS_H264_32 },
+ { 40, D3D12_VIDEO_ENCODER_LEVELS_H264_4 },
+ { 41, D3D12_VIDEO_ENCODER_LEVELS_H264_41 },
+ { 42, D3D12_VIDEO_ENCODER_LEVELS_H264_42 },
+ { 50, D3D12_VIDEO_ENCODER_LEVELS_H264_5 },
+ { 51, D3D12_VIDEO_ENCODER_LEVELS_H264_51 },
+ { 52, D3D12_VIDEO_ENCODER_LEVELS_H264_52 },
+ { 60, D3D12_VIDEO_ENCODER_LEVELS_H264_6 },
+ { 61, D3D12_VIDEO_ENCODER_LEVELS_H264_61 },
+ { 62, D3D12_VIDEO_ENCODER_LEVELS_H264_62 },
+};
+
+static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_main = D3D12_VIDEO_ENCODER_PROFILE_H264_MAIN;
+static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_high = D3D12_VIDEO_ENCODER_PROFILE_H264_HIGH;
+static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_high_10 = D3D12_VIDEO_ENCODER_PROFILE_H264_HIGH_10;
+
+#define D3D_PROFILE_DESC(name) \
+ { sizeof(D3D12_VIDEO_ENCODER_PROFILE_H264), { .pH264Profile = (D3D12_VIDEO_ENCODER_PROFILE_H264 *)&profile_ ## name } }
+static const D3D12VAEncodeProfile d3d12va_encode_h264_profiles[] = {
+ { AV_PROFILE_H264_MAIN, 8, 3, 1, 1, D3D_PROFILE_DESC(main) },
+ { AV_PROFILE_H264_HIGH, 8, 3, 1, 1, D3D_PROFILE_DESC(high) },
+ { AV_PROFILE_H264_HIGH_10, 10, 3, 1, 1, D3D_PROFILE_DESC(high_10) },
+ { AV_PROFILE_UNKNOWN },
+};
+
+static int d3d12va_encode_h264_write_access_unit(AVCodecContext *avctx,
+ char *data, size_t *data_len,
+ CodedBitstreamFragment *au)
+{
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+ int err;
+
+ err = ff_cbs_write_fragment_data(priv->cbc, au);
+ if (err < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
+ return err;
+ }
+
+ if (*data_len < 8 * au->data_size - au->data_bit_padding) {
+ av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
+ "%zu < %zu.\n", *data_len,
+ 8 * au->data_size - au->data_bit_padding);
+ return AVERROR(ENOSPC);
+ }
+
+ memcpy(data, au->data, au->data_size);
+ *data_len = 8 * au->data_size - au->data_bit_padding;
+
+ return 0;
+}
+
+static int d3d12va_encode_h264_add_nal(AVCodecContext *avctx,
+ CodedBitstreamFragment *au,
+ void *nal_unit)
+{
+ H264RawNALUnitHeader *header = nal_unit;
+ int err;
+
+ err = ff_cbs_insert_unit_content(au, -1,
+ header->nal_unit_type, nal_unit, NULL);
+ if (err < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
+ "type = %d.\n", header->nal_unit_type);
+ return err;
+ }
+
+ return 0;
+}
+
+static int d3d12va_encode_h264_write_sequence_header(AVCodecContext *avctx,
+ char *data, size_t *data_len)
+{
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+ CodedBitstreamFragment *au = &priv->current_access_unit;
+ int err;
+
+ err = d3d12va_encode_h264_add_nal(avctx, au, &priv->units.raw_sps);
+ if (err < 0)
+ goto fail;
+
+ err = d3d12va_encode_h264_add_nal(avctx, au, &priv->units.raw_pps);
+ if (err < 0)
+ goto fail;
+
+ err = d3d12va_encode_h264_write_access_unit(avctx, data, data_len, au);
+fail:
+ ff_cbs_fragment_reset(au);
+ return err;
+}
+
+static int d3d12va_encode_h264_init_sequence_params(AVCodecContext *avctx)
+{
+ FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
+ D3D12VAEncodeContext *ctx = avctx->priv_data;
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+ AVD3D12VAFramesContext *hwctx = base_ctx->input_frames->hwctx;
+ H264RawSPS *sps = &priv->units.raw_sps;
+ H264RawPPS *pps = &priv->units.raw_pps;
+ H264RawVUI *vui = &sps->vui;
+ D3D12_VIDEO_ENCODER_PROFILE_H264 profile = D3D12_VIDEO_ENCODER_PROFILE_H264_MAIN;
+ D3D12_VIDEO_ENCODER_LEVELS_H264 level = { 0 };
+ const AVPixFmtDescriptor *desc;
+ HRESULT hr;
+ int err;
+
+ D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT support = {
+ .NodeIndex = 0,
+ .Codec = D3D12_VIDEO_ENCODER_CODEC_H264,
+ .InputFormat = hwctx->format,
+ .RateControl = ctx->rc,
+ .IntraRefresh = D3D12_VIDEO_ENCODER_INTRA_REFRESH_MODE_NONE,
+ .SubregionFrameEncoding = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
+ .ResolutionsListCount = 1,
+ .pResolutionList = &ctx->resolution,
+ .CodecGopSequence = ctx->gop,
+ .MaxReferenceFramesInDPB = MAX_DPB_SIZE - 1,
+ .CodecConfiguration = ctx->codec_conf,
+ .SuggestedProfile.DataSize = sizeof(D3D12_VIDEO_ENCODER_PROFILE_H264),
+ .SuggestedProfile.pH264Profile = &profile,
+ .SuggestedLevel.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264),
+ .SuggestedLevel.pH264LevelSetting = &level,
+ .pResolutionDependentSupport = &ctx->res_limits,
+ };
+
+ hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT,
+ &support, sizeof(support));
+
+ if (FAILED(hr)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to check encoder support(%lx).\n", (long)hr);
+ return AVERROR(EINVAL);
+ }
+
+ if (!(support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_GENERAL_SUPPORT_OK)) {
+ av_log(avctx, AV_LOG_ERROR, "Driver does not support some request features. %#x\n",
+ support.ValidationFlags);
+ return AVERROR(EINVAL);
+ }
+
+ if (support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RECONSTRUCTED_FRAMES_REQUIRE_TEXTURE_ARRAYS) {
+ ctx->is_texture_array = 1;
+ av_log(avctx, AV_LOG_DEBUG, "D3D12 video encode on this device uses texture array mode.\n");
+ }
+
+ desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format);
+ av_assert0(desc);
+
+ sps->pic_width_in_mbs_minus1 = ((base_ctx->surface_width + 0x0F) >> 4) - 1;
+ sps->pic_height_in_map_units_minus1 = ((base_ctx->surface_height + 0x0F) >> 4) - 1;
+
+ priv->unit_opts.mb_width = sps->pic_width_in_mbs_minus1 + 1;
+ priv->unit_opts.mb_height = sps->pic_height_in_map_units_minus1 +1;
+
+ err = ff_hw_base_encode_init_params_h264(base_ctx, avctx,
+ &priv->units, &priv->unit_opts);
+ if (err < 0)
+ return err;
+
+ avctx->level = priv->units.raw_sps.level_idc;
+
+ ctx->gop.pH264GroupOfPictures->pic_order_cnt_type = sps->pic_order_cnt_type;
+
+ //override the default value according to the gop size
+ sps->log2_max_frame_num_minus4 = FFMAX(ceil(log2(base_ctx->gop_size)) - 4, 0);
+ ctx->gop.pH264GroupOfPictures->log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4;
+ pps->deblocking_filter_control_present_flag = 1;
+
+ return 0;
+}
+
+static int d3d12va_encode_h264_get_encoder_caps(AVCodecContext *avctx)
+{
+ HRESULT hr;
+ FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
+ D3D12VAEncodeContext *ctx = avctx->priv_data;
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+
+ D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264 *config;
+ D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264 h264_caps;
+
+ D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT codec_caps = {
+ .NodeIndex = 0,
+ .Codec = D3D12_VIDEO_ENCODER_CODEC_H264,
+ .Profile = ctx->profile->d3d12_profile,
+ .CodecSupportLimits.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264),
+ };
+
+ codec_caps.CodecSupportLimits.pH264Support = &h264_caps;
+ hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT,
+ &codec_caps, sizeof(codec_caps));
+ if (!(SUCCEEDED(hr) && codec_caps.IsSupported))
+ return AVERROR(EINVAL);
+
+ ctx->codec_conf.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264);
+ ctx->codec_conf.pH264Config = av_mallocz(ctx->codec_conf.DataSize);
+ if (!ctx->codec_conf.pH264Config)
+ return AVERROR(ENOMEM);
+
+ config = ctx->codec_conf.pH264Config;
+
+ config->ConfigurationFlags = D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_NONE;
+
+ if (h264_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_CABAC_ENCODING_SUPPORT) {
+ config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_ENABLE_CABAC_ENCODING;
+ priv->unit_opts.cabac = 1;
+ }
+
+ base_ctx->surface_width = FFALIGN(avctx->width, 16);
+ base_ctx->surface_height = FFALIGN(avctx->height, 16);
+
+ return 0;
+}
+
+static int d3d12va_encode_h264_configure(AVCodecContext *avctx)
+{
+ FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
+ D3D12VAEncodeContext *ctx = avctx->priv_data;
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+ int fixed_qp_idr, fixed_qp_p, fixed_qp_b;
+ int err;
+
+ err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
+ if (err < 0)
+ return err;
+
+ // Rate control
+ if (ctx->rc.Mode == D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP) {
+ D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP *cqp_ctl;
+ fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
+ if (avctx->i_quant_factor > 0.0)
+ fixed_qp_idr = av_clip((avctx->i_quant_factor * fixed_qp_p +
+ avctx->i_quant_offset) + 0.5, 1, 51);
+ else
+ fixed_qp_idr = fixed_qp_p;
+ if (avctx->b_quant_factor > 0.0)
+ fixed_qp_b = av_clip((avctx->b_quant_factor * fixed_qp_p +
+ avctx->b_quant_offset) + 0.5, 1, 51);
+ else
+ fixed_qp_b = fixed_qp_p;
+
+ av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
+ "%d / %d / %d for IDR- / P- / B-frames.\n",
+ fixed_qp_idr, fixed_qp_p, fixed_qp_b);
+
+ ctx->rc.ConfigParams.DataSize = sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP);
+ cqp_ctl = av_mallocz(ctx->rc.ConfigParams.DataSize);
+ if (!cqp_ctl)
+ return AVERROR(ENOMEM);
+
+ cqp_ctl->ConstantQP_FullIntracodedFrame = fixed_qp_idr;
+ cqp_ctl->ConstantQP_InterPredictedFrame_PrevRefOnly = fixed_qp_p;
+ cqp_ctl->ConstantQP_InterPredictedFrame_BiDirectionalRef = fixed_qp_b;
+
+ ctx->rc.ConfigParams.pConfiguration_CQP = cqp_ctl;
+ }
+ priv->unit_opts.fixed_qp_idr = 26;
+
+ // GOP
+ ctx->gop.DataSize = sizeof(D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE_H264);
+ ctx->gop.pH264GroupOfPictures = av_mallocz(ctx->gop.DataSize);
+ if (!ctx->gop.pH264GroupOfPictures)
+ return AVERROR(ENOMEM);
+
+ ctx->gop.pH264GroupOfPictures->GOPLength = base_ctx->gop_size;
+ ctx->gop.pH264GroupOfPictures->PPicturePeriod = base_ctx->b_per_p + 1;
+
+ return 0;
+}
+
+static int d3d12va_encode_h264_set_level(AVCodecContext *avctx)
+{
+ D3D12VAEncodeContext *ctx = avctx->priv_data;
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+ int i;
+
+ ctx->level.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264);
+ ctx->level.pH264LevelSetting = av_mallocz(ctx->level.DataSize);
+ if (!ctx->level.pH264LevelSetting)
+ return AVERROR(ENOMEM);
+
+ for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
+ if (avctx->level == h264_levels[i].level) {
+ *ctx->level.pH264LevelSetting = h264_levels[i].d3d12_level;
+ break;
+ }
+ }
+
+ if (i == FF_ARRAY_ELEMS(h264_levels)) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid level %d.\n", avctx->level);
+ return AVERROR(EINVAL);
+ }
+
+ return 0;
+}
+
+static void d3d12va_encode_h264_free_picture_params(D3D12VAEncodePicture *pic)
+{
+ if (!pic->pic_ctl.pH264PicData)
+ return;
+
+ av_freep(&pic->pic_ctl.pH264PicData->pList0ReferenceFrames);
+ av_freep(&pic->pic_ctl.pH264PicData->pList1ReferenceFrames);
+ av_freep(&pic->pic_ctl.pH264PicData->pReferenceFramesReconPictureDescriptors);
+ av_freep(&pic->pic_ctl.pH264PicData);
+}
+
+static int d3d12va_encode_h264_init_picture_params(AVCodecContext *avctx,
+ FFHWBaseEncodePicture *base_pic)
+{
+ D3D12VAEncodeH264Context *ctx = avctx->priv_data;
+ D3D12VAEncodePicture *pic = base_pic->priv;
+ D3D12VAEncodeH264Picture *hpic = base_pic->codec_priv;
+ FFHWBaseEncodePicture *prev = base_pic->prev;
+ D3D12VAEncodeH264Picture *hprev = prev ? prev->codec_priv : NULL;
+ D3D12_VIDEO_ENCODER_REFERENCE_PICTURE_DESCRIPTOR_H264 *pd = NULL;
+ UINT *ref_list0 = NULL, *ref_list1 = NULL;
+ int i, idx = 0;
+
+ pic->pic_ctl.DataSize = sizeof(D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_H264);
+ pic->pic_ctl.pH264PicData = av_mallocz(pic->pic_ctl.DataSize);
+ if (!pic->pic_ctl.pH264PicData)
+ return AVERROR(ENOMEM);
+
+ if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) {
+ av_assert0(base_pic->display_order == base_pic->encode_order);
+ hpic->last_idr_frame = base_pic->display_order;
+ ctx->idr_pic_id++;
+ } else {
+ av_assert0(prev);
+ hpic->last_idr_frame = hprev->last_idr_frame;
+ }
+ hpic->pic_order_cnt = base_pic->display_order - hpic->last_idr_frame;
+
+ switch(base_pic->type) {
+ case FF_HW_PICTURE_TYPE_IDR:
+ pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_IDR_FRAME;
+ pic->pic_ctl.pH264PicData->idr_pic_id = ctx->idr_pic_id;
+ break;
+ case FF_HW_PICTURE_TYPE_I:
+ pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_I_FRAME;
+ break;
+ case FF_HW_PICTURE_TYPE_P:
+ pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_P_FRAME;
+ break;
+ case FF_HW_PICTURE_TYPE_B:
+ pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_B_FRAME;
+ break;
+ default:
+ av_assert0(0 && "invalid picture type");
+ }
+
+ pic->pic_ctl.pH264PicData->PictureOrderCountNumber = hpic->pic_order_cnt;
+ pic->pic_ctl.pH264PicData->FrameDecodingOrderNumber = hpic->pic_order_cnt;
+
+ if (base_pic->type == FF_HW_PICTURE_TYPE_P || base_pic->type == FF_HW_PICTURE_TYPE_B) {
+ pd = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*pd));
+ if (!pd)
+ return AVERROR(ENOMEM);
+
+ ref_list0 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list0));
+ if (!ref_list0)
+ return AVERROR(ENOMEM);
+
+ pic->pic_ctl.pH264PicData->List0ReferenceFramesCount = base_pic->nb_refs[0];
+ for (i = 0; i < base_pic->nb_refs[0]; i++) {
+ FFHWBaseEncodePicture *ref = base_pic->refs[0][i];
+ D3D12VAEncodeH264Picture *href;
+
+ av_assert0(ref && ref->encode_order < base_pic->encode_order);
+ href = ref->codec_priv;
+
+ ref_list0[i] = idx;
+ pd[idx].ReconstructedPictureResourceIndex = idx;
+ pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
+ idx++;
+ }
+ }
+
+ if (base_pic->type == FF_HW_PICTURE_TYPE_B) {
+ ref_list1 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list1));
+ if (!ref_list1)
+ return AVERROR(ENOMEM);
+
+ pic->pic_ctl.pH264PicData->List1ReferenceFramesCount = base_pic->nb_refs[1];
+ for (i = 0; i < base_pic->nb_refs[1]; i++) {
+ FFHWBaseEncodePicture *ref = base_pic->refs[1][i];
+ D3D12VAEncodeH264Picture *href;
+
+ av_assert0(ref && ref->encode_order < base_pic->encode_order);
+ href = ref->codec_priv;
+
+ ref_list1[i] = idx;
+ pd[idx].ReconstructedPictureResourceIndex = idx;
+ pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
+ idx++;
+ }
+ }
+
+ pic->pic_ctl.pH264PicData->pList0ReferenceFrames = ref_list0;
+ pic->pic_ctl.pH264PicData->pList1ReferenceFrames = ref_list1;
+ pic->pic_ctl.pH264PicData->ReferenceFramesReconPictureDescriptorsCount = idx;
+ pic->pic_ctl.pH264PicData->pReferenceFramesReconPictureDescriptors = pd;
+
+ return 0;
+}
+
+static const D3D12VAEncodeType d3d12va_encode_type_h264 = {
+ .profiles = d3d12va_encode_h264_profiles,
+
+ .d3d12_codec = D3D12_VIDEO_ENCODER_CODEC_H264,
+
+ .flags = FF_HW_FLAG_B_PICTURES |
+ FF_HW_FLAG_B_PICTURE_REFERENCES |
+ FF_HW_FLAG_NON_IDR_KEY_PICTURES,
+
+ .default_quality = 25,
+
+ .get_encoder_caps = &d3d12va_encode_h264_get_encoder_caps,
+
+ .configure = &d3d12va_encode_h264_configure,
+
+ .set_level = &d3d12va_encode_h264_set_level,
+
+ .picture_priv_data_size = sizeof(D3D12VAEncodeH264Picture),
+
+ .init_sequence_params = &d3d12va_encode_h264_init_sequence_params,
+
+ .init_picture_params = &d3d12va_encode_h264_init_picture_params,
+
+ .free_picture_params = &d3d12va_encode_h264_free_picture_params,
+
+ .write_sequence_header = &d3d12va_encode_h264_write_sequence_header,
+};
+
+static int d3d12va_encode_h264_init(AVCodecContext *avctx)
+{
+ D3D12VAEncodeContext *ctx = avctx->priv_data;
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+
+ ctx->codec = &d3d12va_encode_type_h264;
+
+ if (avctx->profile == AV_PROFILE_UNKNOWN)
+ avctx->profile = priv->profile;
+ if (avctx->level == AV_LEVEL_UNKNOWN)
+ avctx->level = priv->level;
+
+ if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0xff) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
+ "in 8-bit unsigned integer.\n", avctx->level);
+ return AVERROR(EINVAL);
+ }
+
+ if (priv->qp > 0)
+ ctx->explicit_qp = priv->qp;
+
+ return ff_d3d12va_encode_init(avctx);
+}
+
+static int d3d12va_encode_h264_close(AVCodecContext *avctx)
+{
+ D3D12VAEncodeH264Context *priv = avctx->priv_data;
+
+ ff_cbs_fragment_free(&priv->current_access_unit);
+ ff_cbs_close(&priv->cbc);
+
+ av_freep(&priv->common.codec_conf.pH264Config);
+ av_freep(&priv->common.gop.pH264GroupOfPictures);
+ av_freep(&priv->common.level.pH264LevelSetting);
+
+ if (priv->common.rc.ConfigParams.pConfiguration_CQP != NULL) {
+ av_freep(&priv->common.rc.ConfigParams.pConfiguration_CQP);
+ }
+
+ return ff_d3d12va_encode_close(avctx);
+}
+
+#define OFFSET(x) offsetof(D3D12VAEncodeH264Context, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
+static const AVOption d3d12va_encode_h264_options[] = {
+ HW_BASE_ENCODE_COMMON_OPTIONS,
+ D3D12VA_ENCODE_RC_OPTIONS,
+
+ { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
+ OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
+
+ { "profile", "Set profile (general_profile_idc)",
+ OFFSET(profile), AV_OPT_TYPE_INT,
+ { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
+
+#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
+ { .i64 = value }, 0, 0, FLAGS, "profile"
+ { PROFILE("baseline", AV_PROFILE_H264_BASELINE) },
+ { PROFILE("main", AV_PROFILE_H264_MAIN) },
+ { PROFILE("high", AV_PROFILE_H264_HIGH) },
+ { PROFILE("high10", AV_PROFILE_H264_HIGH_10) },
+#undef PROFILE
+
+ { "level", "Set level (general_level_idc)",
+ OFFSET(level), AV_OPT_TYPE_INT,
+ { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
+
+#define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
+ { .i64 = value }, 0, 0, FLAGS, "level"
+ { LEVEL("1", 10) },
+ { LEVEL("1.1", 11) },
+ { LEVEL("1.2", 12) },
+ { LEVEL("1.3", 13) },
+ { LEVEL("2", 20) },
+ { LEVEL("2.1", 21) },
+ { LEVEL("2.2", 22) },
+ { LEVEL("3", 30) },
+ { LEVEL("3.1", 31) },
+ { LEVEL("3.2", 32) },
+ { LEVEL("4", 40) },
+ { LEVEL("4.1", 41) },
+ { LEVEL("4.2", 42) },
+ { LEVEL("5", 50) },
+ { LEVEL("5.1", 51) },
+ { LEVEL("5.2", 52) },
+ { LEVEL("6", 60) },
+ { LEVEL("6.1", 61) },
+ { LEVEL("6.2", 62) },
+#undef LEVEL
+
+ { NULL },
+};
+
+static const FFCodecDefault d3d12va_encode_h264_defaults[] = {
+ { "b", "0" },
+ { "bf", "2" },
+ { "g", "120" },
+ { "qmin", "-1" },
+ { "qmax", "-1" },
+ { NULL },
+};
+
+static const AVClass d3d12va_encode_h264_class = {
+ .class_name = "h264_d3d12va",
+ .item_name = av_default_item_name,
+ .option = d3d12va_encode_h264_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+const FFCodec ff_h264_d3d12va_encoder = {
+ .p.name = "h264_d3d12va",
+ CODEC_LONG_NAME("D3D12VA h264 encoder"),
+ .p.type = AVMEDIA_TYPE_VIDEO,
+ .p.id = AV_CODEC_ID_H264,
+ .priv_data_size = sizeof(D3D12VAEncodeH264Context),
+ .init = &d3d12va_encode_h264_init,
+ FF_CODEC_RECEIVE_PACKET_CB(&ff_d3d12va_encode_receive_packet),
+ .close = &d3d12va_encode_h264_close,
+ .p.priv_class = &d3d12va_encode_h264_class,
+ .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
+ AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
+ .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
+ FF_CODEC_CAP_INIT_CLEANUP,
+ .defaults = d3d12va_encode_h264_defaults,
+ CODEC_PIXFMTS(AV_PIX_FMT_D3D12),
+ .hw_configs = ff_d3d12va_encode_hw_configs,
+ .p.wrapper_name = "d3d12va",
+};
--
2.49.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-09-09 8:27 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-09 8:26 [FFmpeg-devel] [PATCH] avcodec/d3d12va_encode: texture array support for H264 (PR #20475) ArazIusubov via ffmpeg-devel
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git