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 690AF4C19D
	for <ffmpegdev@gitmailbox.com>; Wed,  2 Apr 2025 00:52:06 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C6258687CB2;
	Wed,  2 Apr 2025 03:52:02 +0300 (EEST)
Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net
 [217.70.183.200])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 56800687D72
 for <ffmpeg-devel@ffmpeg.org>; Wed,  2 Apr 2025 03:51:56 +0300 (EEST)
Received: by mail.gandi.net (Postfix) with ESMTPSA id 84181441E4
 for <ffmpeg-devel@ffmpeg.org>; Wed,  2 Apr 2025 00:51:55 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1743555115;
 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=TJX3Ks39RWWsL6j7joYhlUtxz9ssEdUd115zXkHxiEU=;
 b=Tl+dQSWmDTZrfw5fJCw6iEoGYw766BOn2ZcYhMqCH0a6evJIHlDYqLqAukcNnrBDBkMNg5
 wseCcevOfUFrSPomKYzbbQNvD1OeCPwnEQ1retbUcHNaic18QWSiV+PlaStvsRl7HIgzrE
 O+Fq7GjDNxEfPR25vBEqy576yuhKhtHOX2hU9AvOp0de6NAvolrdcAxd8tVrF5CQ0tXwpO
 p4g1inMRwbFqirX7qII0tSx3OlbzISCy7t1sqJ+SnPW6vogENI/iLxdLjW3KdQwsorBxCe
 +eNyvBeJ4PVYvF3hlcEg7s6Mrt00TKRo5zRjHho8NRR+XYzA+Z9J3wGeOtSSGA==
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Date: Wed,  2 Apr 2025 02:51:54 +0200
Message-ID: <20250402005154.1096898-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: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukeegvdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdeftddmnecujfgurhephffvufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgrvghlucfpihgvuggvrhhmrgihvghruceomhhitghhrggvlhesnhhivgguvghrmhgrhigvrhdrtggtqeenucggtffrrghtthgvrhhnpefhledvvdehgfettdefueevtdeifffhkeeljeetjeeugfelgfffieduheevvefgvdenucfkphepgedurdeiiedrieejrdduudefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepgedurdeiiedrieejrdduudefpdhhvghloheplhhotggrlhhhohhsthdpmhgrihhlfhhrohhmpehmihgthhgrvghlsehnihgvuggvrhhmrgihvghrrdgttgdpnhgspghrtghpthhtohepuddprhgtphhtthhopehffhhmphgvghdquggvvhgvlhesfhhfmhhpvghgrdhorhhg
X-GND-Sasl: michael@niedermayer.cc
Subject: [FFmpeg-devel] [PATCH] avcodec/ffv1: Allocate unit only when needed
 and only as large as 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/20250402005154.1096898-1-michael@niedermayer.cc/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

That is instead of a fixed 65536, we now allocate only as many as there
are pixels.
We also allocate only for the encoder and only when remapping is enabled
and only for 32bit per sample

This should reduce memory consumption, the 2nd array will be
dealt with in a future commit

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1.h    |  2 +-
 libavcodec/ffv1enc.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index 578b504c9f1..9e71e0709d1 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -114,7 +114,7 @@ typedef struct FFV1SliceContext {
     struct Unit {
         uint32_t val; //this is unneeded if you accept a dereference on each access
         uint16_t ndx;
-    } unit[4][65536];
+    } *unit[4];
 } FFV1SliceContext;
 
 typedef struct FFV1Context {
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index cd1f6165f75..852eb0c02d2 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1003,12 +1003,22 @@ static av_cold int encode_init_internal(AVCodecContext *avctx)
     s->slice_count = s->max_slice_count;
 
     for (int j = 0; j < s->slice_count; j++) {
+        FFV1SliceContext *sc = &s->slices[j];
+
         for (int i = 0; i < s->plane_count; i++) {
             PlaneContext *const p = &s->slices[j].plane[i];
 
             p->quant_table_index = s->context_model;
             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) {
+            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);
+            }
+        }
 
         ff_build_rac_states(&s->slices[j].c, 0.05 * (1LL << 32), 256 - 8);
 
@@ -1808,6 +1818,13 @@ static av_cold int encode_close(AVCodecContext *avctx)
 {
     FFV1Context *const s = avctx->priv_data;
 
+    for (int j = 0; j < s->max_slice_count; j++) {
+        FFV1SliceContext *sc = &s->slices[j];
+
+        for(int p = 0; p<4; p++)
+            av_freep(&sc->unit[p]);
+    }
+
     av_freep(&avctx->stats_out);
     ff_ffv1_close(s);
 
-- 
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".