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 23B254BA70
	for <ffmpegdev@gitmailbox.com>; Fri, 28 Mar 2025 02:27:01 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 63D72687C1C;
	Fri, 28 Mar 2025 04:26:57 +0200 (EET)
Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net
 [217.70.183.197])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 70592687BBF
 for <ffmpeg-devel@ffmpeg.org>; Fri, 28 Mar 2025 04:26:50 +0200 (EET)
Received: by mail.gandi.net (Postfix) with ESMTPSA id C814944300
 for <ffmpeg-devel@ffmpeg.org>; Fri, 28 Mar 2025 02:26:49 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1743128810;
 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=O7uReIVYErWS5mbdePiLbERUx4FpGRDi94sVb0TQYpM=;
 b=Rn5Xr5lyUl2YckSVx5Jg3ONu/Xr6pYRbOfp3kofcWQoLoG61sZKe+4PJoRGcnZRpPLSsU3
 F8DWmiVjRM82CeY1m0hGy9DraioDDyTbEs9nkO61bArpPqbaOEZ9Q58MbiuhSfjnip06Gz
 NJ8J+r2/j6x8nAUnDgGRSL4U5Er9ADdCThTX//mMu3U+pnyABEgAW8fwmr/h9csbHnS7hH
 VapfkLubPoJTK+ImICFBZcNF+4ZMS3dpzOlAM/OPleNoLHODobdS4hEAdQhABnpQLgghtt
 FZKDGZRCsajGWxOa+p8fnAR/M8FLOgSfzXBiSUJZKv0+GFOOgZQ/YxiZlcGe5w==
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Date: Fri, 28 Mar 2025 03:26:49 +0100
Message-ID: <20250328022649.3866952-1-michael@niedermayer.cc>
X-Mailer: git-send-email 2.48.1
MIME-Version: 1.0
X-GND-State: clean
X-GND-Score: -70
X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddujedttdekucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfghrlhcuvffnffculdeftddmnecujfgurhephffvufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefoihgthhgrvghlucfpihgvuggvrhhmrgihvghruceomhhitghhrggvlhesnhhivgguvghrmhgrhigvrhdrtggtqeenucggtffrrghtthgvrhhnpefhledvvdehgfettdefueevtdeifffhkeeljeetjeeugfelgfffieduheevvefgvdenucfkphepgedurdeiiedrieejrdduudefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepgedurdeiiedrieejrdduudefpdhhvghloheplhhotggrlhhhohhsthdpmhgrihhlfhhrohhmpehmihgthhgrvghlsehnihgvuggvrhhmrgihvghrrdgttgdpnhgspghrtghpthhtohepuddprhgtphhtthhopehffhhmphgvghdquggvvhgvlhesfhhfmhhpvghgrdhorhhg
X-GND-Sasl: michael@niedermayer.cc
Subject: [FFmpeg-devel] [PATCH v2] avcodec/ffv1enc: Better heuristic for
 selecting mul values.
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/20250328022649.3866952-1-michael@niedermayer.cc/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

This increases the search space from the hardcoded 17 tables
to ~ 23^512 tables

The mul_count choice is chosen by bruteforce

All testcases tried, improve

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/ffv1enc.c | 89 +++++++++++++++++++++++++-------------------
 1 file changed, 51 insertions(+), 38 deletions(-)

diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index bc59f96746c..5e7c714035e 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1409,52 +1409,65 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
     av_assert0 (s.pixel_num <= 65536);
 
     for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
-        float score_tab[16] = {0};
-        int64_t last_val = -1;
-        int best_index = 0;
+        float score_sum[2] = {0};
+        int mul_all[2][513];
         s.rc = sc->c;
         s.i = 0;
         s.p = p;
 
-        for(int v = 0; v< 512; v++) {
-            if (v >= 0x378/8 && v <= 23 + 0x378/8) {
-                s.mul[v] = -(0x800080 >> (v - 0x378/8));
-            } else
-                s.mul[v] = -1;
-        }
-        for (int i= 0; i<s.pixel_num; i++) {
-            int64_t val = sc->unit[p][i].val;
-            if (val != last_val) {
-                av_assert2(last_val < val);
-                for(int si= 0; si < FF_ARRAY_ELEMS(score_tab); si++) {
-                    int64_t delta = val - last_val;
-                    int mul;
-                    int64_t cost;
-
-                    if (last_val < 0) {
-                        mul = 1;
-                    } else if (si + 1 == FF_ARRAY_ELEMS(score_tab)) {
-                        mul = -s.mul[ (last_val + 1) >> (32-9) ];
-                    } else
-                        mul = 1<<si;
-
-                    cost = FFMAX((delta + mul/2)  / mul, 1);
-                    score_tab[si] += log2(cost) + log2(fabs(delta - cost*mul)+1);
+        for (int mul_count= 1; mul_count<=512; mul_count+=511) {
+            float score_tab_all[513][23] = {0};
+            int64_t last_val = -1;
+            int *mul_tab = mul_all[mul_count>>9];
+            int last_mul_index = -1;
+            score_sum[mul_count>>9] += log2(mul_count);
+            for (int i= 0; i<s.pixel_num; i++) {
+                int64_t val = sc->unit[p][i].val;
+                int mul_index = (val + 1LL)*mul_count >> 32;
+                if (val != last_val) {
+                    float *score_tab = score_tab_all[(last_val + 1LL)*mul_count >> 32];
+                    av_assert2(last_val < val);
+                    for(int si= 0; si < FF_ARRAY_ELEMS(*score_tab_all); si++) {
+                        int64_t delta = val - last_val;
+                        int mul;
+                        int64_t cost;
+
+                        if (last_val < 0) {
+                            mul = 1;
+                        } else {
+                            mul = (0x10001LL)<<si >> 16;
+                        }
+
+                        cost = FFMAX((delta + mul/2)  / mul, 1);
+                        score_tab[si] += log2(cost);
+                        if (mul > 1)
+                            score_tab[si] += log2(fabs(delta - cost*mul)+1) * (1 + (mul_count > 1));
+                        if (mul_index != last_mul_index)
+                            score_tab[si] += 0.5*log2(mul);
+                    }
                 }
                 last_val = val;
+                last_mul_index = mul_index;
             }
+            for(int i= 0; i<mul_count; i++) {
+                int best_index = 0;
+                float *score_tab = score_tab_all[i];
+                for(int si= 1; si < FF_ARRAY_ELEMS(*score_tab_all); si++) {
+                    if (score_tab[si] < score_tab[ best_index ])
+                        best_index = si;
+                }
+                mul_tab[i] = -((0x10001LL)<<best_index >> 16);
+                score_sum[mul_count>>9] += score_tab[ best_index ];
+            }
+            mul_tab[mul_count] = 1;
+
+            s.mul_count = mul_count;
+            memcpy(s.mul, mul_all[s.mul_count>>9], sizeof(*s.mul)*(s.mul_count+1));
+            score_sum[mul_count>>9] = encode_float32_remap_segment(sc, &s, 0, 0);
         }
-        for(int si= 1; si < FF_ARRAY_ELEMS(score_tab); si++) {
-            if (score_tab[si] < score_tab[ best_index ])
-                best_index = si;
-        }
-        if (best_index + 1 < FF_ARRAY_ELEMS(score_tab)) {
-            s.mul[0] = -1 << best_index;
-            s.mul_count = 1;
-        } else {
-            s.mul_count = 512;
-        }
-        s.mul[s.mul_count] = 1;
+
+        s.mul_count = score_sum[0] <= score_sum[1] ? 1 : 512;
+        memcpy(s.mul, mul_all[s.mul_count>>9], sizeof(*s.mul)*(s.mul_count+1));
 
         encode_float32_remap_segment(sc, &s, 1, 1);
 
-- 
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".