[FFmpeg-devel] [PATCH] avcodec/gif: fix duration of last packet/frame

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Aug 23 15:56:00 EEST 2022


Paul B Mahol:
> diff --git a/libavformat/gif.c b/libavformat/gif.c
> index b52ff4dd39..afb5767541 100644
> --- a/libavformat/gif.c
> +++ b/libavformat/gif.c
> @@ -36,7 +36,6 @@ typedef struct GIFContext {
>      int duration;
>      int64_t last_pos;
>      int have_end;
> -    AVPacket *prev_pkt;
>  } GIFContext;
>  
>  static int gif_write_header(AVFormatContext *s)
> @@ -81,28 +80,20 @@ static int gif_parse_packet(AVFormatContext *s, const uint8_t *data, int size)
>      return 0;
>  }
>  
> -static int gif_get_delay(GIFContext *gif, AVPacket *prev, AVPacket *new)
> +static int gif_get_delay(GIFContext *gif, AVPacket *pkt)
>  {
> -    if (new && new->pts != AV_NOPTS_VALUE)
> -        gif->duration = av_clip_uint16(new->pts - prev->pts);
> -    else if (!new && gif->last_delay >= 0)
> +    if (pkt->duration != 0)
> +        gif->duration = av_clip_uint16(pkt->duration);
> +    else if (!pkt && gif->last_delay >= 0)

pkt is always non-NULL here. Muxers that don't have the
AVFMT_ALLOW_FLUSH flag set never get NULL packets.
Adding said flag would require more changes in this muxer (it would
actually need to handle the case in which pkt is NULL). Anyway, this
patch as-is breaks muxing for people who don't set pkt->duration, but
rely on the final_delay option.

Anyway, you can modify gif_get_delay() to take AVPacket.duration into
account if it is set. As soon as we manage to properly set
AVPacket.duration in avcodec/the fftools, we can deprecate the
final_delay option.

>          gif->duration = gif->last_delay;
>  
>      return gif->duration;
>  }
>  
> -static int gif_write_packet(AVFormatContext *s, AVPacket *new_pkt)
> +static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
>  {
>      GIFContext *gif = s->priv_data;
>      AVIOContext *pb = s->pb;
> -    AVPacket *pkt = gif->prev_pkt;
> -
> -    if (!gif->prev_pkt) {
> -        gif->prev_pkt = av_packet_alloc();
> -        if (!gif->prev_pkt)
> -            return AVERROR(ENOMEM);
> -        return av_packet_ref(gif->prev_pkt, new_pkt);
> -    }
>  
>      gif->last_pos = avio_tell(pb);
>      if (pkt->size > 0)
> @@ -144,7 +135,7 @@ static int gif_write_packet(AVFormatContext *s, AVPacket *new_pkt)
>          delay_pos = gif_parse_packet(s, pkt->data + off, pkt->size - off);
>          if (delay_pos > 0 && delay_pos < pkt->size - off - 2) {
>              avio_write(pb, pkt->data + off, delay_pos);
> -            avio_wl16(pb, gif_get_delay(gif, pkt, new_pkt));
> +            avio_wl16(pb, gif_get_delay(gif, pkt));
>              avio_write(pb, pkt->data + off + delay_pos + 2, pkt->size - off - delay_pos - 2);
>          } else {
>              avio_write(pb, pkt->data + off, pkt->size - off);
> @@ -154,17 +145,13 @@ static int gif_write_packet(AVFormatContext *s, AVPacket *new_pkt)
>  
>          if (delay_pos > 0 && delay_pos < pkt->size - 2) {
>              avio_write(pb, pkt->data, delay_pos);
> -            avio_wl16(pb, gif_get_delay(gif, pkt, new_pkt));
> +            avio_wl16(pb, gif_get_delay(gif, pkt));
>              avio_write(pb, pkt->data + delay_pos + 2, pkt->size - delay_pos - 2);
>          } else {
>              avio_write(pb, pkt->data, pkt->size);
>          }
>      }
>  
> -    av_packet_unref(gif->prev_pkt);
> -    if (new_pkt)
> -        return av_packet_ref(gif->prev_pkt, new_pkt);
> -
>      return 0;
>  }
>  
> @@ -173,14 +160,8 @@ static int gif_write_trailer(AVFormatContext *s)
>      GIFContext *gif = s->priv_data;
>      AVIOContext *pb = s->pb;
>  
> -    if (!gif->prev_pkt)
> -        return AVERROR(EINVAL);
> -
> -    gif_write_packet(s, NULL);
> -
>      if (!gif->have_end)
>          avio_w8(pb, GIF_TRAILER);
> -    av_packet_free(&gif->prev_pkt);
>  
>      return 0;
>  }




More information about the ffmpeg-devel mailing list