1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-10 06:10:52 +02:00

avcodec/ffv1enc: Eliminate RemapEncoderState

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Michael Niedermayer
2025-03-28 09:23:25 +01:00
parent 3faee894fc
commit f765085115

View File

@@ -1252,64 +1252,47 @@ static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc,
AV_QSORT(sc->unit[3], i, struct Unit, CMP); AV_QSORT(sc->unit[3], i, struct Unit, CMP);
} }
typedef struct RemapEncoderState {
uint8_t state[2][3][32];
int mul[4096+1];
RangeCoder rc;
int lu;
int run;
int64_t last_val;
int compact_index;
int mul_count;
int i;
int pixel_num;
int p;
int current_mul_index;
int run1final;
int64_t run1start_i;
int64_t run1start_last_val;
int run1start_mul_index;
} RemapEncoderState;
static int encode_float32_remap_segment(FFV1SliceContext *sc, static int encode_float32_remap_segment(FFV1SliceContext *sc,
int p, int mul_count, int *mul_tab, int update, int final) int p, int mul_count, int *mul_tab, int update, int final)
{ {
RemapEncoderState s; const int pixel_num = sc->slice_width * sc->slice_height;
uint8_t state[2][3][32];
int mul[4096+1];
RangeCoder rc = sc->c;
int lu = 0;
int run = 0;
int64_t last_val = -1;
int compact_index = -1;
int i = 0;
int current_mul_index = -1;
int run1final = 0;
int64_t run1start_i;
int64_t run1start_last_val;
int run1start_mul_index;
s.pixel_num = sc->slice_width * sc->slice_height; memcpy(mul, mul_tab, sizeof(*mul_tab)*(mul_count+1));
s.p = p; memset(state, 128, sizeof(state));
s.i = 0; put_symbol(&rc, state[0][0], mul_count, 0);
s.rc = sc->c; memset(state, 128, sizeof(state));
s.mul_count = mul_count;
memcpy(s.mul, mul_tab, sizeof(*mul_tab)*(mul_count+1));
memset(s.state, 128, sizeof(s.state));
put_symbol(&s.rc, s.state[0][0], s.mul_count, 0);
memset(s.state, 128, sizeof(s.state));
s.last_val = -1;
s.compact_index = -1;
s.lu = 0;
s.run = 0;
s.current_mul_index = -1;
s.run1final = 0;
for (; s.i < s.pixel_num+1; s.i++) { for (; i < pixel_num+1; i++) {
int current_mul = s.current_mul_index < 0 ? 1 : FFABS(s.mul[s.current_mul_index]); int current_mul = current_mul_index < 0 ? 1 : FFABS(mul[current_mul_index]);
int64_t val; int64_t val;
if (s.i == s.pixel_num) { if (i == pixel_num) {
if (s.last_val == 0xFFFFFFFF) { if (last_val == 0xFFFFFFFF) {
break; break;
} else { } else {
val = s.last_val + ((1LL<<32) - s.last_val + current_mul - 1) / current_mul * current_mul; val = last_val + ((1LL<<32) - last_val + current_mul - 1) / current_mul * current_mul;
av_assert2(val >= (1LL<<32)); av_assert2(val >= (1LL<<32));
val += s.lu * current_mul; //ensure a run1 ends val += lu * current_mul; //ensure a run1 ends
} }
} else } else
val = sc->unit[s.p][s.i].val; val = sc->unit[p][i].val;
if (s.last_val != val) { if (last_val != val) {
int64_t delta = val - s.last_val; int64_t delta = val - last_val;
int64_t step = FFMAX(1, (delta + current_mul/2) / current_mul); int64_t step = FFMAX(1, (delta + current_mul/2) / current_mul);
av_assert2(s.last_val < val); av_assert2(last_val < val);
av_assert2(current_mul > 0); av_assert2(current_mul > 0);
delta -= step*current_mul; delta -= step*current_mul;
@@ -1317,69 +1300,69 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc,
av_assert2(delta > -current_mul); av_assert2(delta > -current_mul);
av_assert2(step > 0); av_assert2(step > 0);
if (s.lu) { if (lu) {
if (!s.run) { if (!run) {
s.run1start_i = s.i - 1; run1start_i = i - 1;
s.run1start_last_val = s.last_val; run1start_last_val = last_val;
s.run1start_mul_index= s.current_mul_index; run1start_mul_index= current_mul_index;
} }
if (step == 1) { if (step == 1) {
if (s.run1final) { if (run1final) {
if (current_mul>1) if (current_mul>1)
put_symbol_inline(&s.rc, s.state[s.lu][1], delta, 1, NULL, NULL); put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
} }
s.run ++; run ++;
av_assert2(s.last_val + current_mul + delta == val); av_assert2(last_val + current_mul + delta == val);
} else { } else {
if (s.run1final) { if (run1final) {
if (s.run == 0) if (run == 0)
s.lu ^= 1; lu ^= 1;
s.i--; // we did not encode val so we need to backstep i--; // we did not encode val so we need to backstep
s.last_val += current_mul; last_val += current_mul;
} else { } else {
put_symbol_inline(&s.rc, s.state[s.lu][0], s.run, 0, NULL, NULL); put_symbol_inline(&rc, state[lu][0], run, 0, NULL, NULL);
s.i = s.run1start_i; i = run1start_i;
s.last_val = s.run1start_last_val; // we could compute this instead of storing last_val = run1start_last_val; // we could compute this instead of storing
s.current_mul_index = s.run1start_mul_index; current_mul_index = run1start_mul_index;
} }
s.run1final ^= 1; run1final ^= 1;
s.run = 0; run = 0;
continue; continue;
} }
} else { } else {
av_assert2(s.run == 0); av_assert2(run == 0);
av_assert2(s.run1final == 0); av_assert2(run1final == 0);
put_symbol_inline(&s.rc, s.state[s.lu][0], step - 1, 0, NULL, NULL); put_symbol_inline(&rc, state[lu][0], step - 1, 0, NULL, NULL);
if (current_mul > 1) if (current_mul > 1)
put_symbol_inline(&s.rc, s.state[s.lu][1], delta, 1, NULL, NULL); put_symbol_inline(&rc, state[lu][1], delta, 1, NULL, NULL);
if (step == 1) if (step == 1)
s.lu ^= 1; lu ^= 1;
av_assert2(s.last_val + step * current_mul + delta == val); av_assert2(last_val + step * current_mul + delta == val);
} }
s.last_val = val; last_val = val;
s.current_mul_index = ((s.last_val + 1) * s.mul_count) >> 32; current_mul_index = ((last_val + 1) * mul_count) >> 32;
if (!s.run || s.run1final) { if (!run || run1final) {
av_assert2(s.mul[ s.current_mul_index ]); av_assert2(mul[ current_mul_index ]);
if (s.mul[ s.current_mul_index ] < 0) { if (mul[ current_mul_index ] < 0) {
av_assert2(s.i < s.pixel_num); av_assert2(i < pixel_num);
s.mul[ s.current_mul_index ] *= -1; mul[ current_mul_index ] *= -1;
put_symbol_inline(&s.rc, s.state[0][2], s.mul[ s.current_mul_index ], 0, NULL, NULL); put_symbol_inline(&rc, state[0][2], mul[ current_mul_index ], 0, NULL, NULL);
} }
s.compact_index ++; compact_index ++;
} }
} }
if (!s.run || s.run1final) if (!run || run1final)
if (final && s.i < s.pixel_num) if (final && i < pixel_num)
sc->bitmap[s.p][sc->unit[s.p][s.i].ndx] = s.compact_index; sc->bitmap[p][sc->unit[p][i].ndx] = compact_index;
} }
if (update) { if (update) {
sc->c = s.rc; sc->c = rc;
} }
return get_rac_count(&s.rc); return get_rac_count(&rc);
} }
static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,