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

# Summary of changes

Some MOV files carry H.264 parameter sets in-band and use Annex B start codes 
even though their avcC record declares four-byte NAL lengths. On these files 
the H.264 parser can lose the keyframe flag on an IDR, which affects playback 
after seeking.

The demuxer checks samples against the active seven-byte avcC record and 
converts Annex B NAL units to the declared framing. A packet that already 
contains valid length-prefixed NAL units passes through unchanged, including 
when the first NAL length resembles a start code.

Two synthetic two-frame samples cover both framings. On master the first packet 
of the Annex B sample lacks the key flag. With this change the two inputs 
produce identical timestamps, key flags, packet sizes and CRC32 hashes. Both 
new FATE tests pass locally.

```fate-samples
mov/empty-avcc-annexb.mp4
mov/empty-avcc-length.mp4
```



>From 987f1fa744ac98dbe93507540e389d73dc515cfb Mon Sep 17 00:00:00 2001
From: panboxiaosa <[email protected]>
Date: Mon, 23 Nov 2026 20:07:39 +0800
Subject: [PATCH] avformat/mov: handle Annex B samples with empty avcC

Some MOV files declare four-byte NAL lengths and no parameter sets in
avcC but use Annex B start codes in the samples. The H.264 parser then
reads the start codes as lengths and can clear the key flag on IDRs.

Convert such samples to length-prefixed NAL units before parsing. Check
the declared framing first because a valid length can resemble a start
code. Keep the in-band parameter sets and packet properties.

Test both framings with the same two frames, including a one-byte NAL
unit in the length-prefixed input.

Signed-off-by: panboxiaosa <[email protected]>
---
 libavformat/Makefile                 |  2 +-
 libavformat/mov.c                    | 74 ++++++++++++++++++++++++++++
 tests/fate/mov.mak                   |  6 +++
 tests/ref/fate/mov-empty-avcc-annexb |  2 +
 4 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/mov-empty-avcc-annexb

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 93664e4c15d..322f77545e5 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -399,7 +399,7 @@ OBJS-$(CONFIG_MMF_DEMUXER)               += mmf.o
 OBJS-$(CONFIG_MMF_MUXER)                 += mmf.o rawenc.o
 OBJS-$(CONFIG_MODS_DEMUXER)              += mods.o
 OBJS-$(CONFIG_MOFLEX_DEMUXER)            += moflex.o
-OBJS-$(CONFIG_MOV_DEMUXER)               += mov.o mov_chan.o mov_esds.o \
+OBJS-$(CONFIG_MOV_DEMUXER)               += mov.o mov_chan.o mov_esds.o nal.o \
                                             qtpalette.o replaygain.o 
dovi_isom.o \
                                             dvdclut.o
 OBJS-$(CONFIG_MOV_MUXER)                 += movenc.o \
diff --git a/libavformat/mov.c b/libavformat/mov.c
index f2fb4c77a26..9f5ded8c85f 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -61,6 +61,7 @@
 #include "libavcodec/mpegaudiodecheader.h"
 #include "libavcodec/mlp_parse.h"
 #include "avformat.h"
+#include "nal.h"
 #include "internal.h"
 #include "avio_internal.h"
 #include "demux.h"
@@ -12849,6 +12850,72 @@ static int mov_switch_root(AVFormatContext *s, int64_t 
target, int index)
     return 1;
 }
 
+static int mov_valid_avc_sample(const uint8_t *data, int size)
+{
+    while (size >= 5) {
+        unsigned len = AV_RB32(data);
+        unsigned type = data[4] & 31;
+        if (!len || len > size - 4 || data[4] & 0x80 || !type || type > 23)
+            return 0;
+        data += 4 + len;
+        size -= 4 + len;
+    }
+    return !size;
+}
+
+static int mov_fix_empty_avcc_sample(MOVStreamContext *sc, AVPacket *pkt)
+{
+    int index = sc->last_stsd_index;
+    int size = pkt->size, expected, ret, first = 2;
+    const uint8_t *config;
+    AVIOContext *pb;
+    AVBufferRef *buf;
+    uint8_t *data;
+
+    if (index < 0 || index >= sc->stsd_count || !sc->extradata_size ||
+        sc->extradata_size[index] != 7 || sc->cenc.default_encrypted_sample ||
+        size < 5 || AV_RB16(pkt->data) || pkt->data[2] > 1)
+        return 0;
+    config = sc->extradata[index];
+    if (!config || config[0] != 1 || config[4] != 0xff ||
+        config[5] != 0xe0 || config[6] != 0)
+        return 0;
+
+    /* A four-byte NAL length can itself look like a start code. */
+    if (mov_valid_avc_sample(pkt->data, size))
+        return 0;
+    while (first < size && !pkt->data[first])
+        first++;
+    if (first == size || pkt->data[first] != 1)
+        return 0;
+    if (size > (INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 4 * 3)
+        return 0;
+
+    ret = avio_open_dyn_buf(&pb);
+    if (ret < 0)
+        return ret;
+    expected = ff_nal_parse_units(pb, pkt->data, size);
+    size = avio_close_dyn_buf(pb, &data);
+    if (size != expected) {
+        av_free(data);
+        return AVERROR(ENOMEM);
+    }
+    if (!size || !mov_valid_avc_sample(data, size)) {
+        av_free(data);
+        return 0;
+    }
+    buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE, NULL, 
NULL, 0);
+    if (!buf) {
+        av_free(data);
+        return AVERROR(ENOMEM);
+    }
+    av_buffer_unref(&pkt->buf);
+    pkt->buf  = buf;
+    pkt->data = data;
+    pkt->size = size;
+    return 0;
+}
+
 static int mov_change_extradata(AVStream *st, AVPacket *pkt)
 {
     MOVStreamContext *sc = st->priv_data;
@@ -13204,6 +13271,13 @@ static int mov_read_packet(AVFormatContext *s, 
AVPacket *pkt)
         return ret;
     }
 
+    if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
+        (sc->stsd_count == 1 || (sc->stsc_data && !mov->frag_index.nb_items))) 
{
+        ret = mov_fix_empty_avcc_sample(sc, pkt);
+        if (ret < 0 && av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, 
NULL))
+            sc->last_stsd_index = -1;
+        return ret;
+    }
     return 0;
 }
 
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 96ee5f9f14d..a7a359fdcd4 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -47,6 +47,12 @@ FATE_MOV_FFPROBE-$(call DEMDEC, MOV, HEVC) += 
fate-mov-dovi-hvce-mp4-read
 
 FATE_MOV_FFPROBE-$(call DEMDEC, MOV, H264) += fate-mov-vfr-bframes-duration
 
+FATE_MOV_FFPROBE-$(call ALLYES, MOV_DEMUXER H264_PARSER H264_DECODER 
FILE_PROTOCOL) += fate-mov-empty-avcc-annexb fate-mov-empty-avcc-length
+fate-mov-empty-avcc-%: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -v error 
-show_packets \
+    -show_entries packet=pts,size,flags,data_hash -show_data_hash crc32 -of 
compact \
+    $(TARGET_SAMPLES)/mov/$(@:fate-mov-%=%).mp4
+fate-mov-empty-avcc-length: REF = 
$(SRC_PATH)/tests/ref/fate/mov-empty-avcc-annexb
+
 # vpcC set to 3/3/3, with no colr box
 FATE_MOV_FFPROBE-$(call DEMDEC, MOV, VP9) += fate-mov-vpcc-reserved-color-tags
 fate-mov-vpcc-reserved-color-tags: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -v 
error \
diff --git a/tests/ref/fate/mov-empty-avcc-annexb 
b/tests/ref/fate/mov-empty-avcc-annexb
new file mode 100644
index 00000000000..578a8217269
--- /dev/null
+++ b/tests/ref/fate/mov-empty-avcc-annexb
@@ -0,0 +1,2 @@
+packet|pts=0|size=66|flags=K__|data_hash=CRC32:fc6fe304
+packet|pts=8192|size=13|flags=___|data_hash=CRC32:61d08838
-- 
2.54.1

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

Reply via email to