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 20D0B4CE85
	for <ffmpegdev@gitmailbox.com>; Sat, 15 Mar 2025 09:34:28 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B5A1F68E278;
	Sat, 15 Mar 2025 11:32:50 +0200 (EET)
Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com
 [209.85.128.44])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 276BE68E26C
 for <ffmpeg-devel@ffmpeg.org>; Sat, 15 Mar 2025 11:32:41 +0200 (EET)
Received: by mail-wm1-f44.google.com with SMTP id
 5b1f17b1804b1-43d04dc73b7so4940365e9.3
 for <ffmpeg-devel@ffmpeg.org>; Sat, 15 Mar 2025 02:32:41 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1742031160; x=1742635960; 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=/ft06vbpJ6RvGwjsIi6CCA2sgkDYi7EuDoN/Sdc/yy8=;
 b=nrsGIJG6DVCzb/u6tUw44+8MYW7jsGmOZlNO81fxGal7pQb0MT72xnsiFkEdyamfHw
 gLSveCpXht+U/bRO2obUC44W+FAWBebTi7TzNDxcFaBQ2RcKXB1jTyvRMwzGwzENF8uD
 CJDSO1PDVfJUr1NnJz1ZiFH2ADimuJRTtE4DcuUrK82Asqlsi+7N1mmIWghBNoxcvmud
 eW/ueC2gYYwgt1lKn6kp5f5p4OazSYrO//4cpV0zLAk/fysRBoISuzpqsZMfTP6ZuHxE
 aKaVueVDkgE1gTFgVgvi/yW+hKcbyh6ScwYwNsueJYPtM08CQXAr9PSh5szOmJimGGKe
 Srag==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1742031160; x=1742635960;
 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=/ft06vbpJ6RvGwjsIi6CCA2sgkDYi7EuDoN/Sdc/yy8=;
 b=OcTfW6CrvXVRnvigDnZtuEjOCL14MNTV64SUa9w6QTVnCW8rEt5BnyNIpOTxS+MPya
 w2pwRJ4oR9qKYJx8t5+l8Zd3l+9VQcb1R09i2G93xZBd5CWNw9EwPTD10moSew29eJW2
 w88YAsw2NsLVnSFcYC+3EaVOeriQ44sHa1pTOvqSBq3mvrnpYosDxl+4h988pQrKOHYz
 KCJQJLxQbxpW9N2nz2hDrVRoLr3gaULYkG6LXyqPadjTSsJnrHr2w9mUkXLu+hH37j5h
 dsTYLHgRxQ9fNshyIjApw2Vsgm1cv4gAKVfSDfMPPnbHw2uirZrV+WRGoqcylt0Of6R6
 r5ag==
X-Gm-Message-State: AOJu0YwYjzMrbVQoDz3F1IGqc0U45ahXp+TvFS7cmMytOOKLLAbfJXe4
 894gsqqGKfyicUruS5oN+/id4e4P/3Ze6+HwZu9VDCfH0/giPMXTu0mKOw==
X-Gm-Gg: ASbGncs2aOAkO7q2iWHkDh+6lKtc6stNUedP9DxySQ5SC8RZ0g8fhkMQElkgytBti8O
 5MfT+XmQA5a5LbwmNMjTtCDftYZ4U+JKJr+3mk9TPIAIvuZGEkvVS25q3BkV5On0kyjHUkKHr3z
 MSCvrhB68Wu/n5QJJ7gDj+8OliApuoYV8z7hG1HuBeMBaIq4TrNxrfxQXYXUOUWPtnzxmBOEf7j
 XSh7NutqKnzDxnrgnu+5tW6wp7SV1x9SCyR20VDB3fto12VQX4ICFVY64Sxak22oNhSpT63dWzg
 vJ+D3Naz1mDDA73kNuomYC2zPSeQvM8oyz2kYgUAoDLIu0o7RMz0eK+G/FqxaehYQoVd4TKARF8
 D/sP3FQ9qJkFRlTFgEMNO9+DaZjDPTBb4uUocK1KmpY/H/Ugta6WiZn4=
X-Google-Smtp-Source: AGHT+IFhYcwB1h7QlHtjMJIUHd6OEnxi73q04xOTq84hLKmwDAuWvHIzswVXU1JpmeMKM3DxzRMxZw==
X-Received: by 2002:adf:a417:0:b0:391:46a6:f0db with SMTP id
 ffacd0b85a97d-3971f4116a4mr6685270f8f.37.1742031160379; 
 Sat, 15 Mar 2025 02:32:40 -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.39
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sat, 15 Mar 2025 02:32:39 -0700 (PDT)
From: Manuel Lauss <manuel.lauss@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Sat, 15 Mar 2025 10:32:14 +0100
Message-ID: <20250315093220.2565-8-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 07/13] avcodec/sanm: codec23 decoder
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-8-manuel.lauss@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

This codec alternatingly skips and changes existing pixels.
A second 16bit parameter in the FOBJ header indicates how to do
the pixel changes: either by specifying a LUT in the codec datastream
or by adding a constant value to the pixel.

Signed-off-by: Manuel Lauss <manuel.lauss@gmail.com>
---
 libavcodec/sanm.c | 66 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 2 deletions(-)

diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c
index c5099dc999..aca5bf49ec 100644
--- a/libavcodec/sanm.c
+++ b/libavcodec/sanm.c
@@ -292,6 +292,7 @@ typedef struct SANMVideoContext {
     int8_t p4x4glyphs[NGLYPHS][16];
     int8_t p8x8glyphs[NGLYPHS][64];
     uint8_t c47itbl[0x10000];
+    uint8_t c23lut[256];
 } SANMVideoContext;
 
 typedef struct SANMFrameHeader {
@@ -557,6 +558,62 @@ static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size)
     return 0;
 }
 
+static int old_codec23(SANMVideoContext *ctx, int top, int left, int width,
+                       int height, uint8_t param, uint16_t param2)
+{
+    const uint32_t maxpxo = ctx->height * ctx->pitch;
+    uint8_t *dst, lut[256], c;
+    int i, j, k, pc, sk;
+    int32_t pxoff;
+
+    if (ctx->subversion < 2) {
+        /* Rebel Assault 1: constant offset + 0xd0 */
+        for (i = 0; i < 256; i++)
+            lut[i] = (i + param + 0xd0) & 0xff;
+    } else if (param2 == 256) {
+        if (bytestream2_get_bytes_left(&ctx->gb) < 256)
+            return AVERROR_INVALIDDATA;
+        bytestream2_get_bufferu(&ctx->gb, ctx->c23lut, 256);
+    } else if (param2 < 256) {
+        for (i = 0; i < 256; i++)
+            lut[i] = (i + param2) & 0xff;
+    } else {
+        memcpy(lut, ctx->c23lut, 256);
+    }
+    if (bytestream2_get_bytes_left(&ctx->gb) < 1)
+        return 0;  /* some c23 frames just set up the LUT */
+
+    dst = (uint8_t *)ctx->frm0;
+    for (i = 0; i < height; i++) {
+        if (bytestream2_get_bytes_left(&ctx->gb) < 2)
+            return 0;
+        pxoff = left + ((top + i) * ctx->pitch);
+        k = bytestream2_get_le16u(&ctx->gb);
+        sk = 1;
+        pc = 0;
+        while (k > 0 && pc <= width) {
+            if (bytestream2_get_bytes_left(&ctx->gb) < 1)
+                return AVERROR_INVALIDDATA;
+            j = bytestream2_get_byteu(&ctx->gb);
+            if (sk) {
+                pxoff += j;
+                pc += j;
+            } else {
+                while (j--) {
+                    if (pxoff >=0 && pxoff < maxpxo) {
+                        c = *(dst + pxoff);
+                        *(dst + pxoff) = lut[c];
+                    }
+                    pxoff++;
+                    pc++;
+                }
+            }
+            sk ^= 1;
+        }
+    }
+    return 0;
+}
+
 static int old_codec1(SANMVideoContext *ctx, int top,
                       int left, int width, int height, int opaque)
 {
@@ -1258,11 +1315,15 @@ static int old_codec48(SANMVideoContext *ctx, int width, int height)
 
 static int process_frame_obj(SANMVideoContext *ctx)
 {
-    uint16_t codec = bytestream2_get_le16u(&ctx->gb);
+    uint16_t parm2;
+    uint8_t  codec = bytestream2_get_byteu(&ctx->gb);
+    uint8_t  param = bytestream2_get_byteu(&ctx->gb);
     int16_t  left  = bytestream2_get_le16u(&ctx->gb);
     int16_t  top   = bytestream2_get_le16u(&ctx->gb);
     uint16_t w     = bytestream2_get_le16u(&ctx->gb);
     uint16_t h     = bytestream2_get_le16u(&ctx->gb);
+    bytestream2_skip(&ctx->gb, 2);
+    parm2 = bytestream2_get_le16u(&ctx->gb);
 
     if (w < 1 || h < 1 || w > 800 || h > 600 || left > 800 || top > 600) {
         av_log(ctx->avctx, AV_LOG_WARNING,
@@ -1316,7 +1377,6 @@ static int process_frame_obj(SANMVideoContext *ctx)
             h = ctx->height;
         }
     }
-    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.
@@ -1333,6 +1393,8 @@ static int process_frame_obj(SANMVideoContext *ctx)
         return old_codec1(ctx, top, left, w, h, codec == 3);
     case 2:
         return old_codec2(ctx, top, left, w, h);
+    case 23:
+        return old_codec23(ctx, top, left, w, h, param, parm2);
     case 37:
         return old_codec37(ctx, w, h);
     case 47:
-- 
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".