Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: replace the remaining log2() by magic
Date: Sun, 30 Mar 2025 17:00:23 +0200
Message-ID: <20250330150023.1484947-2-michael@niedermayer.cc> (raw)
In-Reply-To: <20250330150023.1484947-1-michael@niedermayer.cc>

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".

  reply	other threads:[~2025-03-30 15:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-30 15:00 [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: replace 3 double precision log2() by 1 single precision log2() Michael Niedermayer
2025-03-30 15:00 ` Michael Niedermayer [this message]
2025-04-02 21:21 ` Michael Niedermayer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250330150023.1484947-2-michael@niedermayer.cc \
    --to=michael@niedermayer.cc \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git