* [FFmpeg-devel] [PATCH] avcodec/ffv1enc: Add -remap_optimizer option
@ 2025-03-28 11:41 Michael Niedermayer
2025-03-30 20:25 ` Michael Niedermayer
0 siblings, 1 reply; 2+ messages in thread
From: Michael Niedermayer @ 2025-03-28 11:41 UTC (permalink / raw)
To: FFmpeg development discussions and patches
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".
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avcodec/ffv1enc: Add -remap_optimizer option
2025-03-28 11:41 [FFmpeg-devel] [PATCH] avcodec/ffv1enc: Add -remap_optimizer option Michael Niedermayer
@ 2025-03-30 20:25 ` Michael Niedermayer
0 siblings, 0 replies; 2+ messages in thread
From: Michael Niedermayer @ 2025-03-30 20:25 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1.1: Type: text/plain, Size: 775 bytes --]
On Fri, Mar 28, 2025 at 12:41:51PM +0100, Michael Niedermayer wrote:
> 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(-)
will apply
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
The day soldiers stop bringing you their problems is the day you have stopped
leading them. They have either lost confidence that you can help or concluded
you do not care. Either case is a failure of leadership. - Colin Powell
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
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".
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-03-30 20:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-28 11:41 [FFmpeg-devel] [PATCH] avcodec/ffv1enc: Add -remap_optimizer option Michael Niedermayer
2025-03-30 20:25 ` Michael Niedermayer
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