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

Lynne dev at lynne.ee
Sun Jul 28 18:55:03 EEST 2024


On 28/07/2024 15:06, Tong Wu wrote:
> Tong Wu:
>> Subject: [FFmpeg-devel][PATCH 2/2] lavc/d3d12va_encode: trim header
>> alignment at output
>>
>> 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
> 
> 
> The first patch in this patchset has been merged earlier. Will merge this patch if there's no more comment.
> 
> -Tong
> 
> 
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".

Somehow I missed that patch. You should've ping me when I pushed the 
first one.

Pushed.
In Vulkan, we expose the raw packet we've just encoded by wrapping it as 
an AVBufferRef, you should consider doing this.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0xA2FEA5F03F034464.asc
Type: application/pgp-keys
Size: 624 bytes
Desc: OpenPGP public key
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240728/147c1931/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 236 bytes
Desc: OpenPGP digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240728/147c1931/attachment.sig>


More information about the ffmpeg-devel mailing list