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 34ABE4BC85
	for <ffmpegdev@gitmailbox.com>; Thu,  3 Apr 2025 10:51:02 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E884B68B1BD;
	Thu,  3 Apr 2025 13:50:57 +0300 (EEST)
Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net
 [217.70.183.199])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1FA0468A67E
 for <ffmpeg-devel@ffmpeg.org>; Thu,  3 Apr 2025 13:50:51 +0300 (EEST)
Received: by mail.gandi.net (Postfix) with ESMTPSA id 6F31A43281
 for <ffmpeg-devel@ffmpeg.org>; Thu,  3 Apr 2025 10:50:50 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1743677450;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding;
 bh=top+bi3VGsKu2EcPn0u3jeJ0tdcRVeaqeCBE787UmAw=;
 b=TLdjKRI5C05rZOhBeKQEa3shdWTTdCLlIfuk0WLKIsgUENqCGWfebKEup0epIDS4eNvVlA
 UbYpO/UYjy3h0dfZKxTJRElFe1xjRcc67FwY8lFNvId4XEjcEB966n5MBr9aA2CnxqJpl3
 eZSHGoofKw82j0m1RlBTRv5lQKKgEQCtsaJ9czwqmzrh7joNBs+LihlBZ+RtV2ZMn930ni
 fsRtF4XkMpWd2BeKO0DztPTvTLCa4PjPpnuhxXZew1GLvlp9e63uuQOTt6OeASjpxgLITB
 cfLQUoAxFdHb5fUm/NG4tXodDr08tP6vGZx07USHdrbjXb6N41JpT5liv+wSuw==
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Date: Thu,  3 Apr 2025 12:50:46 +0200
Message-ID: <20250403105049.883948-1-michael@niedermayer.cc>
X-Mailer: git-send-email 2.49.0
MIME-Version: 1.0
X-GND-State: clean
X-GND-Score: -70
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukeekfeeiucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdeftddmnecujfgurhephffvufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgrvghlucfpihgvuggvrhhmrgihvghruceomhhitghhrggvlhesnhhivgguvghrmhgrhigvrhdrtggtqeenucggtffrrghtthgvrhhnpefhledvvdehgfettdefueevtdeifffhkeeljeetjeeugfelgfffieduheevvefgvdenucfkphepgedurdeiiedrieejrdduudefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepgedurdeiiedrieejrdduudefpdhhvghloheplhhotggrlhhhohhsthdpmhgrihhlfhhrohhmpehmihgthhgrvghlsehnihgvuggvrhhmrgihvghrrdgttgdpnhgspghrtghpthhtohepuddprhgtphhtthhopehffhhmphgvghdquggvvhgvlhesfhhfmhhpvghgrdhorhhg
X-GND-Sasl: michael@niedermayer.cc
Subject: [FFmpeg-devel] [PATCH 1/4] avcodec/ffv1dec: Limit size of fltmap*
 to pixel number
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>
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/20250403105049.883948-1-michael@niedermayer.cc/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

This reduces needed memory and also removes the 65536 maximum for remap
on the decoder side

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1.c    |  2 ++
 libavcodec/ffv1.h    |  2 ++
 libavcodec/ffv1dec.c | 25 ++++++++++---------------
 3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index 86fe9455c4c..885cef265f7 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -273,6 +273,8 @@ av_cold void ff_ffv1_close(FFV1Context *s)
         for(int p = 0; p < 4 ; p++) {
             av_freep(&sc->fltmap[p]);
             av_freep(&sc->fltmap32[p]);
+            sc->fltmap_size  [p] = 0;
+            sc->fltmap32_size[p] = 0;
         }
 
         av_refstruct_unref(&sc->plane);
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index c95508e2844..664a48d1f0d 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -111,6 +111,8 @@ typedef struct FFV1SliceContext {
     uint16_t   *bitmap  [4]; //float encode
     uint16_t   *fltmap  [4]; //halffloat encode & decode
     uint32_t   *fltmap32[4]; //float decode
+    unsigned int fltmap_size[4];
+    unsigned int fltmap32_size[4];
     struct Unit {
         uint32_t val; //this is unneeded if you accept a dereference on each access
         uint16_t ndx;
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index dc4eeecc87c..8d10d2cf8b7 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -264,10 +264,6 @@ static int decode_slice_header(const FFV1Context *f,
             av_log(f->avctx, AV_LOG_ERROR, "unsupported remap\n");
             return AVERROR_INVALIDDATA;
         }
-        if (sc->slice_width * sc->slice_height > 65536) {
-            av_log(f->avctx, AV_LOG_ERROR, "32bit needs remap\n");
-            return AVERROR_INVALIDDATA;
-        }
     }
 
     return 0;
@@ -298,6 +294,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
 {
     unsigned int end = (1LL<<f->avctx->bits_per_raw_sample) - 1;
     int flip = sc->remap == 2 ? (end>>1) : 0;
+    const int pixel_num = sc->slice_width * sc->slice_height;
 
     for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
         int j = 0;
@@ -336,7 +333,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
                 }
                 if (i - 1 >= end)
                     break;
-                if (j >= 65536 /*FF_ARRAY_ELEMS(sc->fltmap[p])*/)
+                if (j >= pixel_num)
                     return AVERROR_INVALIDDATA;
                 if (end <= 0xFFFF) {
                     sc->fltmap  [p][j++] = i ^ ((i&    0x8000) ? 0 : flip);
@@ -398,19 +395,17 @@ static int decode_slice(AVCodecContext *c, void *arg)
     y      = sc->slice_y;
 
     if (sc->remap) {
+        const int pixel_num = sc->slice_width * sc->slice_height;
+
         for(int p = 0; p < 1 + 2*f->chroma_planes + f->transparency ; p++) {
             if (f->avctx->bits_per_raw_sample == 32) {
-                if (!sc->fltmap32[p]) {
-                    sc->fltmap32[p] = av_malloc_array(65536, sizeof(*sc->fltmap32[p]));
-                    if (!sc->fltmap32[p])
-                        return AVERROR(ENOMEM);
-                }
+                av_fast_malloc(&sc->fltmap32[p], &sc->fltmap32_size[p], pixel_num * sizeof(*sc->fltmap32[p]));
+                if (!sc->fltmap32[p])
+                    return AVERROR(ENOMEM);
             } else {
-                if (!sc->fltmap[p]) {
-                    sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
-                    if (!sc->fltmap[p])
-                        return AVERROR(ENOMEM);
-                }
+                av_fast_malloc(&sc->fltmap[p], &sc->fltmap_size[p], pixel_num * sizeof(*sc->fltmap[p]));
+                if (!sc->fltmap[p])
+                    return AVERROR(ENOMEM);
             }
         }
 
-- 
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".