Fix several issues in the decode loop related to return values and empty
packet signaling:

- Return correct values after decoding frames to avoid premature
  end-of-stream by checking if more frames are available
- Add proper empty packet handling: return AVERROR(EINVAL) when decoder
  is uninitialized, flush buffered frames when initialized, and signal
  end-of-stream correctly after all frames are drained
- Return 0 instead of avpkt->size when avpkt is NULL to handle flush
  packets safely

Based on an initial patch by Peter Xia <[email protected]>

Signed-off-by: heipa <[email protected]>
---
 libavcodec/libwebpdec.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/libavcodec/libwebpdec.c b/libavcodec/libwebpdec.c
index 760cd82..0b820aa 100644
--- a/libavcodec/libwebpdec.c
+++ b/libavcodec/libwebpdec.c
@@ -125,6 +125,20 @@ static int libwebp_decode_frame(AVCodecContext *avctx, 
AVFrame *p,
 
         if (anim_info.frame_count > 0)
             avctx->framerate = av_make_q(1000, 1);
+    } else if (!avpkt || avpkt->size <= 0) {
+        if (!WebPAnimDecoderHasMoreFrames(s->dec)) {
+            if (!s->infinite_loop && s->loop_sent >= s->loop_count) {
+                *got_frame = 0;
+                return 0;
+            }
+            s->loop_sent++;
+            WebPAnimDecoderReset(s->dec);
+            s->frame_sent = 0;
+            s->prev_timestamp_ms = 0;
+            s->first_frame_pts = -1;
+            av_log(avctx, AV_LOG_DEBUG, "Loop %u/%u (flush)\n", s->loop_sent + 
1,
+                   s->infinite_loop ? 0 : s->loop_count);
+        }
     }
 
     if (!WebPAnimDecoderHasMoreFrames(s->dec)) {
@@ -185,7 +199,13 @@ static int libwebp_decode_frame(AVCodecContext *avctx, 
AVFrame *p,
                         avctx->width * 4, avctx->height);
 
     *got_frame = 1;
-    return avpkt->size;
+
+    if (WebPAnimDecoderHasMoreFrames(s->dec) || s->infinite_loop ||
+        s->loop_sent < s->loop_count) {
+        return 0;
+    }
+
+    return avpkt ? avpkt->size : 0;
 }
 
 static av_cold int libwebp_decode_close(AVCodecContext *avctx)
-- 
2.43.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to