diff mbox series

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

Message ID SY7P282MB45027EAFDD9F182948E9B36DC7DA2@SY7P282MB4502.AUSP282.PROD.OUTLOOK.COM
State New
Headers show
Series [FFmpeg-devel,1/2] lavc/hw_base_encode: correct the timestamp when input_order = decode_delay | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Tong Wu July 8, 2024, 3:13 p.m. UTC
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@outlook.com>
---
 libavcodec/d3d12va_encode.c | 18 ++++++++++++------
 libavcodec/d3d12va_encode.h |  1 +
 2 files changed, 13 insertions(+), 6 deletions(-)
diff mbox series

Patch

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;