From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/ffv1enc: Eliminate RemapEncoderState
Date: Fri, 28 Mar 2025 09:45:42 +0100
Message-ID: <20250328084542.263642-2-michael@niedermayer.cc> (raw)
In-Reply-To: <20250328084542.263642-1-michael@niedermayer.cc>
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".
next prev parent reply other threads:[~2025-03-28 8:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-28 8:45 [FFmpeg-devel] [PATCH 1/2] avcodec/ffv1enc: Eliminate copy_state Michael Niedermayer
2025-03-28 8:45 ` 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=20250328084542.263642-2-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