From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 7F1CC44B3A for ; Wed, 13 Aug 2025 22:32:19 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 1287D68D01D; Thu, 14 Aug 2025 01:32:14 +0300 (EEST) Received: from 1e8b7847f7d1 (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id BA37468C069 for ; Thu, 14 Aug 2025 01:32:11 +0300 (EEST) MIME-Version: 1.0 From: Manuel Lauss To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] =?utf-8?q?=5BPATCH=5D_avcodec/sanm=3A_bl16=3A_fix?= =?utf-8?q?_artifacts_in_larger_videos_=28PR_=2320235=29?= X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Message-Id: <20250813223214.1287D68D01D@ffbox0-bg.ffmpeg.org> Date: Thu, 14 Aug 2025 01:32:14 +0300 (EEST) Archived-At: List-Archive: List-Post: 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 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".