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 A5C1E4C230
	for <ffmpegdev@gitmailbox.com>; Wed,  2 Apr 2025 15:19:13 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C7B50687D0F;
	Wed,  2 Apr 2025 18:19:08 +0300 (EEST)
Received: from mslow3.mail.gandi.net (mslow3.mail.gandi.net [217.70.178.249])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 814B5687AE4
 for <ffmpeg-devel@ffmpeg.org>; Wed,  2 Apr 2025 18:19:02 +0300 (EEST)
Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net
 [217.70.183.199])
 by mslow3.mail.gandi.net (Postfix) with ESMTP id 417D7580A66
 for <ffmpeg-devel@ffmpeg.org>; Wed,  2 Apr 2025 14:49:27 +0000 (UTC)
Received: by mail.gandi.net (Postfix) with ESMTPSA id 57770442A2
 for <ffmpeg-devel@ffmpeg.org>; Wed,  2 Apr 2025 14:49:21 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1743605361;
 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=oBnHMQxwnghKxIVhWQ21Qr46OR4Si6fhDkITDF5rbas=;
 b=IdVI4RXwG7M1YLuksmA1lbqhLgD5QRLGVuzrTNokPPcALT9u0kb15SEwRNfxuaCe6dPwU0
 egopLhhK2Zc1xOVjnv9Ky+jIcZz0FiJMJZdh/rMjrBgUOcYjdccK1Bg3V1mUjX0RYTwpRD
 ThttCzfX9Jrji2bmZmDM/1f5MOw+qrln403BZsGnwZ7KlVjwsQzOY5PADO3RxTkZaV7Wnk
 FU9pCizDGfGXsVhAIxALgm2UE0bwvQ528zWj3aen6vSj2k6WdaGSk9p/ABUnzRUABmkfwq
 bGpHWPqIyIHNjOMsMO7pUa+9evt4B95ZyYjGUimQgnbwnCXihc6Ymgxzfm9SyA==
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Date: Wed,  2 Apr 2025 16:49:20 +0200
Message-ID: <20250402144920.1729875-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: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukeehleegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdeftddmnecujfgurhephffvufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgrvghlucfpihgvuggvrhhmrgihvghruceomhhitghhrggvlhesnhhivgguvghrmhgrhigvrhdrtggtqeenucggtffrrghtthgvrhhnpefhledvvdehgfettdefueevtdeifffhkeeljeetjeeugfelgfffieduheevvefgvdenucfkphepgedurdeiiedrieejrdduudefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepgedurdeiiedrieejrdduudefpdhhvghloheplhhotggrlhhhohhsthdpmhgrihhlfhhrohhmpehmihgthhgrvghlsehnihgvuggvrhhmrgihvghrrdgttgdpnhgspghrtghpthhtohepuddprhgtphhtthhopehffhhmphgvghdquggvvhgvlhesfhhfmhhpvghgrdhorhhg
X-GND-Sasl: michael@niedermayer.cc
Subject: [FFmpeg-devel] [PATCH] avcodec/ffv1: Only allocate fltmap* and
 bitmap when needed
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/20250402144920.1729875-1-michael@niedermayer.cc/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

This reduces memory requirements

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1.c             |  4 ++++
 libavcodec/ffv1.h             |  9 ++++-----
 libavcodec/ffv1dec.c          | 18 +++++++++++++++++-
 libavcodec/ffv1enc.c          | 23 +++++++++++++++++------
 libavcodec/ffv1enc_template.c |  3 ++-
 5 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index 54c2e7a8c63..cff16c5cc76 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -233,6 +233,10 @@ av_cold void ff_ffv1_close(FFV1Context *s)
 
         av_freep(&sc->sample_buffer);
         av_freep(&sc->sample_buffer32);
+        for(int p = 0; p < 4 ; p++) {
+            av_freep(&sc->fltmap[p]);
+            av_freep(&sc->fltmap32[p]);
+        }
 
         av_refstruct_unref(&sc->plane);
     }
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index 9e71e0709d1..24cbb694d41 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -106,11 +106,10 @@ typedef struct FFV1SliceContext {
             uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
         };
     };
-    union {
-        uint16_t   bitmap  [4][65536]; //float encode
-        uint16_t   fltmap  [4][65536]; //halffloat encode & decode
-        uint32_t   fltmap32[4][65536]; //float decode
-    };
+
+    uint16_t   *bitmap  [4]; //float encode
+    uint16_t   *fltmap  [4]; //halffloat encode & decode
+    uint32_t   *fltmap32[4]; //float decode
     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 3395f514f42..ff7935ed722 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -326,7 +326,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
                 }
                 if (i - 1 >= end)
                     break;
-                if (j >= FF_ARRAY_ELEMS(sc->fltmap[p]))
+                if (j >= 65536 /*FF_ARRAY_ELEMS(sc->fltmap[p])*/)
                     return AVERROR_INVALIDDATA;
                 if (end <= 0xFFFF) {
                     sc->fltmap  [p][j++] = i ^ ((i&    0x8000) ? 0 : flip);
@@ -387,6 +387,22 @@ static int decode_slice(AVCodecContext *c, void *arg)
     y      = sc->slice_y;
 
     if (sc->remap) {
+        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);
+                }
+            } else {
+                if (!sc->fltmap[p]) {
+                    sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
+                    if (!sc->fltmap[p])
+                        return AVERROR(ENOMEM);
+                }
+            }
+        }
+
         ret = decode_remap(f, sc);
         if (ret < 0)
             return ret;
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 852eb0c02d2..b704cec9f3a 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -326,7 +326,7 @@ static void load_plane(FFV1Context *f, FFV1SliceContext *sc,
 {
     int x, y;
 
-    memset(sc->fltmap[remap_index], 0, sizeof(sc->fltmap[remap_index]));
+    memset(sc->fltmap[remap_index], 0, 65536 * sizeof(*sc->fltmap[remap_index]));
 
     for (y = 0; y < h; y++) {
         if (f->bits_per_raw_sample <= 8) {
@@ -1012,11 +1012,20 @@ static av_cold int encode_init_internal(AVCodecContext *avctx)
             p->context_count     = s->context_count[p->quant_table_index];
         }
         av_assert0(s->remap_mode >= 0);
-        if (s->remap_mode && s->bits_per_raw_sample == 32) {
+        if (s->remap_mode) {
             for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) {
-                sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
-                if (!sc->unit[p])
-                    return AVERROR(ENOMEM);
+                if (s->bits_per_raw_sample == 32) {
+                    sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
+                    if (!sc->unit[p])
+                        return AVERROR(ENOMEM);
+                    sc->bitmap[p] = av_malloc_array(sc->slice_width * sc->slice_height, sizeof(*sc->bitmap[p]));
+                    if (!sc->bitmap[p])
+                        return AVERROR(ENOMEM);
+                } else {
+                    sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
+                    if (!sc->fltmap[p])
+                        return AVERROR(ENOMEM);
+                }
             }
         }
 
@@ -1821,8 +1830,10 @@ static av_cold int encode_close(AVCodecContext *avctx)
     for (int j = 0; j < s->max_slice_count; j++) {
         FFV1SliceContext *sc = &s->slices[j];
 
-        for(int p = 0; p<4; p++)
+        for(int p = 0; p<4; p++) {
             av_freep(&sc->unit[p]);
+            av_freep(&sc->bitmap[p]);
+        }
     }
 
     av_freep(&avctx->stats_out);
diff --git a/libavcodec/ffv1enc_template.c b/libavcodec/ffv1enc_template.c
index 29dcedd1b6e..85d09da99a5 100644
--- a/libavcodec/ffv1enc_template.c
+++ b/libavcodec/ffv1enc_template.c
@@ -134,7 +134,8 @@ static void RENAME(load_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc,
     int x, y;
     int transparency = f->transparency;
 
-    memset(sc->fltmap, 0, sizeof(sc->fltmap));
+    for (int p = 0; p<3 + transparency; p++)
+        memset(sc->fltmap[p], 0, 65536 * sizeof(**sc->fltmap));
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-- 
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".