From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avcodec/ffv1enc: Add -remap_optimizer option
Date: Fri, 28 Mar 2025 12:41:51 +0100
Message-ID: <20250328114151.918548-1-michael@niedermayer.cc> (raw)
This allows tuning how much effort (time) the encoder spends on
optimizing the remap table
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
doc/encoders.texi | 4 ++++
libavcodec/ffv1.h | 2 +-
libavcodec/ffv1enc.c | 52 +++++++++++++++++++++++++++++++-------------
3 files changed, 42 insertions(+), 16 deletions(-)
diff --git a/doc/encoders.texi b/doc/encoders.texi
index f3fcc1aa60a..f987a234bc6 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -554,6 +554,10 @@ use 8bit default
@item greater8bit
use >8bit default
@end table
+
+@item remap_optimizer
+0 - 5, default 3, how much effort the encoder puts into optimizing the remap table.
+
@end table
@anchor{flac}
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index 09118e0b7dc..578b504c9f1 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -148,7 +148,7 @@ typedef struct FFV1Context {
int colorspace;
int flt;
int remap_mode;
-
+ int remap_optimizer;
int use32bit;
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index b156ff2dc10..a09b31218db 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -1369,33 +1369,46 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
const uint8_t *src[4])
{
int pixel_num = sc->slice_width * sc->slice_height;
- int mul_count;
+ const int max_log2_mul_count = ((int[]){ 1, 1, 1, 9, 9, 10})[f->remap_optimizer];
+ const int log2_mul_count_step = ((int[]){ 1, 1, 1, 9, 9, 1})[f->remap_optimizer];
+ const int max_log2_mul = ((int[]){ 1, 8, 8, 9, 22, 22})[f->remap_optimizer];
+ 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];
av_assert0 (pixel_num <= 65536);
for (int p= 0; p < 1 + 2*f->chroma_planes + f->transparency; p++) {
- float score_sum[2] = {0};
- int mul_all[2][513];
+ int best_log2_mul_count = 0;
+ float score_sum[11] = {0};
+ int mul_all[11][1025];
- for (mul_count= 1; mul_count<=512; mul_count+=511) {
- float score_tab_all[513][23] = {0};
+ for (int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; log2_mul_count += log2_mul_count_step) {
+ float score_tab_all[1025][23] = {0};
int64_t last_val = -1;
- int *mul_tab = mul_all[mul_count>>9];
+ int *mul_tab = mul_all[log2_mul_count];
int last_mul_index = -1;
- score_sum[mul_count>>9] += log2(mul_count);
+ int mul_count = 1 << log2_mul_count;
+
+ score_sum[log2_mul_count] += log2_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;
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++) {
+ for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
int64_t delta = val - last_val;
int mul;
int64_t cost;
if (last_val < 0) {
mul = 1;
+ } else if (stair_mode && mul_count == 512 && si == max_log2_mul ) {
+ if (mul_index >= 0x378/8 && mul_index <= 23 + 0x378/8) {
+ mul = (0x800080 >> (mul_index - 0x378/8));
+ } else
+ mul = 1;
} else {
mul = (0x10001LL)<<si >> 16;
}
@@ -1414,20 +1427,29 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
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++) {
+ for(int si= 0; si <= max_log2_mul; si += log2_mul_step) {
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 ];
+ if (stair_mode && mul_count == 512 && best_index == max_log2_mul ) {
+ if (i >= 0x378/8 && i <= 23 + 0x378/8) {
+ mul_tab[i] = -(0x800080 >> (i - 0x378/8));
+ } else
+ mul_tab[i] = -1;
+ } else
+ mul_tab[i] = -((0x10001LL)<<best_index >> 16);
+ score_sum[log2_mul_count] += score_tab[ best_index ];
}
mul_tab[mul_count] = 1;
- score_sum[mul_count>>9] = encode_float32_remap_segment(sc, p, mul_count, mul_all[mul_count>>9], 0, 0);
+ if (bruteforce_count)
+ score_sum[log2_mul_count] = encode_float32_remap_segment(sc, p, mul_count, mul_all[log2_mul_count], 0, 0);
+
+ if (score_sum[log2_mul_count] < score_sum[best_log2_mul_count])
+ best_log2_mul_count = log2_mul_count;
}
- mul_count = score_sum[0] <= score_sum[1] ? 1 : 512;
- encode_float32_remap_segment(sc, p, mul_count, mul_all[mul_count>>9], 1, 1);
+ encode_float32_remap_segment(sc, p, 1<<best_log2_mul_count, mul_all[best_log2_mul_count], 1, 1);
}
}
@@ -1813,7 +1835,7 @@ static const AVOption options[] = {
{ .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
{ "flipdualrle", "Dual RLE", 0, AV_OPT_TYPE_CONST,
{ .i64 = 2 }, INT_MIN, INT_MAX, VE, .unit = "remap_mode" },
-
+ { "remap_optimizer", "Remap Optimizer", OFFSET(remap_optimizer), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 5, VE, .unit = "remap_optimizer" },
{ NULL }
};
--
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".
next reply other threads:[~2025-03-28 11:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-28 11:41 Michael Niedermayer [this message]
2025-03-30 20:25 ` 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=20250328114151.918548-1-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