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 D9DDB4CE98 for <ffmpegdev@gitmailbox.com>; Sat, 15 Mar 2025 09:33:20 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A858468E24D; Sat, 15 Mar 2025 11:32:41 +0200 (EET) Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A5F7C68E233 for <ffmpeg-devel@ffmpeg.org>; Sat, 15 Mar 2025 11:32:34 +0200 (EET) Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4394345e4d5so3231265e9.0 for <ffmpeg-devel@ffmpeg.org>; Sat, 15 Mar 2025 02:32:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742031154; x=1742635954; 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=mwwzOHfVN+XKR0T51RGFjOnkk+9lNXQMVV8tnLZkUJw=; b=jvX5uEc4OXK/0/8Mtx0NURPLXqpGgrzw1wkgxcQEm0EZUITApPw4srC0+TLpmpMpJ2 KDyO6kbikfMQ2J5EKMNvRvd3aQXJr6tCIvjkQciGfngDDXEvmvB+IrTRJrJviQrPC2ir XYGD+MJ02d4177P1/nhv/1I9YWkFoC85SoXvwAx5+eTUXSaxt+hk/rEY4XDGc8+90fH3 uucrPBE6N73PBdsMY6JGPW05HBNHCVqLDxTGytWJdVDlvwu3SD0zEd0eLQ7cahsZDtUn ze4ySLVkbMBUa9Qq6Wqo7hyngBpSR4Npm2uVCYyACggdTQAYW76MO5ZQu8yxtX+eCD3y II3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742031154; x=1742635954; 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=mwwzOHfVN+XKR0T51RGFjOnkk+9lNXQMVV8tnLZkUJw=; b=qqsSMMy+EXnuPXDsDfOkedKZmOJ+2zYkW+LotSHU6Z4NssiqeQSv8wmJ89xsCu2/eN f1gqCWUz12YjmVQTG+A9Jxt/MUc0UC7hSncGDeUROm7NZpKCHcUGu44UoWV6nUiy3bEO rESgLer9ed6zjtWFJqfWnOczRiiQk8HYprVVQEqDmVnHKOMSeK2jlXZN1ulmCNiVd1aJ NOCVPlFk56P9trompQKrS5Lj2KDmItki41oRxT7M6EucBaBiHIk4snewpwVdZu4wfhB0 bpKxwJeMb3ZTham4IMS74JTjcX30PNX1fm6gAWNErax1osME3X84rFlheuPjucnXp99Q PM+w== X-Gm-Message-State: AOJu0YwshH8tNGNaBEA07/mMVXSJ1VqK50toiNVjzSsKFmbOs9Oc5Vk5 gD399NHsGIHLfg5kHIIu2D1hdzoGuYMAa1yMLMCxupOdCpqfCuCupwelDw== X-Gm-Gg: ASbGncvUXw7mDxgqFUgLyx8fmqP57ZowyYYzGWYhwWzO3TFaRnEV4I41FfN/47d6bDd ZPXthPremIO5NzEbxmkIG2EX9fPJl5soGLiAxQ5jq9HXq+GIt+j2zelbBc9eH1JR7UmQ/15uxma Hx28cNb24AJ4gSaOiWbUxtQc3xETpPyGFY3788bnzAxbYM8gPn4SWoXosGEwq5rTDCQZ52R5Jzf jyAxO6Qp7q15vJ6t+Ub7yprDzli3sCuzKzbuBM08vo5QpvRQdIdb+KjAoZmzst5LqLgVbYiT3cm IkNi17+UY1yBu8imyWpQ020GrZJ4jJgt3GDU+xf/VGt3sy0oiwiU2CI3j4XdGsdzcpHBczdDcha yovyJE/Bx4tN1aGKEh7F2wI1tG8+j3QB5ncAp465oAbnZiQfYq8TdzWQ= X-Google-Smtp-Source: AGHT+IFndRIvTe8ZWeANrzk2jufqFmayF4EQIFq44sWKy3rOjm4nTL5eElddapVPRUEv6WMancLLeg== X-Received: by 2002:a05:600c:470e:b0:43c:ee62:33f5 with SMTP id 5b1f17b1804b1-43d1eccf8c6mr62448815e9.27.1742031153582; Sat, 15 Mar 2025 02:32:33 -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.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Mar 2025 02:32:32 -0700 (PDT) From: Manuel Lauss <manuel.lauss@gmail.com> To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Mar 2025 10:32:10 +0100 Message-ID: <20250315093220.2565-4-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 03/13] avcodec/sanm: better frame size detection for old codecs 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-4-manuel.lauss@gmail.com/> List-Archive: <https://master.gitmailbox.com/ffmpegdev/> List-Post: <mailto:ffmpegdev@gitmailbox.com> The size of the video frame (FOBJ) of the old codecs (ANIMv0/1/2) can very reliably be determined: - ANIMv0/1 (=Rebel Assault 1) uses a 384x242 internal buffer for everything. The codec parameters only describe the size and offset of the specific FOBJ on that buffer. - ANIMv2 titles usually use one of the fullscreen codecs (37/47/48) as first FOBJ, and their dimensions can generally be trusted. - RA2 uses 424x260 as internal buffer, use that if encountered: 08PLAY.SAN does not use codec37 so we need to guess using the codec coordinates. - ignore sizes smaller than 2x2 or larger than 800x600. - some game videos have an initial fobj with either 1x1 or -1x-1 pixels in size, ignore them with a warning (Full Throttle and the Rebel Assault 2 xxRETRY.SAN videos). Once a known/valid dimension set has been discovered, use it and don't change it for subsequent FOBJs, rather clamp the large frame to the determined dimensions. Tested with RA1, RA2, Full Throttle, Dig, Outlaws, SotE and MotS videos. Signed-off-by: Manuel Lauss <manuel.lauss@gmail.com> --- libavcodec/sanm.c | 63 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c index 65ca525b9d..38cdb533eb 100644 --- a/libavcodec/sanm.c +++ b/libavcodec/sanm.c @@ -264,7 +264,7 @@ typedef struct SANMVideoContext { AVCodecContext *avctx; GetByteContext gb; - int version, subversion; + int version, subversion, have_dimensions; uint32_t pal[PALETTE_SIZE]; int16_t delta_pal[PALETTE_DELTA]; @@ -1244,21 +1244,56 @@ static int process_frame_obj(SANMVideoContext *ctx) uint16_t w = bytestream2_get_le16u(&ctx->gb); uint16_t h = bytestream2_get_le16u(&ctx->gb); - if (!w || !h) { - av_log(ctx->avctx, AV_LOG_ERROR, "Dimensions are invalid.\n"); - return AVERROR_INVALIDDATA; + if (w < 1 || h < 1 || w > 800 || h > 600 || left > 800 || top > 600) { + av_log(ctx->avctx, AV_LOG_WARNING, + "ignoring invalid fobj dimensions: c%d %d %d @ %d %d\n", + codec, w, h, left, top); + return 0; } - if (ctx->width < left + w || ctx->height < top + h) { - int ret = ff_set_dimensions(ctx->avctx, FFMAX(left + w, ctx->width), - FFMAX(top + h, ctx->height)); - if (ret < 0) - return ret; - init_sizes(ctx, FFMAX(left + w, ctx->width), - FFMAX(top + h, ctx->height)); - if (init_buffers(ctx)) { - av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n"); - return AVERROR(ENOMEM); + if (!ctx->have_dimensions) { + int xres, yres; + if (ctx->subversion < 2) { + /* Rebel Assault 1: 384x242 internal size */ + xres = 384; + yres = 242; + ctx->have_dimensions = 1; + } else if (codec == 37 || codec == 47 || codec == 48) { + /* these codecs work on full frames, trust their dimensions */ + xres = w; + yres = h; + ctx->have_dimensions = 1; + } else { + /* Rebel Assault 2: 424x260 internal size */ + if (((left + w) == 424) && ((top + h) == 260)) + ctx->have_dimensions = 1; + + xres = FFMAX(left + w, ctx->width); + yres = FFMAX(top + h, ctx->height); + } + + if (ctx->width < xres || ctx->height < yres) { + int ret = ff_set_dimensions(ctx->avctx, xres, yres); + if (ret < 0) + return ret; + init_sizes(ctx, xres, yres); + if (init_buffers(ctx)) { + av_log(ctx->avctx, AV_LOG_ERROR, "Error resizing buffers.\n"); + return AVERROR(ENOMEM); + } + } + } else { + if (((left + w > ctx->width) || (top + h > ctx->height)) + && (codec >= 37)) { + /* correct unexpected overly large frames: this happens + * for instance with The Dig's sq1.san video: it has a few + * (all black) 640x480 frames halfway in, while the rest is + * 320x200. + */ + av_log(ctx->avctx, AV_LOG_WARNING, + "resizing too large fobj: c%d %d %d @ %d %d\n", codec, w, h, left, top); + w = ctx->width; + h = ctx->height; } } bytestream2_skip(&ctx->gb, 4); -- 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".