Commit 50276fac71 for ffmpeg

commit 50276fac71863c3ffd6e3edfe765086103a695d5
Author: Fredrik Mellbin <fredrik.mellbin@gmail.com>
Date:   Wed Oct 28 15:58:05 2026 +0100

    avcodec/vc1_parser: reset the parse state for every complete frame

    With complete frames, a short header (skipped frame) was left pending and
    trailing zeros could fake a start code, giving wrong picture types.

    Fixes #25565.

    Assisted-by: Fairy
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>

diff --git a/libavcodec/vc1_parser.c b/libavcodec/vc1_parser.c
index 224ff3ee58..4a4eb8398f 100644
--- a/libavcodec/vc1_parser.c
+++ b/libavcodec/vc1_parser.c
@@ -169,6 +169,14 @@ static int vc1_parse(AVCodecParserContext *s,
     int next = END_NOT_FOUND;
     int i = vpc->bytes_to_skip;

+    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
+        /* Each call is a whole frame: scan it from its own start code */
+        unesc_index          = 0;
+        search_state         = NO_MATCH;
+        vpc->prev_start_code = 0;
+        i                    = 0;
+    }
+
     if (!vpc->extradata_parsed) {
         vpc->extradata_parsed = 1;
         vc1_parse_extradata(s, avctx);
@@ -205,12 +213,14 @@ static int vc1_parse(AVCodecParserContext *s,
             }
         }
         if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
-                unesc_index >= UNESCAPED_THRESHOLD &&
+                (unesc_index >= UNESCAPED_THRESHOLD || i == buf_size) &&
                 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
         {
             // No need to keep scanning the rest of the buffer for
             // start codes if we know it contains a complete frame and
-            // we've already unescaped all we need of the frame header
+            // we've already unescaped all we need of the frame header,
+            // or the frame ended first, as a skipped frame does
+            memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
             unesc_index = 0;
             break;
diff --git a/tests/api/Makefile b/tests/api/Makefile
index 1c7a350273..75df91492d 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -13,6 +13,7 @@ APITESTPROGS-$(HAVE_THREADS) += api-threadmessage
 APITESTPROGS-$(CONFIG_AVCODEC) += api-enc-info
 APITESTPROGS-$(CONFIG_PCM_S16LE_DECODER) += api-skip-samples
 APITESTPROGS-$(call ALLYES, H261_ENCODER H261_PARSER) += api-enc-parser
+APITESTPROGS-$(CONFIG_VC1_PARSER) += api-vc1-parser
 APITESTPROGS += $(APITESTPROGS-yes)

 APITESTOBJS  := $(APITESTOBJS:%=$(APITESTSDIR)%) $(APITESTPROGS:%=$(APITESTSDIR)/%-test.o)
diff --git a/tests/api/api-vc1-parser-test.c b/tests/api/api-vc1-parser-test.c
new file mode 100644
index 0000000000..e1006f0790
--- /dev/null
+++ b/tests/api/api-vc1-parser-test.c
@@ -0,0 +1,85 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libavcodec/avcodec.h"
+#include "libavutil/avutil.h"
+
+/* Synthetic advanced-profile headers for a progressive 16x16 sequence,
+ * with pulldown enabled and no optional display or HRD information.
+ * Macroblock data is unnecessary: the parser only reads frame headers. */
+static const uint8_t sequence[] = {
+    0, 0, 1, 0x0f, 0xc2, 0, 0, 0x70, 0x07, 0x88, 0x80,
+    0, 0, 1, 0x0e, 0, 0, 0x80,
+};
+
+static void parse(AVCodecParserContext *parser, AVCodecContext *ctx,
+                  const char *name, int init, uint8_t header,
+                  int payload_size, int trailing_zeros)
+{
+    uint8_t buf[128 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
+    uint8_t *out;
+    int size = 0, out_size;
+
+    if (init) {
+        memcpy(buf, sequence, sizeof(sequence));
+        size += sizeof(sequence);
+    }
+    buf[size++] = 0;
+    buf[size++] = 0;
+    buf[size++] = 1;
+    buf[size++] = 0x0d;
+    memset(buf + size, 0xff, payload_size);
+    buf[size] = header;
+    if (payload_size > 1)
+        buf[size + 1] = header == 0x80 ? 0x80 : 0;
+    size += payload_size;
+    memset(buf + size - trailing_zeros, 0, trailing_zeros);
+
+    av_parser_parse2(parser, ctx, &out, &out_size, buf, size,
+                     AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
+    printf("%s: %c\n", name, av_get_picture_type_char(parser->pict_type));
+}
+
+int main(void)
+{
+    AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_VC1);
+    AVCodecContext *ctx = avcodec_alloc_context3(NULL);
+
+    if (!parser || !ctx) {
+        av_parser_close(parser);
+        avcodec_free_context(&ctx);
+        return 1;
+    }
+    parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
+    ctx->codec_id = AV_CODEC_ID_VC1;
+    ctx->codec_type = AVMEDIA_TYPE_VIDEO;
+
+    parse(parser, ctx, "initial I",         1, 0xc1, 37, 0);
+    parse(parser, ctx, "skipped P",         0, 0xf2,  1, 0);
+    parse(parser, ctx, "B after skipped P", 0, 0x80,  2, 0);
+    parse(parser, ctx, "I ending in zeros", 0, 0xc1, 37, 2);
+    parse(parser, ctx, "P starting in 01",  0, 0x01, 37, 0);
+    parse(parser, ctx, "I after P",         0, 0xc1, 37, 0);
+
+    av_parser_close(parser);
+    avcodec_free_context(&ctx);
+    return 0;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 7b5e00f331..69df95b90d 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -1,3 +1,7 @@
+FATE_API_LIBAVCODEC-$(CONFIG_VC1_PARSER) += fate-api-vc1-parser
+fate-api-vc1-parser: $(APITESTSDIR)/api-vc1-parser-test$(EXESUF)
+fate-api-vc1-parser: CMD = run $(APITESTSDIR)/api-vc1-parser-test$(EXESUF)
+
 FATE_API_LIBAVCODEC-$(call ALLYES, AC3_ENCODER AC3_DECODER EAC3_DECODER AC3_FIXED_DECODER) += fate-api-ac3-flush
 fate-api-ac3-flush: $(APITESTSDIR)/api-ac3-flush-test$(EXESUF)
 fate-api-ac3-flush: CMD = run $(APITESTSDIR)/api-ac3-flush-test$(EXESUF)
diff --git a/tests/ref/fate/api-vc1-parser b/tests/ref/fate/api-vc1-parser
new file mode 100644
index 0000000000..a2ef564da6
--- /dev/null
+++ b/tests/ref/fate/api-vc1-parser
@@ -0,0 +1,6 @@
+initial I: I
+skipped P: P
+B after skipped P: B
+I ending in zeros: I
+P starting in 01: P
+I after P: I