diff mbox series

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

Message ID CAPYw7P6hxe_Awcd5bAahCwHQREwWt-u1o1nhA5VzsK9vbpi6hg@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] avcodec/gif: fix duration of last packet/frame | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Paul B Mahol Aug. 23, 2022, 9:56 a.m. UTC
Hi,

patch attached.

Comments

Andreas Rheinhardt Aug. 23, 2022, 12:18 p.m. UTC | #1
Paul B Mahol:
> Hi,
> 
> patch attached.
> 
>              return ret;
>      }
>  
> +    pkt->duration = av_rescale_q(pict->duration, (AVRational){ 1, 100 },
> +                                 avctx->time_base);
>      pkt->size   = outbuf_ptr - pkt->data;
>      if (s->image || !avctx->frame_number)
>          pkt->flags |= AV_PKT_FLAG_KEY;

Where does the magic 1/100 timebase come from? For encoding, the
AVFrame's time base is supposed to be AVCodecContext.time_base. This
could actually be done generically for all video-encoders without delay.
(I know that AVFrame has a time_base of its own that is currently unset
and unused.)

- Andreas
Paul B Mahol Aug. 23, 2022, 12:36 p.m. UTC | #2
On Tue, Aug 23, 2022 at 2:18 PM Andreas Rheinhardt <
andreas.rheinhardt@outlook.com> wrote:

> Paul B Mahol:
> > Hi,
> >
> > patch attached.
> >
> >              return ret;
> >      }
> >
> > +    pkt->duration = av_rescale_q(pict->duration, (AVRational){ 1, 100 },
> > +                                 avctx->time_base);
> >      pkt->size   = outbuf_ptr - pkt->data;
> >      if (s->image || !avctx->frame_number)
> >          pkt->flags |= AV_PKT_FLAG_KEY;
>
> Where does the magic 1/100 timebase come from? For encoding, the
> AVFrame's time base is supposed to be AVCodecContext.time_base. This
> could actually be done generically for all video-encoders without delay.
> (I know that AVFrame has a time_base of its own that is currently unset
> and unused.)
>
> It is from gif demuxer/"container", it uses 1/100 timebase.

Similar thing is done for audio encoders.



> - Andreas
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
Andreas Rheinhardt Aug. 23, 2022, 12:37 p.m. UTC | #3
Andreas Rheinhardt:
> Paul B Mahol:
>> Hi,
>>
>> patch attached.
>>
>>              return ret;
>>      }
>>  
>> +    pkt->duration = av_rescale_q(pict->duration, (AVRational){ 1, 100 },
>> +                                 avctx->time_base);
>>      pkt->size   = outbuf_ptr - pkt->data;
>>      if (s->image || !avctx->frame_number)
>>          pkt->flags |= AV_PKT_FLAG_KEY;
> 
> Where does the magic 1/100 timebase come from? For encoding, the
> AVFrame's time base is supposed to be AVCodecContext.time_base. This
> could actually be done generically for all video-encoders without delay.
> (I know that AVFrame has a time_base of its own that is currently unset
> and unused.)
> 

Strangely, if I do exactly as above, the gifenc tests show weird
durations like:

+0,          0,          0,       10,     1368, 0x6cf0befd

+0,          1,          1,       10,      158, 0xcd173bb4, F=0x0

+0,          2,          2,       10,      163, 0x4f7a451d, F=0x0

+0,          3,          3,       10,      152, 0x17723839, F=0x0

+0,          4,          4,       10,      160, 0x67854056, F=0x0


So somewhere a timebase is wrong or AVFrame.duration is not converted to
a new timebase somewhere.

- Andreas
Andreas Rheinhardt Aug. 23, 2022, 12:56 p.m. UTC | #4
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;
>  }
Tomas Härdin Aug. 24, 2022, 4:15 p.m. UTC | #5
tis 2022-08-23 klockan 14:18 +0200 skrev Andreas Rheinhardt:
> Paul B Mahol:
> > Hi,
> > 
> > patch attached.
> > 
> >              return ret;
> >      }
> >  
> > +    pkt->duration = av_rescale_q(pict->duration, (AVRational){ 1,
> > 100 },
> > +                                 avctx->time_base);
> >      pkt->size   = outbuf_ptr - pkt->data;
> >      if (s->image || !avctx->frame_number)
> >          pkt->flags |= AV_PKT_FLAG_KEY;
> 
> Where does the magic 1/100 timebase come from?

From the spec

/Tomas
Paul B Mahol Aug. 24, 2022, 4:23 p.m. UTC | #6
On Wed, Aug 24, 2022 at 6:15 PM Tomas Härdin <tjoppen@acc.umu.se> wrote:

> tis 2022-08-23 klockan 14:18 +0200 skrev Andreas Rheinhardt:
> > Paul B Mahol:
> > > Hi,
> > >
> > > patch attached.
> > >
> > >              return ret;
> > >      }
> > >
> > > +    pkt->duration = av_rescale_q(pict->duration, (AVRational){ 1,
> > > 100 },
> > > +                                 avctx->time_base);
> > >      pkt->size   = outbuf_ptr - pkt->data;
> > >      if (s->image || !avctx->frame_number)
> > >          pkt->flags |= AV_PKT_FLAG_KEY;
> >
> > Where does the magic 1/100 timebase come from?
>
> From the spec
>

This patch need frame duration handling in libavfilter that is currently
WIP.


>
> /Tomas
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
diff mbox series

Patch

From f126c6ef9e5fa8b51272508af6603f05c26a3f26 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Tue, 23 Aug 2022 11:51:24 +0200
Subject: [PATCH] avcodec/gif: fix duration of last packet/frame

Fixes #6294

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/gif.c  |  2 ++
 libavformat/gif.c | 33 +++++++--------------------------
 2 files changed, 9 insertions(+), 26 deletions(-)

diff --git a/libavcodec/gif.c b/libavcodec/gif.c
index 8e84b79b8c..2484cc92e2 100644
--- a/libavcodec/gif.c
+++ b/libavcodec/gif.c
@@ -513,6 +513,8 @@  static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
             return ret;
     }
 
+    pkt->duration = av_rescale_q(pict->duration, (AVRational){ 1, 100 },
+                                 avctx->time_base);
     pkt->size   = outbuf_ptr - pkt->data;
     if (s->image || !avctx->frame_number)
         pkt->flags |= AV_PKT_FLAG_KEY;
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)
         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;
 }
-- 
2.37.2