[FFmpeg-devel] [PATCH] lavc/vaapi_encode: grow packet if vaMapBuffer returns multiple buffers

Mark Thompson sw at jkqxz.net
Thu May 30 02:17:01 EEST 2019


On 29/05/2019 21:57, Linjie Fu wrote:
> It seems that VA_CODED_BUF_STATUS_SINGLE_NALU allows driver to map
> buffer for each slice.
> 
> Currently, assigning new buffer for pkt when multiple buffer returns
> from vaMapBuffer will cover the previous encoded pkt data and lead
> to encode issues.
> 
> Using av_grow_packet to expand pkt if several buffers are returned.
> 
> Signed-off-by: Linjie Fu <linjie.fu at intel.com>
> ---
>  libavcodec/vaapi_encode.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)

What driver uses this?

> diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
> index 2dda451..2812237 100644
> --- a/libavcodec/vaapi_encode.c
> +++ b/libavcodec/vaapi_encode.c
> @@ -490,6 +490,7 @@ static int vaapi_encode_output(AVCodecContext *avctx,
>      VACodedBufferSegment *buf_list, *buf;
>      VAStatus vas;
>      int err;
> +    uint8_t *ptr;
>  
>      err = vaapi_encode_wait(avctx, pic);
>      if (err < 0)
> @@ -509,11 +510,18 @@ static int vaapi_encode_output(AVCodecContext *avctx,
>          av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
>                 "(status %08x).\n", buf->size, buf->status);
>  
> -        err = av_new_packet(pkt, buf->size);
> +        if (pkt->size)
> +            err = av_grow_packet(pkt, buf->size);

av_grow_packet() can reallocate the buffer, which will change its address.

To avoid repeated new allocations and copies, perhaps it would be better to iterate through the list first to find out how large the single packet needs to be?

> +        else {
> +            err = av_new_packet(pkt, buf->size);
> +            ptr = pkt->data;
> +        }
> +
>          if (err < 0)
>              goto fail_mapped;
>  
> -        memcpy(pkt->data, buf->buf, buf->size);
> +        memcpy(ptr, buf->buf, buf->size);
> +        ptr += buf->size;
>      }
>  
>      if (pic->type == PICTURE_TYPE_IDR)
> 

- Mark


More information about the ffmpeg-devel mailing list