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 2CFC04E898
	for <ffmpegdev@gitmailbox.com>; Mon, 17 Mar 2025 17:50:24 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2DF39687C12;
	Mon, 17 Mar 2025 19:49:34 +0200 (EET)
Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com
 [209.85.221.51])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A541687BC1
 for <ffmpeg-devel@ffmpeg.org>; Mon, 17 Mar 2025 19:49:24 +0200 (EET)
Received: by mail-wr1-f51.google.com with SMTP id
 ffacd0b85a97d-39130ee05b0so4614100f8f.3
 for <ffmpeg-devel@ffmpeg.org>; Mon, 17 Mar 2025 10:49:24 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1742233764; x=1742838564; 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=woBF+sbpgseqT1ghMbfNHas/dbEBY1BfTa6qlZTzJgw=;
 b=icxlxXeTEqXBr0V8w1iCsb+kvIQplKLK9/tlWW5vJw2XjTfDXFiQxNfbOspPznW5Aq
 UiwmRERCiNd4G+i+WVfczeOjCZo0bIrnreZzjwDvwZKX81US4+O4Y/4wIiCS5E+Fm3u8
 +9znBRP8f8n5R/BuIx+62yYnYoz1Qq3Nk/qENXRFPemdsWgpa40KpjwM+siQkQjAhYin
 Z+/dWD1vflWYX1NByvUXsGFmr5fxNzCm7D7KGcYu024m5Wh7ptN6hbxM7Vw5gUuikZh1
 GX4Pvu3mE2MLt+wncowoVlrSwHi5CX/cgbKzXdAVQOgiNSwlTzEAS4lKVV7Hyah/Huo9
 Qi6g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1742233764; x=1742838564;
 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=woBF+sbpgseqT1ghMbfNHas/dbEBY1BfTa6qlZTzJgw=;
 b=gfi97SlfP7XkulE968auNUAW9uJUKdQUS+KEEgDx/u0NvJ0TEepA6wcBjwAmRBJtf9
 Rj4HVuTOVelrTm/OTuGXG9BPeSzTGqOyIZx3+JXl7N+j9xOn6WBE9zoTPEPdEA7spMaT
 TCd54ia/w5bEwr978tSF9CuN2PmUCABthHcx13BBUVTtSVYeUJQlUZGTlQgRqCeDOMwa
 7U/wz8mkkyEslr5JJUQHIhHrHZcSk/nwf8T2rrCKbqQNj5+fUVlZ/8Dq37DiKmnOIH6V
 p4AF8i5Aw5AW8xDN/8SB196KSUiSmY2vrqakU9mePxKKO3W9LTmmytOzVVFR9sJEaVVZ
 jYBA==
X-Gm-Message-State: AOJu0YzuBqbxivgyO14MEKgTWWNeJeYyVVClI2FVoLKv45Sxpy1rVKq8
 3JkMBu3t4NMf9Pnw3Iwz1N0tfU1CCwlwgC5kVZcId3AxNYtoa9KzW/mbSV1L
X-Gm-Gg: ASbGnctq/c1mv8FSqhO7t1Q/59LiOyNrgdr659KtAULsx5pvLG2jcnbS4jdHVuvZCkL
 6dQXC+4SsTCaNWiKz0qQvsmZwkiOzq/muZEoG6FL9lwD53PTq1IY4T9WLI/MuEd1B8QOpE3atVJ
 IM8RJxvtrT4PGAFgHivX4k6ecmICO+HnduUnQ0defvvTIewCNPy6Hay6XO0d93gQ/21D72TZfXz
 vUyc1wfbUZceEpUC3UyJy12Oclh7q14D4267/mUHTWfGkgnz99BiIgCuxKYxzdxfAxRJbAHwQZO
 UrLSBgrpWojvQ82FoNNOO1MrdG/Hk+Qu9gyjCOdvyoFacXUpH/8pJ3DOOO7HzCGK+16A8BM30Uy
 dgZvLzBOdQbbu5va88C4/mFEHHEsfpg==
X-Google-Smtp-Source: AGHT+IHKAkAOUGPYzWX0YBi+hL7PiKvBCDH8ZM2kGb+zoBSrCE05Bg4dXUDkpkCEh48ENrHS2eSluA==
X-Received: by 2002:a5d:47c5:0:b0:390:ee01:68fa with SMTP id
 ffacd0b85a97d-3971daebb31mr13940273f8f.24.1742233763647; 
 Mon, 17 Mar 2025 10:49:23 -0700 (PDT)
Received: from flagship3.deu.mlau.at (p54bc8686.dip0.t-ipconnect.de.
 [84.188.134.134]) by smtp.gmail.com with ESMTPSA id
 ffacd0b85a97d-395c7df35ecsm15809347f8f.16.2025.03.17.10.49.23
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Mon, 17 Mar 2025 10:49:23 -0700 (PDT)
From: Manuel Lauss <manuel.lauss@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Mon, 17 Mar 2025 18:49:06 +0100
Message-ID: <20250317174917.6872-5-manuel.lauss@gmail.com>
X-Mailer: git-send-email 2.49.0
In-Reply-To: <20250317174917.6872-1-manuel.lauss@gmail.com>
References: <20250317174917.6872-1-manuel.lauss@gmail.com>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH v3 04/14] 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/20250317174917.6872-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>
---
v3: no changes
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..395548ee2d 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) {
+            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.49.0

_______________________________________________
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".