diff mbox series

[FFmpeg-devel,v3,3/5] avcodec/parser: fix fetch_timestamp in a scenario with unaligned packets

Message ID 20240304173219.1877658-4-nicolas.gaullier@cji.paris
State New
Headers show
Series avcodec/parser: fix fetch_timestamp in a scenario with unaligned packets | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Nicolas Gaullier March 4, 2024, 5:32 p.m. UTC
Fix fetch_timestamp when the frame start is in a previous packet.

Signed-off-by: Nicolas Gaullier <nicolas.gaullier@cji.paris>
---
 libavcodec/parser.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/parser.c b/libavcodec/parser.c
index 496ebbc231..3c22fdcc2f 100644
--- a/libavcodec/parser.c
+++ b/libavcodec/parser.c
@@ -102,7 +102,7 @@  void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
                 s->dts    = s->cur_frame_dts[i];
                 s->pts    = s->cur_frame_pts[i];
                 s->pos    = s->cur_frame_pos[i];
-                s->offset = s->next_frame_offset - s->cur_frame_offset[i];
+                s->offset = FFMAX( 0, s->next_frame_offset - s->cur_frame_offset[i]);
             }
             if (remove)
                 s->cur_frame_offset[i] = INT64_MAX;
@@ -158,11 +158,11 @@  int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
     }
 
     if (s->fetch_timestamp) {
-        s->fetch_timestamp = 0;
         s->last_pts        = s->pts;
         s->last_dts        = s->dts;
         s->last_pos        = s->pos;
-        ff_fetch_timestamp(s, 0, 0, 0);
+        ff_fetch_timestamp(s, FFMIN(s->fetch_timestamp, 0), 0, 0);
+        s->fetch_timestamp = 0;
     }
     /* WARNING: the returned index can be negative */
     index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
@@ -179,12 +179,13 @@  int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
 
     /* update the file pointer */
     if (*poutbuf_size) {
+        s->fetch_timestamp   = index >= 0 || !s->frame_offset ? 1 : index;
+
         /* fill the data for the current frame */
         s->frame_offset = s->next_frame_offset;
 
         /* offset of the next frame */
         s->next_frame_offset = s->cur_offset + index;
-        s->fetch_timestamp   = 1;
     } else {
         /* Don't return a pointer to dummy_buf. */
         *poutbuf = NULL;