From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id B8E6D4CE6D
	for <ffmpegdev@gitmailbox.com>; Sat, 15 Mar 2025 09:33:48 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A7B8668E266;
	Sat, 15 Mar 2025 11:32:44 +0200 (EET)
Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com
 [209.85.221.47])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2E2BA68E247
 for <ffmpeg-devel@ffmpeg.org>; Sat, 15 Mar 2025 11:32:37 +0200 (EET)
Received: by mail-wr1-f47.google.com with SMTP id
 ffacd0b85a97d-3912d2c89ecso2528627f8f.2
 for <ffmpeg-devel@ffmpeg.org>; Sat, 15 Mar 2025 02:32:37 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1742031156; x=1742635956; darn=ffmpeg.org;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=bnomqQdqt7T349eR56jRDa5z0rVFtRLaxbwx1647EjI=;
 b=Bbw6pxyhmmMBdEFIvRMrGwsKHjgMZox0kHQAwX2/lIB+W91p7zIqMIgoSB5AJUVwpL
 hLIV5m0Hmu4wKt+HCXUJGUt0ktRtpMHwUlMxgo3yeLSgn3sWFn+5JbbTyaJHHZQKCqS1
 YGtT5lJFkC1/VUiS7ITTuWIvaBcfz44MnCGGSpet8lzQwHkN9zbcAiGdvzpJrfsr2Uoy
 8LXj/v52/qk3fB+rKfxxL312dXqosAOh7XI1Eh5Uq99PLkkjF/+97/IBZeuIZHf6+/hz
 sDtkcaz0fPSB0reHPEyqJ3/kjQiy7Ga7th3RQAYjuK30A6KTR3n3y6S8QjdCNXDBASMU
 ch+A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1742031156; x=1742635956;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=bnomqQdqt7T349eR56jRDa5z0rVFtRLaxbwx1647EjI=;
 b=g3reBlaZOS7mm55KFQdSsSBSddl7s8bM0mb8SqXU6MnLcIHNl6tz7CKcM0I/0TugUk
 5l0MRB42Y/HJK0OQJAYE4V3HuKuKFTAPlaTC9uKD5hS26TFKfNecuNC24vMknRTlQZy2
 6sD39XBmcwGZU98jWViMGNRug4VvHxGgNPhW5XLXAkcL4+UASTZOGCegEZcncGdlvplK
 r7TXZKtYwQH+jihld3q3GrH+8mmdiGPGU+CkmDS0auYAF5eshh0mkHC87OxJCrTRuuWa
 9RXehLsdDQ+Mafi2o+4+kxy3KmWLmazEITowaC8dpLOza7M/0E7nO5eE2qF7sinx4zp5
 nR2g==
X-Gm-Message-State: AOJu0Yw3E5s3YU8+qg7hgiV9J4y61dxtQjfkUmKNgrY77JhXh9fbSRte
 +w4dJ/7sQ8stL13Ez0IeXWYhnhc/LsG6qMsdmlacgikzdHcTvT1+AA0A3Q==
X-Gm-Gg: ASbGncsURDdcVxJK+PuN3ch9NFpOOQrK9hwwTrf6KhwOCf+Xlz55R9LCLc773YHdn0C
 r0J+QFokJQ/G0F9lRdnQ5oJWbVZG9U/OddSa5s/E3HWcQ2pxoiqDHNFYHI3D86aAvgRGzBLojTs
 f56EFPJfX1Zan6HKV8o9Jz36kznL6rO2xypl7dTqoCsCQHbQRuILqri/Lswjet40eImVrxOlOuX
 0Rhg6QUKZCzFWePELc6lhJQeuqfP99VMH1ITgfx5CiIbxtvE7U50FlClVglhat6Uqmcdimuw2vB
 MCg6D7gRAa5thKhMHkL3L4nWJxJO5AZzHaZvWRF1KS7TRRdxwoV4oZJlqDgkmoqw9ELoA+4fedV
 l0F6AeIApm5sKJGoO9BWJO4Mjheex/j0F80wgOjGmG0lwf05egiv2dk8=
X-Google-Smtp-Source: AGHT+IFL74gnQ0owZmWcYpmJFoOhV0fPj4ulntHSiSPaYA3QPn+hB9vb/Te3u3HGBSoiMwx36g7H7Q==
X-Received: by 2002:adf:a31b:0:b0:390:eb50:37c3 with SMTP id
 ffacd0b85a97d-3971e0bfa2emr6362764f8f.27.1742031156353; 
 Sat, 15 Mar 2025 02:32:36 -0700 (PDT)
Received: from flagship3.deu.mlau.at
 (p200300c0271f29004c5623f045c27bca.dip0.t-ipconnect.de.
 [2003:c0:271f:2900:4c56:23f0:45c2:7bca])
 by smtp.gmail.com with ESMTPSA id
 5b1f17b1804b1-43d1fe659b3sm43302905e9.34.2025.03.15.02.32.33
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sat, 15 Mar 2025 02:32:34 -0700 (PDT)
From: Manuel Lauss <manuel.lauss@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Sat, 15 Mar 2025 10:32:11 +0100
Message-ID: <20250315093220.2565-5-manuel.lauss@gmail.com>
X-Mailer: git-send-email 2.48.1
In-Reply-To: <20250315093220.2565-1-manuel.lauss@gmail.com>
References: <20250315093220.2565-1-manuel.lauss@gmail.com>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH v2 04/13] avcodec/sanm: better ANIMv1 engine
 support
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Manuel Lauss <manuel.lauss@gmail.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250315093220.2565-5-manuel.lauss@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

- clear the front buffer with color 0 on the first FOBJ.
  Fixes a lot of Rebel Assault 1 videos and Rebel Assault 2 space
  scenes (e.g. 08PLAY.SAN which consists only of codec1/2/21 objects
  which only ever touch parts of the buffer).
- for ANIMv1 (Rebel Assault 1): set palette index 0 to all zeroes.
  This fixes a lot of stray colors in e.g L1HANGAR.ANM, L2INTRO.ANM,
  space scenes.
- Esp in RA1, there are a lot of FRME objects which don't contain
  any video data (prebuffering some audio only). Account for that.
- In RA1 L2PLAY.ANM there are a few unaligned FOBJs, handle this
  in a generic way.

Signed-off-by: Manuel Lauss <manuel.lauss@gmail.com>
---
v2: added handling of unaligned objects

 libavcodec/sanm.c | 49 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 9 deletions(-)

diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c
index 38cdb533eb..069bc0400a 100644
--- a/libavcodec/sanm.c
+++ b/libavcodec/sanm.c
@@ -264,7 +264,7 @@ typedef struct SANMVideoContext {
     AVCodecContext *avctx;
     GetByteContext gb;
 
-    int version, subversion, have_dimensions;
+    int version, subversion, have_dimensions, first_fob;
     uint32_t pal[PALETTE_SIZE];
     int16_t delta_pal[PALETTE_DELTA];
 
@@ -515,6 +515,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
         ctx->subversion = AV_RL16(avctx->extradata);
         for (i = 0; i < PALETTE_SIZE; i++)
             ctx->pal[i] = 0xFFU << 24 | AV_RL32(avctx->extradata + 2 + i * 4);
+        if (ctx->subversion < 2)
+            ctx->pal[0] = 0xFFU << 24;
     }
 
     return 0;
@@ -1298,6 +1300,15 @@ static int process_frame_obj(SANMVideoContext *ctx)
     }
     bytestream2_skip(&ctx->gb, 4);
 
+    /* on first FOBJ, when the codec is not one of the
+     * full-buffer codecs (37/47/48), frm0 needs to be cleared.
+     */
+    if (ctx->first_fob) {
+        ctx->first_fob = 0;
+        if (codec < 37)
+            memset(ctx->frm0, 0, ctx->frm0_size);
+    }
+
     switch (codec) {
     case 1:
     case 3:
@@ -1349,6 +1360,8 @@ static int process_xpal(SANMVideoContext *ctx, int size)
         if (size >= PALETTE_DELTA * 2 + 4 + PALETTE_SIZE * 3) {
             for (i = 0; i < PALETTE_SIZE; i++)
                 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
+            if (ctx->subversion < 2)
+                ctx->pal[0] = 0xFFU << 24;
         }
     }
     return 0;
@@ -1762,7 +1775,9 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
     bytestream2_init(&ctx->gb, pkt->data, pkt->size);
 
     if (!ctx->version) {
-        int to_store = 0;
+        int to_store = 0, have_img = 0;
+
+        ctx->first_fob = 1;
 
         while (bytestream2_get_bytes_left(&ctx->gb) >= 8) {
             uint32_t sig, size;
@@ -1785,12 +1800,15 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
                 }
                 for (i = 0; i < PALETTE_SIZE; i++)
                     ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->gb);
+                if (ctx->subversion < 2)
+                    ctx->pal[0] = 0xFFU << 24;
                 break;
             case MKBETAG('F', 'O', 'B', 'J'):
                 if (size < 16)
                     return AVERROR_INVALIDDATA;
                 if (ret = process_frame_obj(ctx))
                     return ret;
+                have_img = 1;
                 break;
             case MKBETAG('X', 'P', 'A', 'L'):
                 if (ret = process_xpal(ctx, size))
@@ -1801,6 +1819,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
                 break;
             case MKBETAG('F', 'T', 'C', 'H'):
                 memcpy(ctx->frm0, ctx->stored_frame, ctx->buf_size);
+                have_img = 1;
                 break;
             default:
                 bytestream2_skip(&ctx->gb, size);
@@ -1809,15 +1828,26 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
                 break;
             }
 
+            /* the sizes of chunks are usually a multiple of 2. However
+             * there are a few unaligned FOBJs in RA1 L2PLAY.ANM only (looks
+             * like a game bug) and IACT audio chunks which have odd sizes
+             * but are padded with a zero byte.
+             */
             bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
-            if (size & 1)
-                bytestream2_skip(&ctx->gb, 1);
+            if ((pos + size) & 1) {
+                if (0 != bytestream2_get_byteu(&ctx->gb))
+                    bytestream2_seek(&ctx->gb, pos + size, SEEK_SET);
+            }
         }
         if (to_store)
             memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
-        if ((ret = copy_output(ctx, NULL)))
-            return ret;
-        memcpy(ctx->frame->data[1], ctx->pal, 1024);
+
+        if (have_img && ctx->have_dimensions) {
+            if ((ret = copy_output(ctx, NULL)))
+                return ret;
+            memcpy(ctx->frame->data[1], ctx->pal, 1024);
+            *got_frame_ptr = 1;
+        }
     } else {
         SANMFrameHeader header;
 
@@ -1848,12 +1878,13 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
 
         if ((ret = copy_output(ctx, &header)))
             return ret;
+
+        *got_frame_ptr = 1;
+
     }
     if (ctx->rotate_code)
         rotate_bufs(ctx, ctx->rotate_code);
 
-    *got_frame_ptr = 1;
-
     return pkt->size;
 }
 
-- 
2.48.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".