PR #20235 opened by Manuel Lauss (mlauss2)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20235
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20235.patch

The DOS/Windows decoder precomputed a linear offset table of all
possible motion vectors into an int16_t with the formula
offset = my * width + mx.
For larger widths (>=768), the pairs 1-4 and 252-255 of motion_vectors[]
will overflow the int16_t, changing the sign.

Playing back the 800x600 "jonesopn_8.snm" video of "Indiana Jones and
the Infernal Machine" reveals a lot of artifacts and a lot of
"Ignoring invalid motion vector (149, -41)->(136, 0), block size = 8"
messages.

Fix this by doing the calculation that the DOS/Windows players do,
let the value overflow and reextract the "new" mvec x/y components.

Here are 2 videos to demonstrate:

[Video pre-patch](http://mlau.at/bl16_before.mp4)
[Video post-patch](http://mlau.at/bl16_after.mp4)


>From 2cfc357042cded849ff4e666b14c68241157b366 Mon Sep 17 00:00:00 2001
From: Manuel Lauss <manuel.la...@gmail.com>
Date: Thu, 14 Aug 2025 00:21:09 +0200
Subject: [PATCH] avcodec/sanm: bl16: fix artifacts in larger videos

The DOS/Windows decoder precomputed a linear offset table of all
possible motion vectors into an int16_t with the formula
offset = my * width + mx.
For larger widths (>=768), the pairs 1-4 and 252-255 of motion_vectors[]
will overflow the int16_t, changing the sign.

Playing back the 800x600 "jonesopn_8.snm" video of "Indiana Jones and
the Infernal Machine" reveals a lot of artifacts and a lot of
"Ignoring invalid motion vector (149, -41)->(136, 0), block size = 8"
messages.

Fix this by doing the calculation that the DOS/Windows players do,
let the value overflow and reextract the "new" mvec x/y components.
---
 libavcodec/sanm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c
index a066a864eb..7319f5ad3b 100644
--- a/libavcodec/sanm.c
+++ b/libavcodec/sanm.c
@@ -2069,6 +2069,18 @@ static int codec2subblock(SANMVideoContext *ctx, int cx, 
int cy, int blk_size)
         mx = motion_vectors[opcode][0];
         my = motion_vectors[opcode][1];
 
+        /* The original implementation of this codec precomputes a table
+         * of all possible linear offsets, with type int16_t.
+         * For larger widths (commonly seen in the 800x600 videos), the 
calculation
+         * of opcodes 1-4 and 252-255 overflows the int16_t, turning the vector
+         * in the opposite direction.  This is actively exploited in e.g. the
+         *  "jonesopn_8.snm" video of "Indiana Jones and the Infernal Machine".
+         * Therefore do the calculation, let it overflow and extract the new
+         * x/y mv components from there.
+         */
+        index = (int16_t)(my * ctx->width + mx);
+        mx = index % ctx->width;
+        my = index / ctx->width;
         if (good_mvec(ctx, cx, cy, mx, my, blk_size)) {
             copy_block(ctx->frm0 + cx      + ctx->pitch *  cy,
                        ctx->frm2 + cx + mx + ctx->pitch * (cy + my),
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to