From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id B34DA4E616 for ; Thu, 13 Mar 2025 11:18:00 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D426868E362; Thu, 13 Mar 2025 13:15:31 +0200 (EET) Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9B30D689AE3 for ; Thu, 13 Mar 2025 13:15:17 +0200 (EET) Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-43cfe63c592so7132975e9.2 for ; Thu, 13 Mar 2025 04:15:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741864517; x=1742469317; 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=WnK2sOWyAfgqX3av7W4uwM6KVWhNJcaYSphoBUr7HKg=; b=mMyd3g+vK+Vr8cYq/CmGqq4C4vxC3GGQhY1+Anw8Mhnk5Nj292LDuhKxAHaqaV/CRT MzcGt65uaQtQKnC3psg1RCiBeUhqARVvhUfapQ+ksyGhgoRg6fnCgo9OMQOZquFWfSOy FJZAwrUJTz/FXpgDOrCRA9aCbH4iyWgCFSQ8BvwjZ+eBhPs9nC8htqMZFMKHK8bWy8cY IZLnuRgC+Ing4A/ytVU7auJEhWvVZ6ntuILVES5eSb+/CEXcW65m45AHYiuM4k2HScsq 9TZsnVy/ZlSIdZMJt9wQV4HKI+sBxJVT/dAo2X/hMUXFk/NnwbFjCYbUBqvsKG4mj/bW zUeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741864517; x=1742469317; 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=WnK2sOWyAfgqX3av7W4uwM6KVWhNJcaYSphoBUr7HKg=; b=C4A93ldYCHPqHBa9Itj0VwzE8L/mAvGShf196f+UKIPFrXjR1eG8tVIbA3QGmst1iy bJoiGVfePdiAQpq0YQadzoXnANBpcYLP7GNicvjB/82w0cw+fgzux6dnfAeCRUF8z3oV Kf2CXiyWRL9P5DWTUxRYqX00NiBraNuM8Sema57lfxe8cLjP8PKgv7SPN3j2x44mRc/x W//7cs+t+KZvzYHJmuDAJokOBYwBae9hfw8Zo4+WCpAj3DwXUuVFuioTWTI1pmZux3yu e2OGhbATPxh4velh2cdo6D0gEdwsFTI5D9mBjDn+3x2/okXa4vw58EHJc0nBqXuEfjhw fpPQ== X-Gm-Message-State: AOJu0YyGNWv3roFY2aHEOEZ9c0ZORiKxeFc3lKLSF7Y+xF0kGkJ0y3ZN jNzUp3Y4yfY0rAxlMQ8Yg8Zb+4GDyoSREiD6muQ7qMJWQmVNH7IfluVGVA== X-Gm-Gg: ASbGncuwXdkUqrC1C+zvWaWo7WBtKLvdvIAOeZg2RHzTVu2RKrM9B0XT9zlivz7I7iM 0LF0B6e96zHKlZr6r/gLoo+zHVsI4gWTl2WdHJdO79FNYsd3Y0SAF+V/dR+E7noH8+RndqAF2Va JiZDv2Jk04Gyf9IFV40xfo/RVJdhwcvYtgYzkJb71zfUlh0LgfWpf1XtO5GfU0nIY1xSrr6DHH5 mTyfpKylreoUpzOgeFUFFVOtH6kH0riP6hv20pDrY5MrI2YQLkUj58D4ydyIc+UrIky8hAT/2D1 UbHdcn5214O06TMqI4IwkvLdSdDXaHutjzyRhm1BwKoSgOvOvHzvVncpUDsc5iCKwY9wfiWvpKh UF0B3VBDwYuC1eHCQgXrfQsDYKp61NfAXf1VxkIRNKZ4cJ+qsSI6y77fYBhBrT/DOHg== X-Google-Smtp-Source: AGHT+IFoQC9PKFGuKTjunm7TlT2G1uHt8WXLbPqQINiC886XzVrtTRAoqhyNauuKiVJVnuJhKQSPCA== X-Received: by 2002:a05:600c:19c8:b0:43d:82c:2b23 with SMTP id 5b1f17b1804b1-43d082c2db3mr66842945e9.23.1741864516703; Thu, 13 Mar 2025 04:15:16 -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-43d0a75b324sm50153795e9.24.2025.03.13.04.15.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 04:15:16 -0700 (PDT) From: Manuel Lauss To: ffmpeg-devel@ffmpeg.org Date: Thu, 13 Mar 2025 12:15:02 +0100 Message-ID: <20250313111505.166786-10-manuel.lauss@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250313111505.166786-1-manuel.lauss@gmail.com> References: <20250313111505.166786-1-manuel.lauss@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/12] avcodec/sanm: codec4/5/33/34 decoder 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 Cc: Manuel Lauss Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: This codec works on 4x4 pixel tiles, which can be generated and read from the datastream. They're both identical, codec5 misses a tile index to skip the current tile. Codecs33/34 are the same as 4/5 but with a different tileset generator. Used only in Rebel Assault 1. Signed-off-by: Manuel Lauss --- libavcodec/sanm.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c index bf2ec40df4..91c8ba658b 100644 --- a/libavcodec/sanm.c +++ b/libavcodec/sanm.c @@ -293,6 +293,8 @@ typedef struct SANMVideoContext { int8_t p8x8glyphs[NGLYPHS][64]; uint8_t c47itbl[0x10000]; uint8_t c23lut[256]; + uint8_t c4tbl[2][256][16]; + uint16_t c4param; } SANMVideoContext; typedef struct SANMFrameHeader { @@ -480,6 +482,142 @@ static av_cold int init_buffers(SANMVideoContext *ctx) return 0; } +static void codec33_gen_tiles(SANMVideoContext *ctx, int8_t param1) +{ + uint8_t *dst = &(ctx->c4tbl[0][0][0]); + int i, j, k, l, m, n, o, p; + + for (i = 0; i < 8; i++) { + for (k = 0; k < 8; k++) { + j = i + param1; + l = k + param1; + p = (j + k) / 2; + j = (j + p) / 2; + m = l / 2; + n = (i + param1); + o = (k + param1); + + *dst++ = p; *dst++ = p; *dst++ = j; *dst++ = n; + *dst++ = p; *dst++ = p; *dst++ = j; *dst++ = i; + *dst++ = m; *dst++ = m; *dst++ = p; *dst++ = j; + *dst++ = l; *dst++ = l; *dst++ = m; *dst++ = p; + } + } + + for (i = 0; i < 8; i++) { + for (k = 0; k < 8; k++) { + j = i + param1; + l = k + param1; + n = ((j + l) / 2); + m = ((l + n) / 2); + + *dst++ = j; *dst++ = j; *dst++ = j; *dst++ = j; + *dst++ = n; *dst++ = n; *dst++ = n; *dst++ = n; + *dst++ = m; *dst++ = m; *dst++ = m; *dst++ = m; + *dst++ = l; *dst++ = l; *dst++ = l; *dst++ = l; + } + } + + for (i = 0; i < 8; i++) { + for (k = 0; k < 8; k++) { + j = i + param1; + l = k + param1; + m = (j + l) / 2; + n = (j + m) / 2; + o = m / 2; + p = j & 0xff; + + *dst++ = p; *dst++ = p; *dst++ = n; *dst++ = m; + *dst++ = p; *dst++ = p; *dst++ = n; *dst++ = m; + *dst++ = n; *dst++ = n; *dst++ = m; *dst++ = o; + *dst++ = m; *dst++ = m; *dst++ = o; *dst++ = l; + } + } + + for (i = 0; i < 8; i++) { + for (k = 0; k < 8; k++) { + j = i + param1; + l = k + param1; + m = (j + l) / 2; + n = m / 2; + + *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l; + *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l; + *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l; + *dst++ = j; *dst++ = m; *dst++ = n; *dst++ = l; + } + } +} + +static void codec4_gen_tiles(SANMVideoContext *ctx, uint16_t param1) +{ + uint8_t *dst = &(ctx->c4tbl[0][0][0]); + int i, j, k, l, m, n, o; + + for (i = 1; i < 16; i += 2) { + for (k = 0; k < 16; k++) { + j = i + param1; + l = k + param1; + m = (j + l) / 2; + n = (j + m) / 2; + o = (l + m) / 2; + if (j == m || l == m) { + *dst++ = l; *dst++ = j; *dst++ = l; *dst++ = j; + *dst++ = j; *dst++ = l; *dst++ = j; *dst++ = j; + *dst++ = l; *dst++ = j; *dst++ = l; *dst++ = j; + *dst++ = l; *dst++ = l; *dst++ = j; *dst++ = l; + } else { + *dst++ = m; *dst++ = m; *dst++ = n; *dst++ = j; + *dst++ = m; *dst++ = m; *dst++ = n; *dst++ = j; + *dst++ = o; *dst++ = o; *dst++ = m; *dst++ = n; + *dst++ = l; *dst++ = l; *dst++ = o; *dst++ = m; + } + } + } + + for (i = 0; i < 16; i += 2) { + for (k = 0; k < 16; k++) { + j = i + param1; + l = k + param1; + m = (j + l) / 2; + n = (j + m) / 2; + o = (l + m) / 2; + if (m == j || m == l) { + *dst++ = j; *dst++ = j; *dst++ = l; *dst++ = j; + *dst++ = j; *dst++ = j; *dst++ = j; *dst++ = l; + *dst++ = l; *dst++ = j; *dst++ = l; *dst++ = l; + *dst++ = j; *dst++ = l; *dst++ = j; *dst++ = l; + } else { + *dst++ = j; *dst++ = j; *dst++ = n; *dst++ = m; + *dst++ = j; *dst++ = j; *dst++ = n; *dst++ = m; + *dst++ = n; *dst++ = n; *dst++ = m; *dst++ = o; + *dst++ = m; *dst++ = m; *dst++ = o; *dst++ = l; + } + } + } +} + + +static int codec4_load_tiles(SANMVideoContext *ctx, uint16_t param2, uint8_t clr) +{ + uint8_t c, *dst = (uint8_t *)&(ctx->c4tbl[1][0][0]); + uint32_t loop = param2 * 8; + + if (param2 > 256) + return AVERROR_INVALIDDATA; + + if (bytestream2_get_bytes_left(&ctx->gb) < loop) + return AVERROR_INVALIDDATA; + + while (loop--) { + c = bytestream2_get_byteu(&ctx->gb); + *dst++ = (c >> 4) + clr; + *dst++ = (c & 0xf) + clr; + } + + return 0; +} + static void rotate_bufs(SANMVideoContext *ctx, int rotate_code) { if (rotate_code == 2) @@ -519,6 +657,7 @@ static av_cold int decode_init(AVCodecContext *avctx) if (ctx->subversion < 2) ctx->pal[0] = 0xFFU << 24; } + ctx->c4param = 0xffff; return 0; } @@ -532,6 +671,91 @@ static av_cold int decode_end(AVCodecContext *avctx) return 0; } +static int old_codec4(SANMVideoContext *ctx, int left, int top, int w, int h, + uint8_t param, uint16_t param2, int codec) +{ + const uint16_t p = ctx->pitch; + const uint32_t maxpxo = ctx->height * p; + uint8_t mask, bits, idx, *gs, *dst = (uint8_t *)ctx->frm0; + int i, j, k, l, bit, ret; + int32_t pxoff, pxo2; + + if (ctx->c4param != param) { + if (codec > 32) + codec33_gen_tiles(ctx, param); + else + codec4_gen_tiles(ctx, param); + ctx->c4param = param; + } + if (param2 > 0) { + ret = codec4_load_tiles(ctx, param2, param); + if (ret) + return ret; + } + + if (codec > 32) + codec -= 29; + + for (j = 0; j < w; j += 4) { + mask = bits = 0; + for (i = 0; i < h; i += 4) { + pxoff = j + left + ((top + i) * p); + if (param2 > 0) { + if (bits == 0) { + if (bytestream2_get_bytes_left(&ctx->gb) < 1) + return AVERROR_INVALIDDATA; + mask = bytestream2_get_byteu(&ctx->gb); + bits = 8; + } + bit = !!(mask & 0x80); + mask <<= 1; + bits--; + } else { + bit = 0; + } + + if (bytestream2_get_bytes_left(&ctx->gb) < 1) + return AVERROR_INVALIDDATA; + idx = bytestream2_get_byteu(&ctx->gb); + if ((bit == 0) && (idx == 0x80) && (codec != 5)) + continue; + + gs = &(ctx->c4tbl[bit][idx][0]); + pxo2 = pxoff; + for (k = 0; k < 4; k++) { + for (l = 0; l < 4; l++) { + if (pxo2 >= 0 && pxo2 < maxpxo) { + *(dst + pxo2) = *gs; + } + gs++; + pxo2++; + } + pxo2 = pxo2 - 4 + p; + } + + /* smooth top and left block borders with neighbours */ + if (((pxoff - p + k) < 0) || ((pxoff - p + k) >= maxpxo) + || ((pxoff + 3 * p) < 0) || ((pxoff + 3 * p) >= maxpxo) + || (i == 0) || (j == 0)) + continue; + if (param & 0x80) { + for (k = 0; k < 4; k++) + *(dst + pxoff + k) = ((*(dst + pxoff + k) + *(dst + pxoff - p + k)) >> 1) | 0x80; + *(dst + pxoff + 1 * p) = (*(dst + pxoff + 1 * p) + *(dst + pxoff + 1 * p - 1)) >> 1 | 0x80; + *(dst + pxoff + 2 * p) = (*(dst + pxoff + 2 * p) + *(dst + pxoff + 2 * p - 1)) >> 1 | 0x80; + *(dst + pxoff + 3 * p) = (*(dst + pxoff + 3 * p) + *(dst + pxoff + 3 * p - 1)) >> 1 | 0x80; + } else { + for (k = 0; k < 4; k++) + *(dst + pxoff + k) = ((*(dst + pxoff + k) + *(dst + pxoff - p + k)) >> 1) & 0x7f; + *(dst + pxoff + 1 * p) = (*(dst + pxoff + 1 * p) + *(dst + pxoff + 1 * p - 1)) >> 1; + *(dst + pxoff + 2 * p) = (*(dst + pxoff + 2 * p) + *(dst + pxoff + 2 * p - 1)) >> 1; + *(dst + pxoff + 3 * p) = (*(dst + pxoff + 3 * p) + *(dst + pxoff + 3 * p - 1)) >> 1; + } + } + } + return 0; +} + static int rle_decode(SANMVideoContext *ctx, uint8_t *dst, const int out_size) { int opcode, color, run_len, left = out_size; @@ -1427,6 +1651,11 @@ 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 4: + case 5: + case 33: + case 34: + return old_codec4(ctx, top, left, w, h, param, parm2, codec); case 21: return old_codec21(ctx, top, left, w, h); case 23: -- 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".