[FFmpeg-devel] [PATCH] Add av_copy_packet()

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Sep 20 10:16:02 CEST 2012


2012/9/18 Andrey Utkin <andrey.krieger.utkin at gmail.com>:
> This function makes full deep copy of AVPacket contents.
> ---
>  libavcodec/avcodec.h  |    7 ++++++
>  libavcodec/avpacket.c |   58 +++++++++++++++++++++++++++++-------------------
>  2 files changed, 42 insertions(+), 23 deletions(-)
>
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 62a261a..783ac96 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -3666,6 +3666,13 @@ int av_grow_packet(AVPacket *pkt, int grow_by);
>  int av_dup_packet(AVPacket *pkt);
>
>  /**
> + * Copy packet, including contents
> + *
> + * @return 0 on success, negative AVERROR on fail
> + */
> +int av_copy_packet(AVPacket *dst, AVPacket *src);
> +
> +/**
>   * Free a packet.
>   *
>   * @param pkt packet to free
> diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
> index 17c100e..6818ef6 100644
> --- a/libavcodec/avpacket.c
> +++ b/libavcodec/avpacket.c
> @@ -125,40 +125,52 @@ int av_grow_packet(AVPacket *pkt, int grow_by)
>          dst = data;                                                     \
>      } while (0)
>
> -int av_dup_packet(AVPacket *pkt)
> +/* Makes duplicates of data, side_data, but does not copy any other fields */
> +static int av_copy_packet_data(AVPacket *dst, AVPacket *src)
>  {
> -    AVPacket tmp_pkt;
> -
> -    if (pkt->destruct == NULL && pkt->data) {
> -        tmp_pkt = *pkt;
> +    dst->data      = NULL;
> +    dst->side_data = NULL;
> +    DUP_DATA(dst->data, src->data, dst->size, 1);
> +    dst->destruct = av_destruct_packet;
>
> -        pkt->data      = NULL;
> -        pkt->side_data = NULL;
> -        DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1);
> -        pkt->destruct = av_destruct_packet;
> +    if (dst->side_data_elems) {
> +        int i;
>
> -        if (pkt->side_data_elems) {
> -            int i;
> -
> -            DUP_DATA(pkt->side_data, tmp_pkt.side_data,
> -                     pkt->side_data_elems * sizeof(*pkt->side_data), 0);
> -            memset(pkt->side_data, 0,
> -                   pkt->side_data_elems * sizeof(*pkt->side_data));
> -            for (i = 0; i < pkt->side_data_elems; i++) {
> -                DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
> -                         tmp_pkt.side_data[i].size, 1);
> -                pkt->side_data[i].size = tmp_pkt.side_data[i].size;
> -                pkt->side_data[i].type = tmp_pkt.side_data[i].type;
> -            }
> +        DUP_DATA(dst->side_data, src->side_data,
> +                dst->side_data_elems * sizeof(*dst->side_data), 0);
> +        memset(dst->side_data, 0,
> +                dst->side_data_elems * sizeof(*dst->side_data));
> +        for (i = 0; i < dst->side_data_elems; i++) {
> +            DUP_DATA(dst->side_data[i].data, src->side_data[i].data,
> +                    src->side_data[i].size, 1);
> +            dst->side_data[i].size = src->side_data[i].size;
> +            dst->side_data[i].type = src->side_data[i].type;
>          }
>      }
>      return 0;
>
>  failed_alloc:
> -    av_destruct_packet(pkt);
> +    av_destruct_packet(dst);
>      return AVERROR(ENOMEM);
>  }
>
> +int av_dup_packet(AVPacket *pkt)
> +{
> +    AVPacket tmp_pkt;
> +
> +    if (pkt->destruct == NULL && pkt->data) {
> +        tmp_pkt = *pkt;
> +        return av_copy_packet_data(pkt, &tmp_pkt);
> +    }
> +    return 0;
> +}
> +
> +int av_copy_packet(AVPacket *dst, AVPacket *src)
> +{
> +    *dst = *src;
> +    return av_copy_packet_data(dst, src);
> +}
> +
>  void av_free_packet(AVPacket *pkt)
>  {
>      if (pkt) {
> --
> 1.7.8.6
>

Could somebody review this new version of patch?

-- 
Andrey Utkin


More information about the ffmpeg-devel mailing list