From 4aeaee1f4e9d3f8b4018c4ced47dbfd32b190e2e Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Sun, 30 Mar 2025 15:12:17 +0200 Subject: [PATCH] avcodec/ffv1enc: replace the remaining log2() by magic 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 --- libavcodec/ffv1enc.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index 2a188ade77..a09ab30038 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; iunit[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;