1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

avcodec/ffv1enc: Add -remap_optimizer option

This allows tuning how much effort (time) the encoder spends on
optimizing the remap table

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer
2025-03-28 11:36:27 +01:00
parent f765085115
commit 1d2c391005
3 changed files with 42 additions and 16 deletions

View File

@ -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}

View File

@ -148,7 +148,7 @@ typedef struct FFV1Context {
int colorspace;
int flt;
int remap_mode;
int remap_optimizer;
int use32bit;

View File

@ -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 }
};