Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec/sanm: bl16: fix artifacts in larger videos (PR #20235)
@ 2025-08-13 22:32 Manuel Lauss
  0 siblings, 0 replies; only message in thread
From: Manuel Lauss @ 2025-08-13 22:32 UTC (permalink / raw)
  To: ffmpeg-devel

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.lauss@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-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-08-13 22:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-13 22:32 [FFmpeg-devel] [PATCH] avcodec/sanm: bl16: fix artifacts in larger videos (PR #20235) Manuel Lauss

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git