PR #25978 opened by SomeComputerGuy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25978
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25978.patch
The MPEG-4 encoder started video packets at B-VOP macroblocks that are
not coded because their co-located macroblock is skipped. Such a packet
is a header with no macroblock data. This happens with a small -ps, and
with slice threads even without -ps, because every slice context
started a packet at its first macroblock. The ISO/IEC 14496-5 reference
decoder (vm_dec) aborts on these streams.
1/2 starts the packet at the next coded macroblock instead. With a
fixed quantizer the decoded frames are the same as before.
2/2 adds an encode/decode FATE test (no sample needed): on master its
stream has 8 such packets.
make fate passes with THREADS=1 and THREADS=4 frame. Independent of
#25898 and #25903 (applies on master and on top of #25898 without
conflicts).
This series was developed with Claude (Anthropic) as a coding assistant;
I reviewed and tested it as described above.
>From c21db727bf473cd2c9a1ff604c5fc03cbd890504 Mon Sep 17 00:00:00 2001
From: Arun Iyengar <[email protected]>
Date: Thu, 12 Nov 2026 11:06:42 +0000
Subject: [PATCH 1/2] avcodec/mpegvideo_enc: do not start MPEG-4 video packets
at skipped B-VOP MBs
A B-VOP macroblock whose co-located macroblock in the following
reference VOP is skipped is not coded: nothing at all is sent for it.
Decoders therefore check for a resync marker only in front of the
macroblocks they actually parse; the ISO/IEC 14496-5 reference decoder
does so in decodeBVOP(), and our decoder runs on through such
macroblocks after a resync marker.
The encoder started a video packet at any macroblock once the packet
size was reached, and every slice context started one at its first
macroblock. With a small -ps or with slice threads this produced
video packets made of only skipped macroblocks, i.e. a header with no
data, which the reference decoder rejects (assertion in
decodeMBTextureHeadOfBVOP()) and which hardware decoders following it
misdecode. A 1080p -ps 1 -bf 2 encode had 53124 such packets in 30
frames.
Start the video packet at the next coded macroblock instead, and let a
slice context that only holds skipped B-VOP macroblocks write nothing.
The decoded pictures are unchanged: with a fixed quantizer the output
of the old and the new streams is bit-identical.
Assisted-by: LLM
Signed-off-by: Arun Iyengar <[email protected]>
---
libavcodec/mpegvideo_enc.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index b1606266104..bcac5773a4d 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -3081,10 +3081,22 @@ static int encode_thread(AVCodecContext *c, void *arg){
case AV_CODEC_ID_MJPEG:
if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
break;
+ case AV_CODEC_ID_MPEG4:
+ /* A B-VOP macroblock whose co-located macroblock is
+ * skipped is not coded at all, and decoders look for a
+ * resync marker only in front of coded macroblocks: start
+ * the video packet at the next coded macroblock instead.
*/
+ if (s->c.pict_type == AV_PICTURE_TYPE_B &&
+ s->c.next_pic.mbskip_table[xy])
+ is_gob_start = 0;
+ else if (s->c.start_mb_y && !put_bits_count(&s->pb))
+ is_gob_start = 1;
+ break;
}
if(is_gob_start){
- if (s->c.start_mb_y != mb_y || mb_x != 0) {
+ if ((s->c.start_mb_y != mb_y || mb_x != 0) &&
+ put_bits_count(&s->pb)) {
write_slice_end(s, mb_y - !mb_x);
if (CONFIG_MPEG4_ENCODER && s->c.codec_id ==
AV_CODEC_ID_MPEG4 && s->partitioned_frame)
@@ -3589,7 +3601,9 @@ static int encode_thread(AVCodecContext *c, void *arg){
ff_msmpeg4_encode_ext_header(s);
#endif
- write_slice_end(s, s->c.mb_y);
+ /* A slice of skipped B-VOP macroblocks writes nothing. */
+ if (s->c.codec_id != AV_CODEC_ID_MPEG4 || put_bits_count(&s->pb))
+ write_slice_end(s, s->c.mb_y);
return 0;
}
--
2.54.0
>From b84d3df4f0d059503d3d87ad01d6185327034c5c Mon Sep 17 00:00:00 2001
From: Arun Iyengar <[email protected]>
Date: Thu, 12 Nov 2026 11:54:41 +0000
Subject: [PATCH 2/2] fate/mpeg4: add a test for video packets in B-VOPs with
skipped MBs
Encode testsrc2, which has static areas, with B-VOPs, small video
packets and two slices, so that video packets fall on B-VOP macroblocks
that are not coded because their co-located macroblock is skipped.
Before the previous commit this stream had 8 video packets without any
macroblock data and the ISO/IEC 14496-5 reference decoder aborted on
it. The decoded frames are the same before and after.
Assisted-by: LLM
Signed-off-by: Arun Iyengar <[email protected]>
---
tests/fate/mpeg4.mak | 8 ++++++++
tests/ref/fate/mpeg4-bvop-skipped-packets | 19 +++++++++++++++++++
2 files changed, 27 insertions(+)
create mode 100644 tests/ref/fate/mpeg4-bvop-skipped-packets
diff --git a/tests/fate/mpeg4.mak b/tests/fate/mpeg4.mak
index d0c680bfc1e..619616533ea 100644
--- a/tests/fate/mpeg4.mak
+++ b/tests/fate/mpeg4.mak
@@ -21,6 +21,14 @@ fate-m4v: CMD = framecrc -flags +bitexact -idct simple
-i $(TARGET_SAMPLES)/
FATE_MPEG4-$(call FRAMECRC, M4V, MPEG4, MPEG4VIDEO_PARSER FPS_FILTER) +=
fate-m4v-cfr
fate-m4v-cfr: CMD = framecrc -flags +bitexact -idct simple -i
$(TARGET_SAMPLES)/mpeg4/demo.m4v -vf fps=5
+# Video packets (-ps, slice threads) in B-VOPs with macroblocks that are not
+# coded because their co-located macroblock is skipped.
+FATE_MPEG4_ENC-$(call TRANSCODE, MPEG4, M4V, LAVFI_INDEV
WRAPPED_AVFRAME_DECODER TESTSRC2_FILTER MPEG4VIDEO_PARSER) +=
fate-mpeg4-bvop-skipped-packets
+fate-mpeg4-bvop-skipped-packets: CMD = transcode lavfi
"testsrc2=size=352x288:rate=25" m4v \
+ "-c:v mpeg4 -qscale 5 -bf 2 -ps 20 -threads 2 -slices 2 -frames:v 12"
+FATE_FFMPEG += $(FATE_MPEG4_ENC-yes)
+fate-mpeg4: $(FATE_MPEG4_ENC-yes)
+
# Test seeking in fragmented MP4 with separate audio/video fragments
# Seeks to 1.04s and extracts 1 frame - should land on I-frame at 1.0s with
fix,
# lands at start (0s) without fix due to get_frag_time() bug
diff --git a/tests/ref/fate/mpeg4-bvop-skipped-packets
b/tests/ref/fate/mpeg4-bvop-skipped-packets
new file mode 100644
index 00000000000..a58632666dd
--- /dev/null
+++ b/tests/ref/fate/mpeg4-bvop-skipped-packets
@@ -0,0 +1,19 @@
+851038f4bfeffed8b95d456c0d0ca358
*tests/data/fate/mpeg4-bvop-skipped-packets.m4v
+62119 tests/data/fate/mpeg4-bvop-skipped-packets.m4v
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 352x288
+#sar 0: 1/1
+0, 0, 0, 1, 152064, 0x55a02dc6
+0, 1, 1, 1, 152064, 0x33c77393
+0, 2, 2, 1, 152064, 0x2c95d001
+0, 3, 3, 1, 152064, 0xe73cf090
+0, 4, 4, 1, 152064, 0xf8972d37
+0, 5, 5, 1, 152064, 0x3a3c3afb
+0, 6, 6, 1, 152064, 0x0a1e2bdb
+0, 7, 7, 1, 152064, 0xb5e4249a
+0, 8, 8, 1, 152064, 0xc2691412
+0, 9, 9, 1, 152064, 0x72e60577
+0, 10, 10, 1, 152064, 0x96281afa
+0, 11, 11, 1, 152064, 0x438200ca
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]