PR #26380 opened by yuygfgg
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26380
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26380.patch

# Summary of changes

Preserve the PTS and DTS of the first packet passed to av_parser_parse2() when 
the packet contains one byte and its file position is unknown (pos = -1).

The packet occupies the byte range [-1, 0). Its end offset matches the 
zero-initialized timestamp slot, so the parser treats the first packet as a 
remainder. Timestamp lookup also assumes that an end offset of zero indicates 
an empty slot. The fix distinguishes the initial slot from the first input 
packet and checks whether each slot covers any bytes.

## Test

Run make fate-api-amr-parser. The regression test supplies a one-byte AMR 
NO_DATA frame with PTS 123, DTS 122 and position -1. It verifies that AMR-NB 
and AMR-WB each return the frame with the supplied timestamps and position.


>From 4c72719fe7e45d6ead8dfd93cc350b5f76989325 Mon Sep 17 00:00:00 2001
From: yuygfgg <[email protected]>
Date: Fri, 27 Nov 2026 00:11:57 +0800
Subject: [PATCH] avcodec/parser: keep timestamps on the first packet at an
 unknown position

A one-byte packet at position -1 ends at offset zero. The initial
packet descriptor also ends at zero, so the parser mistakes the first
packet for a remainder. Timestamp lookup also treats the end of this
valid packet as an empty slot.

Distinguish occupied slots from empty ones and allow the first frame
to start at an unknown position. Test both AMR parsers with a one-byte
NO_DATA frame and distinct PTS and DTS.

Signed-off-by: yuygfgg <[email protected]>
---
 libavcodec/parser.c             |  9 +++++----
 tests/api/api-amr-parser-test.c | 13 +++++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/libavcodec/parser.c b/libavcodec/parser.c
index cf5d65ee169..72921a03ac7 100644
--- a/libavcodec/parser.c
+++ b/libavcodec/parser.c
@@ -97,11 +97,10 @@ void ff_fetch_timestamp(AVCodecParserContext *s, int off, 
int remove, int fuzzy)
         s->offset = 0;
     }
     for (i = 0; i < AV_PARSER_PTS_NB; i++) {
-        if (s->cur_offset + off >= s->cur_frame_offset[i] &&
+        if (s->cur_frame_offset[i] < s->cur_frame_end[i] &&
+            s->cur_offset + off >= s->cur_frame_offset[i] &&
             (s->frame_offset < s->cur_frame_offset[i] ||
-             (!s->frame_offset && !s->next_frame_offset)) && // first 
field/frame
-            // check disabled since MPEG-TS does not send complete PES packets
-            /*s->next_frame_offset + off <*/  s->cur_frame_end[i]){
+             (!s->frame_offset && s->next_frame_offset <= 0))) { // first 
field/frame
 
             if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
                 s->dts    = s->cur_frame_dts[i];
@@ -140,6 +139,8 @@ int av_parser_parse2(AVCodecParserContext *s, 
AVCodecContext *avctx,
     desc = avcodec_descriptor_get(avctx->codec_id);
 
     if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
+        /* A one-byte packet at an unknown position ends at zero. */
+        s->cur_frame_end[s->cur_frame_start_index] = -1;
         s->next_frame_offset =
         s->cur_offset        = pos;
         s->flags            |= PARSER_FLAG_FETCHED_OFFSET;
diff --git a/tests/api/api-amr-parser-test.c b/tests/api/api-amr-parser-test.c
index 0c42c041cc2..2d2ad7359ca 100644
--- a/tests/api/api-amr-parser-test.c
+++ b/tests/api/api-amr-parser-test.c
@@ -81,6 +81,19 @@ static int test_parser(enum AVCodecID codec, int channels, 
int frame_size)
     parser = av_parser_init(codec);
     if (!parser)
         goto end;
+    if (channels == 1) {
+        /* An AMR NO_DATA frame fits in a one-byte packet. */
+        static const uint8_t no_data[1 + AV_INPUT_BUFFER_PADDING_SIZE] = { 
0x7c };
+        uint8_t *output;
+        int size;
+
+        if (av_parser_parse2(parser, ctx, &output, &size, no_data, 1,
+                             123, 122, -1) != 1 || size != 1 ||
+            parser->pts != 123 || parser->dts != 122 || parser->pos != -1) {
+            fprintf(stderr, "Lost timestamps on first one-byte packet\n");
+            goto end;
+        }
+    }
     parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
     ctx->bit_rate = 0;
     parse(parser, ctx, "complete frames", blocks, 2 * block_size);
-- 
2.54.1

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

Reply via email to