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 7C9C14A934
	for <ffmpegdev@gitmailbox.com>; Sun, 30 Mar 2025 15:00:50 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C8A58687CB6;
	Sun, 30 Mar 2025 18:00:34 +0300 (EEST)
Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net
 [217.70.183.201])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0DC7C687BA0
 for <ffmpeg-devel@ffmpeg.org>; Sun, 30 Mar 2025 18:00:27 +0300 (EEST)
Received: by mail.gandi.net (Postfix) with ESMTPSA id 34BDC4441E
 for <ffmpeg-devel@ffmpeg.org>; Sun, 30 Mar 2025 15:00:25 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1743346826;
 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:
 in-reply-to:in-reply-to:references:references;
 bh=CFe6QRpNSuZUwWSui84Qy4pLZ8wrP7EipB5wTEtKJqg=;
 b=PF9yUC6lirTM35zmif3exjBS2TcnxRJlHRMjyt44be10uUaENNXF3O5Oa8GaUXARINFNkv
 bp5Isw9FbZGBMwPQD9jzDLUWsQsu3nwu5TwIPk53BkY+w3CxGOugYMbqy8EVtECa8VuxSB
 zCvFzICCw44p3VdSYs9TYqYvOs9zsyz9C/HcDdb4XZHNjKeSCEI5dHIubQtVerbrXt0AXX
 +mP/V3VTBTDKuwSMSQCLv31GruTIKnOd+ixjxhIDHk9DhY/7/EEFs9PQ64v6ftk+DihXsW
 AinxZebfObvJSXBjb0TiqdzJ8BQgQhMzai4Lr/tBGc+hzillWvh6/87B9A3Sew==
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Date: Sun, 30 Mar 2025 17:00:23 +0200
Message-ID: <20250330150023.1484947-2-michael@niedermayer.cc>
X-Mailer: git-send-email 2.49.0
In-Reply-To: <20250330150023.1484947-1-michael@niedermayer.cc>
References: <20250330150023.1484947-1-michael@niedermayer.cc>
MIME-Version: 1.0
X-GND-State: clean
X-GND-Score: -70
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddujeejfeeiucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdeftddmnecujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgrvghlucfpihgvuggvrhhmrgihvghruceomhhitghhrggvlhesnhhivgguvghrmhgrhigvrhdrtggtqeenucggtffrrghtthgvrhhnpedvgfefudeijeetieejkefgfffhtdeludeuffdvfeelieevjeetvedufeetfeejfeenucfkphepgedurdeiiedrieejrdduudefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepgedurdeiiedrieejrdduudefpdhhvghloheplhhotggrlhhhohhsthdpmhgrihhlfhhrohhmpehmihgthhgrvghlsehnihgvuggvrhhmrgihvghrrdgttgdpnhgspghrtghpthhtohepuddprhgtphhtthhopehffhhmphgvghdquggvvhgvlhesfhhfmhhpvghgrdhorhhg
X-GND-Sasl: michael@niedermayer.cc
Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: replace the remaining
 log2() by magic
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/20250330150023.1484947-2-michael@niedermayer.cc/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

big secret, reading a float as the corresponding integer is a good
approximation of log2() for numbers not too close to 1.0. At the same
time it maintains strict monotonicity

this reduces run time from 19sec to 17sec for the slowest of my testcases
(with default remap_optimizer 3)

Compression is about the same:

-rw-r----- 1 michael michael  497603370 Mar 30 15:23 float-303503-fixed-40-optim-3-1log2F.nut
-rw-r----- 1 michael michael  497603374 Mar 28 11:27 float-303503-fixed-40-optim-3b.nut
-rw-r----- 1 michael michael  549938852 Mar 28 11:27 float-303503-float16-40-optim-3b.nut
-rw-r----- 1 michael michael  549938857 Mar 30 15:23 float-303503-float16-40-optim-3-1log2F.nut
-rw-r----- 1 michael michael 1150827841 Mar 28 11:28 float-303503-float-40-optim-3b.nut
-rw-r----- 1 michael michael 1150832913 Mar 30 15:22 float-303503-float-40-optim-3ref-log2F2.nut

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1enc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index 2a188ade776..a09ab300389 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1375,6 +1375,7 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
     const int log2_mul_step       = ((int[]){  1,  8,  1,  1,  1,   1})[f->remap_optimizer];
     const int bruteforce_count    = ((int[]){  0,  0,  0,  1,  1,   1})[f->remap_optimizer];
     const int stair_mode          = ((int[]){  0,  0,  0,  1,  0,   0})[f->remap_optimizer];
+    const int magic_log2          = ((int[]){  1,  1,  1,  1,  0,   0})[f->remap_optimizer];
 
     av_assert0 (pixel_num <= 65536);
 
@@ -1390,7 +1391,9 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
             int last_mul_index = -1;
             int mul_count = 1 << log2_mul_count;
 
-            score_sum[log2_mul_count] += log2_mul_count * log2_mul_count;
+            score_sum[log2_mul_count] = 2 * log2_mul_count;
+            if (magic_log2)
+                score_sum[log2_mul_count] = av_float2int((float)mul_count * mul_count);
             for (int i= 0; i<pixel_num; i++) {
                 int64_t val = sc->unit[p][i].val;
                 int mul_index = (val + 1LL)*mul_count >> 32;
@@ -1424,7 +1427,10 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
                         score *= score;
                         if (mul_index != last_mul_index)
                             score *= mul;
-                        score_tab[si] += log2f(score);
+                        if (magic_log2) {
+                            score_tab[si] += av_float2int(score);
+                        } else
+                            score_tab[si] += log2f(score);
                     }
                 }
                 last_val = val;
-- 
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".