PR #25933 opened by witaly-iwanow URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25933 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25933.patch
Fixes #25923: on Intel, `h264_d3d12va` IDR packets lost their SPS/PPS and part of the slice, because the driver writes the frame at offset 0 regardless of `FrameStartOffset`. The sequence header now stays in the picture and is prepended to the packet, and the frame is encoded at offset 0. >From a84e0081bc0465fb6b2fbbc5adbf44b5e99265f2 Mon Sep 17 00:00:00 2001 From: Vitaly Ivanov <[email protected]> Date: Wed, 11 Nov 2026 14:47:11 +0700 Subject: [PATCH] avcodec/d3d12va_encode: keep the sequence header out of the bitstream buffer The header was written to the start of the output buffer and the frame placed after it with FrameStartOffset. The Intel H.264 encoder ignores that offset and writes the frame at 0, over the header, so every IDR lost its SPS/PPS and part of its slice data. Keep the header in the picture and prepend it to the packet instead, encoding at offset 0. This also removes a read of aligned_header_size bytes from a MAX_PARAM_BUFFER_SIZE stack buffer. Fixes #25923. Signed-off-by: Vitaly Ivanov <[email protected]> --- libavcodec/d3d12va_encode.c | 29 +++++++---------------------- libavcodec/d3d12va_encode.h | 3 ++- libavcodec/d3d12va_encode_av1.c | 4 ++-- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c index 19a6901c357..2243c6db742 100644 --- a/libavcodec/d3d12va_encode.c +++ b/libavcodec/d3d12va_encode.c @@ -325,8 +325,6 @@ static int d3d12va_encode_issue(AVCodecContext *avctx, AVD3D12VAFramesContext *frames_hwctx = base_ctx->input_frames->hwctx; int err, i, j; HRESULT hr; - char data[MAX_PARAM_BUFFER_SIZE]; - void *ptr; size_t bit_len; ID3D12CommandAllocator *command_allocator = NULL; ID3D12VideoEncodeCommandList2 *cmd_list = ctx->command_list; @@ -367,8 +365,6 @@ static int d3d12va_encode_issue(AVCodecContext *avctx, D3D12_VIDEO_ENCODER_RESOLVE_METADATA_OUTPUT_ARGUMENTS output_metadata = { 0 }; - memset(data, 0, sizeof(data)); - av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" " "as type %s.\n", base_pic->display_order, base_pic->encode_order, ff_hw_base_encode_get_pictype_name(base_pic->type)); @@ -446,28 +442,18 @@ static int d3d12va_encode_issue(AVCodecContext *avctx, } } + // The header stays out of the output buffer: some drivers (Intel H.264) + // write the frame at offset 0 regardless of FrameStartOffset. if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) { if (ctx->codec->write_sequence_header) { - bit_len = 8 * sizeof(data); - err = ctx->codec->write_sequence_header(avctx, data, &bit_len); + bit_len = 8 * sizeof(pic->header_data); + err = ctx->codec->write_sequence_header(avctx, pic->header_data, &bit_len); if (err < 0) { av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence " "header: %d.\n", err); goto fail; } pic->header_size = (int)bit_len / 8; - pic->aligned_header_size = pic->header_size % ctx->req.CompressedBitstreamBufferAccessAlignment ? - FFALIGN(pic->header_size, ctx->req.CompressedBitstreamBufferAccessAlignment) : - pic->header_size; - - hr = ID3D12Resource_Map(pic->output_buffer, 0, NULL, (void **)&ptr); - if (FAILED(hr)) { - err = AVERROR_UNKNOWN; - goto fail; - } - - memcpy(ptr, data, pic->aligned_header_size); - ID3D12Resource_Unmap(pic->output_buffer, 0, NULL); } } @@ -510,10 +496,10 @@ static int d3d12va_encode_issue(AVCodecContext *avctx, input_args.PictureControlDesc.PictureControlCodecData = pic->pic_ctl; input_args.PictureControlDesc.ReferenceFrames = d3d12_refs; - input_args.CurrentFrameBitstreamMetadataSize = pic->aligned_header_size; + input_args.CurrentFrameBitstreamMetadataSize = pic->header_size; output_args.Bitstream.pBuffer = pic->output_buffer; - output_args.Bitstream.FrameStartOffset = pic->aligned_header_size; + output_args.Bitstream.FrameStartOffset = 0; output_args.ReconstructedPicture.pReconstructedPicture = pic->recon_surface->texture; output_args.ReconstructedPicture.ReconstructedPictureSubresource = ctx->is_texture_array ? pic->subresource_index : 0; output_args.EncoderOutputMetadata.pBuffer = pic->encoded_metadata; @@ -874,10 +860,9 @@ static int d3d12va_encode_get_coded_data(AVCodecContext *avctx, goto end; ptr = pkt->data; - memcpy(ptr, mapped_data, pic->header_size); + memcpy(ptr, pic->header_data, pic->header_size); ptr += pic->header_size; - mapped_data += pic->aligned_header_size; total_size -= pic->header_size; memcpy(ptr, mapped_data, total_size); diff --git a/libavcodec/d3d12va_encode.h b/libavcodec/d3d12va_encode.h index bfc5dd55786..41d6aaafb78 100644 --- a/libavcodec/d3d12va_encode.h +++ b/libavcodec/d3d12va_encode.h @@ -40,8 +40,9 @@ extern const AVCodecHWConfigInternal *const ff_d3d12va_encode_hw_configs[]; #define D3D12VA_VIDEO_ENC_ASYNC_DEPTH 8 typedef struct D3D12VAEncodePicture { + // Sequence header of an IDR frame, prepended to the coded data on output. + char header_data[MAX_PARAM_BUFFER_SIZE]; int header_size; - int aligned_header_size; AVD3D12VAFrame *input_surface; AVD3D12VAFrame *recon_surface; diff --git a/libavcodec/d3d12va_encode_av1.c b/libavcodec/d3d12va_encode_av1.c index 0b89c02bf21..96a5be0ca67 100644 --- a/libavcodec/d3d12va_encode_av1.c +++ b/libavcodec/d3d12va_encode_av1.c @@ -546,7 +546,7 @@ static int d3d12va_encode_av1_get_coded_data(AVCodecContext *avctx, } uint8_t *dst = tile_group_buf; - uint8_t *tiles_base = mapped_data + pic->aligned_header_size; + uint8_t *tiles_base = mapped_data; uint64_t src_off = 0; for (uint64_t i = 0; i < nb_subregions; i++) { /* Tiles are laid out contiguously by their full size; the actual @@ -614,7 +614,7 @@ static int d3d12va_encode_av1_get_coded_data(AVCodecContext *avctx, ptr = pkt->data + prev_stash_sz; } - memcpy(ptr, mapped_data, pic->header_size); + memcpy(ptr, pic->header_data, pic->header_size); ptr += pic->header_size; memcpy(ptr, pic_hd_data, av1_pic_hd_size); -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
