[FFmpeg-devel] [PATCH 2/2] lavc/d3d12va_encode: trim header alignment at output

Tong Wu wutong1208 at outlook.com
Mon Jul 8 18:13:23 EEST 2024


It is d3d12va's requirement that the FrameStartOffset must be aligned as
per hardware limitation. However, we could trim this alignment at output
to reduce coded size. A aligned_header_size is added to
D3D12VAEncodePicture.

Signed-off-by: Tong Wu <wutong1208 at outlook.com>
---
 libavcodec/d3d12va_encode.c | 18 ++++++++++++------
 libavcodec/d3d12va_encode.h |  1 +
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
index 9f7a42911e..9ee9da41e3 100644
--- a/libavcodec/d3d12va_encode.c
+++ b/libavcodec/d3d12va_encode.c
@@ -308,9 +308,9 @@ static int d3d12va_encode_issue(AVCodecContext *avctx,
         }
 
         pic->header_size = (int)bit_len / 8;
-        pic->header_size = pic->header_size % ctx->req.CompressedBitstreamBufferAccessAlignment ?
-                           FFALIGN(pic->header_size, ctx->req.CompressedBitstreamBufferAccessAlignment) :
-                           pic->header_size;
+        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)) {
@@ -318,7 +318,7 @@ static int d3d12va_encode_issue(AVCodecContext *avctx,
             goto fail;
         }
 
-        memcpy(ptr, data, pic->header_size);
+        memcpy(ptr, data, pic->aligned_header_size);
         ID3D12Resource_Unmap(pic->output_buffer, 0, NULL);
     }
 
@@ -344,10 +344,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->header_size;
+    input_args.CurrentFrameBitstreamMetadataSize          = pic->aligned_header_size;
 
     output_args.Bitstream.pBuffer                                    = pic->output_buffer;
-    output_args.Bitstream.FrameStartOffset                           = pic->header_size;
+    output_args.Bitstream.FrameStartOffset                           = pic->aligned_header_size;
     output_args.ReconstructedPicture.pReconstructedPicture           = pic->recon_surface->texture;
     output_args.ReconstructedPicture.ReconstructedPictureSubresource = 0;
     output_args.EncoderOutputMetadata.pBuffer                        = pic->encoded_metadata;
@@ -663,6 +663,12 @@ static int d3d12va_encode_get_coded_data(AVCodecContext *avctx,
         goto end;
     ptr = pkt->data;
 
+    memcpy(ptr, mapped_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);
 
     ID3D12Resource_Unmap(pic->output_buffer, 0, NULL);
diff --git a/libavcodec/d3d12va_encode.h b/libavcodec/d3d12va_encode.h
index 1a0abc5bd0..51440428e4 100644
--- a/libavcodec/d3d12va_encode.h
+++ b/libavcodec/d3d12va_encode.h
@@ -43,6 +43,7 @@ typedef struct D3D12VAEncodePicture {
     FFHWBaseEncodePicture base;
 
     int             header_size;
+    int             aligned_header_size;
 
     AVD3D12VAFrame *input_surface;
     AVD3D12VAFrame *recon_surface;
-- 
2.45.1.windows.1



More information about the ffmpeg-devel mailing list