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

Three MPEG-4 Part 2 decoder bugs found while checking the Allwinner H616
(cedrus) hardware decoder on 1080p and ISO/IEC 14496-4 conformance
streams, using the ISO/IEC 14496-5 reference decoder (vm_dec) as the
arbiter. All three are in code shared with the encoder's
reconstruction, so some encode tests change their bitstream too.

1/4 h263: a macroblock with four vectors whose third block starts a
    video packet wrote zero into the left macroblock's stored block 3
    vector. B-VOP direct mode reads that vector as the co-located one,
    so direct macroblocks next to packet starts were predicted from a
    zero vector. Changes vsynth*-mpeg4-thread (-ps 200, +mv4, -bf 2)
    and two B-pictures of rv20-1239 (H.263 Annex O direct mode).
2/4 mpegvideo_motion: the 8x8 and 4MV chroma paths clipped reference
    positions to the picture size, the 16x16 path and the edge
    emulation to the macroblock aligned size. Pictures with a height
    or width that is not a multiple of 16 (1080p, 400x300) mispredicted
    edge blocks, reaching visible samples later. Changes
    mpeg4-resolution-change-* (400x300 frames only) and
    vsynth3-mpeg4-qpel (35x35; encoder decisions only).
3/4 adds an encode/decode FATE test for interlaced quarter sample field
    prediction (no sample needed); 4/4 fixes it: the 16x8 field block
    was predicted as two 8x8 blocks, which mirror the reference in the
    middle of the block (14496-2 7.6.2.2). This is the existing
    "FIXME boundary mirroring is not exactly correct here".

Numbers against vm_dec are in the commit messages (1080p streams,
conformance streams a1ge2/a1ge4/a1ge9, the FATE resize sample). B-VOP
chroma is not compared: vm_dec differs there from FFmpeg (with and
without these patches) and from the H616 hardware on every FFmpeg
encoded B-VOP stream tried.

Independent of #25898 (applies on master and on top of it without
conflicts, either order). With both applied, a1ge4_asp matches vm_dec
(0 macroblocks above 4); without #25898 its field direct B-VOPs still
differ.

make fate passes with THREADS=1 and THREADS=4 frame (and THREADS=4
slice), alone and on top of #25898.



>From 1ccbabebfe93228c1470d32ecfeaaf62430b333b Mon Sep 17 00:00:00 2001
From: Arun Iyengar <[email protected]>
Date: Tue, 10 Nov 2026 04:54:46 +0000
Subject: [PATCH 1/4] avcodec/h263: keep the stored motion vector at a video
 packet start

When the third block of a macroblock with four motion vectors starts a
video packet (or GOB/slice) on its first line, ff_h263_pred_motion()
cleared the left candidate by writing zero into the vector that the
left macroblock, in the previous packet, had stored for its last block.
The comment above the special case already says that the stored vectors
must not be changed because B-pictures need them: direct mode reads them
as the co-located vectors (MPEG-4 B-VOPs, and H.263 Annex O B-pictures
as used by RV20). Every direct macroblock whose co-located macroblock
was such a left neighbour was predicted from a zero vector instead of
the decoded one, in the decoder and in the encoder's reconstruction.

Use a zero predictor candidate instead.

Checked against the ISO/IEC 14496-5 reference decoder (vm_dec), luma
macroblocks with max|diff| > 4, -threads 1, on 1920x1080 B-VOP streams
encoded by FFmpeg (testsrc2 + noise, mpeg4 -g 15 -flags +mv4 -bf 2):
  -ps 1 -q:v 2, 30 frames:      48 -> 1 (fixed by the next commit)
  -ps 300 -b:v 20M, 30 frames:  26 -> 0
  -ps 1 -b:v 20M, 9 frames:      9 -> 0 (vm_dec aborts in frame 10)
and on the vsynth2 and vsynth_lena mpeg4-thread bitstreams as encoded
with this patch: 21 -> 0 and 13 -> 0. B-VOP chroma is left out: on every
FFmpeg encoded B-VOP stream tried, with or without four motion vectors,
vm_dec differs there from FFmpeg before and after this patch, while the
Allwinner H616 hardware decoder matches the patched FFmpeg on the 1080p
streams above within the IDCT tolerance (max|diff| 2-3). The 30 ISO/IEC
14496-4 Advanced Simple profile streams do not hit this case and decode
unchanged.

FATE: vsynth*-mpeg4-thread (-ps 200, +mv4, -bf 2) and rv20-1239 change.
For the vsynth tests the bitstream changes too, because the encoder
chooses B-frame modes from its own reconstruction. rv20-1239 changes in
one small area of each of two B-pictures (71 and 86); no RealVideo
reference decoder was available to check it, but direct mode there now
uses the decoded vector as H.263 Annex O specifies.

Assisted-by: LLM
Signed-off-by: Arun Iyengar <[email protected]>
---
 libavcodec/h263.c                         |  6 +++--
 tests/ref/fate/rv20-1239                  |  4 ++--
 tests/ref/seek/vsynth_lena-mpeg4-thread   | 28 +++++++++++------------
 tests/ref/vsynth/vsynth1-mpeg4-thread     |  6 ++---
 tests/ref/vsynth/vsynth2-mpeg4-thread     |  4 ++--
 tests/ref/vsynth/vsynth_lena-mpeg4-thread |  8 +++----
 6 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/libavcodec/h263.c b/libavcodec/h263.c
index 61a8fa96b46..d074b7c7f37 100644
--- a/libavcodec/h263.c
+++ b/libavcodec/h263.c
@@ -183,7 +183,8 @@ int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, 
int dir,
                              int *px, int *py)
 {
     int wrap;
-    int16_t *A, *B, *C, (*mot_val)[2];
+    const int16_t *A, *B, *C;
+    int16_t (*mot_val)[2];
     static const int off[4]= {2, 1, 1, -1};
 
     wrap = s->b8_stride;
@@ -220,10 +221,11 @@ int16_t *ff_h263_pred_motion(MpegEncContext * s, int 
block, int dir,
                 *py = A[1];
             }
         }else{ /* block==2*/
+            static const int16_t zero[2] = { 0, 0 };
             B = mot_val[ - wrap];
             C = mot_val[off[block] - wrap];
             if(s->mb_x == s->resync_mb_x) //rare
-                A[0]=A[1]=0;
+                A = zero;
 
             *px = mid_pred(A[0], B[0], C[0]);
             *py = mid_pred(A[1], B[1], C[1]);
diff --git a/tests/ref/fate/rv20-1239 b/tests/ref/fate/rv20-1239
index 3be1b8bc66d..a5cba8db3df 100644
--- a/tests/ref/fate/rv20-1239
+++ b/tests/ref/fate/rv20-1239
@@ -74,7 +74,7 @@
 0,         68,         68,        1,    28800, 0x8ccf3a60
 0,         69,         69,        1,    28800, 0x4b1b4623
 0,         70,         70,        1,    28800, 0xc1da9fc6
-0,         71,         71,        1,    28800, 0xb9bc1eed
+0,         71,         71,        1,    28800, 0x5b4f1eef
 0,         72,         72,        1,    28800, 0x50f7d1eb
 0,         73,         73,        1,    28800, 0xf8c53f59
 0,         74,         74,        1,    28800, 0x950e3515
@@ -89,7 +89,7 @@
 0,         83,         83,        1,    28800, 0x384d48ac
 0,         84,         84,        1,    28800, 0xf17e18f7
 0,         85,         85,        1,    28800, 0xa9e00e3f
-0,         86,         86,        1,    28800, 0x5d110bb3
+0,         86,         86,        1,    28800, 0x2f880bd3
 0,         87,         87,        1,    28800, 0x337d3e31
 0,         88,         88,        1,    28800, 0xfea3737b
 0,         89,         89,        1,    28800, 0xce7d6020
diff --git a/tests/ref/seek/vsynth_lena-mpeg4-thread 
b/tests/ref/seek/vsynth_lena-mpeg4-thread
index 5561ce44d42..d62db04d151 100644
--- a/tests/ref/seek/vsynth_lena-mpeg4-thread
+++ b/tests/ref/seek/vsynth_lena-mpeg4-thread
@@ -2,45 +2,45 @@ ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos: 
  5652 size: 14805
 ret: 0         st:-1 flags:0  ts:-1.000000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:   5652 size: 
14805
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227424 size: 
16320
+ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227398 size: 
16361
 ret: 0         st: 0 flags:0  ts: 0.800000
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161074 size: 
16517
+ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161026 size: 
16522
 ret:-1         st: 0 flags:1  ts:-0.320000
 ret:-1         st:-1 flags:0  ts: 2.576668
 ret: 0         st:-1 flags:1  ts: 1.470835
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 194454 size: 
16155
+ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 194432 size: 
16162
 ret: 0         st: 0 flags:0  ts: 0.360000
-ret: 0         st: 0 flags:1 dts: 0.400000 pts: NOPTS    pos:  96980 size: 
33032
+ret: 0         st: 0 flags:1 dts: 0.400000 pts: NOPTS    pos:  96950 size: 
33032
 ret:-1         st: 0 flags:1  ts:-0.760000
 ret:-1         st:-1 flags:0  ts: 2.153336
 ret: 0         st:-1 flags:1  ts: 1.047503
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161074 size: 
16517
+ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161026 size: 
16522
 ret: 0         st: 0 flags:0  ts:-0.040000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:   5652 size: 
14805
 ret: 0         st: 0 flags:1  ts: 2.840000
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227424 size: 
16320
+ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227398 size: 
16361
 ret: 0         st:-1 flags:0  ts: 1.730004
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227424 size: 
16320
+ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227398 size: 
16361
 ret: 0         st:-1 flags:1  ts: 0.624171
-ret: 0         st: 0 flags:1 dts: 0.400000 pts: NOPTS    pos:  96980 size: 
33032
+ret: 0         st: 0 flags:1 dts: 0.400000 pts: NOPTS    pos:  96950 size: 
33032
 ret: 0         st: 0 flags:0  ts:-0.480000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:   5652 size: 
14805
 ret: 0         st: 0 flags:1  ts: 2.400000
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227424 size: 
16320
+ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227398 size: 
16361
 ret: 0         st:-1 flags:0  ts: 1.306672
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 194454 size: 
16155
+ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 194432 size: 
16162
 ret: 0         st:-1 flags:1  ts: 0.200839
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:   5652 size: 
14805
 ret: 0         st: 0 flags:0  ts:-0.920000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:   5652 size: 
14805
 ret: 0         st: 0 flags:1  ts: 2.000000
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227424 size: 
16320
+ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 227398 size: 
16361
 ret: 0         st:-1 flags:0  ts: 0.883340
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161074 size: 
16517
+ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161026 size: 
16522
 ret:-1         st:-1 flags:1  ts:-0.222493
 ret:-1         st: 0 flags:0  ts: 2.680000
 ret: 0         st: 0 flags:1  ts: 1.560000
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 194454 size: 
16155
+ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 194432 size: 
16162
 ret: 0         st:-1 flags:0  ts: 0.460008
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161074 size: 
16517
+ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 161026 size: 
16522
 ret:-1         st:-1 flags:1  ts:-0.645825
diff --git a/tests/ref/vsynth/vsynth1-mpeg4-thread 
b/tests/ref/vsynth/vsynth1-mpeg4-thread
index 511c3ce6373..6de83941ff9 100644
--- a/tests/ref/vsynth/vsynth1-mpeg4-thread
+++ b/tests/ref/vsynth/vsynth1-mpeg4-thread
@@ -1,4 +1,4 @@
-308dc848b4dcd6efe69d686f7ef7d10a *tests/data/fate/vsynth1-mpeg4-thread.avi
-754568 tests/data/fate/vsynth1-mpeg4-thread.avi
-609e79f7e905420b147fd8d18ff523db 
*tests/data/fate/vsynth1-mpeg4-thread.out.rawvideo
+2ddc51668eaf4a9c35411ca80fc02643 *tests/data/fate/vsynth1-mpeg4-thread.avi
+754552 tests/data/fate/vsynth1-mpeg4-thread.avi
+c174aea8f8173a92908f9a7c340634a3 
*tests/data/fate/vsynth1-mpeg4-thread.out.rawvideo
 stddev:   10.25 PSNR: 27.91 MAXDIFF:  170 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth2-mpeg4-thread 
b/tests/ref/vsynth/vsynth2-mpeg4-thread
index f4a835814f0..56724787857 100644
--- a/tests/ref/vsynth/vsynth2-mpeg4-thread
+++ b/tests/ref/vsynth/vsynth2-mpeg4-thread
@@ -1,4 +1,4 @@
-4f432bc95ae8e73d337e31fcee0e0a64 *tests/data/fate/vsynth2-mpeg4-thread.avi
+c5ac9eadf34f4a8e8517400d20d67a75 *tests/data/fate/vsynth2-mpeg4-thread.avi
 267756 tests/data/fate/vsynth2-mpeg4-thread.avi
-834596a98212d4cbb381e29c91d78a2c 
*tests/data/fate/vsynth2-mpeg4-thread.out.rawvideo
+b4a71fabc2c5ff85b5a8618cf90c3493 
*tests/data/fate/vsynth2-mpeg4-thread.out.rawvideo
 stddev:    4.89 PSNR: 34.33 MAXDIFF:   86 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth_lena-mpeg4-thread 
b/tests/ref/vsynth/vsynth_lena-mpeg4-thread
index b40eeff8264..a14006d0c06 100644
--- a/tests/ref/vsynth/vsynth_lena-mpeg4-thread
+++ b/tests/ref/vsynth/vsynth_lena-mpeg4-thread
@@ -1,4 +1,4 @@
-9c2826d4bf5c7d2be6cc86790ba0c20a *tests/data/fate/vsynth_lena-mpeg4-thread.avi
-249342 tests/data/fate/vsynth_lena-mpeg4-thread.avi
-2c7384a4c8460c6145fde22967746415 
*tests/data/fate/vsynth_lena-mpeg4-thread.out.rawvideo
-stddev:    3.69 PSNR: 36.78 MAXDIFF:   65 bytes:  7603200/  7603200
+67f7c18660ed06b83f40c211aa5df822 *tests/data/fate/vsynth_lena-mpeg4-thread.avi
+249372 tests/data/fate/vsynth_lena-mpeg4-thread.avi
+faeb6a252b7bbb7edca03891bef77d3a 
*tests/data/fate/vsynth_lena-mpeg4-thread.out.rawvideo
+stddev:    3.69 PSNR: 36.79 MAXDIFF:   65 bytes:  7603200/  7603200
-- 
2.54.0


>From 8df16a1dec71c7e02ba3e34f23cc1c8a37f0322c Mon Sep 17 00:00:00 2001
From: Arun Iyengar <[email protected]>
Date: Tue, 10 Nov 2026 05:03:01 +0000
Subject: [PATCH 2/4] avcodec/mpegvideo_motion: clip 4MV references to the edge
 positions

hpel_motion(), chroma_4mv_motion() and the quarter sample 8x8 path of
apply_8x8() clip the reference position to the picture width and
height, while the edge emulation, and the 16x16 paths, pad from
h_edge_pos and v_edge_pos, which are the macroblock aligned size. For a
picture size that is not a multiple of 16 (1080, 300), the lower or
right blocks of the last macroblock row or column lie beyond the
picture size; a vector pointing further out was clipped to the picture
size and lost its half sample component, so those blocks were predicted
from other samples than the padded reference that the 16x16 path and
other decoders use. The difference starts outside the visible picture
but reaches visible samples through later vectors pointing back in.

Clip to the edge positions, beyond which every sample is padding.

Checked against the ISO/IEC 14496-5 reference decoder (vm_dec), luma
macroblocks with max|diff| > 4 (max|diff|), -threads 1:
  1920x1080 P-only FFmpeg stream (mpeg4 -g 15 -flags +mv4 -ps 1
  -b:v 20M, 30 frames):                      25 (16) -> 0 (3)
  1920x1080 B-VOP stream -ps 1 -q:v 2 (see the previous commit):
                                              1 -> 0
  400x300 part of the FATE sample mpeg4/resize_down-up.h263
  (50 frames):                                4 (10) -> 0 (3)
The Allwinner H616 hardware decoder, whose reference boundary is the
macroblock aligned size, matches the patched FFmpeg on these 1080p
streams within the IDCT tolerance. The ISO/IEC 14496-4 Advanced Simple
profile streams have macroblock aligned sizes and decode unchanged.

FATE: mpeg4-resolution-change-* change in the 400x300 frames only, as
measured above. vsynth3-mpeg4-qpel (35x35) changes because the encoder
reconstruction of its edge blocks changes, and with it the encoder's
decisions; each bitstream (old and new) decodes to the same visible
output with and without this patch.

Assisted-by: LLM
Signed-off-by: Arun Iyengar <[email protected]>
---
 libavcodec/mpegvideo_motion.c                 | 24 ++++----
 .../fate/mpeg4-resolution-change-down-down    | 16 ++---
 .../ref/fate/mpeg4-resolution-change-down-up  | 16 ++---
 .../ref/fate/mpeg4-resolution-change-up-down  | 60 +++++++++----------
 tests/ref/fate/mpeg4-resolution-change-up-up  | 16 ++---
 tests/ref/vsynth/vsynth3-mpeg4-qpel           |  6 +-
 6 files changed, 69 insertions(+), 69 deletions(-)

diff --git a/libavcodec/mpegvideo_motion.c b/libavcodec/mpegvideo_motion.c
index 696cc4980da..67b9b614b2b 100644
--- a/libavcodec/mpegvideo_motion.c
+++ b/libavcodec/mpegvideo_motion.c
@@ -49,11 +49,11 @@ static inline int hpel_motion(MpegEncContext *s,
     src_y += motion_y >> 1;
 
     /* WARNING: do no forget half pels */
-    src_x = av_clip(src_x, -16, s->width); // FIXME unneeded for emu?
-    if (src_x != s->width)
+    src_x = av_clip(src_x, -16, s->h_edge_pos); // FIXME unneeded for emu?
+    if (src_x != s->h_edge_pos)
         dxy |= motion_x & 1;
-    src_y = av_clip(src_y, -16, s->height);
-    if (src_y != s->height)
+    src_y = av_clip(src_y, -16, s->v_edge_pos);
+    if (src_y != s->v_edge_pos)
         dxy |= (motion_y & 1) << 1;
     src += src_y * s->linesize + src_x;
 
@@ -462,11 +462,11 @@ static void chroma_4mv_motion(MpegEncContext *s,
 
     src_x = s->mb_x * 8 + mx;
     src_y = s->mb_y * 8 + my;
-    src_x = av_clip(src_x, -8, (s->width >> 1));
-    if (src_x == (s->width >> 1))
+    src_x = av_clip(src_x, -8, (s->h_edge_pos >> 1));
+    if (src_x == (s->h_edge_pos >> 1))
         dxy &= ~1;
-    src_y = av_clip(src_y, -8, (s->height >> 1));
-    if (src_y == (s->height >> 1))
+    src_y = av_clip(src_y, -8, (s->v_edge_pos >> 1));
+    if (src_y == (s->v_edge_pos >> 1))
         dxy &= ~2;
 
     offset = src_y * s->uvlinesize + src_x;
@@ -622,11 +622,11 @@ static inline void apply_8x8(MpegEncContext *s,
             src_y = mb_y * 16 + (motion_y >> 2) + (i >> 1) * 8;
 
             /* WARNING: do no forget half pels */
-            src_x = av_clip(src_x, -16, s->width);
-            if (src_x == s->width)
+            src_x = av_clip(src_x, -16, s->h_edge_pos);
+            if (src_x == s->h_edge_pos)
                 dxy &= ~3;
-            src_y = av_clip(src_y, -16, s->height);
-            if (src_y == s->height)
+            src_y = av_clip(src_y, -16, s->v_edge_pos);
+            if (src_y == s->v_edge_pos)
                 dxy &= ~12;
 
             ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
diff --git a/tests/ref/fate/mpeg4-resolution-change-down-down 
b/tests/ref/fate/mpeg4-resolution-change-down-down
index 60cb4c11370..e6cfdd5fc9f 100644
--- a/tests/ref/fate/mpeg4-resolution-change-down-down
+++ b/tests/ref/fate/mpeg4-resolution-change-down-down
@@ -79,8 +79,8 @@
 0,         69,         69,        1,   460800, 766e386ffb13da751d050473a0fa1b96
 0,         70,         70,        1,   460800, 8e16d03d200e77aa27de07d29f5ea815
 0,         71,         71,        1,   460800, a5a4a596decb5d03e4e4e205e415b9c5
-0,         72,         72,        1,   460800, 88e3508a4a813cbc7efb0d05836a649e
-0,         73,         73,        1,   460800, 6f069883279d0a8d3900a38e31b0cc15
+0,         72,         72,        1,   460800, 79addbe6793d1de53de8630424c3f53b
+0,         73,         73,        1,   460800, 49d7698531f012e32a3fb973fec323ee
 0,         74,         74,        1,   460800, 35396b104783d643d419bd60639d8ab6
 0,         75,         75,        1,   460800, 3419184146d5c9609f4195cd4e15b03e
 0,         76,         76,        1,   460800, 0d50c625929ca57039a8b2fab8531293
@@ -96,17 +96,17 @@
 0,         86,         86,        1,   460800, 59581e77b743b8bada11d877496b19a5
 0,         87,         87,        1,   460800, f7af9a65ccc0d657cb6d4d4427fd0b94
 0,         88,         88,        1,   460800, b3da8f0651f4277b3b27235d32ca961e
-0,         89,         89,        1,   460800, 0beb4e69d0150ccbf48ebc8e0da3d3b0
-0,         90,         90,        1,   460800, c1172a6e0ac2241f8873ffc7f589572f
-0,         91,         91,        1,   460800, 9a70fa5ea60c07350faeb54f9f09e9c4
-0,         92,         92,        1,   460800, 9c3680dbb25c36f1ee83bd304459e1f2
-0,         93,         93,        1,   460800, 1a8ae6e12a6599dc98a1314233dabecc
+0,         89,         89,        1,   460800, 74061392412fafb4b7317bbfea26e001
+0,         90,         90,        1,   460800, 577808d70e7f086f9fbfa3f168c89af0
+0,         91,         91,        1,   460800, aa2281a40d5cbea1618c385e74b71ecd
+0,         92,         92,        1,   460800, c3c2216236af9a44f97bcd85ede327e5
+0,         93,         93,        1,   460800, 11109c2f33f248436329d30b87cff64b
 0,         94,         94,        1,   460800, 43900b75016dd7964fbbadc94543d100
 0,         95,         95,        1,   460800, 01100cb1b7d05feabcc54e99f6d9a3b7
 0,         96,         96,        1,   460800, 68fd71a18081a5599698a5403e9912a5
 0,         97,         97,        1,   460800, c454879f15639f9e3178cfe320989b58
 0,         98,         98,        1,   460800, ad1713c21bc6e9b6cb35b7a44b6af456
-0,         99,         99,        1,   460800, 08b5f5cdc2cb17f6b218d9ae4c266aa1
+0,         99,         99,        1,   460800, 61416559af41b530beae156b4b7bc794
 0,        100,        100,        1,   460800, 00d7e717e8bafa20b6eea717b66761d1
 0,        101,        101,        1,   460800, 109e73772fea7ad604ed18a8779190be
 0,        102,        102,        1,   460800, b34a3f0d81302d566a949f266ff6182c
diff --git a/tests/ref/fate/mpeg4-resolution-change-down-up 
b/tests/ref/fate/mpeg4-resolution-change-down-up
index 6d0ad045796..92bcc6d8569 100644
--- a/tests/ref/fate/mpeg4-resolution-change-down-up
+++ b/tests/ref/fate/mpeg4-resolution-change-down-up
@@ -129,8 +129,8 @@
 0,        119,        119,        1,   460800, 766e386ffb13da751d050473a0fa1b96
 0,        120,        120,        1,   460800, 8e16d03d200e77aa27de07d29f5ea815
 0,        121,        121,        1,   460800, a5a4a596decb5d03e4e4e205e415b9c5
-0,        122,        122,        1,   460800, 88e3508a4a813cbc7efb0d05836a649e
-0,        123,        123,        1,   460800, 6f069883279d0a8d3900a38e31b0cc15
+0,        122,        122,        1,   460800, 79addbe6793d1de53de8630424c3f53b
+0,        123,        123,        1,   460800, 49d7698531f012e32a3fb973fec323ee
 0,        124,        124,        1,   460800, 35396b104783d643d419bd60639d8ab6
 0,        125,        125,        1,   460800, 3419184146d5c9609f4195cd4e15b03e
 0,        126,        126,        1,   460800, 0d50c625929ca57039a8b2fab8531293
@@ -146,14 +146,14 @@
 0,        136,        136,        1,   460800, 59581e77b743b8bada11d877496b19a5
 0,        137,        137,        1,   460800, f7af9a65ccc0d657cb6d4d4427fd0b94
 0,        138,        138,        1,   460800, b3da8f0651f4277b3b27235d32ca961e
-0,        139,        139,        1,   460800, 0beb4e69d0150ccbf48ebc8e0da3d3b0
-0,        140,        140,        1,   460800, c1172a6e0ac2241f8873ffc7f589572f
-0,        141,        141,        1,   460800, 9a70fa5ea60c07350faeb54f9f09e9c4
-0,        142,        142,        1,   460800, 9c3680dbb25c36f1ee83bd304459e1f2
-0,        143,        143,        1,   460800, 1a8ae6e12a6599dc98a1314233dabecc
+0,        139,        139,        1,   460800, 74061392412fafb4b7317bbfea26e001
+0,        140,        140,        1,   460800, 577808d70e7f086f9fbfa3f168c89af0
+0,        141,        141,        1,   460800, aa2281a40d5cbea1618c385e74b71ecd
+0,        142,        142,        1,   460800, c3c2216236af9a44f97bcd85ede327e5
+0,        143,        143,        1,   460800, 11109c2f33f248436329d30b87cff64b
 0,        144,        144,        1,   460800, 43900b75016dd7964fbbadc94543d100
 0,        145,        145,        1,   460800, 01100cb1b7d05feabcc54e99f6d9a3b7
 0,        146,        146,        1,   460800, 68fd71a18081a5599698a5403e9912a5
 0,        147,        147,        1,   460800, c454879f15639f9e3178cfe320989b58
 0,        148,        148,        1,   460800, ad1713c21bc6e9b6cb35b7a44b6af456
-0,        149,        149,        1,   460800, 08b5f5cdc2cb17f6b218d9ae4c266aa1
+0,        149,        149,        1,   460800, 61416559af41b530beae156b4b7bc794
diff --git a/tests/ref/fate/mpeg4-resolution-change-up-down 
b/tests/ref/fate/mpeg4-resolution-change-up-down
index 432569712a8..a40f20e4b0d 100644
--- a/tests/ref/fate/mpeg4-resolution-change-up-down
+++ b/tests/ref/fate/mpeg4-resolution-change-up-down
@@ -8,16 +8,16 @@
 #sar 0: 1/1
 #stream#, dts,        pts, duration,     size, hash
 0,          0,          0,        1,   180000, 9fc6302026cf2a2dd310646b83c5dfa1
-0,          1,          1,        1,   180000, b1b2646c8df579ddf8676bc2488411a5
-0,          2,          2,        1,   180000, 5aca8cdf4914a96577cffbbc18508043
-0,          3,          3,        1,   180000, d5bec8091a32a6669e76b8ab8f82db1f
-0,          4,          4,        1,   180000, d8e8543d19d0d71959321ffcebf820ad
-0,          5,          5,        1,   180000, 90520f273b47e423ae8f519e17b9994b
-0,          6,          6,        1,   180000, 3f6b9fc1fc30804b98a16264b944ff55
-0,          7,          7,        1,   180000, 3cd3b36f6ee370c27ef86c6cd9fc7c1d
-0,          8,          8,        1,   180000, 44f44f2ac0c9160d3e8ae184c7e4caa9
-0,          9,          9,        1,   180000, c7c96bf89dd79249755b9ae0af6a259e
-0,         10,         10,        1,   180000, 8ba3f9677fb07be37b0964cf847cb538
+0,          1,          1,        1,   180000, 847ff99720850856a57d5fad3653cab6
+0,          2,          2,        1,   180000, 7ec9df622fb6c1726294eac9a4cc61b0
+0,          3,          3,        1,   180000, 7e474e3e7adccc7399a54a4f7674d628
+0,          4,          4,        1,   180000, 1cb89422a835780f465e3a6c560593b9
+0,          5,          5,        1,   180000, 1fd6e70411d4d4506cc7b9954ec093fe
+0,          6,          6,        1,   180000, 3bda9f1b147969297acf29b039574d50
+0,          7,          7,        1,   180000, 5080d36d6378e5b97ae01033138fab38
+0,          8,          8,        1,   180000, 526a0a9e19f2dbc1b914660f39d8eec2
+0,          9,          9,        1,   180000, 43fdbeb99203af00e48cb045762aa03c
+0,         10,         10,        1,   180000, fdc714194431bff135dd8e55966d120f
 0,         11,         11,        1,   180000, 59614cce923cf836395b7628dddaba06
 0,         12,         12,        1,   180000, 6497b597654c9c90e290feec2f8b11c8
 0,         13,         13,        1,   180000, df2774f8219a29629bbda3c368ca3046
@@ -29,32 +29,32 @@
 0,         19,         19,        1,   180000, 960dcbf47536653087afc917a99f97b4
 0,         20,         20,        1,   180000, 05f7ba88fd8a73416cfab0427db30c5f
 0,         21,         21,        1,   180000, 51809dcb798fc5cc45066faf56f6b533
-0,         22,         22,        1,   180000, b637cc799280b57a770d8c3417f5a9f3
-0,         23,         23,        1,   180000, 900632153e8a06208550d62c7f327e06
+0,         22,         22,        1,   180000, 9c205c71355b9b924f745b7035c8c344
+0,         23,         23,        1,   180000, 6e849f4edcf3784525d2b39361a182b4
 0,         24,         24,        1,   180000, fcbce1d8ee2a284e7a8a2923d878ca1a
 0,         25,         25,        1,   180000, ac64fdfeef02c97bb7ee3bd8d42cda4a
-0,         26,         26,        1,   180000, 528b693654f55eac5793222591fc32ca
-0,         27,         27,        1,   180000, d2d23bf508c6ab797cfa9093824e7271
-0,         28,         28,        1,   180000, 249d71065ba6170ee82fffeb84489a57
-0,         29,         29,        1,   180000, ab8a7ac5bdaeb22a4cc5834ff4dbc838
-0,         30,         30,        1,   180000, bc9a0da67f7dd812e4dc29adab36c79c
-0,         31,         31,        1,   180000, 4fd1c8d536616622d2acf5313ffad839
-0,         32,         32,        1,   180000, 32241cbbf06fc238ac8f852db8004788
-0,         33,         33,        1,   180000, 99cdd2026fae42d0f5de666596073c23
-0,         34,         34,        1,   180000, 9fc705da6bdf42c7f41b858185851816
-0,         35,         35,        1,   180000, 8acbd773806f2933aaadb85b4a693390
+0,         26,         26,        1,   180000, 21deb25d5b061308d3edc8154e5a3cc6
+0,         27,         27,        1,   180000, a3e193755a44e2578f0b32835c68b0dd
+0,         28,         28,        1,   180000, 283cdccdbbeeac8effedd7752d22a256
+0,         29,         29,        1,   180000, 5397e2a45c487eda64eda8ca14519d29
+0,         30,         30,        1,   180000, 6e4cee7d6708a221323e32cddc6bb398
+0,         31,         31,        1,   180000, 7b7eec392dec4383dac7abb375fd9030
+0,         32,         32,        1,   180000, 7731a0617aab4214f031cf02a21725e7
+0,         33,         33,        1,   180000, b22b8ae7a1fe26f3f539a013da1ae33d
+0,         34,         34,        1,   180000, 3781bc0cdfd24a33be6c2ad01ac3c7d3
+0,         35,         35,        1,   180000, 0190cbc19b4042b42095be7a9ad93eea
 0,         36,         36,        1,   180000, 781045116f6e6576cd53453fada887d4
 0,         37,         37,        1,   180000, 81a53875f718c706de28881c340e79a1
 0,         38,         38,        1,   180000, d183354aa17b8d65d3583264ae81bd54
 0,         39,         39,        1,   180000, 42dc484e20e9779405b205d8aa879373
-0,         40,         40,        1,   180000, d2034292cfb3a3a61b815a6a434d8f01
-0,         41,         41,        1,   180000, 625bde305548017781ed1f5bc5cb8cc4
-0,         42,         42,        1,   180000, 260544b15d99fff9e5782e30a165e5bf
-0,         43,         43,        1,   180000, 7ba5b56979a3ec49e407f7fd51912dc4
-0,         44,         44,        1,   180000, 78e40b052b8773631a014692c667590d
-0,         45,         45,        1,   180000, a79266a1d52b21594467f1efde654e3d
-0,         46,         46,        1,   180000, 33f00d3f9dd037ab9cf4dcf8a68ab013
-0,         47,         47,        1,   180000, 91d7f5d53a0d5f37c2cf485e73db66bb
+0,         40,         40,        1,   180000, 5c11740829b2e826b7deeea599623437
+0,         41,         41,        1,   180000, f344489d9bdfbd058875f175970d1e33
+0,         42,         42,        1,   180000, 056b0700e421f1efd8583c02338e3cfc
+0,         43,         43,        1,   180000, a1f1a11646549ddb0b427ad04899c995
+0,         44,         44,        1,   180000, 250fb669c897a70f3934920ad3cbebce
+0,         45,         45,        1,   180000, 52b0542a5b1af10ea553520a5c8d4733
+0,         46,         46,        1,   180000, 18b4685273d0d5a0673b0388f966d3ae
+0,         47,         47,        1,   180000, 4ec3a392df635764b7adc22b8bda07fe
 0,         48,         48,        1,   180000, 74463929bc9e7e56bd8b2f9cc194e0e9
 0,         49,         49,        1,   180000, 14c4f3cead9eda56e96dbaedce61e4c8
 0,         50,         50,        1,   180000, 33f305cb19059fff989aae29dda951be
diff --git a/tests/ref/fate/mpeg4-resolution-change-up-up 
b/tests/ref/fate/mpeg4-resolution-change-up-up
index 52845df0602..0910089a1bf 100644
--- a/tests/ref/fate/mpeg4-resolution-change-up-up
+++ b/tests/ref/fate/mpeg4-resolution-change-up-up
@@ -79,8 +79,8 @@
 0,         69,         69,        1,   152064, 263e4c6ed69b45a9be2606a5912a3ca5
 0,         70,         70,        1,   152064, 5b796d9c415f3c3aa9631517714688bf
 0,         71,         71,        1,   152064, a335aeeed0205a092bed9eae3d4c9106
-0,         72,         72,        1,   152064, cbdcbc84551d0b66bbce2e9044043766
-0,         73,         73,        1,   152064, 5691dd0167802ff2b1a1c6e1d147ae90
+0,         72,         72,        1,   152064, c7b50934d789b646a7e3b5fc5f2598fd
+0,         73,         73,        1,   152064, c9744c2163577f487215be8a518b2a3e
 0,         74,         74,        1,   152064, 520229693218b0f2d31a885014253641
 0,         75,         75,        1,   152064, 685c55eb7659d593b16669de73b68807
 0,         76,         76,        1,   152064, f8bf6dd5944a4df224bdff15d526ec77
@@ -96,17 +96,17 @@
 0,         86,         86,        1,   152064, fe9e1da8fb02978fdb4319d59d472f9d
 0,         87,         87,        1,   152064, 2231456cf6c5ee96b8519c25acc24ef3
 0,         88,         88,        1,   152064, 20c1b2a50871fe0141bfe3ddb6e1bf63
-0,         89,         89,        1,   152064, fce76e1924b17c36d6f01861908d3673
-0,         90,         90,        1,   152064, fa402801b810c25b7be688a3ea908fd8
-0,         91,         91,        1,   152064, aba8c63972d3f4f3070c8afa7003fe61
-0,         92,         92,        1,   152064, 3ef1103ffec41435357ddb999d0d9a59
-0,         93,         93,        1,   152064, 775182be3e9c8d8a4c986b3d55b30364
+0,         89,         89,        1,   152064, 572bf9d287e4bb25f34857ea9e4daf0f
+0,         90,         90,        1,   152064, bbe951d47641bbc1b3ba2bcc5dcafdc7
+0,         91,         91,        1,   152064, f2db49ee93d664de7562edd3089b91e6
+0,         92,         92,        1,   152064, 2fe689376fbe219dd490eb397ac806ea
+0,         93,         93,        1,   152064, 0b926a1b6ab07d8d87b35e1b6292ab12
 0,         94,         94,        1,   152064, f3f6dd075d1cd55ba0cee9a52cb95900
 0,         95,         95,        1,   152064, 7889063018605fb6075bdddf2a4e621a
 0,         96,         96,        1,   152064, d58a385b4e9a8396edc63d2a3c8dc834
 0,         97,         97,        1,   152064, 09184b913f12643465c2cc9c95c3a9e7
 0,         98,         98,        1,   152064, 9c4d9bb57e09ddd5981e80c739706c6c
-0,         99,         99,        1,   152064, a271f597bb3a78e1b1e39045da86cbae
+0,         99,         99,        1,   152064, d99a7e7f3f1645ff251a0f0c118cf33e
 0,        100,        100,        1,   152064, 01ac9ab6aa0b627dabb545b6e9c15c16
 0,        101,        101,        1,   152064, 9b1a098e5ce1813e5655162c45daaf3d
 0,        102,        102,        1,   152064, fdd1e9f12df4af24b334495ef90a2712
diff --git a/tests/ref/vsynth/vsynth3-mpeg4-qpel 
b/tests/ref/vsynth/vsynth3-mpeg4-qpel
index 723edd9feb1..f4be48cbc36 100644
--- a/tests/ref/vsynth/vsynth3-mpeg4-qpel
+++ b/tests/ref/vsynth/vsynth3-mpeg4-qpel
@@ -1,4 +1,4 @@
-4fa46716af2a92cb4fb9ba582a174ba3 *tests/data/fate/vsynth3-mpeg4-qpel.avi
-41976 tests/data/fate/vsynth3-mpeg4-qpel.avi
-09ff5a4254203ee5ed74468838eb2d5a 
*tests/data/fate/vsynth3-mpeg4-qpel.out.rawvideo
+daf3314a6e4b59e425d9669ea9c43b6c *tests/data/fate/vsynth3-mpeg4-qpel.avi
+41974 tests/data/fate/vsynth3-mpeg4-qpel.avi
+99f320d4b4ebcb48b59ced95a8d31e92 
*tests/data/fate/vsynth3-mpeg4-qpel.out.rawvideo
 stddev:    6.96 PSNR: 31.27 MAXDIFF:   53 bytes:    86700/    86700
-- 
2.54.0


>From 5771df8d5da109d97287952e1712b96145b64834 Mon Sep 17 00:00:00 2001
From: Arun Iyengar <[email protected]>
Date: Tue, 10 Nov 2026 05:04:33 +0000
Subject: [PATCH 3/4] fate/mpeg4: add an interlaced quarter sample field
 prediction test

Encodes and decodes interlaced P-VOPs with quarter sample field motion
vectors (16x8 blocks) from lavfi testsrc2 + weave; no sample needed.
The reference is the current output; the next commit fixes the field
prediction and updates it.

Assisted-by: LLM
Signed-off-by: Arun Iyengar <[email protected]>
---
 tests/fate/mpeg4.mak            |  8 +++++++-
 tests/ref/fate/mpeg4-field-qpel | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/mpeg4-field-qpel

diff --git a/tests/fate/mpeg4.mak b/tests/fate/mpeg4.mak
index d0c680bfc1e..982ea93f6bb 100644
--- a/tests/fate/mpeg4.mak
+++ b/tests/fate/mpeg4.mak
@@ -11,6 +11,11 @@ FATE_MPEG4-$(call DEMMUX, AVI, AVI, 
MPEG4_UNPACK_BFRAMES_BSF) += fate-mpeg4-bsf-
 fate-mpeg4-packed: CMD = framecrc -flags +bitexact -idct simple -i 
$(TARGET_SAMPLES)/mpeg4/packed_bframes.avi -flags +bitexact -fflags +bitexact 
-fps_mode cfr
 FATE_MPEG4-$(call FRAMECRC, AVI, MPEG4, MPEG4VIDEO_PARSER) += fate-mpeg4-packed
 
+# Interlaced P-VOPs with quarter sample field vectors (16x8 blocks).
+FATE_MPEG4_FFMPEG-$(call TRANSCODE, MPEG4, M4V, LAVFI_INDEV TESTSRC2_FILTER 
WEAVE_FILTER MPEG4VIDEO_PARSER) += fate-mpeg4-field-qpel
+fate-mpeg4-field-qpel: CMD = transcode lavfi 
"testsrc2=size=352x144:rate=50,weave" m4v \
+    "-c:v mpeg4 -qscale 5 -flags +qpel+ildct+ilme -mbd rd -frames:v 12"
+
 FATE_MPEG4-$(call FRAMECRC, M4V, MPEG4, MPEG4VIDEO_PARSER SCALE_FILTER) \
                           += fate-mpeg4-simple-studio-profile
 fate-mpeg4-simple-studio-profile: CMD = framecrc -bitexact -idct simple -i 
$(TARGET_SAMPLES)/mpeg4/mpeg4_sstp_dpcm.m4v -sws_flags +accurate_rnd+bitexact 
-pix_fmt yuv422p10le -vf scale
@@ -34,5 +39,6 @@ fate-mpeg4-reserved-color-tags: CMD = run 
ffprobe$(PROGSSUF)$(EXESUF) -v error \
     $(TARGET_SAMPLES)/mpeg4/reserved-color-tags.m4v
 
 FATE_SAMPLES_AVCONV += $(FATE_MPEG4-yes)
+FATE_FFMPEG += $(FATE_MPEG4_FFMPEG-yes)
 FATE_SAMPLES_FFPROBE += $(FATE_MPEG4_FFPROBE-yes)
-fate-mpeg4: $(FATE_MPEG4-yes) $(FATE_MPEG4_FFPROBE-yes)
+fate-mpeg4: $(FATE_MPEG4-yes) $(FATE_MPEG4_FFPROBE-yes) 
$(FATE_MPEG4_FFMPEG-yes)
diff --git a/tests/ref/fate/mpeg4-field-qpel b/tests/ref/fate/mpeg4-field-qpel
new file mode 100644
index 00000000000..8576b82eb74
--- /dev/null
+++ b/tests/ref/fate/mpeg4-field-qpel
@@ -0,0 +1,19 @@
+0fd22d28d23619572c445075432a4da4 *tests/data/fate/mpeg4-field-qpel.m4v
+57760 tests/data/fate/mpeg4-field-qpel.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, 0xbd4707e9
+0,          1,          1,        1,   152064, 0xb2ff1da3
+0,          2,          2,        1,   152064, 0x1f8727b4
+0,          3,          3,        1,   152064, 0x586f39ed
+0,          4,          4,        1,   152064, 0x49a36d83
+0,          5,          5,        1,   152064, 0x8db6b917
+0,          6,          6,        1,   152064, 0x54250a99
+0,          7,          7,        1,   152064, 0x2eb65097
+0,          8,          8,        1,   152064, 0x02358ffc
+0,          9,          9,        1,   152064, 0xf882ea5f
+0,         10,         10,        1,   152064, 0x112a3f85
+0,         11,         11,        1,   152064, 0x626e47b6
-- 
2.54.0


>From 5a4cabb57322270964139a401f3adf5ec4cac443 Mon Sep 17 00:00:00 2001
From: Arun Iyengar <[email protected]>
Date: Tue, 10 Nov 2026 05:05:13 +0000
Subject: [PATCH 4/4] avcodec/mpegvideo_motion: mirror the 16x8 field quarter
 sample block

The quarter sample interpolation of MPEG-4 Part 2 (ISO/IEC 14496-2,
7.6.2.2) mirrors the reference area of the block at its edges. The block
of a field motion vector is 16x8, but qpel_motion() predicts it with two
8x8 operations, which mirror at the middle of the block as well: luma
columns 5-10 of a field predicted block with a fractional horizontal
vector differ from the standard, and the error propagates through the
following pictures.

Run the 16x16 operation on the 17x9 field reference area mirrored below
the block instead (line 8 + k is line 9 - k, as the 8 line vertical
filter does), and keep its first 8 lines.

Checked against the ISO/IEC 14496-5 reference decoder (vm_dec), luma
macroblocks with max|diff| > 4 (max|diff|), -threads 1, on the
interlaced quarter sample streams of ISO/IEC 14496-4:
  a1ge9_asp_L1   2 (5) -> 0 (4)
  a1ge9_asp_L3  87 (8) -> 0 (4)
  a1ge9_asp_L4  13 (6) -> 0 (4)
  a1ge9_asp_L2   0 (4) -> 0 (4)
  a1ge4_asp    156 (29) -> 64 (29); P-VOPs 8 (6) -> 0 (2). The rest
               is field direct mode in B-VOPs, a separate bug; with
               the field direct fix (PR #25898) applied too:
               92 (14) -> 0 (4)
  a1ge2_asp_L4 13857 (9) -> 1326 (6). This 1920x1088 P-only stream
               has 20 picture GOPs; FFmpeg's own simple and int IDCTs
               differ on it by 1605 (6).
  fate-mpeg4-field-qpel stream    44 (12) -> 0 (3)
The other ISO/IEC 14496-4 Advanced Simple and Simple profile streams,
the MPEG-4, H.263 and FLV samples of the FATE suite decode unchanged.

FATE: mpeg4-field-qpel changes; its bitstream too, as the encoder
reconstructs from the same prediction.

Assisted-by: LLM
Signed-off-by: Arun Iyengar <[email protected]>
---
 libavcodec/mpegvideo_motion.c   | 25 +++++++++++++++++++++----
 tests/ref/fate/mpeg4-field-qpel | 26 +++++++++++++-------------
 2 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/libavcodec/mpegvideo_motion.c b/libavcodec/mpegvideo_motion.c
index 67b9b614b2b..8023954dbde 100644
--- a/libavcodec/mpegvideo_motion.c
+++ b/libavcodec/mpegvideo_motion.c
@@ -416,6 +416,9 @@ static inline void qpel_motion(MpegEncContext *s,
     if (!field_based)
         qpix_op[0][dxy](dest_y, ptr_y, linesize);
     else {
+        LOCAL_ALIGNED_16(uint8_t, field_src, [32 * 17]);
+        LOCAL_ALIGNED_16(uint8_t, field_dst, [32 * 16]);
+
         if (bottom_field) {
             dest_y  += s->linesize;
             dest_cb += s->uvlinesize;
@@ -427,10 +430,24 @@ static inline void qpel_motion(MpegEncContext *s,
             ptr_cb += s->uvlinesize;
             ptr_cr += s->uvlinesize;
         }
-        // damn interlaced mode
-        // FIXME boundary mirroring is not exactly correct here
-        qpix_op[1][dxy](dest_y, ptr_y, linesize);
-        qpix_op[1][dxy](dest_y + 8, ptr_y + 8, linesize);
+        /*
+         * The block of a field vector is 16x8, and the quarter sample
+         * interpolation mirrors the 17x9 reference area at its edges
+         * (ISO/IEC 14496-2, 7.6.2.2). Two 8x8 operations would mirror in the
+         * middle of the block: run a 16x16 one on the 9 field lines mirrored
+         * below the block (line 8 + k is line 9 - k) instead, whose first
+         * 8 output lines are then the 16x8 prediction.
+         */
+        for (int i = 0; i < 9; i++)
+            memcpy(field_src + 32 * i, ptr_y + i * linesize, 17);
+        for (int i = 9; i < 17; i++)
+            memcpy(field_src + 32 * i, field_src + 32 * (17 - i), 17);
+        /* the averaging operations of bidirectional prediction read dest */
+        for (int i = 0; i < 8; i++)
+            memcpy(field_dst + 32 * i, dest_y + i * linesize, 16);
+        qpix_op[0][dxy](field_dst, field_src, 32);
+        for (int i = 0; i < 8; i++)
+            memcpy(dest_y + i * linesize, field_dst + 32 * i, 16);
     }
     if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
         pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
diff --git a/tests/ref/fate/mpeg4-field-qpel b/tests/ref/fate/mpeg4-field-qpel
index 8576b82eb74..c426a80df4f 100644
--- a/tests/ref/fate/mpeg4-field-qpel
+++ b/tests/ref/fate/mpeg4-field-qpel
@@ -1,19 +1,19 @@
-0fd22d28d23619572c445075432a4da4 *tests/data/fate/mpeg4-field-qpel.m4v
-57760 tests/data/fate/mpeg4-field-qpel.m4v
+58420ce2a14b1a22ed7cfddd64b017b3 *tests/data/fate/mpeg4-field-qpel.m4v
+57952 tests/data/fate/mpeg4-field-qpel.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, 0xbd4707e9
-0,          1,          1,        1,   152064, 0xb2ff1da3
-0,          2,          2,        1,   152064, 0x1f8727b4
-0,          3,          3,        1,   152064, 0x586f39ed
-0,          4,          4,        1,   152064, 0x49a36d83
-0,          5,          5,        1,   152064, 0x8db6b917
-0,          6,          6,        1,   152064, 0x54250a99
-0,          7,          7,        1,   152064, 0x2eb65097
-0,          8,          8,        1,   152064, 0x02358ffc
-0,          9,          9,        1,   152064, 0xf882ea5f
-0,         10,         10,        1,   152064, 0x112a3f85
-0,         11,         11,        1,   152064, 0x626e47b6
+0,          1,          1,        1,   152064, 0x21101d1f
+0,          2,          2,        1,   152064, 0xe4d3286d
+0,          3,          3,        1,   152064, 0x71913bc2
+0,          4,          4,        1,   152064, 0x8c9a6df2
+0,          5,          5,        1,   152064, 0xae6bb8f4
+0,          6,          6,        1,   152064, 0x7c690ce8
+0,          7,          7,        1,   152064, 0x6de95560
+0,          8,          8,        1,   152064, 0x109e9314
+0,          9,          9,        1,   152064, 0x5df4e924
+0,         10,         10,        1,   152064, 0x0cae3cf4
+0,         11,         11,        1,   152064, 0x2cf14940
-- 
2.54.0

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

Reply via email to