* [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state @ 2025-03-28 8:45 Michael Niedermayer 2025-03-28 8:45 ` [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: Eliminate RemapEncoderState Michael Niedermayer 2025-03-30 20:25 ` [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state Michael Niedermayer 0 siblings, 2 replies; 3+ messages in thread From: Michael Niedermayer @ 2025-03-28 8:45 UTC (permalink / raw) To: FFmpeg development discussions and patches Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> --- libavcodec/ffv1enc.c | 77 ++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 52 deletions(-) diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index 5e7c714035e..e7372e572b9 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -1271,44 +1271,26 @@ typedef struct RemapEncoderState { int run1start_mul_index; } RemapEncoderState; -static inline void copy_state(RemapEncoderState *dst, const RemapEncoderState *src) -{ - dst->rc = src->rc; - memcpy(dst->mul, src->mul, (src->mul_count + 1) * sizeof(src->mul[0])); - memcpy(dst->state, src->state, sizeof(dst->state)); - dst->lu = src->lu; - dst->run = src->run; - dst->last_val = src->last_val; - dst->compact_index = src->compact_index; - dst->mul_count = src->mul_count; - dst->i = src->i; - dst->pixel_num = src->pixel_num; - dst->p = src->p; - dst->current_mul_index = src->current_mul_index; - dst->run1final = src->run1final; - dst->run1start_i = src->run1start_i; - dst->run1start_last_val = src->run1start_last_val; - dst->run1start_mul_index = src->run1start_mul_index; -} - static int encode_float32_remap_segment(FFV1SliceContext *sc, - RemapEncoderState *state_arg, int update, int final) + int p, int mul_count, int *mul_tab, int update, int final) { RemapEncoderState s; - copy_state(&s, state_arg); - - if (s.i == 0) { - 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; - } + s.pixel_num = sc->slice_width * sc->slice_height; + s.p = p; + s.i = 0; + s.rc = sc->c; + 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++) { int current_mul = s.current_mul_index < 0 ? 1 : FFABS(s.mul[s.current_mul_index]); @@ -1395,7 +1377,7 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, } if (update) { - copy_state(state_arg, &s); + sc->c = s.rc; } return get_rac_count(&s.rc); } @@ -1403,25 +1385,22 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, const uint8_t *src[4]) { - RemapEncoderState s; - s.pixel_num = sc->slice_width * sc->slice_height; + int pixel_num = sc->slice_width * sc->slice_height; + int mul_count; - av_assert0 (s.pixel_num <= 65536); + 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]; - s.rc = sc->c; - s.i = 0; - s.p = p; - for (int mul_count= 1; mul_count<=512; mul_count+=511) { + for (mul_count= 1; mul_count<=512; mul_count+=511) { float score_tab_all[513][23] = {0}; int64_t last_val = -1; int *mul_tab = mul_all[mul_count>>9]; int last_mul_index = -1; score_sum[mul_count>>9] += log2(mul_count); - for (int i= 0; i<s.pixel_num; i++) { + 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) { @@ -1461,17 +1440,11 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, } mul_tab[mul_count] = 1; - s.mul_count = mul_count; - memcpy(s.mul, mul_all[s.mul_count>>9], sizeof(*s.mul)*(s.mul_count+1)); - score_sum[mul_count>>9] = encode_float32_remap_segment(sc, &s, 0, 0); + score_sum[mul_count>>9] = encode_float32_remap_segment(sc, p, mul_count, mul_all[mul_count>>9], 0, 0); } - s.mul_count = score_sum[0] <= score_sum[1] ? 1 : 512; - memcpy(s.mul, mul_all[s.mul_count>>9], sizeof(*s.mul)*(s.mul_count+1)); - - encode_float32_remap_segment(sc, &s, 1, 1); - - sc->c = s.rc; + 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); } } -- 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] 3+ messages in thread
* [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: Eliminate RemapEncoderState 2025-03-28 8:45 [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state Michael Niedermayer @ 2025-03-28 8:45 ` Michael Niedermayer 2025-03-30 20:25 ` [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state Michael Niedermayer 1 sibling, 0 replies; 3+ messages in thread From: Michael Niedermayer @ 2025-03-28 8:45 UTC (permalink / raw) To: FFmpeg development discussions and patches Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> --- libavcodec/ffv1enc.c | 151 +++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 84 deletions(-) diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index e7372e572b9..b156ff2dc10 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -1252,64 +1252,47 @@ static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc, AV_QSORT(sc->unit[3], i, struct Unit, CMP); } -typedef struct RemapEncoderState { +static int encode_float32_remap_segment(FFV1SliceContext *sc, + int p, int mul_count, int *mul_tab, int update, int final) +{ + const int pixel_num = sc->slice_width * sc->slice_height; 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; + 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; -} RemapEncoderState; -static int encode_float32_remap_segment(FFV1SliceContext *sc, - int p, int mul_count, int *mul_tab, int update, int final) -{ - RemapEncoderState s; - - s.pixel_num = sc->slice_width * sc->slice_height; - s.p = p; - s.i = 0; - s.rc = sc->c; - 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++) { - int current_mul = s.current_mul_index < 0 ? 1 : FFABS(s.mul[s.current_mul_index]); + memcpy(mul, mul_tab, sizeof(*mul_tab)*(mul_count+1)); + memset(state, 128, sizeof(state)); + put_symbol(&rc, state[0][0], mul_count, 0); + memset(state, 128, sizeof(state)); + + for (; i < pixel_num+1; i++) { + int current_mul = current_mul_index < 0 ? 1 : FFABS(mul[current_mul_index]); int64_t val; - if (s.i == s.pixel_num) { - if (s.last_val == 0xFFFFFFFF) { + if (i == pixel_num) { + if (last_val == 0xFFFFFFFF) { break; } 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)); - val += s.lu * current_mul; //ensure a run1 ends + val += lu * current_mul; //ensure a run1 ends } } else - val = sc->unit[s.p][s.i].val; + val = sc->unit[p][i].val; - if (s.last_val != val) { - int64_t delta = val - s.last_val; + if (last_val != val) { + int64_t delta = val - last_val; 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); delta -= step*current_mul; @@ -1317,69 +1300,69 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, av_assert2(delta > -current_mul); av_assert2(step > 0); - if (s.lu) { - if (!s.run) { - s.run1start_i = s.i - 1; - s.run1start_last_val = s.last_val; - s.run1start_mul_index= s.current_mul_index; + if (lu) { + if (!run) { + run1start_i = i - 1; + run1start_last_val = last_val; + run1start_mul_index= current_mul_index; } if (step == 1) { - if (s.run1final) { + if (run1final) { 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 ++; - av_assert2(s.last_val + current_mul + delta == val); + run ++; + av_assert2(last_val + current_mul + delta == val); } else { - if (s.run1final) { - if (s.run == 0) - s.lu ^= 1; - s.i--; // we did not encode val so we need to backstep - s.last_val += current_mul; + if (run1final) { + if (run == 0) + lu ^= 1; + i--; // we did not encode val so we need to backstep + last_val += current_mul; } else { - put_symbol_inline(&s.rc, s.state[s.lu][0], s.run, 0, NULL, NULL); - s.i = s.run1start_i; - s.last_val = s.run1start_last_val; // we could compute this instead of storing - s.current_mul_index = s.run1start_mul_index; + put_symbol_inline(&rc, state[lu][0], run, 0, NULL, NULL); + i = run1start_i; + last_val = run1start_last_val; // we could compute this instead of storing + current_mul_index = run1start_mul_index; } - s.run1final ^= 1; + run1final ^= 1; - s.run = 0; + run = 0; continue; } } else { - av_assert2(s.run == 0); - av_assert2(s.run1final == 0); - put_symbol_inline(&s.rc, s.state[s.lu][0], step - 1, 0, NULL, NULL); + av_assert2(run == 0); + av_assert2(run1final == 0); + put_symbol_inline(&rc, state[lu][0], step - 1, 0, NULL, NULL); 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) - 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; - s.current_mul_index = ((s.last_val + 1) * s.mul_count) >> 32; - if (!s.run || s.run1final) { - av_assert2(s.mul[ s.current_mul_index ]); - if (s.mul[ s.current_mul_index ] < 0) { - av_assert2(s.i < s.pixel_num); - s.mul[ s.current_mul_index ] *= -1; - put_symbol_inline(&s.rc, s.state[0][2], s.mul[ s.current_mul_index ], 0, NULL, NULL); + last_val = val; + current_mul_index = ((last_val + 1) * mul_count) >> 32; + if (!run || run1final) { + av_assert2(mul[ current_mul_index ]); + if (mul[ current_mul_index ] < 0) { + av_assert2(i < pixel_num); + mul[ current_mul_index ] *= -1; + 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 (final && s.i < s.pixel_num) - sc->bitmap[s.p][sc->unit[s.p][s.i].ndx] = s.compact_index; + if (!run || run1final) + if (final && i < pixel_num) + sc->bitmap[p][sc->unit[p][i].ndx] = compact_index; } 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, -- 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] 3+ messages in thread
* Re: [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state 2025-03-28 8:45 [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state Michael Niedermayer 2025-03-28 8:45 ` [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: Eliminate RemapEncoderState Michael Niedermayer @ 2025-03-30 20:25 ` Michael Niedermayer 1 sibling, 0 replies; 3+ 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: 458 bytes --] On Fri, Mar 28, 2025 at 09:45:41AM +0100, Michael Niedermayer wrote: > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> > --- > libavcodec/ffv1enc.c | 77 ++++++++++++++------------------------------ > 1 file changed, 25 insertions(+), 52 deletions(-) will apply patchset [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Democracy is the form of government in which you can choose your dictator [-- 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] 3+ messages in thread
end of thread, other threads:[~2025-03-30 20:25 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2025-03-28 8:45 [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state Michael Niedermayer 2025-03-28 8:45 ` [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: Eliminate RemapEncoderState Michael Niedermayer 2025-03-30 20:25 ` [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state 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